This repository has been archived on 2024-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
snakeinabox/Assets/Scripts/Game.cs
2020-07-22 12:25:26 -07:00

113 lines
No EOL
2.2 KiB
C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Game
{
Vector3Int[] poss = new Vector3Int[9 * 7 * 9]; // not bounds compliant...
public delegate void Step();
public static event Step OnStep;
public delegate void Eat(Vector3 pos);
public static event Eat OnEat;
public delegate void Smell();
public static event Smell OnSmell;
public delegate void Bump(Vector3 dir);
public static event Bump OnBump;
public delegate void Crash();
public static event Crash OnCrash;
bool danger;
public void SnakeStep(Main main, Design design) // snake data?
{
OnStep();
Vector3Int future = main.snake[0] + main.dir;
if (main.snake.Contains(future) || Main.OutOfBounds(future, design.bounds))
{
if (!danger)
{
// main.snake.RemoveAt(0);
OnBump(main.dir);
danger = true;
}
else
{
main.snake.Insert(0, future);
OnCrash();
}
return;
}
danger = false;
if (Vector3.Distance(future, main.food) <= 1)
{
OnSmell();
}
// Grow System (OOP *don't run into tail)
if (main.growQueue > 0)
{
main.growQueue--;
}
else
{
main.space.Add(main.snake[main.snake.Count - 1]);
main.snake.RemoveAt(main.snake.Count - 1);
}
main.space.Remove(future);
main.snake.Insert(0, future); // Grow
if (main.fastQueue > 0)
{
main.fastQueue--;
}
// Eat Packing Peanut
if (main.food == future)
{
main.growQueue += design.foodValue;
main.justAte = 0;
OnEat(future);
if (main.space.Count == 0)
{
PlayerPrefs.SetInt("BoxFills", 1 + PlayerPrefs.GetInt("BoxFills", 0));
}
else
{
main.food = main.space[Random.Range(0, main.space.Count)];
main.graphX.foodScale = 0;
}
}
// Eat main.Core Piece
for (int i = 0; i < main.core.Count; i++)
{
if (main.core[i] == future)
{
main.core.RemoveAt(i);
main.growQueue += design.foodValue;
main.justAte = 0;
main.fastQueue += design.foodValue;
OnEat(future);
break;
}
}
}
}