Scripting

I was also in charge of scripting several aspects of the game. I created the wave system, a scene fader to load multiple levels (for future additions), Pause functionality, and the rotating sky, and reversing the normals on the skysphere to make it work correctly.
here is a link to my github to view my project there.
https://github.com/CrudenDarkeyes/FourthNight

GameManager
using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{

public static bool GameIsOver;

public GameObject gameOverUI;
public GameObject completeLevelUI;

void Start()
{
GameIsOver = false;
}

// Update is called once per frame
void Update()
{
if (GameIsOver)
return;

if (PlayerStats.Lives <= 0)
{
EndGame();
}
}

void EndGame()
{
GameIsOver = true;
gameOverUI.SetActive(true);
}

public void WinLevel()
{
GameIsOver = true;
completeLevelUI.SetActive(true);
}

}




using UnityEngine;
using UnityEngine.SceneManagement;

public class PauseMenu : MonoBehaviour {

    public GameObject ui;

    public string menuSceneName = "MainMenu";

    public SceneFader sceneFader;

    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.P))
        {
            Toggle();
        }
    }

    public void Toggle ()
    {
        ui.SetActive(!ui.activeSelf);

        if (ui.activeSelf)
        {
            Time.timeScale = 0f;
        } else
        {
            Time.timeScale = 1f;
        }
    }

    public void Retry ()
    {
        Toggle();
        sceneFader.FadeTo(SceneManager.GetActiveScene().name);
}

    public void Menu ()
    {
        Toggle();
        sceneFader.FadeTo(menuSceneName);
    }

}




using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
public class ReverseNormals : MonoBehaviour
{

void Start()
{
MeshFilter filter = GetComponent(typeof(MeshFilter)) as MeshFilter;
if (filter != null)
{
Mesh mesh = filter.mesh;

Vector3[] normals = mesh.normals;
for (int i = 0; i < normals.Length; i++)
normals[i] = -normals[i];
mesh.normals = normals;

for (int m = 0; m < mesh.subMeshCount; m++)
{
int[] triangles = mesh.GetTriangles(m);
for (int i = 0; i < triangles.Length; i += 3)
{
int temp = triangles[i + 0];
triangles[i + 0] = triangles[i + 1];
triangles[i + 1] = temp;
}
mesh.SetTriangles(triangles, m);
}
}
}
}






using UnityEngine;
using System.Collections;

public class PlayerStats : MonoBehaviour {

    public static int Money;
    public int startMoney = 400;

    public static int Lives;
    public int startLives = 20;

    public static int Rounds;

    void Start ()
    {
        Money = startMoney;
        Lives = startLives;

        Rounds = 0;
    }

}



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotateSky : MonoBehaviour {

public Vector3 targetAngle = new Vector3(0f, 0f, 0f);
 public Vector3 targetAngle2;
private Vector3 currentAngle;
private bool day = true;

public void Start()
{
targetAngle2 = transform.eulerAngles;
currentAngle = transform.eulerAngles;
        //StartCoroutine (RotateSky ());

}

public void Update()
{
}


public void FlipDayNight()
{

        Debug.Log("Requested Rotate Sky");

        StartCoroutine(RotateSky(currentAngle, targetAngle));
        day = false;
        currentAngle = targetAngle;
        targetAngle += targetAngle;

// Debug.Log("Requested Rotate Sky");
// if(day)
// {
    //      Debug.Log ("It is day and day = " + day + " calling function in if");
// StartCoroutine(RotateSky(targetAngle2, targetAngle));
// day = false;
// } else
// {
    //      Debug.Log ("It is night and day = " + day + " calling function in else");
// StartCoroutine(RotateSky(targetAngle, targetAngle2));
// day = true;
// }
}


public IEnumerator RotateSky(Vector3 currentPosition, Vector3 targetPosition)
{
Debug.Log("Rotating Sky" + currentPosition + " " + targetPosition);
float timeToStart = Time.time;
while(Vector3.Distance(transform.position, targetPosition) > 0.05f)
{

currentPosition = new Vector3(
Mathf.LerpAngle(currentPosition.x, targetPosition.x, Time.deltaTime),
Mathf.LerpAngle(currentPosition.y, targetPosition.y, Time.deltaTime),
Mathf.LerpAngle(currentPosition.z, targetPosition.z, Time.deltaTime));

transform.eulerAngles = currentPosition;
yield return null;
}

yield return new WaitForSeconds(3f);

}



public IEnumerator RotateSky()
{
        Debug.Log ("RotateSky Function Called");
float timeToStart = Time.time;
while (Vector3.Distance(transform.position, targetAngle) > 0.05f)
{

currentAngle = new Vector3(
Mathf.LerpAngle(currentAngle.x, targetAngle.x, Time.deltaTime),
Mathf.LerpAngle(currentAngle.y, targetAngle.y, Time.deltaTime),
Mathf.LerpAngle(currentAngle.z, targetAngle.z, Time.deltaTime));

transform.eulerAngles = currentAngle;
yield return null;
}

yield return new WaitForSeconds(3f);

}

}




using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections;

public class SceneFader : MonoBehaviour {

    public Image img;
    public AnimationCurve curve;

    void Start ()
    {
        StartCoroutine(FadeIn());
    }

    public void FadeTo (string scene)
    {
        StartCoroutine(FadeOut(scene));
    }

    IEnumerator FadeIn ()
    {
        float t = 1f;

        while (t > 0f)
        {
            t -= Time.deltaTime;
            float a = curve.Evaluate(t);
            img.color = new Color (0f, 0f, 0f, a);
            yield return 0;
        }
    }

    IEnumerator FadeOut(string scene)
    {
        float t = 0f;

        while (t < 1f)
        {
            t += Time.deltaTime;
            float a = curve.Evaluate(t);
            img.color = new Color(0f, 0f, 0f, a);
            yield return 0;
        }

        SceneManager.LoadScene(scene);
    }

}





using UnityEngine;

[System.Serializable]
public class Wave
{

public GameObject enemy;
public int count;
public float rate;

}




using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class WaveSpawner : MonoBehaviour
{

public int EnemiesAlive = 0;

public Wave[] waves;

public Transform spawnPoint;

public float timeBetweenWaves = 20f;
private float countdown = 2f;

public Text waveCountdownText;

public GameManager gameManager;

public rotateSky changeDayCycle;

private int waveIndex = 0;

void Update()
{
if (EnemiesAlive > 0)
{
return;
}

if (waveIndex == waves.Length)
{
gameManager.WinLevel();
this.enabled = false;
}

if (countdown <= 0f)
{
            changeDayCycle.FlipDayNight ();
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}

countdown -= Time.deltaTime;

countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);

waveCountdownText.text = string.Format("{0:00.00}", countdown);
}

IEnumerator SpawnWave()
{

PlayerStats.Rounds++;

Wave wave = waves[waveIndex];

EnemiesAlive = wave.count;

for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}

waveIndex++;
}

void SpawnEnemy(GameObject enemy)
{
Instantiate(enemy, spawnPoint.position, spawnPoint.rotation);
}

}

Comments