diff --git a/Assets/Scripts/AI.cs b/Assets/Scripts/AI.cs index 5f9f756..c8d8544 100644 --- a/Assets/Scripts/AI.cs +++ b/Assets/Scripts/AI.cs @@ -9,38 +9,36 @@ public class AI : MonoBehaviour { private List tiles = new List(); private TileManager manager; - public GhostMove blinky; + public GhostMove ghost; public TileManager.Tile nextTile = null; + public TileManager.Tile targetTile; + TileManager.Tile currentTile; void Awake() { manager = GameObject.Find("Game Manager").GetComponent(); tiles = manager.tiles; - if(blinky == null) Debug.Log ("game object blinky not found"); + if(ghost == null) Debug.Log ("game object ghost not found"); if(manager == null) Debug.Log ("game object Game Manager not found"); } void FixedUpdate() { - if(blinky.AI){ - - // get the target tile position (round it down to int so we can reach with Index() function) - Vector3 targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f); - TileManager.Tile targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; - + if(ghost.AI){ // get current tile Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f); - TileManager.Tile currentTile = tiles[manager.Index ((int)currentPos.x, (int)currentPos.y)]; + currentTile = tiles[manager.Index ((int)currentPos.x, (int)currentPos.y)]; - //Debug.Log (blinky.direction.x + " " + blinky.direction.y); + targetTile = GetTargetTilePerGhost(); + //Debug.Log ("Target Tile: " + targetTile.x + ", " + targetTile.y); // get the next tile according to direction - if(blinky.direction.x > 0) nextTile = tiles[manager.Index ((int)(currentPos.x+1), (int)currentPos.y)]; - if(blinky.direction.x < 0) nextTile = tiles[manager.Index ((int)(currentPos.x-1), (int)currentPos.y)]; - if(blinky.direction.y > 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y+1))]; - if(blinky.direction.y < 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y-1))]; + if(ghost.direction.x > 0) nextTile = tiles[manager.Index ((int)(currentPos.x+1), (int)currentPos.y)]; + if(ghost.direction.x < 0) nextTile = tiles[manager.Index ((int)(currentPos.x-1), (int)currentPos.y)]; + if(ghost.direction.y > 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y+1))]; + if(ghost.direction.y < 0) nextTile = tiles[manager.Index ((int)currentPos.x, (int)(currentPos.y-1))]; if(nextTile.occupied || currentTile.isIntersection) { @@ -48,19 +46,19 @@ void FixedUpdate() // IF WE BUMP INTO WALL if(nextTile.occupied && !currentTile.isIntersection) { - // if blinky moves to right or left and there is wall next tile - if(blinky.direction.x != 0) + // if ghost moves to right or left and there is wall next tile + if(ghost.direction.x != 0) { - if(currentTile.down == null) blinky.direction = Vector3.up; - else blinky.direction = Vector3.down; + if(currentTile.down == null) ghost.direction = Vector3.up; + else ghost.direction = Vector3.down; } - // if blinky moves to up or down and there is wall next tile - else if(blinky.direction.y != 0) + // if ghost moves to up or down and there is wall next tile + else if(ghost.direction.y != 0) { - if(currentTile.left == null) blinky.direction = Vector3.right; - else blinky.direction = Vector3.left; + if(currentTile.left == null) ghost.direction = Vector3.right; + else ghost.direction = Vector3.left; } @@ -74,16 +72,16 @@ void FixedUpdate() float dist1, dist2, dist3, dist4; dist1 = dist2 = dist3 = dist4 = 999999f; - if(currentTile.up != null && !currentTile.up.occupied && !(blinky.direction.y < 0)) dist1 = manager.distance(currentTile.up, targetTile); - if(currentTile.down != null && !currentTile.down.occupied && !(blinky.direction.y > 0)) dist2 = manager.distance(currentTile.down, targetTile); - if(currentTile.left != null && !currentTile.left.occupied && !(blinky.direction.x > 0)) dist3 = manager.distance(currentTile.left, targetTile); - if(currentTile.right != null && !currentTile.right.occupied && !(blinky.direction.x < 0)) dist4 = manager.distance(currentTile.right, targetTile); + if(currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) dist1 = manager.distance(currentTile.up, targetTile); + if(currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) dist2 = manager.distance(currentTile.down, targetTile); + if(currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) dist3 = manager.distance(currentTile.left, targetTile); + if(currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) dist4 = manager.distance(currentTile.right, targetTile); float min = Mathf.Min(dist1, dist2, dist3, dist4); - if(min == dist1) blinky.direction = Vector3.up; - if(min == dist2) blinky.direction = Vector3.down; - if(min == dist3) blinky.direction = Vector3.left; - if(min == dist4) blinky.direction = Vector3.right; + if(min == dist1) ghost.direction = Vector3.up; + if(min == dist2) ghost.direction = Vector3.down; + if(min == dist3) ghost.direction = Vector3.left; + if(min == dist4) ghost.direction = Vector3.right; } @@ -92,9 +90,58 @@ void FixedUpdate() // if there is no decision to be made, designate next waypoint for the ghost else { - blinky.direction = blinky.direction; // setter updates the waypoint + ghost.direction = ghost.direction; // setter updates the waypoint } } } + + TileManager.Tile GetTargetTilePerGhost() + { + Vector3 targetPos; + TileManager.Tile targetTile; + Vector3 dir; + + // get the target tile position (round it down to int so we can reach with Index() function) + switch(name) + { + case "blinky": // target = pacman + targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f); + targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; + break; + case "pinky": // target = pacman + 4*pacman's direction (4 steps ahead of pacman) + dir = target.GetComponent().getDir(); + targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f) + 4*dir; + + // if pacmans going up, not 4 ahead but 4 up and 4 left is the target + // read about it here: http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior + // so subtract 4 from X coord from target position + if(dir == Vector3.up) targetPos -= new Vector3(4, 0, 0); + + targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; + break; + case "inky": // target = ambushVector(pacman+2 - blinky) added to pacman+2 + dir = target.GetComponent().getDir(); + Vector3 blinkyPos = GameObject.Find ("blinky").transform.position; + Vector3 ambushVector = target.position + 2*dir - blinkyPos ; + targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f) + 2*dir + ambushVector; + targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; + break; + case "clyde": + targetPos = new Vector3 (target.position.x+0.499f, target.position.y+0.499f); + targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)]; + if(manager.distance(targetTile, currentTile) < 9) + targetTile = tiles[manager.Index (0, 2)]; + break; + default: + targetTile = null; + Debug.Log ("TARGET TILE NOT ASSIGNED"); + break; + + } + + + + return targetTile; + } } \ No newline at end of file diff --git a/Assets/Scripts/GhostMove.cs b/Assets/Scripts/GhostMove.cs index ba79ad8..8bb183f 100644 --- a/Assets/Scripts/GhostMove.cs +++ b/Assets/Scripts/GhostMove.cs @@ -185,7 +185,7 @@ void OnTriggerEnter2D(Collider2D other) if(other.name == "pacman") { //Destroy(other.gameObject); - PlayerController.LoseLife(); + //PlayerController.LoseLife(); } } diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index cabfbb4..ad15ae5 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -6,6 +6,7 @@ public class PlayerController : MonoBehaviour { public static int lives = 3; public float speed = 0.4f; Vector2 dest = Vector2.zero; + Vector2 dir = Vector2.right; // script handles static UIScript UI; @@ -38,6 +39,7 @@ void FixedUpdate () StartCoroutine("PlayDeadAnimation"); break; } + } @@ -92,14 +94,31 @@ void readInputAndMove() // Check for Input if not moving if ((Vector2)transform.position == dest) { if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up)) + { dest = (Vector2)transform.position + Vector2.up; + dir = Vector2.up; + } if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right)) + { dest = (Vector2)transform.position + Vector2.right; + dir = Vector2.right; + } if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up)) + { dest = (Vector2)transform.position - Vector2.up; + dir = -Vector2.up; + } if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right)) + { dest = (Vector2)transform.position - Vector2.right; + dir = -Vector2.right; + } } } + public Vector2 getDir() + { + return dir; + } + } diff --git a/Assets/Scripts/TargetGizmo.cs b/Assets/Scripts/TargetGizmo.cs new file mode 100644 index 0000000..94c860a --- /dev/null +++ b/Assets/Scripts/TargetGizmo.cs @@ -0,0 +1,24 @@ +using UnityEngine; +using System.Collections; + +public class TargetGizmo : MonoBehaviour { + + public GameObject ghost; + + // Use this for initialization + void Start () + { + + } + + // Update is called once per frame + void Update () + { + if(ghost.GetComponent().targetTile != null) + { + Vector3 pos = new Vector3(ghost.GetComponent().targetTile.x, + ghost.GetComponent().targetTile.y, 0f); + transform.position = pos; + } + } +} diff --git a/Assets/Scripts/TargetGizmo.cs.meta b/Assets/Scripts/TargetGizmo.cs.meta new file mode 100644 index 0000000..2abe539 --- /dev/null +++ b/Assets/Scripts/TargetGizmo.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 639eefc5c0711a442a6dd627f8069587 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Scripts/TileManager.cs b/Assets/Scripts/TileManager.cs index 5425a94..ad7d735 100644 --- a/Assets/Scripts/TileManager.cs +++ b/Assets/Scripts/TileManager.cs @@ -131,12 +131,28 @@ void DrawNeighbors() } } - + + //---------------------------------------------------------------------- // returns the index in the tiles list of a given tile's coordinates public int Index(int X, int Y) { - return (31-Y)*28 + X-1; + // if the requsted index is in bounds + //Debug.Log ("Index called for X: " + X + ", Y: " + Y); + if(X>=1 && X<=28 && Y<=31 && Y>=1) + return (31-Y)*28 + X-1; + + // else, if the requested index is out of bounds + // return closest in-bounds tile's index + else + { + if(X<1) X = 1; + if(X>28) X = 28; + if(Y<1) Y = 1; + if(Y>31) Y = 31; + + return (31-Y)*28 + X-1; + } } public int Index(Tile tile) diff --git a/Assets/game.unity b/Assets/game.unity index d2c2e0b..137846b 100644 Binary files a/Assets/game.unity and b/Assets/game.unity differ