protected BattleTurn.ActionArgs PerformAction(Player[] players, HexGrid grid, bool commitAction) { //if in range of target => attack if (CoordRange.CoordInRange(cubeCoordinate, target.CubeCoordinate, Stats.Range.Value)) { if (commitAction) { BasicAttack(players, target, grid); } return(new BattleTurn.ActionArgs(this, target, BattleTurn.updateAction.Attack, null, -1)); } //if out of range => try to move into range, move by step distance in unit stats. else { //follow shortest path if possible. Vector3[] path = AStarPathFinder.GetPath(cubeCoordinate, target.CubeCoordinate, Stats.Range.Value, grid.Size, grid.GetAllOccupiedTiles()); if (!(path == null)) { //calculate the index of the path to move to depending on move distance. // i.e. a unit with move distance 2 can skip the first location on the path, // and move to the second in one update call. int moveIndex = (Stats.MoveDistance.Value > path.Length) ? path.Length - 1 : Stats.MoveDistance.Value - 1; Vector3 stepDestination = path[moveIndex]; if (commitAction) { MoveToTile(grid, stepDestination); } //return action log data about what move the unit is making i.e. (this unit is acting, its moving, along path specified)... return(new BattleTurn.ActionArgs(this, target, BattleTurn.updateAction.Move, path, moveIndex)); } //if no possible path is found, do nothing - there is no possible move. return(new BattleTurn.ActionArgs(this, null, BattleTurn.updateAction.None, null, -1)); } }
private Command DoMouseMovement() { var destination = InputManager.GetMouseWorldPosition(Program.Game.Camera); if (Entity.X == destination.X && Entity.Y == destination.Y) { return(null); } if ((Program.Game.Map.FovMap.IsInFov(destination.X, destination.Y) || Program.Game.Map.IsExplored(destination.X, destination.Y))) { if (Program.Game.Map.CanEnter(destination.X, destination.Y)) { Program.Game.Map.FovMap.SetCellProperties(Entity.X, Entity.Y, true, true); CurrentPath = AStarPathFinder.GetPath(new Point(Entity.X, Entity.Y), destination); if (CurrentPath != null) { CurrentPath.Dequeue(); // Discard starting point var step = CurrentPath.Dequeue(); var x = step.X - Entity.X; var y = step.Y - Entity.Y; if (CurrentPath.Count == 0) { CurrentPath = null; } return(new MoveCommand(x, y)); } } else { var target = Program.Game.Entities.FirstOrDefault(e => e.HasComponent <FighterComponent>() && e.X == destination.X && e.Y == destination.Y); if (target != null && Program.Game.Player.DistanceTo(target) < 2) { return(new AttackCommand(target)); } } } return(null); }
internal List <ATile> GetPath(ATile a_origin, ATile a_dest) { AStarPathFinder pathFinder = new AStarPathFinder(); return(pathFinder.GetPath(floors, a_origin, a_dest)); }