Beispiel #1
0
        protected override BehaviourTreeStatus internalUpdate(GameTime gameTime)
        {
            var movingEntity = (MovingEntity)subject;
            var target       = SimulationGame.World.LivingEntities.ContainsKey(targetID) ? SimulationGame.World.LivingEntities[targetID] : null;

            if (target == null)
            {
                movingEntity.StopWalking();
                return(BehaviourTreeStatus.Failure);
            }

            if (GeometryUtils.VectorsWithinDistance(movingEntity.Position, target.Position, tillDistance))
            {
                movingEntity.StopWalking();
                return(BehaviourTreeStatus.Success);
            }
            else
            {
                if (teleportDistance > 0 && movingEntity.InteriorID == target.InteriorID && GeometryUtils.GetEuclideanDistance(movingEntity.Position, target.Position) > teleportDistance)
                {
                    var teleportBlockDistance = teleportDistance / WorldGrid.BlockSize.X;
                    var teleportPosition      = AIUtils.GetWalkablePositionCloseTo(movingEntity.Position, target.Position, (int)((teleportBlockDistance + 16) - teleportBlockDistance) * WorldGrid.BlockSize.X);

                    if (teleportPosition != null)
                    {
                        movingEntity.UpdatePosition(teleportPosition);

                        return(BehaviourTreeStatus.Running);
                    }
                }

                // Only rewalk if new position is greater than 3 blocks
                if (movingEntity.InteriorID == target.InteriorID && movingEntity.DestBlockPosition != null && GeometryUtils.VectorsWithinDistance(movingEntity.DestBlockPosition, target.Position.ToBlockPosition(), 5))
                {
                    return(BehaviourTreeStatus.Running);
                }

                movingEntity.WalkToPosition(target.Position);
                return(BehaviourTreeStatus.Running);
            }
        }