Jogo Pacman-problemas com os fantasmas
Página 1 de 1
Jogo Pacman-problemas com os fantasmas
Alguém me ajuda com um problema que não estou conseguindo resolver, por favor! Irei colocar um link para o vídeo mostrando o problema, basicamente é que os 3 fantasmas que estão dentro do negocio não seguem certinho o waypoints eles saem passando por tudo e vão embora e deveria ser igual o do fantasma vermelho mas com outros caminhos, deveria seguir os waypoints certinhos deles e quando tiver perto do pacman seguir ele, e com o estado de animação em right sem parar, irei mandar os códigos que estão implementados nele e mostro no começo do vídeo como está implementado os códigos em cada!
[url= https://youtu.be/ADlxPra_1-k]https://youtu.be/ADlxPra_1-k[/url]
AI----------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AI : MonoBehaviour
{
public Transform target;
private List<TileManager.Tile> tiles = new List<TileManager.Tile>();
private TileManager manager;
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;
}
public void AILogic()
{
Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f);
currentTile = tiles[manager.Index((int)currentPos.x, (int)currentPos.y)];
targetTile = GetTargetTilePerGhost();
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 (nextTile.occupied && !currentTile.isIntersection)
{
if (ghost.direction.x != 0)
{
if (currentTile.down == null) ghost.direction = Vector3.up;
else ghost.direction = Vector3.down;
}
else if (ghost.direction.y != 0)
{
if (currentTile.left == null) ghost.direction = Vector3.right;
else ghost.direction = Vector3.left;
}
}
if (currentTile.isIntersection)
{
float dist1, dist2, dist3, dist4;
dist1 = dist2 = dist3 = dist4 = 999999f;
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) 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;
}
}
else
{
ghost.direction = ghost.direction;
}
}
public void RunLogic()
{
Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f);
currentTile = tiles[manager.Index((int)currentPos.x, (int)currentPos.y)];
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 (nextTile.occupied && !currentTile.isIntersection)
{
if (ghost.direction.x != 0)
{
if (currentTile.down == null) ghost.direction = Vector3.up;
else ghost.direction = Vector3.down;
}
else if (ghost.direction.y != 0)
{
if (currentTile.left == null) ghost.direction = Vector3.right;
else ghost.direction = Vector3.left;
}
}
if (currentTile.isIntersection)
{
List<TileManager.Tile> availableTiles = new List<TileManager.Tile>();
TileManager.Tile chosenTile;
if (currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) availableTiles.Add(currentTile.up);
if (currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) availableTiles.Add(currentTile.down);
if (currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) availableTiles.Add(currentTile.left);
if (currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) availableTiles.Add(currentTile.right);
int rand = Random.Range(0, availableTiles.Count);
chosenTile = availableTiles[rand];
ghost.direction = Vector3.Normalize(new Vector3(chosenTile.x - currentTile.x, chosenTile.y - currentTile.y, 0));
}
}
else
{
ghost.direction = ghost.direction;
}
}
TileManager.Tile GetTargetTilePerGhost()
{
Vector3 targetPos;
TileManager.Tile targetTile;
Vector3 dir;
switch (name)
{
case "blinky":
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":
dir = target.GetComponent<PlayerController>().getDir();
targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 4 * dir;
if (dir == Vector3.up) targetPos -= new Vector3(4, 0, 0);
targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)];
break;
case "inky":
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;
break;
}
return targetTile;
}
}
GhostMove--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class GhostMove : MonoBehaviour
{
private Vector3 waypoint;
private Queue<Vector3> waypoints;
public Vector3 _direction;
public Vector3 direction
{
get
{
return _direction;
}
set
{
_direction = value;
Vector3 pos = new Vector3((int)transform.position.x, (int)transform.position.y, (int)transform.position.z);
waypoint = pos + _direction;
}
}
public float speed = 0.3f;
public float scatterLength = 5f;
public float waitLength = 0.0f;
private float timeToEndScatter;
private float timeToEndWait;
enum State { Wait, Init, Scatter, Chase, Run };
State state;
private Vector3 _startPos;
private float _timeToWhite;
private float _timeToToggleWhite;
private float _toggleInterval;
private bool isWhite = false;
public GameGUINavigation GUINav;
public PlayerController pacman;
private GameManager _gm;
void Start()
{
_gm = GameObject.Find("Game Manager").GetComponent<GameManager>();
_toggleInterval = _gm.scareLength * 0.33f * 0.20f;
InitializeGhost();
}
public float DISTANCE;
void FixedUpdate()
{
DISTANCE = Vector3.Distance(transform.position, waypoint);
if (GameManager.gameState == GameManager.GameState.Game)
{
animate();
switch (state)
{
case State.Wait:
Wait();
break;
case State.Init:
Init();
break;
case State.Scatter:
Scatter();
break;
case State.Chase:
ChaseAI();
break;
case State.Run:
RunAway();
break;
}
}
}
//Iniciar os fantasmas no labirinto
public void InitializeGhost()
{
_startPos = getStartPosAccordingToName();
waypoint = transform.position;
state = State.Wait;
timeToEndWait = Time.time + waitLength + GUINav.initialDelay;
InitializeWaypoints(state);
}
public void InitializeGhost(Vector3 pos)
{
transform.position = pos;
waypoint = transform.position;
state = State.Wait;
timeToEndWait = Time.time + waitLength + GUINav.initialDelay;
InitializeWaypoints(state);
}
private void InitializeWaypoints(State st)
{
string data = "";
switch (name)
{
case "blinky":
data = @"22 20
22 26
27 26
27 30
22 30
22 26";
break;
case "pinky":
data = @"14.5 17
14 17
7 26
7 30
2 30
2 26";
break;
case "inky":
data = @"16.5 17
15 17
16 2
27 2
27 5
22 5";
break;
case "clyde":
data = @"12.5 17
14 17
13 2
13 5
10 5
10 8";
break;
}
string line;
waypoints = new Queue<Vector3>();
Vector3 wp;
if (st == State.Init)
{
using (StringReader reader = new StringReader(data))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Length == 0) break;
string[] values = line.Split(' ');
float x = float.Parse(values[0]);
float y = float.Parse(values[1]);
wp = new Vector3(x, y, 0);
waypoints.Enqueue(wp);
}
}
}
if (st == State.Scatter)
{
bool scatterWps = false;
using (StringReader reader = new StringReader(data))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Length == 0)
{
scatterWps = true;
continue;
}
if (scatterWps)
{
string[] values = line.Split(' ');
int x = Int32.Parse(values[0]);
int y = Int32.Parse(values[1]);
wp = new Vector3(x, y, 0);
waypoints.Enqueue(wp);
}
}
}
}
if (st == State.Wait)
{
Vector3 pos = transform.position;
if (transform.name == "inky" || transform.name == "clyde")
{
waypoints.Enqueue(new Vector3(pos.x, pos.y - 0.5f, 0f));
waypoints.Enqueue(new Vector3(pos.x, pos.y + 0.5f, 0f));
}
else
{
waypoints.Enqueue(new Vector3(pos.x, pos.y + 0.5f, 0f));
waypoints.Enqueue(new Vector3(pos.x, pos.y - 0.5f, 0f));
}
}
}
private Vector3 getStartPosAccordingToName()
{
switch (gameObject.name)
{
case "blinky":
return new Vector3(15f, 20f, 0f);
case "pinky":
return new Vector3(14.5f, 17f, 0f);
case "inky":
return new Vector3(16.5f, 17f, 0f);
case "clyde":
return new Vector3(12.5f, 17f, 0f);
}
return new Vector3();
}
void animate()
{
Vector3 dir = waypoint - transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
GetComponent<Animator>().SetBool("Run", false);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "pacman")
{
if (state == State.Run)
{
Calm();
InitializeGhost(_startPos);
pacman.UpdateScore();
}
else
{
_gm.LoseLife();
}
}
}
void Wait()
{
if (Time.time >= timeToEndWait)
{
state = State.Init;
waypoints.Clear();
InitializeWaypoints(state);
}
MoveToWaypoint(true);
}
void Init()
{
_timeToWhite = 0;
if (waypoints.Count == 0)
{
state = State.Scatter;
string name = GetComponent<SpriteRenderer>().sprite.name;
if (name[name.Length - 1] == '0' || name[name.Length - 1] == '1') direction = Vector3.right;
if (name[name.Length - 1] == '2' || name[name.Length - 1] == '3') direction = Vector3.left;
if (name[name.Length - 1] == '4' || name[name.Length - 1] == '5') direction = Vector3.up;
if (name[name.Length - 1] == '6' || name[name.Length - 1] == '7') direction = Vector3.down;
InitializeWaypoints(state);
timeToEndScatter = Time.time + scatterLength;
return;
}
MoveToWaypoint();
}
// Espalhar os fantasmas pelo labirinto
void Scatter()
{
if (Time.time >= timeToEndScatter)
{
waypoints.Clear();
state = State.Chase;
return;
}
MoveToWaypoint(true);
}
void ChaseAI()
{
if (Vector3.Distance(transform.position, waypoint) > 0.000000000001)
{
Vector2 p = Vector2.MoveTowards(transform.position, waypoint, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else GetComponent<AI>().AILogic();
}
// Pacman correndo atras dos fantasmas para comer
void RunAway()
{
GetComponent<Animator>().SetBool("Run", true);
if (Time.time >= _timeToWhite && Time.time >= _timeToToggleWhite) ToggleBlueWhite();
if (Vector3.Distance(transform.position, waypoint) > 0.000000000001)
{
Vector2 p = Vector2.MoveTowards(transform.position, waypoint, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else GetComponent<AI>().RunLogic();
}
// Movimentação pelos caminhos do waypoints
void MoveToWaypoint(bool loop = false)
{
waypoint = waypoints.Peek();
if (Vector3.Distance(transform.position, waypoint) > 0.000000000001)
{ // move towards it
_direction = Vector3.Normalize(waypoint - transform.position);
Vector2 p = Vector2.MoveTowards(transform.position, waypoint, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else
{
if (loop) waypoints.Enqueue(waypoints.Dequeue());
else waypoints.Dequeue();
}
}
// Modo assustar do fantasma movimentar
public void Frighten()
{
state = State.Run;
_direction *= -1;
_timeToWhite = Time.time + _gm.scareLength * 0.66f;
_timeToToggleWhite = _timeToWhite;
GetComponent<Animator>().SetBool("Run_White", false);
}
// Modo normal do fantasma movimentar
public void Calm()
{
if (state != State.Run) return;
waypoints.Clear();
state = State.Chase;
_timeToToggleWhite = 0;
_timeToWhite = 0;
GetComponent<Animator>().SetBool("Run_White", false);
GetComponent<Animator>().SetBool("Run", false);
}
// Alterar para azul e branco que o pacman consegue comer
public void ToggleBlueWhite()
{
isWhite = !isWhite;
GetComponent<Animator>().SetBool("Run_White", isWhite);
_timeToToggleWhite = Time.time + _toggleInterval;
}
}
[url= https://youtu.be/ADlxPra_1-k]https://youtu.be/ADlxPra_1-k[/url]
AI----------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AI : MonoBehaviour
{
public Transform target;
private List<TileManager.Tile> tiles = new List<TileManager.Tile>();
private TileManager manager;
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;
}
public void AILogic()
{
Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f);
currentTile = tiles[manager.Index((int)currentPos.x, (int)currentPos.y)];
targetTile = GetTargetTilePerGhost();
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 (nextTile.occupied && !currentTile.isIntersection)
{
if (ghost.direction.x != 0)
{
if (currentTile.down == null) ghost.direction = Vector3.up;
else ghost.direction = Vector3.down;
}
else if (ghost.direction.y != 0)
{
if (currentTile.left == null) ghost.direction = Vector3.right;
else ghost.direction = Vector3.left;
}
}
if (currentTile.isIntersection)
{
float dist1, dist2, dist3, dist4;
dist1 = dist2 = dist3 = dist4 = 999999f;
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) 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;
}
}
else
{
ghost.direction = ghost.direction;
}
}
public void RunLogic()
{
Vector3 currentPos = new Vector3(transform.position.x + 0.499f, transform.position.y + 0.499f);
currentTile = tiles[manager.Index((int)currentPos.x, (int)currentPos.y)];
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 (nextTile.occupied && !currentTile.isIntersection)
{
if (ghost.direction.x != 0)
{
if (currentTile.down == null) ghost.direction = Vector3.up;
else ghost.direction = Vector3.down;
}
else if (ghost.direction.y != 0)
{
if (currentTile.left == null) ghost.direction = Vector3.right;
else ghost.direction = Vector3.left;
}
}
if (currentTile.isIntersection)
{
List<TileManager.Tile> availableTiles = new List<TileManager.Tile>();
TileManager.Tile chosenTile;
if (currentTile.up != null && !currentTile.up.occupied && !(ghost.direction.y < 0)) availableTiles.Add(currentTile.up);
if (currentTile.down != null && !currentTile.down.occupied && !(ghost.direction.y > 0)) availableTiles.Add(currentTile.down);
if (currentTile.left != null && !currentTile.left.occupied && !(ghost.direction.x > 0)) availableTiles.Add(currentTile.left);
if (currentTile.right != null && !currentTile.right.occupied && !(ghost.direction.x < 0)) availableTiles.Add(currentTile.right);
int rand = Random.Range(0, availableTiles.Count);
chosenTile = availableTiles[rand];
ghost.direction = Vector3.Normalize(new Vector3(chosenTile.x - currentTile.x, chosenTile.y - currentTile.y, 0));
}
}
else
{
ghost.direction = ghost.direction;
}
}
TileManager.Tile GetTargetTilePerGhost()
{
Vector3 targetPos;
TileManager.Tile targetTile;
Vector3 dir;
switch (name)
{
case "blinky":
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":
dir = target.GetComponent<PlayerController>().getDir();
targetPos = new Vector3(target.position.x + 0.499f, target.position.y + 0.499f) + 4 * dir;
if (dir == Vector3.up) targetPos -= new Vector3(4, 0, 0);
targetTile = tiles[manager.Index((int)targetPos.x, (int)targetPos.y)];
break;
case "inky":
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;
break;
}
return targetTile;
}
}
GhostMove--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class GhostMove : MonoBehaviour
{
private Vector3 waypoint;
private Queue<Vector3> waypoints;
public Vector3 _direction;
public Vector3 direction
{
get
{
return _direction;
}
set
{
_direction = value;
Vector3 pos = new Vector3((int)transform.position.x, (int)transform.position.y, (int)transform.position.z);
waypoint = pos + _direction;
}
}
public float speed = 0.3f;
public float scatterLength = 5f;
public float waitLength = 0.0f;
private float timeToEndScatter;
private float timeToEndWait;
enum State { Wait, Init, Scatter, Chase, Run };
State state;
private Vector3 _startPos;
private float _timeToWhite;
private float _timeToToggleWhite;
private float _toggleInterval;
private bool isWhite = false;
public GameGUINavigation GUINav;
public PlayerController pacman;
private GameManager _gm;
void Start()
{
_gm = GameObject.Find("Game Manager").GetComponent<GameManager>();
_toggleInterval = _gm.scareLength * 0.33f * 0.20f;
InitializeGhost();
}
public float DISTANCE;
void FixedUpdate()
{
DISTANCE = Vector3.Distance(transform.position, waypoint);
if (GameManager.gameState == GameManager.GameState.Game)
{
animate();
switch (state)
{
case State.Wait:
Wait();
break;
case State.Init:
Init();
break;
case State.Scatter:
Scatter();
break;
case State.Chase:
ChaseAI();
break;
case State.Run:
RunAway();
break;
}
}
}
//Iniciar os fantasmas no labirinto
public void InitializeGhost()
{
_startPos = getStartPosAccordingToName();
waypoint = transform.position;
state = State.Wait;
timeToEndWait = Time.time + waitLength + GUINav.initialDelay;
InitializeWaypoints(state);
}
public void InitializeGhost(Vector3 pos)
{
transform.position = pos;
waypoint = transform.position;
state = State.Wait;
timeToEndWait = Time.time + waitLength + GUINav.initialDelay;
InitializeWaypoints(state);
}
private void InitializeWaypoints(State st)
{
string data = "";
switch (name)
{
case "blinky":
data = @"22 20
22 26
27 26
27 30
22 30
22 26";
break;
case "pinky":
data = @"14.5 17
14 17
7 26
7 30
2 30
2 26";
break;
case "inky":
data = @"16.5 17
15 17
16 2
27 2
27 5
22 5";
break;
case "clyde":
data = @"12.5 17
14 17
13 2
13 5
10 5
10 8";
break;
}
string line;
waypoints = new Queue<Vector3>();
Vector3 wp;
if (st == State.Init)
{
using (StringReader reader = new StringReader(data))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Length == 0) break;
string[] values = line.Split(' ');
float x = float.Parse(values[0]);
float y = float.Parse(values[1]);
wp = new Vector3(x, y, 0);
waypoints.Enqueue(wp);
}
}
}
if (st == State.Scatter)
{
bool scatterWps = false;
using (StringReader reader = new StringReader(data))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Length == 0)
{
scatterWps = true;
continue;
}
if (scatterWps)
{
string[] values = line.Split(' ');
int x = Int32.Parse(values[0]);
int y = Int32.Parse(values[1]);
wp = new Vector3(x, y, 0);
waypoints.Enqueue(wp);
}
}
}
}
if (st == State.Wait)
{
Vector3 pos = transform.position;
if (transform.name == "inky" || transform.name == "clyde")
{
waypoints.Enqueue(new Vector3(pos.x, pos.y - 0.5f, 0f));
waypoints.Enqueue(new Vector3(pos.x, pos.y + 0.5f, 0f));
}
else
{
waypoints.Enqueue(new Vector3(pos.x, pos.y + 0.5f, 0f));
waypoints.Enqueue(new Vector3(pos.x, pos.y - 0.5f, 0f));
}
}
}
private Vector3 getStartPosAccordingToName()
{
switch (gameObject.name)
{
case "blinky":
return new Vector3(15f, 20f, 0f);
case "pinky":
return new Vector3(14.5f, 17f, 0f);
case "inky":
return new Vector3(16.5f, 17f, 0f);
case "clyde":
return new Vector3(12.5f, 17f, 0f);
}
return new Vector3();
}
void animate()
{
Vector3 dir = waypoint - transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
GetComponent<Animator>().SetBool("Run", false);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "pacman")
{
if (state == State.Run)
{
Calm();
InitializeGhost(_startPos);
pacman.UpdateScore();
}
else
{
_gm.LoseLife();
}
}
}
void Wait()
{
if (Time.time >= timeToEndWait)
{
state = State.Init;
waypoints.Clear();
InitializeWaypoints(state);
}
MoveToWaypoint(true);
}
void Init()
{
_timeToWhite = 0;
if (waypoints.Count == 0)
{
state = State.Scatter;
string name = GetComponent<SpriteRenderer>().sprite.name;
if (name[name.Length - 1] == '0' || name[name.Length - 1] == '1') direction = Vector3.right;
if (name[name.Length - 1] == '2' || name[name.Length - 1] == '3') direction = Vector3.left;
if (name[name.Length - 1] == '4' || name[name.Length - 1] == '5') direction = Vector3.up;
if (name[name.Length - 1] == '6' || name[name.Length - 1] == '7') direction = Vector3.down;
InitializeWaypoints(state);
timeToEndScatter = Time.time + scatterLength;
return;
}
MoveToWaypoint();
}
// Espalhar os fantasmas pelo labirinto
void Scatter()
{
if (Time.time >= timeToEndScatter)
{
waypoints.Clear();
state = State.Chase;
return;
}
MoveToWaypoint(true);
}
void ChaseAI()
{
if (Vector3.Distance(transform.position, waypoint) > 0.000000000001)
{
Vector2 p = Vector2.MoveTowards(transform.position, waypoint, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else GetComponent<AI>().AILogic();
}
// Pacman correndo atras dos fantasmas para comer
void RunAway()
{
GetComponent<Animator>().SetBool("Run", true);
if (Time.time >= _timeToWhite && Time.time >= _timeToToggleWhite) ToggleBlueWhite();
if (Vector3.Distance(transform.position, waypoint) > 0.000000000001)
{
Vector2 p = Vector2.MoveTowards(transform.position, waypoint, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else GetComponent<AI>().RunLogic();
}
// Movimentação pelos caminhos do waypoints
void MoveToWaypoint(bool loop = false)
{
waypoint = waypoints.Peek();
if (Vector3.Distance(transform.position, waypoint) > 0.000000000001)
{ // move towards it
_direction = Vector3.Normalize(waypoint - transform.position);
Vector2 p = Vector2.MoveTowards(transform.position, waypoint, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
}
else
{
if (loop) waypoints.Enqueue(waypoints.Dequeue());
else waypoints.Dequeue();
}
}
// Modo assustar do fantasma movimentar
public void Frighten()
{
state = State.Run;
_direction *= -1;
_timeToWhite = Time.time + _gm.scareLength * 0.66f;
_timeToToggleWhite = _timeToWhite;
GetComponent<Animator>().SetBool("Run_White", false);
}
// Modo normal do fantasma movimentar
public void Calm()
{
if (state != State.Run) return;
waypoints.Clear();
state = State.Chase;
_timeToToggleWhite = 0;
_timeToWhite = 0;
GetComponent<Animator>().SetBool("Run_White", false);
GetComponent<Animator>().SetBool("Run", false);
}
// Alterar para azul e branco que o pacman consegue comer
public void ToggleBlueWhite()
{
isWhite = !isWhite;
GetComponent<Animator>().SetBool("Run_White", isWhite);
_timeToToggleWhite = Time.time + _toggleInterval;
}
}
lucaspoiob- Membro
- PONTOS : 2852
REPUTAÇÃO : 0
Respeito as regras :
Tópicos semelhantes
» Estou com problemas na iluminação da minha Lanterna (jogo de terror)
» De 0 a 10, qual a chance de isso causar problemas legais ao vender o jogo?
» Teleporte Estilo PACMAN
» Da pra colocar sisteminha em um jogo da unity pra trolar quem for piratear o jogo?
» Como patentear um jogo? ou seja, o jogo ter um copyright meu?
» De 0 a 10, qual a chance de isso causar problemas legais ao vender o jogo?
» Teleporte Estilo PACMAN
» Da pra colocar sisteminha em um jogo da unity pra trolar quem for piratear o jogo?
» Como patentear um jogo? ou seja, o jogo ter um copyright meu?
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos