public GameController.direction GetAction(GameMaze m, GameController c, xyLoc yourLoc) { if (c.CanMove(yourLoc, currentDirection)) { lastDirection = currentDirection; return(currentDirection); } return(lastDirection); }
// Use this for initialization void Start() { objs = new GameObject[GameMaze.boardHeight, GameMaze.boardWidth]; maze = new GameMaze(); for (int x = 0; x < GameMaze.boardWidth; x++) { for (int z = 0; z < GameMaze.boardHeight; z++) { switch (maze.GetCellType(x, z)) { case GameMaze.occupancy.kWall: { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(x + 0.5f, 0.5f, z + 0.5f); cube.GetComponent <Renderer> ().material.color = Color.blue; objs [z, x] = cube; } break; case GameMaze.occupancy.kSpawnWall: { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(x + 0.5f, 0.5f, z + 0.5f); cube.transform.localScale = new Vector3(1f, 1f, 0.25f); cube.GetComponent <Renderer> ().material.color = Color.yellow; objs [z, x] = cube; } break; case GameMaze.occupancy.kDot: { GameObject dot = GameObject.CreatePrimitive(PrimitiveType.Sphere); dot.transform.position = new Vector3(x + 0.5f, 0.5f, z + 0.5f); dot.transform.localScale = new Vector3(0.25f, 0.25f, 0.25f); dot.GetComponent <Renderer>().material.color = new Vector4(233f / 255f, 196f / 255f, 184f / 255f, 1.0f); objs [z, x] = dot; } break; case GameMaze.occupancy.kPowerUp: { GameObject dot = GameObject.CreatePrimitive(PrimitiveType.Sphere); dot.transform.position = new Vector3(x + 0.5f, 0.5f, z + 0.5f); dot.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f); dot.GetComponent <Renderer>().material.color = new Vector4(233f / 255f, 196f / 255f, 184f / 255f, 1.0f); objs [z, x] = dot; } break; default: break; } } } }
void AddNeighbors(Queue <xyLoc> q, GameMaze m, int x, int y) { if (m.CanMove(x, y, x + 1, y)) { q.Enqueue(new xyLoc(x + 1, y)); } if (m.CanMove(x, y, x - 1, y)) { q.Enqueue(new xyLoc(x - 1, y)); } if (m.CanMove(x, y, x, y + 1)) { q.Enqueue(new xyLoc(x, y + 1)); } if (m.CanMove(x, y, x, y - 1)) { q.Enqueue(new xyLoc(x, y - 1)); } }
public GameController.direction GetAction(GameMaze m, GameController c, xyLoc yourLoc) { if ((yourLoc.x / 8 != lastx || yourLoc.y / 8 != lasty) && (yourLoc.x % 8 == 0 && yourLoc.y % 8 == 0)) { lastx = yourLoc.x / 8; lasty = yourLoc.y / 8; GetDistances(c, m, lastx, lasty); float lUtil, rUtil, dUtil, uUtil; lUtil = GetDirectionUtility(c, m, GameController.direction.kLeft); rUtil = GetDirectionUtility(c, m, GameController.direction.kRight); dUtil = GetDirectionUtility(c, m, GameController.direction.kDown); uUtil = GetDirectionUtility(c, m, GameController.direction.kUp); float max = Mathf.NegativeInfinity; if (lUtil > max) { lastDirection = GameController.direction.kLeft; max = lUtil; } if (rUtil > max) { lastDirection = GameController.direction.kRight; max = rUtil; } if (dUtil > max) { lastDirection = GameController.direction.kDown; max = dUtil; } if (uUtil > max) { lastDirection = GameController.direction.kUp; max = uUtil; } } return(lastDirection); }
void GetDistances(GameController c, GameMaze m, int x, int y) { dotx = doty = 1000; pelletx = pellety = 1000; for (int xLoc = 0; xLoc < GameMaze.boardWidth; xLoc++) { for (int yLoc = 0; yLoc < GameMaze.boardHeight; yLoc++) { distances[xLoc, yLoc] = 1000; } } distances [x, y] = 0; // Here we are using xyLoc to store grid cells, not pixel cells Queue <xyLoc> q = new Queue <xyLoc> (); AddNeighbors(q, m, x, y); while (q.Count > 0) { xyLoc l = q.Dequeue(); if (distances[l.x, l.y] == 1000) { if (dotx == 1000 && (m.GetCellType(l.x, l.y) == GameMaze.occupancy.kDot)) { dotx = l.x; doty = l.y; } if (pelletx == 1000 && m.GetCellType(l.x, l.y) == GameMaze.occupancy.kPowerUp) { pelletx = l.x; pellety = l.y; } distances [l.x, l.y] = GetBestNeighborDist(l.x, l.y) + 1; AddNeighbors(q, m, l.x, l.y); } } }
public GameController.direction GetAction(GameMaze m, GameController c, xyLoc myLoc) { timeElapsed += Time.deltaTime; // Ghosts are forced to reverse direction by the system anytime the mode changes from: // chase-to-scatter // chase-to-frightened // scatter-to-chase // scatter-to-frightened if ((lastMode == ghostMode.kChase && currentMode == ghostMode.kScatter) || (lastMode == ghostMode.kChase && currentMode == ghostMode.kFrightened) || (lastMode == ghostMode.kScatter && currentMode == ghostMode.kChase) || (lastMode == ghostMode.kScatter && currentMode == ghostMode.kFrightened)) { lastMode = currentMode; lastDirection = c.InvertDirection(lastDirection); return(lastDirection); } if (!c.AtIntersection(myLoc)) { return(lastDirection); } GameController.direction forbiddenDirection = c.InvertDirection(lastDirection); int targetx, targety; switch (currentMode) { case ghostMode.kChase: if (timeElapsed >= 20) // Implement: when do you leave chase mode? { lastMode = currentMode; //currentMode = // What mode do you change to? currentMode = ghostMode.kScatter; timeElapsed = 0; } GetTarget(out targetx, out targety, c); lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c); return(lastDirection); case ghostMode.kScatter: if (timeElapsed >= 7) // Implement: when do you leave chase mode? { lastMode = currentMode; //currentMode = // What mode do you change to? currentMode = ghostMode.kChase; timeElapsed = 0; } GetTarget(out targetx, out targety, c); lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c); return(lastDirection); case ghostMode.kDead: GetTarget(out targetx, out targety, c); // Reached home location if (myLoc.x / 8 == targetx && myLoc.y / 8 == targety) { lastMode = currentMode; //currentMode = // What mode do you change to? Debug.Log("Reached target from death"); currentMode = ghostMode.kChase; this.GetComponent <Renderer> ().material.color = this.GetComponent <GhostSpecialization> ().GetBaseColor(); this.transform.localScale = Vector3.one; timeElapsed = 0; } else { lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c); } return(lastDirection); case ghostMode.kFrightened: lastDirection = MakeRandomAction(myLoc, forbiddenDirection, c); return(lastDirection); case ghostMode.kLeavingHouse: // Ready to leave home area GetTarget(out targetx, out targety, c); if (myLoc.x / 8 == targetx && myLoc.y / 8 == targety) { lastMode = currentMode; //currentMode = // What mode do you change to? currentMode = ghostMode.kScatter; timeElapsed = 0; } else { lastDirection = MoveToTarget(targetx, targety, myLoc, forbiddenDirection, c); } return(lastDirection); case ghostMode.kInHouse: if (timeElapsed > this.GetComponent <GhostSpecialization> ().GetStartDelay()) { lastMode = currentMode; //currentMode = // What mode do you change to? currentMode = ghostMode.kLeavingHouse; timeElapsed = 0; } break; } // Should never get here return(GameController.direction.kNone); }
xyLoc HandleAction(xyLoc currLoc, ControlledObject obj, bool canEat) { GameMaze m = board.GetComponent <BoardController> ().GetMaze(); GameController.direction act = obj.GetAction(m, this, currLoc); switch (act) { case direction.kUp: if (CanMove(currLoc, direction.kUp)) { currLoc.y++; if (canEat) { pacmanLastMove = direction.kUp; } } break; case direction.kDown: if (CanMove(currLoc, direction.kDown)) { currLoc.y--; if (canEat) { pacmanLastMove = direction.kDown; } } break; case direction.kRight: if (CanMove(currLoc, direction.kRight)) { currLoc.x++; if (canEat) { pacmanLastMove = direction.kRight; } } break; case direction.kLeft: if (CanMove(currLoc, direction.kLeft)) { currLoc.x--; if (canEat) { pacmanLastMove = direction.kLeft; } } break; } if (currLoc.x < 0) { currLoc.x = pixelWidth - 9; } if (currLoc.x >= pixelWidth - 8) { currLoc.x = 0; } if (AtGridCenter(currLoc) && canEat) { bool powerUp, dot; eatenDots += board.GetComponent <BoardController> ().ClearCell(currLoc.x / 8, currLoc.y / 8, out dot, out powerUp); if (powerUp) { StartPowerUp(); GetSound(gameSound.kEatPowerup).Play(); } else if (dot) { GetSound(gameSound.kEatDot).Play(); } if (eatenDots == 244) { eatenDots = 0; currentGameMode = gameMode.kLevelComplete; GetSound(gameSound.kBackgroundSound).Stop(); } } return(currLoc); }
public bool CanMove(xyLoc loc, direction d) { GameMaze m = board.GetComponent <BoardController> ().GetMaze(); GameMaze.occupancy currCellType, nextCellType; switch (d) { case direction.kUp: if (loc.x % 8 != 0) { return(false); } if (loc.y % 8 != 0) { return(true); } currCellType = m.GetCellType(loc.x / 8, loc.y / 8); nextCellType = m.GetCellType(loc.x / 8, loc.y / 8 + 1); if (currCellType == GameMaze.occupancy.kSpawn && nextCellType == GameMaze.occupancy.kSpawn) { return(true); } if (currCellType == GameMaze.occupancy.kSpawn && nextCellType == GameMaze.occupancy.kSpawnWall) { return(true); } if (nextCellType == GameMaze.occupancy.kSpawn || nextCellType == GameMaze.occupancy.kWall || nextCellType == GameMaze.occupancy.kSpawnWall) { return(false); } return(true); case direction.kDown: if (loc.x % 8 != 0) { return(false); } if (loc.y % 8 != 0) { return(true); } currCellType = m.GetCellType(loc.x / 8, loc.y / 8); nextCellType = m.GetCellType(loc.x / 8, loc.y / 8 - 1); if (currCellType == GameMaze.occupancy.kSpawn && nextCellType == GameMaze.occupancy.kSpawn) { return(true); } if (currCellType == GameMaze.occupancy.kSpawn && nextCellType == GameMaze.occupancy.kSpawnWall) { return(true); } if (nextCellType == GameMaze.occupancy.kSpawn || nextCellType == GameMaze.occupancy.kWall || nextCellType == GameMaze.occupancy.kSpawnWall) { return(false); } return(true); case direction.kRight: return((loc.y % 8 == 0) && (loc.x >= pixelWidth - 8 - 1 || loc.x % 8 != 0 || (m.GetCellType(loc.x / 8 + 1, loc.y / 8) != GameMaze.occupancy.kWall))); case direction.kLeft: return((loc.y % 8 == 0) && (loc.x == 0 || loc.x % 8 != 0 || (m.GetCellType(loc.x / 8 - 1, loc.y / 8) != GameMaze.occupancy.kWall))); } return(true); }
float GetDirectionUtility(GameController c, GameMaze m, GameController.direction dir) { xyLoc ghostLoc = GetClosestGhostLoc(c); float util = 0; int x = c.pacmanLoc.x / 8; int y = c.pacmanLoc.y / 8; xyLoc nextSpot = new xyLoc(); switch (dir) { case GameController.direction.kLeft: { nextSpot = new xyLoc(x - 1, y); if (ghostLoc.x >= x) { util += GetGhostFleeValue(c) * GhostFleeM; } } break; case GameController.direction.kRight: { nextSpot = new xyLoc(x + 1, y); if (ghostLoc.x <= x) { util += GetGhostFleeValue(c) * GhostFleeM; } } break; case GameController.direction.kDown: { nextSpot = new xyLoc(x, y - 1); if (ghostLoc.y >= y) { util += GetGhostFleeValue(c) * GhostFleeM; } } break; case GameController.direction.kUp: { nextSpot = new xyLoc(x, y + 1); if (ghostLoc.y <= y) { util += GetGhostFleeValue(c) * GhostFleeM; } } break; default: break; } if (m.GetCellType(nextSpot) == GameMaze.occupancy.kWall) { return(-1); } if (powerUp) { util += GetChaseGhostValue(c) * ChaseGhostM; } if (MoveTowards(nextSpot) != MoveTowards(ghostLoc)) { util += GetGhostFleeValue(c) * GhostFleeM; } if (MoveTowards(nextSpot) == MoveTowards(dotx, doty)) { util += GetChaseDotValue(c, new xyLoc(dotx, doty)) * ChaseDotM; } if (powerUpsGotten < 4 && MoveTowards(nextSpot) == MoveTowards(pelletx, pellety)) { util += GetGhostFleeValue(c) * ChasePowerPelletM; } util = Mathf.Clamp(util, 0, 1); return(util); }