From b5f23bd877eecf47cc49475cefbf0924107a1572 Mon Sep 17 00:00:00 2001 From: SnpM Date: Sun, 14 Aug 2016 22:26:27 -0600 Subject: [PATCH] Fix agent auto-stopping during war; Re-enable AgentController DeathingAgents; Tweak AutoSpawner automatic movement command position; Add 2 factions to Example Scene; Tweak stats; --- Core/Game/Abilities/Essential/Move.cs | 1127 +++++++++++++------------ Core/Game/Abilities/Essential/Scan.cs | 2 +- Core/Game/Agents/AgentController.cs | 4 +- Example/AutoSpawner.cs | 9 +- Example/ExampleScene.unity | 16 +- Example/TestAgent.prefab | 2 +- 6 files changed, 591 insertions(+), 569 deletions(-) diff --git a/Core/Game/Abilities/Essential/Move.cs b/Core/Game/Abilities/Essential/Move.cs index 1b19a726..90a9d1a0 100755 --- a/Core/Game/Abilities/Essential/Move.cs +++ b/Core/Game/Abilities/Essential/Move.cs @@ -1,558 +1,571 @@ -using System; -using UnityEngine; - -namespace Lockstep -{ - public class Move : ActiveAbility - { - public const long FormationStop = FixedMath.One / 4; - public const long GroupDirectStop = FixedMath.One; - public const long DirectStop = FixedMath.One / 8; - private const int MinimumOtherStopTime = (int)(LockstepManager.FrameRate / 4); - private const int repathRate = (int)LockstepManager.FrameRate; - private const int CollisionStopCount = LockstepManager.FrameRate * 2; - private const long CollisionStopTreshold = FixedMath.One / 2; - - public int GridSize {get {return cachedBody.Radius <= FixedMath.One ? - (cachedBody.Radius * 2).CeilToInt() : - (cachedBody.Radius * 2).CeilToInt ();}} - - - public Vector2d Position { get { return cachedBody._position; } } - - public long CollisionSize { get { return cachedBody.Radius; } } - - public MovementGroup MyMovementGroup { get; set; } - - private int RepathRate - { - get { return FixedMath.Create(repathRate).Div(this.Speed > FixedMath.One ? this.Speed : FixedMath.One).CeilToInt(); } - } - - private const int straightRepathRate = repathRate * 4; - - private int StraightRepathRate - { - get { return LSUtility.GetRandom(straightRepathRate); } - } - - public int MyMovementGroupID { get; set; } - - public bool IsFormationMoving { get; set; } - - public bool IsMoving { get; private set; } - - private bool hasPath; - private bool straightPath; - private bool viableDestination; - private readonly FastList myPath = new FastList(); - private int pathIndex; - private int stopTime; - private Vector2d targetPos; - [HideInInspector] - public Vector2d Destination; - private int repathCount; - - public bool CanCollisionStop { get; set; } - - public long CollisionStopMultiplier { get; set; } - - private bool forcePathfind { get; set; } - - private bool collisionTicked; - private int stuckTick; - - public bool CollisionTicked { get { return collisionTicked; } } - public int StuckTick { get { return stuckTick; } } - - //Has this unit arrived at destination? Default set to false. - public bool Arrived { get; private set; } - - //Called when unit arrives at destination - public event Action onArrive; - - public event Action onStartMove; - - //Called whenever movement is stopped... i.e. to attack - public event Action OnStopMove; - - - private LSBody cachedBody {get; set;} - private Turn CachedTurn {get; set;} - - public Vector2d LastPosition {get; set;} - - public long timescaledSpeed - { - get - { - long ret = ((this.Speed + this.AdditiveSpeedModifier) / LockstepManager.FrameRate).Mul(FixedMath.One + this.MultiplicativeSpeedModifier); - if (ret < 0) - ret = 0; - return ret; - } - } - - private long collisionStopTreshold; - private long timescaledAcceleration; - - [Lockstep (true)] - public long AdditiveSpeedModifier { get; set; } - [Lockstep (true)] - public long MultiplicativeSpeedModifier { get; set; } - - private Vector2d lastTargetPos; - private Vector2d targetDirection; - private GridNode currentNode; - private GridNode destinationNode; - private Vector2d movementDirection; - private Vector2d lastMovementDirection; - private long distance; - private long closingDistance; - private long stuckTolerance; - - [Lockstep (true)] - public bool SlowArrival { - get; - set; - } - - #region Serialized - - [SerializeField] - private bool _canMove = true; - public bool CanMove { - get { return _canMove; } - } - - [SerializeField] - private bool _canTurn = true; - public bool CanTurn {get; private set;} - - [SerializeField, FixedNumber] - private long _speed = FixedMath.One * 4; - - public virtual long Speed - { - get { return _speed ; } - } - - - [SerializeField, FixedNumber] - private long _acceleration = FixedMath.One; - public long Acceleration {get {return _acceleration;}} - [SerializeField, UnityEngine.Serialization.FormerlySerializedAs ("Flying")] - private bool _canPathfind = true; - public bool CanPathfind {get; private set;} - - #endregion - - protected override void OnSetup() - { - cachedBody = Agent.Body; - cachedBody.OnContact += HandleCollision; - CachedTurn = Agent.GetAbility(); - CanTurn = _canTurn && CachedTurn != null; - collisionStopTreshold = FixedMath.Mul(timescaledSpeed, CollisionStopTreshold); - collisionStopTreshold *= collisionStopTreshold; - timescaledAcceleration = Acceleration * 32 / LockstepManager.FrameRate; - if (timescaledAcceleration > FixedMath.One) - timescaledAcceleration = FixedMath.One; - closingDistance = cachedBody.Radius; - stuckTolerance = ((Agent.Body.Radius * Speed) >> FixedMath.SHIFT_AMOUNT) / LockstepManager.FrameRate; - stuckTolerance *= stuckTolerance; - CanPathfind = _canPathfind; - this.SlowArrival = true; - } - - protected override void OnInitialize() - { - myPath.FastClear(); - pathIndex = 0; - stopTime = 0; - - IsFormationMoving = false; - MyMovementGroupID = -1; - CanCollisionStop = true; - CollisionStopMultiplier = DirectStop; - - repathCount = RepathRate; - viableDestination = false; - - Destination = Vector2d.zero; - hasPath = false; - IsMoving = false; - collisionTicked = false; - stuckTick = 0; - - forcePathfind = false; - lastMovementDirection = Vector2d.up; - - Arrived = true; - LastPosition = Agent.Body.Position; - } - - public int StopTimer; - - protected override void OnSimulate() - { - if (!CanMove) - { - return; - } - if (IsMoving) - { - if (CanPathfind) - { - if (repathCount <= 0) - { - if (viableDestination) - { - if (Pathfinder.GetPathNode(cachedBody._position.x, cachedBody._position.y, out currentNode)) - { - if (straightPath) - { - if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize)) - { - if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath, - GridSize)) - { - hasPath = true; - pathIndex = 0; - } else - { - if (IsFormationMoving) - { - StartMove(MyMovementGroup.Destination); - IsFormationMoving = false; - } - } - straightPath = false; - repathCount = RepathRate; - } else - { - repathCount = StraightRepathRate; - } - } else - { - if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize)) - { - if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath, - GridSize)) - { - hasPath = true; - pathIndex = 0; - } else - { - if (IsFormationMoving) - { - StartMove(MyMovementGroup.Destination); - IsFormationMoving = false; - } - } - repathCount = RepathRate; - } else - { - straightPath = true; - repathCount = StraightRepathRate; - } - } - } else - { - - } - } else - { - hasPath = false; - if (IsFormationMoving) - { - StartMove(MyMovementGroup.Destination); - IsFormationMoving = false; - } - } - } else - { - if (hasPath) - { - //TODO: fix this shit - repathCount--; - } else - { - repathCount-= 2; - } - } - - if (straightPath) - { - targetPos = Destination; - } else if (hasPath) - { - if (pathIndex >= myPath.Count) - { - targetPos = this.Destination; - } - else { - targetPos = myPath [pathIndex]; - } - } else - { - targetPos = Destination; - } - } else - { - targetPos = Destination; - } - - movementDirection = targetPos - cachedBody._position; - - movementDirection.Normalize(out distance); - if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y) - { - lastTargetPos = targetPos; - targetDirection = movementDirection; - } - bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1); - if (distance > closingDistance || movingToWaypoint) - { - desiredVelocity = (movementDirection); - if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y).AbsMoreThan(FixedMath.One / 4)) - { - lastMovementDirection = movementDirection; - if (CanTurn) - CachedTurn.StartTurnDirection(movementDirection); - } - - long threshold = this.timescaledSpeed / 4; - - if ((Agent.Body.Position - this.LastPosition).FastMagnitude () < (threshold * threshold)) { - StopTimer++; - if (StopTimer >= LockstepManager.FrameRate) { - StopTimer = 0; - this.Arrive (); - } - } else { - StopTimer = 0; - } - } else - { - StopTimer = 0; - if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier)) - { - Arrive(); - return; - } - if (this.SlowArrival) { - desiredVelocity = (movementDirection * (distance) / (closingDistance)); - } - else { - desiredVelocity = (movementDirection); - } - } - - - if (movingToWaypoint) { - if (distance < FixedMath.Mul(closingDistance, FixedMath.Half)) - { - this.pathIndex++; - } - } - desiredVelocity *= timescaledSpeed; - - cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration; - - cachedBody.VelocityChanged = true; - - - } else - { - if (cachedBody.VelocityFastMagnitude > 0) - { - cachedBody._velocity -= cachedBody._velocity * timescaledAcceleration; - cachedBody.VelocityChanged = true; - } - stopTime++; - } - LastPosition = Agent.Body.Position; - - } - - public Command LastCommand; - - protected override void OnExecute(Command com) - { - LastCommand = com; - if (com.ContainsData ()) - { - StartFormalMove(com.GetData()); - } - } - public void StartFormalMove (Vector2d position) - { - Agent.StopCast(ID); - IsCasting = true; - RegisterGroup(); - if (straightPath) - { - repathCount /= 8; - } - else - { - repathCount /= 8; - } - } - - public void RegisterGroup(bool moveOnProcessed = true) - { - MoveOnGroupProcessed = moveOnProcessed; - if (MovementGroupHelper.CheckValidAndAlert ()) - MovementGroupHelper.LastCreatedGroup.Add(this); - - - } - - public void Arrive() - { - - StopMove(); - - if (onArrive.IsNotNull()) - { - onArrive(); - } - this.OnArrive(); - Arrived = true; - } - - protected virtual void OnArrive () { - - } - - - public void StopMove() - { - if (IsMoving) - { - forcePathfind = false; - - if (MyMovementGroup.IsNotNull()) - { - MyMovementGroup.Remove(this); - } - - IsMoving = false; - stopTime = 0; - - IsCasting = false; - stuckTick = 0; - if (OnStopMove.IsNotNull()) - { - OnStopMove(); - } - } - } - - public void OnGroupProcessed(Vector2d destination) - { - Destination = destination; - if (MoveOnGroupProcessed) - { - StartMove(destination); - MoveOnGroupProcessed = false; - } else - { - this.Destination = destination; - } - if (this.onGroupProcessed != null) - this.onGroupProcessed(); - } - - public event Action onGroupProcessed; - - public bool MoveOnGroupProcessed { get; private set; } - - public void StartMove(Vector2d destination) - { - if (CanTurn) - CachedTurn.StartTurnVector(destination - cachedBody._position); - Agent.SetState(AnimState.Moving); - hasPath = false; - straightPath = false; - this.Destination = destination; - IsMoving = true; - stopTime = 0; - Arrived = false; - - viableDestination = Pathfinder.GetPathNode(this.Destination.x, this.Destination.y, out destinationNode); - - IsCasting = true; - stuckTick = 0; - forcePathfind = false; - if (onStartMove != null) - onStartMove(); - } - - protected override void OnStopCast() - { - StopMove(); - } - - - private int collidedCount; - private ushort collidedID; - - static LSAgent tempAgent; - - private void HandleCollision(LSBody other) - { - - if (!CanMove) - { - return; - } - if ((tempAgent = other.Agent) == null) - { - return; - } - - Move otherMover = tempAgent.GetAbility(); - if (ReferenceEquals(otherMover, null) == false) - { - if (IsMoving && CanCollisionStop) - { - if (otherMover.MyMovementGroupID == MyMovementGroupID) - { - if (otherMover.IsMoving == false && otherMover.Arrived && otherMover.stopTime > MinimumOtherStopTime) - { - Arrive(); - } else if (hasPath && otherMover.hasPath && otherMover.pathIndex > 0 && otherMover.lastTargetPos.SqrDistance(targetPos.x, targetPos.y) < FixedMath.One) - { - if (movementDirection.Dot(targetDirection.x, targetDirection.y) < 0) - { - pathIndex++; - } - } - } - } - } - } - - static Vector2d desiredVelocity; - - #if UNITY_EDITOR - public bool DrawPath = true; - - void OnDrawGizmos() - { - if (DrawPath) - { - const float height = 0f; - for (int i = 1; i < myPath.Count; i++) - { - UnityEditor.Handles.Label(myPath[i - 1].ToVector3(height), i.ToString()); - Gizmos.DrawLine(myPath[i - 1].ToVector3(height), myPath[i].ToVector3(height)); - } - } - } - #endif - } +using System; +using UnityEngine; + +namespace Lockstep +{ + public class Move : ActiveAbility + { + public const long FormationStop = FixedMath.One / 4; + public const long GroupDirectStop = FixedMath.One; + public const long DirectStop = FixedMath.One / 8; + private const int MinimumOtherStopTime = (int)(LockstepManager.FrameRate / 4); + private const int repathRate = (int)LockstepManager.FrameRate; + private const int CollisionStopCount = LockstepManager.FrameRate * 2; + private const long CollisionStopTreshold = FixedMath.One / 2; + + public int GridSize {get {return cachedBody.Radius <= FixedMath.One ? + (cachedBody.Radius * 2).CeilToInt() : + (cachedBody.Radius * 2).CeilToInt ();}} + + + public Vector2d Position { get { return cachedBody._position; } } + + public long CollisionSize { get { return cachedBody.Radius; } } + + public MovementGroup MyMovementGroup { get; set; } + + private int RepathRate + { + get { return FixedMath.Create(repathRate).Div(this.Speed > FixedMath.One ? this.Speed : FixedMath.One).CeilToInt(); } + } + + private const int straightRepathRate = repathRate * 4; + + private int StraightRepathRate + { + get { return LSUtility.GetRandom(straightRepathRate); } + } + + public int MyMovementGroupID { get; set; } + + public bool IsFormationMoving { get; set; } + + public bool IsMoving { get; private set; } + + private bool hasPath; + private bool straightPath; + private bool viableDestination; + private readonly FastList myPath = new FastList(); + private int pathIndex; + private int stopTime; + private Vector2d targetPos; + [HideInInspector] + public Vector2d Destination; + private int repathCount; + + public bool GetFullCanCollisionStop() + { + return CanCollisionStop && TempCanCollisionStop; + } + public bool CanCollisionStop { get; set; } + + public bool TempCanCollisionStop { get; set;} + + public long CollisionStopMultiplier { get; set; } + + private bool forcePathfind { get; set; } + + private bool collisionTicked; + private int stuckTick; + + public bool CollisionTicked { get { return collisionTicked; } } + public int StuckTick { get { return stuckTick; } } + + //Has this unit arrived at destination? Default set to false. + public bool Arrived { get; private set; } + + //Called when unit arrives at destination + public event Action onArrive; + + public event Action onStartMove; + + //Called whenever movement is stopped... i.e. to attack + public event Action OnStopMove; + + + private LSBody cachedBody {get; set;} + private Turn CachedTurn {get; set;} + + public Vector2d LastPosition {get; set;} + + public long timescaledSpeed + { + get + { + long ret = ((this.Speed + this.AdditiveSpeedModifier) / LockstepManager.FrameRate).Mul(FixedMath.One + this.MultiplicativeSpeedModifier); + if (ret < 0) + ret = 0; + return ret; + } + } + + private long collisionStopTreshold; + private long timescaledAcceleration; + + [Lockstep (true)] + public long AdditiveSpeedModifier { get; set; } + [Lockstep (true)] + public long MultiplicativeSpeedModifier { get; set; } + + private Vector2d lastTargetPos; + private Vector2d targetDirection; + private GridNode currentNode; + private GridNode destinationNode; + private Vector2d movementDirection; + private Vector2d lastMovementDirection; + private long distance; + private long closingDistance; + private long stuckTolerance; + + [Lockstep (true)] + public bool SlowArrival { + get; + set; + } + + #region Serialized + + [SerializeField] + private bool _canMove = true; + public bool CanMove { + get { return _canMove; } + } + + [SerializeField] + private bool _canTurn = true; + public bool CanTurn {get; private set;} + + [SerializeField, FixedNumber] + private long _speed = FixedMath.One * 4; + + public virtual long Speed + { + get { return _speed ; } + } + + + [SerializeField, FixedNumber] + private long _acceleration = FixedMath.One; + public long Acceleration {get {return _acceleration;}} + [SerializeField, UnityEngine.Serialization.FormerlySerializedAs ("Flying")] + private bool _canPathfind = true; + public bool CanPathfind {get; private set;} + + #endregion + + protected override void OnSetup() + { + cachedBody = Agent.Body; + cachedBody.OnContact += HandleCollision; + CachedTurn = Agent.GetAbility(); + CanTurn = _canTurn && CachedTurn != null; + collisionStopTreshold = FixedMath.Mul(timescaledSpeed, CollisionStopTreshold); + collisionStopTreshold *= collisionStopTreshold; + timescaledAcceleration = Acceleration * 32 / LockstepManager.FrameRate; + if (timescaledAcceleration > FixedMath.One) + timescaledAcceleration = FixedMath.One; + closingDistance = cachedBody.Radius; + stuckTolerance = ((Agent.Body.Radius * Speed) >> FixedMath.SHIFT_AMOUNT) / LockstepManager.FrameRate; + stuckTolerance *= stuckTolerance; + CanPathfind = _canPathfind; + this.SlowArrival = true; + } + + protected override void OnInitialize() + { + myPath.FastClear(); + pathIndex = 0; + stopTime = 0; + + IsFormationMoving = false; + MyMovementGroupID = -1; + CanCollisionStop = true; + TempCanCollisionStop = true; + CollisionStopMultiplier = DirectStop; + + repathCount = RepathRate; + viableDestination = false; + + Destination = Vector2d.zero; + hasPath = false; + IsMoving = false; + collisionTicked = false; + stuckTick = 0; + + forcePathfind = false; + lastMovementDirection = Vector2d.up; + + Arrived = true; + LastPosition = Agent.Body.Position; + } + + public int StopTimer; + + protected override void OnSimulate() + { + if (!CanMove) + { + return; + } + if (IsMoving) + { + if (CanPathfind) + { + if (repathCount <= 0) + { + if (viableDestination) + { + if (Pathfinder.GetPathNode(cachedBody._position.x, cachedBody._position.y, out currentNode)) + { + if (straightPath) + { + if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize)) + { + if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath, + GridSize)) + { + hasPath = true; + pathIndex = 0; + } else + { + if (IsFormationMoving) + { + StartMove(MyMovementGroup.Destination); + IsFormationMoving = false; + } + } + straightPath = false; + repathCount = RepathRate; + } else + { + repathCount = StraightRepathRate; + } + } else + { + if (forcePathfind || Pathfinder.NeedsPath(currentNode, destinationNode, this.GridSize)) + { + if (Pathfinder.FindPath(Destination, currentNode, destinationNode, myPath, + GridSize)) + { + hasPath = true; + pathIndex = 0; + } else + { + if (IsFormationMoving) + { + StartMove(MyMovementGroup.Destination); + IsFormationMoving = false; + } + } + repathCount = RepathRate; + } else + { + straightPath = true; + repathCount = StraightRepathRate; + } + } + } else + { + + } + } else + { + hasPath = false; + if (IsFormationMoving) + { + StartMove(MyMovementGroup.Destination); + IsFormationMoving = false; + } + } + } else + { + if (hasPath) + { + //TODO: fix this shit + repathCount--; + } else + { + repathCount-= 2; + } + } + + if (straightPath) + { + targetPos = Destination; + } else if (hasPath) + { + if (pathIndex >= myPath.Count) + { + targetPos = this.Destination; + } + else { + targetPos = myPath [pathIndex]; + } + } else + { + targetPos = Destination; + } + } else + { + targetPos = Destination; + } + + movementDirection = targetPos - cachedBody._position; + + movementDirection.Normalize(out distance); + if (targetPos.x != lastTargetPos.x || targetPos.y != lastTargetPos.y) + { + lastTargetPos = targetPos; + targetDirection = movementDirection; + } + bool movingToWaypoint = (this.hasPath && this.pathIndex < myPath.Count - 1); + if (distance > closingDistance || movingToWaypoint) + { + desiredVelocity = (movementDirection); + if (movementDirection.Cross(lastMovementDirection.x, lastMovementDirection.y).AbsMoreThan(FixedMath.One / 4)) + { + lastMovementDirection = movementDirection; + if (CanTurn) + CachedTurn.StartTurnDirection(movementDirection); + } + + long threshold = this.timescaledSpeed / 4; + + if (GetFullCanCollisionStop () && (Agent.Body.Position - this.LastPosition).FastMagnitude () < (threshold * threshold)) { + StopTimer++; + if (StopTimer >= LockstepManager.FrameRate * 2) { + StopTimer = 0; + this.Arrive (); + } + } else { + StopTimer = 0; + } + } else + { + StopTimer = 0; + if (distance < FixedMath.Mul(closingDistance, CollisionStopMultiplier)) + { + Arrive(); + return; + } + if (this.SlowArrival) { + desiredVelocity = (movementDirection * (distance) / (closingDistance)); + } + else { + desiredVelocity = (movementDirection); + } + } + + + if (movingToWaypoint) { + if (distance < FixedMath.Mul(closingDistance, FixedMath.Half)) + { + this.pathIndex++; + } + } + desiredVelocity *= timescaledSpeed; + + cachedBody._velocity += (desiredVelocity - cachedBody._velocity) * timescaledAcceleration; + + cachedBody.VelocityChanged = true; + + TempCanCollisionStop = false; + } else + { + if (cachedBody.VelocityFastMagnitude > 0) + { + cachedBody._velocity -= cachedBody._velocity * timescaledAcceleration; + cachedBody.VelocityChanged = true; + } + stopTime++; + } + LastPosition = Agent.Body.Position; + + } + + public Command LastCommand; + + protected override void OnExecute(Command com) + { + LastCommand = com; + if (com.ContainsData ()) + { + StartFormalMove(com.GetData()); + } + } + public void StartFormalMove (Vector2d position) + { + Agent.StopCast(ID); + IsCasting = true; + RegisterGroup(); + if (straightPath) + { + repathCount /= 8; + } + else + { + repathCount /= 8; + } + } + + public void RegisterGroup(bool moveOnProcessed = true) + { + MoveOnGroupProcessed = moveOnProcessed; + if (MovementGroupHelper.CheckValidAndAlert ()) + MovementGroupHelper.LastCreatedGroup.Add(this); + + + } + + public void Arrive() + { + + StopMove(); + + if (onArrive.IsNotNull()) + { + onArrive(); + } + this.OnArrive(); + Arrived = true; + } + + protected virtual void OnArrive () { + + } + + + public void StopMove() + { + if (IsMoving) + { + forcePathfind = false; + + if (MyMovementGroup.IsNotNull()) + { + MyMovementGroup.Remove(this); + } + + IsMoving = false; + stopTime = 0; + + IsCasting = false; + stuckTick = 0; + if (OnStopMove.IsNotNull()) + { + OnStopMove(); + } + } + } + + public void OnGroupProcessed(Vector2d destination) + { + Destination = destination; + if (MoveOnGroupProcessed) + { + StartMove(destination); + MoveOnGroupProcessed = false; + } else + { + this.Destination = destination; + } + if (this.onGroupProcessed != null) + this.onGroupProcessed(); + } + + public event Action onGroupProcessed; + + public bool MoveOnGroupProcessed { get; private set; } + + public void StartMove(Vector2d destination) + { + if (CanTurn) + CachedTurn.StartTurnVector(destination - cachedBody._position); + Agent.SetState(AnimState.Moving); + hasPath = false; + straightPath = false; + this.Destination = destination; + IsMoving = true; + stopTime = 0; + Arrived = false; + + viableDestination = Pathfinder.GetPathNode(this.Destination.x, this.Destination.y, out destinationNode); + + IsCasting = true; + stuckTick = 0; + forcePathfind = false; + if (onStartMove != null) + onStartMove(); + } + + protected override void OnStopCast() + { + StopMove(); + } + + + private int collidedCount; + private ushort collidedID; + + static LSAgent tempAgent; + + private void HandleCollision(LSBody other) + { + + if (!CanMove) + { + return; + } + if ((tempAgent = other.Agent) == null) + { + return; + } + + Move otherMover = tempAgent.GetAbility(); + if (ReferenceEquals(otherMover, null) == false) + { + if (IsMoving && GetFullCanCollisionStop()) + { + if (otherMover.MyMovementGroupID == MyMovementGroupID) + { + if (otherMover.IsMoving == false && otherMover.Arrived && otherMover.stopTime > MinimumOtherStopTime) + { + if (otherMover.CanCollisionStop == false) + { + TempCanCollisionStop = true; + } + else { + Arrive(); + } + } else if (hasPath && otherMover.hasPath && otherMover.pathIndex > 0 && otherMover.lastTargetPos.SqrDistance(targetPos.x, targetPos.y) < FixedMath.One) + { + if (movementDirection.Dot(targetDirection.x, targetDirection.y) < 0) + { + pathIndex++; + } + } + } + } + } + } + + static Vector2d desiredVelocity; + + #if UNITY_EDITOR + public bool DrawPath = true; + + void OnDrawGizmos() + { + if (DrawPath) + { + const float height = 0f; + for (int i = 1; i < myPath.Count; i++) + { + UnityEditor.Handles.Label(myPath[i - 1].ToVector3(height), i.ToString()); + Gizmos.DrawLine(myPath[i - 1].ToVector3(height), myPath[i].ToVector3(height)); + } + } + } + #endif + } } \ No newline at end of file diff --git a/Core/Game/Abilities/Essential/Scan.cs b/Core/Game/Abilities/Essential/Scan.cs index 5cdb394b..47f0cf4b 100755 --- a/Core/Game/Abilities/Essential/Scan.cs +++ b/Core/Game/Abilities/Essential/Scan.cs @@ -322,7 +322,7 @@ void BehaveWithTarget() cachedBody.Priority = basePriority; } else { - if (Target.Body.PositionChanged || inRange) + if (Target.Body.PositionChangedBuffer || inRange) { cachedMove.Destination = Target.Body._position; } diff --git a/Core/Game/Agents/AgentController.cs b/Core/Game/Agents/AgentController.cs index 3f7b4b2a..9bde7393 100755 --- a/Core/Game/Agents/AgentController.cs +++ b/Core/Game/Agents/AgentController.cs @@ -106,7 +106,7 @@ public static void Deactivate() } } CheckDestroyAgent(); - /* + for (int i = 0; i < DeathingAgents.PeakCount; i++) { if (DeathingAgents.arrayAllocation[i]) @@ -116,7 +116,7 @@ public static void Deactivate() } } DeathingAgents.FastClear(); -*/ + } private static ushort GenerateGlobalID() diff --git a/Example/AutoSpawner.cs b/Example/AutoSpawner.cs index 1cb48343..c862b3e6 100644 --- a/Example/AutoSpawner.cs +++ b/Example/AutoSpawner.cs @@ -44,8 +44,15 @@ protected override void OnGameStart() } } + //Find average of spawn positions + Vector2d battlePos = Vector2d.zero; + for (int i = 0; i < Spawns.Length; i++) + { + battlePos += Spawns[i].Position; + } + battlePos /= Spawns.Length; Command com = new Command(Lockstep.Data.AbilityDataItem.FindInterfacer().ListenInputID); - com.Add(Vector2d.zero); + com.Add(battlePos); PlayerManager.SendCommand(com); diff --git a/Example/ExampleScene.unity b/Example/ExampleScene.unity index ad223373..342d9c51 100644 --- a/Example/ExampleScene.unity +++ b/Example/ExampleScene.unity @@ -487,13 +487,9 @@ Prefab: propertyPath: m_RootOrder value: 6 objectReference: {fileID: 0} - - target: {fileID: 11425566, guid: 7adf26d256267454bb6f9b55470051d7, type: 2} - propertyPath: _speed - value: 131072 - objectReference: {fileID: 0} - - target: {fileID: 11425566, guid: 7adf26d256267454bb6f9b55470051d7, type: 2} - propertyPath: _canTurn - value: 1 + - target: {fileID: 11447240, guid: 7adf26d256267454bb6f9b55470051d7, type: 2} + propertyPath: _maxHealth + value: 6553600 objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: 7adf26d256267454bb6f9b55470051d7, type: 2} @@ -712,6 +708,12 @@ MonoBehaviour: Position: x: -655360 y: 0 + - AgentCode: TestAgent + Count: 100 + ControllerIndex: 1 + Position: + x: 1638400 + y: 1966080 --- !u!4 &1811928844 Transform: m_ObjectHideFlags: 0 diff --git a/Example/TestAgent.prefab b/Example/TestAgent.prefab index bd2ff148..72a74aee 100644 --- a/Example/TestAgent.prefab +++ b/Example/TestAgent.prefab @@ -242,7 +242,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: cc61d323ede6e5c4e934b394d3ec5080, type: 3} m_Name: m_EditorClassIdentifier: - _maxHealth: 65536000 + _maxHealth: 6553600 _currentHealth: 0 --- !u!114 &11479720 MonoBehaviour: