Skip to content

Instantly share code, notes, and snippets.

@SeeringPhil
Last active April 12, 2016 15:45
Show Gist options
  • Save SeeringPhil/3983c70a01fc2b0c92cfb8f31c49133c to your computer and use it in GitHub Desktop.
Save SeeringPhil/3983c70a01fc2b0c92cfb8f31c49133c to your computer and use it in GitHub Desktop.
This is the class we're using for our pacman to be controlled and managed in the game.
using UnityEngine;
using System.Collections;
public class Pacman : MonoBehaviour {
public float defaultSpeed = 0.2f;
public Vector2 direction;
Vector2 next;
public int[,] game;
public int state;
// Use this for initialization
void Start () {
direction = Vector2.zero;
transform.position = new Vector2(14.5f, 15.5f);
}
void Update()
{
SetDirection();
}
void FixedUpdate()
{
if (this.gameObject.transform.position.x < 0.7f)
this.gameObject.transform.position = new Vector3(27.5f - this.gameObject.transform.position.x, this.gameObject.transform.position.y, 0f);
else if (this.gameObject.transform.position.x > 27.5f)
this.gameObject.transform.position = new Vector3(0.6f, this.gameObject.transform.position.y, 0f);
Move();
}
void OnCollisionEnter(Collision col)
{
if (direction == Vector2.up && game[(int)this.gameObject.transform.position.y + 1, (int)this.gameObject.transform.position.x] == 1)
direction = Vector2.zero;
if (direction == Vector2.down && game[(int)this.gameObject.transform.position.y - 1, (int)this.gameObject.transform.position.x] == 1)
direction = Vector2.zero;
if (direction == Vector2.left && game[(int)this.gameObject.transform.position.y, (int)this.gameObject.transform.position.x - 1] == 1)
direction = Vector2.zero;
if (direction == Vector2.right && game[(int)this.gameObject.transform.position.y, (int)this.gameObject.transform.position.x + 1] == 1)
direction = Vector2.zero;
}
public void Pause()
{
}
void SetDirection()
{
if (Input.GetKeyDown(Player1CtrlRemap.kcForward) || Input.GetAxis("Vertical") > 0.7)
{
Debug.Log("haut :" + Player1CtrlRemap.kcForward.ToString() + "bas" + Player1CtrlRemap.kcDownward.ToString() + "droite" + Player1CtrlRemap.kcRight.ToString() + "gauche:" + Player1CtrlRemap.kcLeft.ToString());
next = Vector2.up;
if (game[(int)this.gameObject.transform.position.y + 1, (int)this.gameObject.transform.position.x] != 1)
direction = Vector2.up;
}
if (Input.GetKeyDown(Player1CtrlRemap.kcDownward) || Input.GetAxis("Vertical") < -0.7)
{
Debug.Log("haut :" + Player1CtrlRemap.kcForward.ToString() + "bas" + Player1CtrlRemap.kcDownward.ToString() + "droite" + Player1CtrlRemap.kcRight.ToString() + "gauche:" + Player1CtrlRemap.kcLeft.ToString());
next = Vector2.down;
if (game[(int)this.gameObject.transform.position.y - 1, (int)this.gameObject.transform.position.x] != 1)
direction = Vector2.down;
}
if (Input.GetKeyDown(Player1CtrlRemap.kcLeft) || Input.GetAxis("Horizontal") < -0.7)
{
Debug.Log("haut :" + Player1CtrlRemap.kcForward.ToString() + "bas" + Player1CtrlRemap.kcDownward.ToString() + "droite" + Player1CtrlRemap.kcRight.ToString() + "gauche:" + Player1CtrlRemap.kcLeft.ToString());
next = Vector2.left;
if (game[(int)this.gameObject.transform.position.y, (int)this.gameObject.transform.position.x - 1] != 1)
direction = Vector2.left;
}
if (Input.GetKeyDown(Player1CtrlRemap.kcRight) || Input.GetAxis("Horizontal") > 0.7)
{
Debug.Log("haut :" + Player1CtrlRemap.kcForward.ToString() + "bas" + Player1CtrlRemap.kcDownward.ToString() + "droite" + Player1CtrlRemap.kcRight.ToString() + "gauche:" + Player1CtrlRemap.kcLeft.ToString());
Debug.Log((int)Player1CtrlRemap.kcDownward);
next = Vector2.right;
if (game[(int)this.gameObject.transform.position.y, (int)this.gameObject.transform.position.x + 1] != 1)
direction = Vector2.right;
}
}
void Move()
{
var pos = new Vector2(this.gameObject.transform.position.x,
this.gameObject.transform.position.y);
var x = System.Math.Round(pos.x, 1) - System.Math.Floor(pos.x);
var y = System.Math.Round(pos.y, 1) - System.Math.Floor(pos.y);
if (x >= 0.4 && x <= 0.6 && y >= 0.4 && y <= 0.6)
//if (x == 0.5 && y == 0.5)
{
if (next == Vector2.up && game[(int)this.gameObject.transform.position.y + 1, (int)this.gameObject.transform.position.x] != 1)
{
direction = next;
next = Vector2.zero;
}
if (next == Vector2.down && game[(int)this.gameObject.transform.position.y - 1, (int)this.gameObject.transform.position.x] != 1)
{
direction = next;
next = Vector2.zero;
}
if (next == Vector2.left && game[(int)this.gameObject.transform.position.y, (int)this.gameObject.transform.position.x - 1] != 1)
{
direction = next;
next = Vector2.zero;
}
if (next == Vector2.right && game[(int)this.gameObject.transform.position.y, (int)this.gameObject.transform.position.x + 1] != 1)
{
direction = next;
next = Vector2.zero;
}
}
if(!pauseScript.paused)
transform.position = (Vector2)transform.position + defaultSpeed * direction;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment