private void Moving(int deltaTime) { PathAgent agent = owner.pathAgent; agent.Move(deltaSpeed); long distance = FixVector3.SqrDistance(targetPosition, owner.position); if (distance <= FixVector3.one.magnitude) { state = State.DashEnd; waitingChangeState = false; LogicUnit target = owner.target; if (target != null && target.Alive() && target.id == owner.target.id) { AttributeEffect ae = GenerateAttributeEffect(attributeEffects[0]); ae.Attach(owner, target); } DebugUtils.Log(DebugUtils.Type.AI_Skill, string.Format("YueGuangChongCi: arrive target position, change state to {0}", state)); } else { DebugUtils.Log(DebugUtils.Type.AI_Skill, string.Format("YueGuangChongCi: distance = {0}, step = {1}", distance, stepLength)); } }
protected virtual void Start() { if (transform.parent != null) { parentCharacter = transform.parent.GetComponent <Entity>(); } IgnoreCollideObjects.AddRange(GetComponentsInChildren <Collider2D>()); spriteRenderers.AddRange(GetComponentsInChildren <SpriteRenderer>()); UpdateHit = BoxHit; if (!IsStatic) { if (BoxCollisionOne) { UpdateCollision = BoxCollisionOneDown; } else { UpdateCollision = BoxCollisionFour; } UpdatePosition = BasicPosition; } if (EnablePathing) { pathAgent = new PathAgent(); pathAgent.Client = this; pathAgent.transform = transform; pathAgent.AgentTypeID = Global.instance.AgentType[AgentTypeName]; } }
public void RegisterPathAgent(PathAgent newAgent) { if (agents.Contains(newAgent) == false) { lock (agents){ agents.Add(newAgent); } } }
public void DeregisterPathAgent(PathAgent existingAgent) { if (agents.Contains(existingAgent) == true) { lock (agents){ agents.Remove(existingAgent); } } }
void FindPath(PathAgent agent) { int curNode, targetNode; curNode = map.PosToNode(agent.CurPos); targetNode = map.PosToNode(agent.TargetPos); int[] newPath = IDAStar.IterativeDeepeningAStar(map, curNode, targetNode, 14); agent.SetAgentPath(newPath, unityTime); }
private void OnEnable() { _enemies.Add(this); agent = GetComponent <PathAgent>(); if (!agent) { Debug.LogError($"Missing path agent for {name}. Disabling"); gameObject.SetActive(false); } }
public override void Fire() { base.Fire(); if (playerOwner) { // check brithPostion is suitable PathAgent agent = owner.pathAgent; FixVector3 destination = owner.position + owner.direction.normalized * 3; // 3 meter front of owner FixVector3 hitPosition; FixVector3 simpleOnMapPoint = FixVector3.zero; // mapping destination on navmesh bool sampleResult = agent.SamplePosition(destination, out simpleOnMapPoint); if (sampleResult) { destination = simpleOnMapPoint; } bool result = agent.Raycast(destination, out hitPosition); if (result) { // didn't encountered obstruction destination = hitPosition; } UpdateC2S message = new UpdateC2S(); message.timestamp = DataManager.GetInstance().GetFrame(); Operation op = new Operation(); op.unitId = id; op.targetId = owner.id; // test op.unitMetaId = metaId; // test op.opType = OperationType.SyncSkillTargetPosition; // wrong type op.x = destination.vector3.x; op.y = destination.vector3.y; op.z = destination.vector3.z; message.operation = op; PostBattleMessage(MsgCode.UpdateMessage, message); DebugUtils.LogWarning(DebugUtils.Type.AI_Skill, string.Format("skill {0} {1},send sync position ", id, skillName)); } DebugUtils.LogWarning(DebugUtils.Type.AI_Skill, string.Format("Fire skill {0} {1},waiting sync position ", id, skillName)); }
// Update is called once per frame void Update() { //move all keyboard commands here //while moving, walk along path otherwise wait if (Input.GetMouseButtonDown(0)) { Vector2Int location = Board.GetBoardCoordinates(Board.GetMouseCoordinates()); for (int i = 0; i < AllUnits.Count; i++) { if (AllUnits[i].Location == location) { if (SelectedUnit == AllUnits[i]) { Selection.Deselect(); SelectedUnit = null; } else { SelectedUnit = AllUnits[i]; Selection.Select(SelectedUnit); } return; } } } else if (Input.GetMouseButtonDown(1)) { Vector3 mouseInWorld = Board.GetMouseCoordinates(); SelectedUnit.GetPath(Board.GetBoardCoordinates(mouseInWorld)); } else if (Input.GetKeyDown(KeyCode.S)) { Vector3 mouseInWorld = Board.GetMouseCoordinates(); PathAgent unit = Instantiate(Prefab); unit.board = Board; unit.pathFinder = PathManager; unit.JumpTo(Board.GetBoardCoordinates(mouseInWorld)); AllUnits.Add(unit); } }
private void SprintState(int deltaTime) { LogicUnit target = owner.target; FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; if (target != null && target.Alive() && target.id == owner.target.id) { if ((target.position - owner.targetPosition).sqrMagnitude > GameConstants.BEGINCHASE_DISTANCE) // 5f is a testing distance { // After find path, Refresh target position owner.targetPosition = target.position; owner.FindChasePath(owner.target.position); return; } if (TargetWithInAttackArea()) { state = State.Attack; } else { if (!owner.CurrentPathAlreadyFinished()) { owner.WaypointHandler(); FixVector3 d = owner.speed * deltaTime; agent.Move(d); } else { // wait for chase path } } } else { // Skill will be shut down when target death. Stop(); owner.Idle(); } }
protected override void Initialize() { base.Initialize(); if (spawnSound) { AudioManager.Instance.PlaySound(spawnSound, gameObject); } ElementalProjectile = Resources.Load <GameObject>("Ice_Wave"); ElementalDeath = Resources.Load <GameObject>("Dark_Explosion"); pathAgent = GetComponent <PathAgent>(); animator = GetComponent <Animator>(); rigidBody = GetComponent <Rigidbody>(); player = GameObject.FindWithTag("Player"); currentState = FSMState.Chase; renderer = GetComponentInChildren <Renderer>(); StartCoroutine(FadeOpacity(true)); }
private void InitMap(object map) { GameObject battleMap = null; if (map == null) { battleMap = GameObject.Find("BattleMap"); } else { battleMap = Instantiate((GameObject)map); } DataManager clientData = DataManager.GetInstance(); id = clientData.GetBattleId(); battleType = clientData.GetBattleType(); clientData.SetBattleStartTime(); PathAgent.Initialize(); InputHandler.Instance.InitializeData(); logicWorld.RegisterRenderMessageHandler(HandleRenderMessage); logicWorld.RegisterAgentMessageHandler(HandleAgentMessage); mark = clientData.GetForceMark(); HealthbarControl.Instance.SetWorldCamera(Camera.main); // TODO: Temp code, EnterBattle must be posted in InputHandler, and after get the matchlist from server UI.UIManager.Instance.EnterBattleMenu(() => { ApplyBattleMode(battleMap); SetUpBattle(); }); }
public override void Update(int deltaTime) { int state = owner.state; DebugUtils.Assert(state == SoldierState.CHASING, string.Format("Soldier {0} is in state {1} when updating in FsmChase!", owner.id, state)); LogicUnit target = owner.target; FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; if (target != null && target.Alive() && target.id == owner.targetId) { long mag = FixVector3.SqrDistance(target.position, owner.targetPosition); //Debug.LogWarning( string.Format( "current position:{0} record position:{1} length:{2}", target.position, owner.targetPosition, mag ) ); if (mag > GameConstants.BEGINCHASE_DISTANCE) // 5f is a testing distance { //Debug.Log( string.Format( "current position:{0} record position:{1} length:{2}", target.position, owner.targetPosition, mag ) ); owner.Chase(owner.target); return; } owner.WaypointHandler(); FixVector3 d = owner.speed * deltaTime; FixVector3 newPosition = position + d * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; FixVector3 avoidance = owner.CalculateAvoidance(owner, newPosition); if (avoidance != FixVector3.zero && subState != CHASE2AVOIDANCE) { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters CHASE2AVOIDANCE.", owner.id)); subState = CHASE2AVOIDANCE; DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has received an avoidance ({1}, {2}, {3}), he's speed is ({4}, {5}, {6})!", owner.id, avoidance.x, avoidance.y, avoidance.z, owner.speed.x, owner.speed.y, owner.speed.z)); avoidSpeed = d + avoidance * deltaTime; FixVector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; bool pass = agent.DirectPassToPosition(pos, NavMesh.AllAreas); if (pass) { agent.Move(avoidSpeed); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has received an avoidance and moved to ({1}, {2}, {3})!", owner.id, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has stopped when avoiding others!", owner.id)); } } else { if (subState == CHASE2AVOIDANCE) { if (avoidance != FixVector3.zero) { avoidSpeed = d + avoidance * deltaTime; //Vector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; //bool pass = agent.DirectPassToPosition( pos, NavMesh.AllAreas ); if (true) //pass ) { agent.ToughMove(avoidSpeed); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has received an avoidance and moved to ({1}, {2}, {3})!", owner.id, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("CHASE : soldier {0} has stopped when avoiding others!", owner.id)); } } else { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters AVOIDANCE2CHASE.", owner.id)); subState = AVOIDANCE2CHASE; agent.Move(d); } } else { agent.Move(d); } } position = owner.position; FixVector3 v = target.position; long distance = FixVector3.SqrDistance(target.position, position); long attackDistance = owner.GetAttackArea(); if (distance < attackDistance) { owner.Attack(owner.target); } /* * else if( distance > chaseArea * chaseArea * 1.5f ) * { * DebugUtils.LogWarning( DebugUtils.Type.AI_Soldier, string.Format( "soldier {0}'s target {1} has escaped! distance = {2}, chaseArea * chaseArea * 1.5f = {3}", id, target.id, distance, chaseArea * chaseArea * 1.5f ) ); * //Idle(); * Walk( destination ); * } */ if (owner.stateListener.PostMoveDistanceChanged(d.magnitude)) { return; } owner.stateListener.PostChasingStateChanged(distance); } else { owner.Idle(); //Walk( destination ); } }
private void SprintState(int deltaTime) { LogicUnit target = owner.target; FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; if (target != null && target.Alive() && target.id == owner.target.id) { if (FixVector3.SqrDistance(target.position, owner.targetPosition) > GameConstants.BEGINCHASE_DISTANCE) // 5f is a testing distance { // if target leave the position too far, need to refresh chase path owner.targetPosition = target.position; owner.FindChasePath(owner.target.position); return; } if (TargetWithInAttackArea()) { if (target != null && target.Alive() && target.id == owner.target.id) { if (playerOwner) { FixVector3 direction = (target.position - position).normalized; FixVector3 destination = owner.position + direction * 5f;// Temp distance. FixVector3 hitPosition = FixVector3.zero; FixVector3 simpleOnMapPoint = FixVector3.zero; // mapping destination on navmesh bool sampleResult = agent.SamplePosition(destination, out simpleOnMapPoint); if (sampleResult) { destination = simpleOnMapPoint; } bool result = agent.Raycast(destination, out hitPosition); if (result) { destination = hitPosition; } DataManager clientData = DataManager.GetInstance(); UpdateC2S message = new UpdateC2S(); message.timestamp = clientData.GetFrame(); Operation op = new Operation(); op.playerId = clientData.GetPlayerId(); op.unitId = id; op.targetId = owner.id; // test op.unitMetaId = metaId; // test op.opType = OperationType.SyncSkillTargetPosition; // wrong type op.x = destination.vector3.x; op.y = destination.vector3.y; op.z = destination.vector3.z; message.operation = op; PostBattleMessage(MsgCode.UpdateMessage, message); DebugUtils.Log(DebugUtils.Type.AI_Skill, string.Format("YueGuangChongCi sync destination : {0}", destination)); } } else { Stop(); } } else { if (!owner.CurrentPathAlreadyFinished()) { owner.WaypointHandler(); FixVector3 d = owner.speed * deltaTime; agent.Move(d); } else { // wait for chase path } } } else { // Skill will be shut down when target death. Stop(); owner.Idle(); } }
void Start() { this.Player = GameObject.FindGameObjectWithTag("Player").GetComponent <PathAgent>(); }
public void Select(PathAgent agent) { Target = agent; Reticle.SetActive(true); }
public override void Update(int deltaTime) { owner.WaypointHandler(); FixVector3 position = owner.position; PathAgent agent = owner.pathAgent; FixVector3 d = owner.speed * deltaTime; FixVector3 newPosition = position + d * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; FixVector3 avoidance = owner.CalculateAvoidance(owner, newPosition); if (avoidance != FixVector3.zero && subState != WALK2AVOIDANCE) { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters WALK2AVOIDANCE.", owner.id)); subState = WALK2AVOIDANCE; DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has received an avoidance ({1}, {2}, {3}), he's speed is ({4}, {5}, {6})!", owner.id, avoidance.x, avoidance.y, avoidance.z, owner.speed.x, owner.speed.y, owner.speed.z)); avoidSpeed = d + avoidance * deltaTime; FixVector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; bool pass = agent.DirectPassToPosition(pos, NavMesh.AllAreas); if (pass) { agent.Move(avoidSpeed); owner.stateListener.PostMoveDistanceChanged(avoidance.magnitude); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has received an avoidance and move to ({1}, {2}, {3})!", owner.id, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has stopped when avoiding others!", owner.id)); } } else { if (subState == WALK2AVOIDANCE) { if (avoidance != FixVector3.zero) { avoidSpeed = d + avoidance * deltaTime; FixVector3 pos = position + avoidSpeed * FixVector3.PrecisionFactor * FixVector3.PrecisionFactor; //bool pass = agent.DirectPassToPosition( pos, NavMesh.AllAreas ); if (true) //pass ) { //agent.Move( avoidSpeed ); agent.ToughMove(avoidSpeed); DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has received an avoidance and wants to move to ({1}, {2}, {3}), then his position is ({4}, {5}, {6})!", owner.id, pos.x, pos.y, pos.z, owner.position.x, owner.position.y, owner.position.z)); } else { //still here. DebugUtils.Log(DebugUtils.Type.Avoidance, string.Format("WALK : soldier {0} has stopped when avoiding others!", owner.id)); } } else { DebugUtils.LogWarning(DebugUtils.Type.AI_Soldier, string.Format("soldier {0} enters AVOIDANCE2WALK.", owner.id)); subState = AVOIDANCE2WALK; agent.Move(d); owner.stateListener.PostMoveDistanceChanged(avoidance.magnitude); } } else { agent.Move(d); owner.stateListener.PostMoveDistanceChanged(avoidance.magnitude); } owner.stateListener.PostMoveDistanceChanged(d.magnitude); } }
protected override void OnAwake() { pathAgent = gameObject.GetOrAddComponent <PathAgent>(); pathAgent.stoppingDistance = arriveDistance; pathAgent.pickNextWaypointDist = pickNextWaypointDist; }
public void Deselect() { Target = null; Reticle.SetActive(false); }