Skip to content

Commit

Permalink
Ghost follow AI is completed.
Browse files Browse the repository at this point in the history
  • Loading branch information
vilbeyli committed Jan 23, 2015
1 parent 79f26df commit 4b44718
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 34 deletions.
109 changes: 78 additions & 31 deletions Assets/Scripts/AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,56 @@ public class AI : MonoBehaviour {

private List<TileManager.Tile> tiles = new List<TileManager.Tile>();
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<TileManager>();
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)
{
//---------------------
// 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;

}

Expand All @@ -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;

}

Expand All @@ -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<PlayerController>().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<PlayerController>().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;
}
}
2 changes: 1 addition & 1 deletion Assets/Scripts/GhostMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ void OnTriggerEnter2D(Collider2D other)
if(other.name == "pacman")
{
//Destroy(other.gameObject);
PlayerController.LoseLife();
//PlayerController.LoseLife();
}
}

Expand Down
19 changes: 19 additions & 0 deletions Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,6 +39,7 @@ void FixedUpdate ()
StartCoroutine("PlayDeadAnimation");
break;
}


}

Expand Down Expand Up @@ -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;
}

}
24 changes: 24 additions & 0 deletions Assets/Scripts/TargetGizmo.cs
Original file line number Diff line number Diff line change
@@ -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<AI>().targetTile != null)
{
Vector3 pos = new Vector3(ghost.GetComponent<AI>().targetTile.x,
ghost.GetComponent<AI>().targetTile.y, 0f);
transform.position = pos;
}
}
}
8 changes: 8 additions & 0 deletions Assets/Scripts/TargetGizmo.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions Assets/Scripts/TileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Binary file modified Assets/game.unity
Binary file not shown.

0 comments on commit 4b44718

Please sign in to comment.