Esempio n. 1
0
 public void AssignCommand(Command command)
 {
     if (command != null) {
         command.Actor = this;
         CommandQueue.Add(command);
     }
 }
        public static Command CreateMoveToCommand(Actor actor,Point destination, bool tryTouchRegardless = false)
        {
            Command newCommand = new Command();

            //Create a 2D byte array from the layer the actor is on. 1s represent a movable tile
            byte[,] moveableAreas = PathFinderHelpers.createByteGridFromLayer(actor.Layer);

            //Initialize the pathfinder with the byte array
            PathFinder pathFinder = new PathFinder(moveableAreas);

            //Try find the nodes in the found path
            List<Common.Direction> directions;
            List<PathFinderNode> nodes = pathFinder.FindPath(
                   new System.Drawing.Point(actor.Location.X, actor.Location.Y),
                   new System.Drawing.Point(destination.X, destination.Y)
               );

            //If we didnt get a direct result but want to try the surrounding points
            if (nodes == null || !nodes.Any() && tryTouchRegardless)
            {
                //Since you can't pathfind ONTOP the actor, we will test each free block around the destination
                List<Point> placesToTry = new List<Point>() {
                    destination.Add(Common.DirectionToPointOffset(Common.Direction.Up)),
                    destination.Add(Common.DirectionToPointOffset(Common.Direction.Down)),
                    destination.Add(Common.DirectionToPointOffset(Common.Direction.Left)),
                    destination.Add(Common.DirectionToPointOffset(Common.Direction.Right))
                };

                List<PathFinderNode> quickestPath = null;
                int quickestPathCount = int.MaxValue;

                foreach (Point point in placesToTry)
                {
                    nodes = pathFinder.FindPath(
                        new System.Drawing.Point(actor.Location.X, actor.Location.Y),
                        new System.Drawing.Point(point.X, point.Y)
                    );

                    if ((nodes != null && nodes.Count() < quickestPathCount))
                    {
                        //Found a new quickest path
                        quickestPath = new List<PathFinderNode>();
                        quickestPath.AddRange(nodes);
                        quickestPathCount = nodes.Count();
                    }
                }
                nodes = quickestPath;
            }

            directions = GetDirectionsFromNodeList(nodes);

            //If there are actual moves to make
            if (directions.Any())
            {
                foreach (Common.Direction direction in directions)
                {
                    newCommand.AddAction(new MoveAction(newCommand, direction));
                }
            }
            else {
                return null;
            }

            return newCommand;
        }
Esempio n. 3
0
        public void GameTick(EventArgs args)
        {
            //Ensure we have a command we're currently running
            if (CurrentCommand == null)
            {
                if (CommandQueue.Any())
                {
                    //Pop the next command off the stack
                    CurrentCommand = GetNextCommand();
                }
                else
                {
                    CurrentCommand = null;
                }
            }

            //Then try to execute any actions left in the command
            if (CurrentCommand != null && CurrentCommand.HasActionToPerform())
            {
                if (TicksTilAction == 0)
                {
                    //Perform the action
                    CurrentCommand.ExecuteNextAction();

                    //Set the ticks left until the actual actually performs
                    RecalculateTicksUntilAction();
                }
                else
                {
                    TicksTilAction--;
                }
            }
            else {
                CurrentCommand = GetNextCommand();
            }
        }
Esempio n. 4
0
 public Command GetNextCommand()
 {
     if (CommandQueue.Any())
     {
         Command command = CommandQueue.First();
         CommandQueue.Remove(command);
         CurrentCommand = command;
         return command;
     }
     else {
         return null;
     }
 }
Esempio n. 5
0
 public Action(Command command)
 {
     this.Command = command;
 }