You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.7 KiB
150 lines
5.7 KiB
using UnityEngine; |
|
using UnityEngine.AI; |
|
|
|
namespace ET |
|
{ |
|
[FriendClass(typeof(Gather))] |
|
[FriendClass(typeof(ResourceViewComponent))] |
|
[FriendClass(typeof(ResourcePoint))] |
|
[FriendClass(typeof(Construct))] |
|
[FriendClass(typeof(PeopleViewComponent))] |
|
[FriendClass(typeof(People))] |
|
public static class PeopleViewHelper |
|
{ |
|
public static string GetHeadIconUrlByGender(int gender) |
|
{ |
|
var iconName = gender == 0? "Head_M" : "Head_F"; |
|
var url = $"ui://{FUIPackage.Common}/{iconName}"; |
|
return url; |
|
} |
|
|
|
public static void CheckWork(People people) |
|
{ |
|
// 检查是否在采集资源的路上 |
|
Unit unit = UnitHelper.GetMyUnitFromZoneScene(people.ZoneScene()); |
|
PeopleViewComponent pView = people.GetComponent<PeopleViewComponent>(); |
|
|
|
int behave = people.GetBehaveType(); |
|
long targetId = people.GetTargetId(); |
|
switch (behave) |
|
{ |
|
case ConstBehaveType.BEHAVE_IDLE: |
|
break; |
|
case ConstBehaveType.BEHAVE_PREPARE_GATHER: |
|
GatherComponent gc = unit.GetComponent<GatherComponent>(); |
|
Gather gather = gc?.GetGatherByPeopleId(people.Id); |
|
if (gather != null && gather.PreparePeopleIdList.Contains(people.Id)) |
|
{ |
|
ResourcePointComponent rc = unit.GetComponent<ResourcePointComponent>(); |
|
var resource = rc.GetChild<ResourcePoint>(gather.ResPointId); |
|
var resourceViewComp = resource.GetComponent<ResourceViewComponent>(); |
|
if (resourceViewComp == null) |
|
{ |
|
Log.Error($"Can't find resourceViewComp of {resource.Id} : {resource.ConfigId}"); |
|
} |
|
|
|
|
|
pView.MoveToPos(resourceViewComp.GetWorkerPos()); |
|
} |
|
break; |
|
case ConstBehaveType.BEHAVE_PREPARE_CONSTRUCT: |
|
ConstructComponent cc = unit.GetComponent<ConstructComponent>(); |
|
|
|
if (cc != null) |
|
{ |
|
Construct c = cc.GetChild<Construct>(targetId); |
|
if (c != null) |
|
{ |
|
if (c.PreparePeopleIdList.Contains(people.Id)) |
|
{ |
|
var cView = c.GetComponent<ConstructViewComponent>(); |
|
pView.MoveToPos(cView.GetWorkerPos(people.Id)); |
|
} |
|
} |
|
} |
|
break; |
|
} |
|
} |
|
|
|
public static string GetPeopleStateMsg(int behaveType) |
|
{ |
|
string msg = "未知"; |
|
switch (behaveType) |
|
{ |
|
case ConstBehaveType.BEHAVE_IDLE: |
|
msg = "空闲"; |
|
break; |
|
case ConstBehaveType.BEHAVE_PREPARE_GATHER: |
|
msg = "去采集的路上"; |
|
break; |
|
case ConstBehaveType.BEHAVE_GATHER: |
|
msg = "正在采集"; |
|
break; |
|
case ConstBehaveType.BEHAVE_PREPARE_CONSTRUCT: |
|
msg = "在去建造的路上"; |
|
break; |
|
case ConstBehaveType.BEHAVE_CONSTRUCT: |
|
msg = "正在建造"; |
|
break; |
|
default: |
|
Log.Error($"Unkown behaviour type:{behaveType}"); |
|
break; |
|
} |
|
|
|
return msg; |
|
} |
|
|
|
public static void UpdateAnimatorByBehave(People people) |
|
{ |
|
var pAnimator = people.GetComponent<PeopleAnimatorComponent>(); |
|
int behave = people.GetBehaveType(); |
|
switch (behave) |
|
{ |
|
case ConstBehaveType.BEHAVE_IDLE: |
|
pAnimator.StopAll(); |
|
pAnimator.PlayAnimation(PeopleAnimEnum.Idle); |
|
break; |
|
case ConstBehaveType.BEHAVE_PREPARE_GATHER: |
|
case ConstBehaveType.BEHAVE_PREPARE_CONSTRUCT: |
|
pAnimator.StopAll(); |
|
pAnimator.PlayAnimation(PeopleAnimEnum.Run); |
|
break; |
|
case ConstBehaveType.BEHAVE_GATHER: |
|
pAnimator.StopAll(); |
|
pAnimator.PlayAnimation(PeopleAnimEnum.Gather); |
|
break; |
|
case ConstBehaveType.BEHAVE_CONSTRUCT: |
|
pAnimator.StopAll(); |
|
pAnimator.PlayAnimation(PeopleAnimEnum.Construct); |
|
break; |
|
default: |
|
Debug.LogWarning("Unhandled behave type:" + behave); |
|
break; |
|
} |
|
} |
|
|
|
public static Vector3 GetRandomMovePos(PeopleViewComponent peopleViewComponent, float radius) |
|
{ |
|
|
|
var navAgent = peopleViewComponent.Agent; |
|
var path = new NavMeshPath(); |
|
Vector3 newDest = peopleViewComponent.PeopleRoot.transform.position; |
|
int count = 0; |
|
do |
|
{ |
|
Vector3 randomDir = Random.insideUnitCircle * radius; |
|
newDest = randomDir + peopleViewComponent.PeopleRoot.transform.position; |
|
|
|
navAgent.CalculatePath(newDest, path); |
|
count++; |
|
if (count > 10) |
|
{ |
|
break; |
|
} |
|
} |
|
while (path.status != NavMeshPathStatus.PathComplete); |
|
|
|
return newDest; |
|
} |
|
} |
|
} |