bool obstacleCheck(MOVEDIR dir, int range) { Vector3Int mycell = GameModeManager.instance.gameGrid.GetWorldFlToCellPos(transform.position); Vector3Int test = Vector3Int.zero; bool fail = false; switch (dir) { case MOVEDIR.E_LEFT: for (int i = 0; i <= range; ++i) { test.Set(mycell.x - i, mycell.y, mycell.z); if (TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_SOLIDWALL, test) || TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_DESTRUCTIBLE, test)) { fail = true; break; } } break; case MOVEDIR.E_RIGHT: for (int i = 0; i <= range; ++i) { test.Set(mycell.x + i, mycell.y, mycell.z); if (TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_SOLIDWALL, test) || TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_DESTRUCTIBLE, test)) { fail = true; break; } } break; case MOVEDIR.E_UP: for (int i = 0; i <= range; ++i) { test.Set(mycell.x, mycell.y + i, mycell.z); if (TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_SOLIDWALL, test) || TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_DESTRUCTIBLE, test)) { fail = true; break; } } break; case MOVEDIR.E_DOWN: for (int i = 0; i <= range; ++i) { test.Set(mycell.x, mycell.y - i, mycell.z); if (TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_SOLIDWALL, test) || TileRefManager.instance.GetTileAtCellPos(TileRefManager.TILEMAP_TYPE.TILEMAP_DESTRUCTIBLE, test)) { fail = true; break; } } break; } return(fail); }
// Use this for initialization void Start() { direction = getRandomDir(); canProceed = true; originalCellsFree = cellsFree; wpIndex = 0; safetyTimer = gameObject.AddComponent <TimerRoutine>(); safetyTimer.initTimer(3); safetyTimer.executedFunction = resetWaypoints; bomberobj = transform.GetChild(1).gameObject; reachedHiding = true; }
// Move Tux Grid-aligned private void gridMove() { if (dir == MOVEDIR.UP) { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x, gridPosition.y + 1)) { position += (Vector3)Vector3.up / moveDivisor; gridPosition += Vector3.up; } else { dir = MOVEDIR.DOWN; } } else if (dir == MOVEDIR.LEFT) { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x - 1, gridPosition.y)) { position += Vector3.left / moveDivisor; gridPosition += Vector3.left; } else { dir = MOVEDIR.RIGHT; } } else if (dir == MOVEDIR.DOWN) { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x, gridPosition.y - 1)) { position += Vector3.down / moveDivisor; gridPosition += Vector3.down; } else { dir = MOVEDIR.UP; } } else if (dir == MOVEDIR.RIGHT) { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x + 1, gridPosition.y)) { position += Vector3.right / moveDivisor; gridPosition += Vector3.right; } else { dir = MOVEDIR.LEFT; } } }
private bool recursionCheck(MOVEDIR dir, int range) { if (!obstacleCheck(dir, range)) { return(true); } else { --range; if (!obstacleCheck(dir, range)) { return(true); } return(false); } }
// Use this for initialization void Start() { speed = 2.0F; dir = MOVEDIR.DOWN; moveCooldown = 0; idle = true; moving = false; moveDivisor = 9; gridPosition = new Vector3(0, 3, 0); gameboard = FindObjectOfType <Gameboard>(); if (null == gameboard) { gameboard = ScriptableObject.CreateInstance <Gameboard>(); gameboard.Start(); } gridPosition = gameboard.getStartVector(); animator = GetComponent <Animator>(); flipAnimator = false; }
// Update is called once per frame void Update() { // if i can move if (canProceed) { // check if the cell in front of me is blocked if (!hasWaypoint) { if (direction == MOVEDIR.E_CENTRE) { direction = getRandomDir(); } if (!obstacleCheck(direction, 1)) { destination = convertDirToVec3(); // not blocked, get next cell to go to depending on my direction moveForward(); // move cell by cell canProceed = false; // until my sprite catches up to me, dont move cellsFree = originalCellsFree; // just resetting a variable } else { MOVEDIR randDir = getRandomDir(); // Blocked, get a random direction if (!obstacleCheck(randDir, cellsFree)) // if i meet with a wall, i will find a new path with preferably as many cells free as the range provided //if (recursionCheck(randDir, 3)) // not guaranteed to work yet, recursion is hard { direction = randDir; } else { cellsFree--; // check from max range to 0 } } } else if (hasWaypoint) // if i have a destination, pathfind { if (waypoints == null) // pathfind gives waypoint cells to move to { GetComponent <AStarPath>().init(GameModeManager.instance.gameGrid.GetWorldFlToCellPos(waypoint)); waypoints = GetComponent <AStarPath>().runPathFinding(); if (waypoints != null) { waypoints = checkWaypointValidity(waypoints); } } else // move { //hasWaypoint = false; if (wpIndex < waypoints.Count - 1) { wpIndex++; int nodeind = TileRefManager.instance.GetNodeIndex(waypoints[wpIndex]); if (!TileRefManager.instance.GetNode(nodeind).passable) { TileRefManager.instance.GetNode(nodeind).passable = true; //TileRefManager.instance.SetTile(TileRefManager.TILEMAP_TYPE.TILEMAP_ENEMY, waypoints[wpIndex], TileRefManager.instance.GetTileRef(TileRefManager.TILE_TYPE.TILE_GRASS)); } direction = convertVec3ToDir(bomberobj.transform.position, waypoints[wpIndex]); moveToWaypoint(); canProceed = false; } else { if (!safetyTimer.hasRun) { safetyTimer.executeFunction(); } direction = MOVEDIR.E_CENTRE; reachedHiding = false; } } //move //if (waypoints != null) //{ // wpIndex++; // checkWaypointValidity(); // for (int i = 0; i < waypoints.Count; ++i) // print(waypoints[i] + "co"); // moveToWaypoint(); // canProceed = false; // if (wpIndex >= waypoints.Count - 1) // { // waypoints = null; // } //} //else //{ // if (bomberobj.GetComponent<Bomber>().bombRef == null) // { // if (!safetyTimer.hasRun) // { // safetyTimer.executeFunction(); // } // } //} } } }
public void setDirection(int dir) { direction = (MOVEDIR)dir; }
// Move Tux Grid-aligned private void gridMove() { if (moveCooldown <= 0) { if (Input.GetKey(KeyCode.W) | Input.GetKey(KeyCode.UpArrow)) { if (dir != MOVEDIR.UP) { dir = MOVEDIR.UP; moveCooldown = 5; animator.Play("tux-up"); } else { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x, gridPosition.y + 1)) { idle = false; moving = true; position += (Vector3)Vector3.up / moveDivisor; gridPosition += Vector3.up; } } } else if (Input.GetKey(KeyCode.A) | Input.GetKey(KeyCode.LeftArrow)) { if (dir != MOVEDIR.LEFT) { dir = MOVEDIR.LEFT; moveCooldown = 5; if (flipAnimator == false) { animator.transform.Rotate(0, 180, 0); flipAnimator = true; } animator.Play("tux-left"); } else { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x - 1, gridPosition.y)) { idle = false; moving = true; position += Vector3.left / moveDivisor; gridPosition += Vector3.left; } } } else if (Input.GetKey(KeyCode.S) | Input.GetKey(KeyCode.DownArrow)) { if (dir != MOVEDIR.DOWN) { dir = MOVEDIR.DOWN; moveCooldown = 5; animator.Play("tux-idle"); } else { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x, gridPosition.y - 1)) { idle = false; moving = true; position += Vector3.down / moveDivisor; gridPosition += Vector3.down; } } } else if (Input.GetKey(KeyCode.D) | Input.GetKey(KeyCode.RightArrow)) { if (dir != MOVEDIR.RIGHT) { dir = MOVEDIR.RIGHT; moveCooldown = 5; if (flipAnimator == true) { animator.transform.Rotate(0, 180, 0); flipAnimator = false; } animator.Play("tux-right"); } else { if (CanMove(gridPosition.x, gridPosition.y, gridPosition.x + 1, gridPosition.y)) { idle = false; moving = true; position += Vector3.right / moveDivisor; gridPosition += Vector3.right; } } } } /* if (gameboard.GetSpace((int) gridPosition.x, (int) gridPosition.y).Equals("door")) * // { * Debug.Log("Calling door"); * } */ }