/// <summary> /// Tries to move the agent in the specified horizontal and vertical direction using the specified CanAgentMoveBy method /// to check whether the agent can move in that direction from it's current position. /// </summary> /// <param name="hDir">Horizontal dir.</param> /// <param name="vDir">Vertical dir.</param> /// <param name="CanMove">Method that will be used to check whether the agent can move in that direction from it's current position.</param> public void Move(HorizontalDirection hDir, VerticalDirection vDir, CanAgentMoveByDelegate CanMove) { if (hDir == HorizontalDirection.None && vDir == VerticalDirection.None) { return; } int xInc = 0, yInc = 0, layerInc = 0; switch (hDir) { case HorizontalDirection.North: yInc = 1; break; case HorizontalDirection.NorthEast: yInc = 1; xInc = 1; break; case HorizontalDirection.East: xInc = 1; break; case HorizontalDirection.SouthEast: yInc = -1; xInc = 1; break; case HorizontalDirection.South: yInc = -1; break; case HorizontalDirection.SouthWest: yInc = -1; xInc = -1; break; case HorizontalDirection.West: xInc = -1; break; case HorizontalDirection.NorthWest: yInc = 1; xInc = -1; break; } switch (vDir) { case VerticalDirection.Up: layerInc = 1; break; case VerticalDirection.Down: layerInc = -1; break; } // Call delegate method to determine if the agent can move to that cell int outX, outY, outLayer; if (CanMove(xInc, yInc, layerInc, out outX, out outY, out outLayer)) { // Move to cell CellCoords += new GridPosition(outX, outY, outLayer); transform.position += new Vector3(outX * Grid.CellWidth, outY * Grid.CellDepth + outLayer * Grid.CellHeight, 0); rend.sortingOrder = CellCoords.Layer - CellCoords.Y; } }
/// <summary> /// Detects keyboard input and translates it into calls to the <see cref="GridAgent.Move"/> method with the appropriate movement directions. /// Also selects which delegate should be used to restrict the movement of the agent in the grid. /// </summary> void Update() { HorizontalDirection hDir; VerticalDirection vDir; // Horizontal direction if (Input.GetKeyDown(KeyCode.UpArrow)) { if (Input.GetKeyDown(KeyCode.LeftArrow)) { hDir = HorizontalDirection.NorthWest; } else if (Input.GetKeyDown(KeyCode.RightArrow)) { hDir = HorizontalDirection.NorthEast; } else { hDir = HorizontalDirection.North; } } else if (Input.GetKeyDown(KeyCode.DownArrow)) { if (Input.GetKeyDown(KeyCode.LeftArrow)) { hDir = HorizontalDirection.SouthWest; } else if (Input.GetKeyDown(KeyCode.RightArrow)) { hDir = HorizontalDirection.SouthEast; } else { hDir = HorizontalDirection.South; } } else if (Input.GetKeyDown(KeyCode.LeftArrow)) { hDir = HorizontalDirection.West; } else if (Input.GetKeyDown(KeyCode.RightArrow)) { hDir = HorizontalDirection.East; } else { hDir = HorizontalDirection.None; } // Vertical direction if (Input.GetKeyDown(KeyCode.W)) { vDir = VerticalDirection.Up; } else if (Input.GetKeyDown(KeyCode.S)) { vDir = VerticalDirection.Down; } else { vDir = VerticalDirection.None; } // Movement type CanAgentMoveByDelegate moveByDelegate; switch (AgentType) { case MoveByDelegate.Ghost: moveByDelegate = new CanAgentMoveByDelegate(CanGhostMoveBy); break; case MoveByDelegate.Fly: moveByDelegate = new CanAgentMoveByDelegate(CanFlyMoveBy); break; case MoveByDelegate.WalkStrict: moveByDelegate = new CanAgentMoveByDelegate(CanWalkStrictMoveBy); break; case MoveByDelegate.WalkClimb: moveByDelegate = new CanAgentMoveByDelegate(CanWalkClimbMoveBy); break; default: moveByDelegate = new CanAgentMoveByDelegate(CanGhostMoveBy); break; } // Try to move Move(hDir, vDir, moveByDelegate); }
/// <summary> /// Tries to move the agent in the specified horizontal and vertical direction using the specified CanAgentMoveBy method /// to check whether the agent can move in that direction from it's current position. /// </summary> /// <param name="hDir">Horizontal dir.</param> /// <param name="vDir">Vertical dir.</param> /// <param name="CanMove">Method that will be used to check whether the agent can move in that direction from it's current position.</param> public void Move(HorizontalDirection hDir, VerticalDirection vDir, CanAgentMoveByDelegate CanMove) { if(hDir == HorizontalDirection.None && vDir == VerticalDirection.None) { return; } int xInc = 0, yInc = 0, layerInc = 0; switch(hDir) { case HorizontalDirection.North: yInc = 1; break; case HorizontalDirection.NorthEast: yInc = 1; xInc = 1; break; case HorizontalDirection.East: xInc = 1; break; case HorizontalDirection.SouthEast: yInc = - 1; xInc = 1; break; case HorizontalDirection.South: yInc = - 1; break; case HorizontalDirection.SouthWest: yInc = - 1; xInc = - 1; break; case HorizontalDirection.West: xInc = - 1; break; case HorizontalDirection.NorthWest: yInc = 1; xInc = - 1; break; } switch(vDir) { case VerticalDirection.Up: layerInc = 1; break; case VerticalDirection.Down: layerInc = - 1; break; } // Call delegate method to determine if the agent can move to that cell int outX, outY, outLayer; if(CanMove(xInc, yInc, layerInc, out outX, out outY, out outLayer)) { // Move to cell CellCoords += new GridPosition(outX, outY, outLayer); transform.position += new Vector3(outX * Grid.CellWidth, outY * Grid.CellDepth + outLayer * Grid.CellHeight, 0); rend.sortingOrder = CellCoords.Layer - CellCoords.Y; } }
/// <summary> /// Detects keyboard input and translates it into calls to the <see cref="GridAgent.Move"/> method with the appropriate movement directions. /// Also selects which delegate should be used to restrict the movement of the agent in the grid. /// </summary> void Update() { HorizontalDirection hDir; VerticalDirection vDir; // Horizontal direction if(Input.GetKeyDown(KeyCode.UpArrow)) { if(Input.GetKeyDown(KeyCode.LeftArrow)) { hDir = HorizontalDirection.NorthWest; } else if(Input.GetKeyDown(KeyCode.RightArrow)) { hDir = HorizontalDirection.NorthEast; } else { hDir = HorizontalDirection.North; } } else if(Input.GetKeyDown(KeyCode.DownArrow)) { if(Input.GetKeyDown(KeyCode.LeftArrow)) { hDir = HorizontalDirection.SouthWest; } else if(Input.GetKeyDown(KeyCode.RightArrow)) { hDir = HorizontalDirection.SouthEast; } else { hDir = HorizontalDirection.South; } } else if(Input.GetKeyDown(KeyCode.LeftArrow)) { hDir = HorizontalDirection.West; } else if(Input.GetKeyDown(KeyCode.RightArrow)) { hDir = HorizontalDirection.East; } else { hDir = HorizontalDirection.None; } // Vertical direction if(Input.GetKeyDown(KeyCode.W)) { vDir = VerticalDirection.Up; } else if(Input.GetKeyDown(KeyCode.S)) { vDir = VerticalDirection.Down; } else { vDir = VerticalDirection.None; } // Movement type CanAgentMoveByDelegate moveByDelegate; switch(AgentType) { case MoveByDelegate.Ghost: moveByDelegate = new CanAgentMoveByDelegate(CanGhostMoveBy); break; case MoveByDelegate.Fly: moveByDelegate = new CanAgentMoveByDelegate(CanFlyMoveBy); break; case MoveByDelegate.WalkStrict: moveByDelegate = new CanAgentMoveByDelegate(CanWalkStrictMoveBy); break; case MoveByDelegate.WalkClimb: moveByDelegate = new CanAgentMoveByDelegate(CanWalkClimbMoveBy); break; default: moveByDelegate = new CanAgentMoveByDelegate(CanGhostMoveBy); break; } // Try to move Move(hDir, vDir, moveByDelegate); }