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/Main.cs
2020-09-04 22:31:02 -07:00

301 lines
No EOL
5.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using Random = UnityEngine.Random;
using NaughtyAttributes;
using TMPro;
public class Main : MonoBehaviour
{
public delegate void GameStart(Main main);
public static event GameStart OnGameStart;
public delegate void Reset();
public static event Reset OnReset;
public bool sideQuest;
[Header("References")]
public Design design;
public Metro metro;
public Rig rig;
public AI ai = new AI();
public Game game = new Game();
public BoxUI boxUI;
public GraphXData graphXData;
public GraphX graphX;
public SnakeFace snakeFace;
public Poof poof;
public Drip drip;
public SoundData soundData;
public Music music;
public AudioSource loopSource;
public AudioSource musicSource;
public SFX sfx;
public AudioSource sfxSource;
[Header("Positions")]
public List<Vector3Int> snake, initialSnake;
public List<Vector3Int> core, initialCore;
public List<Vector3Int> space, initialSpace;
public Vector3Int food, initialFood;
[Header("Variables")]
public int bpm = 80;
public Vector3Int dir = new Vector3Int(0, 0, 1);
public bool aiMode = true;
public int growQueue, fastQueue;
public int justAte;
public Quaternion inputRot = Quaternion.identity;
public float dirDelay;
public float BPM()
{
if (fastQueue > 0)
{
return bpm * 2;
}
else
{
return bpm;
}
}
public void Start()
{
initialSpace = InitialSpace();
// plenty of space
// Metro.OnBeat += Beat;
metro.tick = false;
Game.OnCrash += Crash;
music = new Music(soundData);
sfx = new SFX(soundData);
graphX = new GraphX(graphXData);
snakeFace = new SnakeFace(graphXData);
poof = new Poof(graphXData);
drip = new Drip(graphXData);
Game.OnBump += drip.Bump;
restart = 0;
}
void Crash(Vector3 dir)
{
fastQueue = 0;
playing = false;
restart = 0;
}
public float lerp = 0;
Quaternion aiRot = Quaternion.identity;
void Update()
{
float beatScale = (float)bpm / 60f;
lerp += beatScale * Time.deltaTime;
lerp = Mathf.Clamp01(lerp);
if (aiMode)
{
aiRot = Quaternion.Lerp(aiRot, ai.rot, Time.deltaTime * 6);
inputRot = aiRot;
}
else
{
inputRot = rig.inputRot;
}
dirDelay -= Time.deltaTime;
if (playing && dirDelay < 0 && snake.Count >= initialSnake.Count)
{
if (aiMode)
{
dir = ai.InputDirection(this);
}
else
{
dir = rig.InputDirection(this);
}
}
// VISUAL
graphX.Render(this);
poof.Run(this);
drip.Rain(this);
music.Fade(this);
}
public Vector3 poofPos;
[Button]
public void Button()
{
poof.Burst(poofPos);
}
public int restart = 0; // set to zero during playtime to restart manually
public void Beat()
{
lerp = 0;
switch (restart)
{
case (0):
playing = false;
if (aiMode)
{
restart++;
}
break;
case (1):
playing = false;
Shader.SetGlobalInt("_Flash", 1);
food = initialFood;
restart++;
OnReset();
break;
case (2):
growQueue = 0;
fastQueue = 0;
snake = new List<Vector3Int>(initialSnake);
space = new List<Vector3Int>(initialSpace);
core = new List<Vector3Int>(initialCore);
dir = new Vector3Int(0, 0, 1);
Shader.SetGlobalInt("_Flash", 0);
if (aiMode)
{
playing = true;
restart = 6;
}
else
{
metro.tick = true;
restart++;
}
break;
case (3):
// some sort of count down to give the player prep time
// BEEP 1
restart++;
break;
case (4):
// BEEP 2
restart++;
break;
case (5):
// BEEP 3
restart++;
metro.tick = false;
playing = true;
OnGameStart(this);
break;
case (6):
if (playing)
{
if (aiMode)
{
ai.aiChances = design.aiMaxChances;
}
game.SnakeStep(this, design);
dirDelay = (60 / BPM()) / 3;
}
break;
}
graphX.Step(this);
drip.Step(this);
sfx.Step(this);
}
public bool playing;
public static bool OutOfBounds(Vector3Int pos, Vector3Int bounds)
{
return Mathf.Abs(pos.x) > bounds.x || Mathf.Abs(pos.y) > bounds.y || Mathf.Abs(pos.z) > bounds.z;
}
public static Vector3Int SnapDir(Vector3 dirty)
{
// Vector3Int snappedDir = new Vector3Int((int)(dirty.x / strongest), (int)(dirty.y / strongest), (int)(dirty.z / strongest));
// 0.3 / 0.3 > 0.5f
// 0.2 / 0.3 > 0.5f
float minDiff = 100;
int minIndex = 0;
for (int i = 0; i < dirs.Length; i++)
{
float diff = 0;
diff += Mathf.Abs(dirty.x - dirs[i].x);
diff += Mathf.Abs(dirty.y - dirs[i].y);
diff += Mathf.Abs(dirty.z - dirs[i].z);
if (diff < minDiff)
{
minIndex = i;
minDiff = diff;
}
}
return dirs[minIndex];
}
public static Vector3Int[] dirs = new Vector3Int[]
{
new Vector3Int(-1, 0, 0),
new Vector3Int(1, 0, 0),
new Vector3Int(0, -1, 0),
new Vector3Int(0, 1, 0),
new Vector3Int(0, 0, -1),
new Vector3Int(0, 0, 1)
};
List<Vector3Int> InitialSpace()
{
List<Vector3Int> list = new List<Vector3Int>();
for (int x = -design.bounds.x; x <= design.bounds.x; x++)
{
for (int y = -design.bounds.y; y <= design.bounds.y; y++)
{
for (int z = -design.bounds.z; z <= design.bounds.z; z++)
{
list.Add(new Vector3Int(x, y, z));
}
}
}
for (int i = 0; i < initialSnake.Count; i++)
{
list.Remove(initialSnake[i]);
}
for (int i = 0; i < initialCore.Count; i++)
{
list.Remove(initialCore[i]);
}
return list;
}
}