Пример #1
0
        public void AddObject(Actor obj, int x, int y)
        {
            if (IsSpaceFree(x,y))
            {
                Point specifiedPoint = new Point(x,y);

                //Inject into world
                SetObjectAtPoint(obj, specifiedPoint);

                //Set the object's world to this world
                obj.Layer = this;
                obj.Location = specifiedPoint;
            }
        }
Пример #2
0
        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;
        }
Пример #3
0
 public void SetObjectAtPoint(Actor obj, Point point)
 {
     SetObjectAtPoint(obj,point.X, point.Y);
 }
Пример #4
0
 public void SetObjectAtPoint(Actor obj, int x, int y)
 {
     Actors[y][x] = obj;
 }