// Update is called once per frame void Update() { if (GameManagerScript.Instance.curState == GameState.PlayerMove) { if (!hasMoved) { if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)) { yPos++; unitScript.SetDirection(Direction.NORTH); } else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) { yPos--; unitScript.SetDirection(Direction.SOUTH); } else if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) { xPos--; unitScript.SetDirection(Direction.WEST); } else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) { xPos++; unitScript.SetDirection(Direction.EAST); } else { return; } Move(); hasMoved = true; } else { moveTimer += Time.deltaTime; if (moveTimer > 0.1f) { moveTimer = 0.0f; GameManagerScript.Instance.curState = GameState.EnemyMove; hasMoved = false; } } } }
public void Move() { int newXPos = 0; int newYPos = 0; bool dirFound = false; for (int i = 0; i < (int)Direction.TOTAL; i++) { switch (unitScript.facingDir) { case Direction.EAST: newXPos = xPos + 1; newYPos = yPos; break; case Direction.WEST: newXPos = xPos - 1; newYPos = yPos; break; case Direction.NORTH: newXPos = xPos; newYPos = yPos + 1; break; case Direction.SOUTH: newXPos = xPos; newYPos = yPos - 1; break; } if (SpawnManagerScript.Instance.IsEnemyPresent(newXPos, newYPos)) { int newDir = (int)unitScript.facingDir + 1; if (newDir >= (int)Direction.TOTAL) { newDir = 0; } unitScript.SetDirection((Direction)newDir); } else { dirFound = true; //A direction is found break; } } if (!dirFound) { return; //If no direction the unit can go, stop } xPos = newXPos; yPos = newYPos; if (yPos >= (int)TileEngineScript.Instance.size.y) { yPos = (int)TileEngineScript.Instance.size.y - 1; unitScript.SetDirection(Direction.SOUTH); } if (xPos >= (int)TileEngineScript.Instance.size.x) { xPos = (int)TileEngineScript.Instance.size.x - 1; unitScript.SetDirection(Direction.WEST); } if (yPos < 0) { yPos = 0; unitScript.SetDirection(Direction.NORTH); } if (xPos < 0) { xPos = 0; unitScript.SetDirection(Direction.EAST); } transform.position = TileEngineScript.Instance.posMap[yPos, xPos]; }