Untitled

mail@pastecode.io avatar
unknown
plain_text
3 days ago
22 kB
5
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFSW.QC;
using System.Threading.Tasks;

public class WaveManager : MonoBehaviour
{

    public static WaveManager Instance = null;

    public int wave_number; 
    public unsafe int* finalScore;

    public Text wave_number_text;
    public int wave_points;
    public int enemies_alive;

    public string state = "wave";

    //Sound
    public AudioSource audio_source;
    public AudioClip end_wave_audio;
    public GameObject shop_audio;
    public AudioClip buy_card_audio;

    //Music
    public AudioSource music_source;
    public AudioClip music_1_easy;
    public AudioClip music_1_hard;
    public AudioClip music_2_easy;
    public AudioClip music_2_hard;
    public AudioClip music_3_easy;
    public AudioClip music_3_hard;
    public AudioClip music_4_easy;
    public AudioClip music_4_hard;
    AudioClip music_easy;
    AudioClip music_hard;
    bool can_change_music = true;

    //Spawning
    public List <GameObject> enemies_to_spawn;
    float generation_cooldown = 0;

    //Enemies
    public GameObject weak_ranged;
    public GameObject medium_ranged;
    public GameObject strong_ranged;
    public GameObject weak_melee;
    public GameObject strong_melee;
    public GameObject cannon;
    public GameObject weak_ranged_plus;
    public GameObject medium_ranged_plus;
    public GameObject strong_ranged_plus;
    public GameObject weak_melee_plus;
    public GameObject strong_melee_plus;
    public GameObject cannon_plus;
    public GameObject weak_ranged_plus_plus;
    public GameObject medium_ranged_plus_plus;
    public GameObject strong_ranged_plus_plus;
    public GameObject weak_melee_plus_plus;
    public GameObject strong_melee_plus_plus;
    public GameObject cannon_plus_plus;
    public GameObject weak_ranged_end;
    public GameObject boss_1;
    public GameObject boss_2;


    //Shop
    ShopManager shop_manager;

    //Wave Debugger
    float debug_countdown = 10;
    public float debug_countdown2 = 45;
    float debug_roll_cd = 3;
    public Text enemy_count;
    int enemy_total;

    //Pause
    float pauseTime;
    bool paused = false;
    public GameObject pause_screen;
    public GameObject options_screen;
    public Options options_;

    //Player stats
    PlayerStats players_stats;
    ProgresisonManager progression_manager;

    //Boss Bar
    public Text boss_name_ui;
    public Slider boss_hp_ui;
    public GameObject boss_ui;
    GameObject boss_obj;

    //Achievments
    float speedrunner_counter = 1200;

    //Victory Screen
    public GameObject victory_ui;

    [Command]
    private void SetWave(int wave) => wave_number = wave;

    private void Awake()
    {
        if (Instance == null) { Instance = this; } else if (Instance != this) { Destroy(gameObject); }
    }

    // Start is called before the first frame update
    unsafe void Start()
    {
        PauseGame();
        enemies_to_spawn = new List<GameObject>();
        fixed (int* ptr = &wave_number) { finalScore = ptr; }
        shop_manager = GameObject.FindGameObjectWithTag("ShopManager").GetComponent<ShopManager>();
        audio_source = GameObject.Find("SFX").GetComponent<AudioSource>();
        players_stats = GameObject.Find("Player").GetComponent<PlayerStats>();
        progression_manager = GameObject.Find("ProgressionManager").GetComponent<ProgresisonManager>();
        RandomMusic();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (state == "wave")
        {
            GenerateWave();
            CheckWaveEnd();
            WaveDebbuger();
        }

        if (boss_ui.activeSelf && boss_obj != null) { boss_hp_ui.maxValue = boss_obj.GetComponent<EnemyStats>().max_hp; boss_hp_ui.value = boss_obj.GetComponent<EnemyStats>().hp; } else { boss_ui.SetActive(false); }
        RunSpeedRunnerCounter();
    }

    private void OnEnable()
    {
        InputManager.Pause += OnPause;
    }

    private void OnDestroy()
    {
        InputManager.Pause -= OnPause;
        Debug.Log("test");
    }

    private void OnPause()
    {
        if (Time.unscaledTime < pauseTime) return;

        pauseTime = Time.unscaledTime + 0.1f;

        if (wave_number <= 0 || ShopManager.OnScreen || players_stats == null) return;
        if (paused) { UnpauseGame(); options_screen.SetActive(false); return; }
        else { PauseGame(); options_screen.SetActive(true); return; }
    }

    private void OnBack()
    {
        if (wave_number <= 0 || ShopManager.OnScreen || players_stats == null) return;
        if (paused) { UnpauseGame(); options_screen.SetActive(false); return; }
    }

    public void StartWave() {
        state = "wave";
        debug_countdown = 10;
        debug_countdown2 = 45;       
        GameObject[] enemies_to_kill = GameObject.FindGameObjectsWithTag("Enemy"); foreach (GameObject e in enemies_to_kill){ Destroy(e); }
        enemies_alive = 0;
        Time.timeScale = 1;
        wave_points = 10 + Mathf.RoundToInt((wave_number * (wave_number)));
        if (players_stats.hat == 4) { wave_points *= 2; }
        ReviveCooldown();
        players_stats.atk_speed = players_stats.gameObject.GetComponent<PlayerController>().atk_speed_before;
        if (players_stats.desperate == true) { players_stats.hp = players_stats.max_hp; }

        if (wave_number == 89) { GetWaveAchievment(); }
        if (wave_number == 59 && speedrunner_counter > 0) { GetSpeedRunnerAchievment(); }

        enemy_total = 0;

        music_source.volume = options_.music_volume.value;
        if (wave_number > 30 && can_change_music) { music_source.Stop(); music_source.clip = music_hard; music_source.Play(); can_change_music = false; }

        FindObjectOfType<PlayerController>().EnableVisualAim(InputManager.IsGamepad);
    }

    void GenerateWave() {

        //Boss
        if (wave_number == 60 && wave_points > 0)
        {
            int rand_boss = Random.Range(0, 2);

            if (rand_boss == 0)
            {
                SpawnMonster(boss_1);
                wave_points = 0;
                enemies_alive++;
                enemy_total++;
                debug_countdown2 = 3000;
                InvokeBoss("Sarbknathus");
            }

            if (rand_boss == 1)
            {
                SpawnMonster(boss_2);
                wave_points = 0;
                enemies_alive++;
                enemy_total++;
                debug_countdown2 = 3000;
                InvokeBoss("Alopepetciamus");
            }
        }

        if (wave_points > 0)
        {
            if (generation_cooldown <= 0)
            {

                if (wave_number > 0)
                {
                    if (wave_number > 0 && wave_number < 20)
                    {
                        SpawnMonster(weak_ranged);
                        wave_points -= 5;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 30 && wave_number < 40) {
                        SpawnMonster(weak_ranged_plus);
                        wave_points -= 5;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 40 && wave_number < 60)
                    {
                        SpawnMonster(weak_ranged_plus_plus);
                        wave_points -= 5;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 60)
                    {
                        SpawnMonster(weak_ranged_end);
                        wave_points -= 5;
                        enemies_alive++;
                        enemy_total++;
                    }
                }

                if (wave_number > 5) {

                    if (wave_number > 5 && wave_number < 25)
                    {
                        SpawnMonster(medium_ranged);
                        wave_points -= 19;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 30 && wave_number < 45)
                    {
                        SpawnMonster(medium_ranged_plus);
                        wave_points -= 19;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 45)
                    {
                        SpawnMonster(medium_ranged_plus_plus);
                        wave_points -= 19;
                        enemies_alive++;
                        enemy_total++;
                    }
                }

                if (wave_number > 10) {
                    int cara_coroa = Random.Range(0, 3);
                    if (cara_coroa == 1)
                    {
                        if (wave_number > 10 && wave_number < 30)
                        {
                            SpawnMonster(weak_melee);
                            wave_points -= 39;
                            enemies_alive++;
                            enemy_total++;
                        }

                        if (wave_number >= 30 && wave_number < 50)
                        {
                            SpawnMonster(weak_melee_plus);
                            wave_points -= 39;
                            enemies_alive++;
                            enemy_total++;
                        }

                        if (wave_number >= 50)
                        {
                            SpawnMonster(weak_melee_plus_plus);
                            wave_points -= 39;
                            enemies_alive++;
                            enemy_total++;
                        }
                    }
                }

                if (wave_number > 15) {
                    if (wave_number > 15 && wave_number < 35)
                    {
                        SpawnMonster(cannon);
                        wave_points -= 65;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 35 && wave_number < 55)
                    {
                        SpawnMonster(cannon_plus);
                        wave_points -= 65;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 55)
                    {
                        SpawnMonster(cannon_plus_plus);
                        wave_points -= 65;
                        enemies_alive++;
                        enemy_total++;
                    }
                }

                if (wave_number > 20)
                {
                    if (wave_number > 20 && wave_number < 40)
                    {
                        SpawnMonster(strong_ranged);
                        wave_points -= 97;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 40 && wave_number < 60)
                    {
                        SpawnMonster(strong_ranged_plus);
                        wave_points -= 97;
                        enemies_alive++;
                        enemy_total++;
                    }

                    if (wave_number >= 60)
                    {
                        SpawnMonster(strong_ranged_plus_plus);
                        wave_points -= 97;
                        enemies_alive++;
                        enemy_total++;
                    }
                }

                if (wave_number > 25)
                {
                    int cara_coroa = Random.Range(0, 4);
                    if (cara_coroa == 1)
                    {
                        if (wave_number > 25 && wave_number < 45)
                        {
                            SpawnMonster(strong_melee);
                            wave_points -= 195;
                            enemies_alive++;
                            enemy_total++;
                        }

                        if (wave_number >= 45 && wave_number < 60)
                        {
                            SpawnMonster(strong_melee_plus);
                            wave_points -= 195;
                            enemies_alive++;
                            enemy_total++;
                        }

                        if (wave_number >= 60)
                        {
                            SpawnMonster(strong_melee_plus_plus);
                            wave_points -= 195;
                            enemies_alive++;
                            enemy_total++;
                        }
                    }

                }

                generation_cooldown = 0.8f + Random.Range(-0.2f, 0.2f);
            }
            else { generation_cooldown -= Time.deltaTime; }
        }
    }

    void SpawnMonster(GameObject monster) {
        float random_x = Random.Range(-7.5f, 7.5f);
        float random_y = Random.Range (5.5f, 6.3f);

        Instantiate(monster, new Vector3(random_x, random_y, 0), Quaternion.identity);
    }

    void CheckWaveEnd()
    {
        //enemy_count.text = enemies_alive.ToString() + "/" + enemy_total.ToString();
        if (enemies_alive <= 0 && wave_points <= 0 && wave_number != 0) {
            wave_number_text.text = wave_number.ToString();

            GetBossAchievment();

            wave_number++;
            wave_number.GenerateConfirmation();

            music_source.volume = options_.music_volume.value / 4;

            state = "shop";
            players_stats.max_hp += 2;
            players_stats.hp += 2;
            Time.timeScale = 0;
            ClearProjectiles();
            shop_manager.StartShop();
            audio_source.PlayOneShot(end_wave_audio, 0.3f);
            shop_audio.SetActive(true);
            GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>().walk_audio.SetActive(false);

            SaveManager.SetValue("soul_prisms", ProgresisonManager.Instance.soul_prisms);

        }
    }

    public void StartGame()
    {
        if (!players_stats.HasSelectedStaff()) players_stats.SelectStaffWithIndex(0);
        if (!players_stats.HasSelectedHat()) players_stats.SelectHatWithIndex(0);

        wave_number++;
        wave_number.InitializeConfirmation();

        wave_number_text.text = wave_number.ToString();
        players_stats.gameObject.GetComponent<PlayerController>().atk_speed_before = players_stats.atk_speed;

        ApplyUpgradeLvl();

        StartWave();

        music_source.Stop(); music_source.clip = music_easy; music_source.Play();

    }

    public void ClearProjectiles()
    {
        GameObject[] projectiles_e = GameObject.FindGameObjectsWithTag("ProjectileEnemy");
        foreach (GameObject pe in projectiles_e)
        {
            Destroy(pe);
        }

        GameObject[] projectiles = GameObject.FindGameObjectsWithTag("Projectile");
        foreach (GameObject p in projectiles)
        {
            Destroy(p);
        }

        GameObject[] holes = GameObject.FindGameObjectsWithTag("BlackHole");
        foreach (GameObject b in holes)
        {
            Destroy(b);
        }
    }

    public void EnterEndless()
    {
        enemies_alive--;
    }

    void WaveDebbuger()
    {
        if (enemies_alive < 3 ) {
            if (debug_countdown2 < 0)
            {
                Debug.Log("Less than 3");
                GameObject [] live_enemies = GameObject.FindGameObjectsWithTag("Enemy");
                foreach (GameObject e in live_enemies)
                {
                    e.GetComponent<EnemyStats>().wave_manager = this;
                    e.GetComponent<EnemyStats>().RemoveHP(e.GetComponent<EnemyStats>().hp + 1);
                }
                debug_countdown2 = 45;
            }
            else { debug_countdown2 -= Time.deltaTime; }
        }

        if (enemies_alive < 6 && wave_number > 40)
        {
            if (debug_countdown2 < 0)
            {
                GameObject[] live_enemies = GameObject.FindGameObjectsWithTag("Enemy");
                foreach (GameObject e in live_enemies)
                {
                    e.GetComponent<EnemyStats>().wave_manager = this;
                    e.GetComponent<EnemyStats>().RemoveHP(e.GetComponent<EnemyStats>().hp + 1);
                }
                debug_countdown2 = 45;
            }
            else { debug_countdown2 -= Time.deltaTime; }
        }

        /*if (debug_countdown <= 0)
        {
            if (debug_roll_cd <= 0)
            {
                debug_roll_cd = 3;
                GameObject[] live_enemies = GameObject.FindGameObjectsWithTag("Enemy");
                foreach (GameObject e in live_enemies)
                {
                    if (e.transform.position.y > 5 || e.transform.position.x > 9 || e.transform.position.x < -9)
                    {
                        e.transform.position = new Vector3(0, 6.2f, 0);
                    }
                }
            }
            else { debug_roll_cd -= Time.fixedDeltaTime; }
        }
        else { debug_countdown -= Time.fixedDeltaTime; }*/
        
    }

    public void PauseGame()
    {
        paused = true;
        Time.timeScale = 0;
        pause_screen.SetActive(true);
    }

    public void UnpauseGame()
    {
        paused = false;
        Time.timeScale = 1;
        pause_screen.SetActive(false);
    }

    public void ApplyUpgradeLvl()
    {
        players_stats.atk_damage += progression_manager.atk_lvl/2;
        players_stats.atk_speed = players_stats.gameObject.GetComponent<PlayerController>().atk_speed_before; players_stats.atk_speed -= progression_manager.atk_speed_lvl * (0.05f * players_stats.atk_speed); players_stats.gameObject.GetComponent<PlayerController>().atk_speed_before = players_stats.atk_speed;
        players_stats.armor += progression_manager.armor_lvl;
        players_stats.max_hp += progression_manager.hp_lvl * 5;
        players_stats.hp = players_stats.max_hp;
        players_stats.speed += progression_manager.speed_lvl * (0.1f * players_stats.speed);
        players_stats.proj_hits += progression_manager.stability_lvl;
        players_stats.soul_orb_level += progression_manager.soul_orb_lvl;
        players_stats.critical_chance += progression_manager.crit_lvl * 2;

    }

    void InvokeBoss(string boss_name)
    {
        boss_obj = GameObject.FindGameObjectWithTag("Enemy");
        boss_hp_ui.maxValue = boss_obj.GetComponent<EnemyStats>().max_hp;
        boss_hp_ui.value = boss_obj.GetComponent<EnemyStats>().hp;
        boss_ui.SetActive(true);
        boss_name_ui.text = boss_name;       
    }

    void RandomMusic()
    {
        int rand_picker = Random.Range(0, 4);

        Debug.Log("Music Chosen: " + rand_picker);

        if (rand_picker == 0)
        {
            music_easy = music_1_easy;
            music_hard = music_1_hard;
        }

        if (rand_picker == 1)
        {
            music_easy = music_2_easy;
            music_hard = music_2_hard;
        }

        if (rand_picker == 2)
        {
            music_easy = music_3_easy;
            music_hard = music_3_hard;
        }

        if (rand_picker == 3)
        {
            music_easy = music_4_easy;
            music_hard = music_4_hard;
        }
    }

    void GetBossAchievment()
    {
#if STEAM_ENABLED
        if (boss_name_ui.text == "Sarbknathus")
        {
            bool achiev_00;

            Steamworks.SteamUserStats.GetAchievement("achiev_00", out achiev_00);
            if (achiev_00 == false) { Steamworks.SteamUserStats.SetAchievement("achiev_00"); }
        }

        if (boss_name_ui.text == "Alopepetciamus")
        {
            bool achiev_01;

            Steamworks.SteamUserStats.GetAchievement("achiev_01", out achiev_01);
            if (achiev_01 == false) { Steamworks.SteamUserStats.SetAchievement("achiev_01"); }
        }
#endif
    }

    void GetWaveAchievment()
    {
#if STEAM_ENABLED
        bool achiev_03;

        Steamworks.SteamUserStats.GetAchievement("achiev_03", out achiev_03);
        if (achiev_03 == false) { Steamworks.SteamUserStats.SetAchievement("achiev_03"); }
#endif
    }

    void GetSpeedRunnerAchievment()
    {
#if STEAM_ENABLED
        bool achiev_04;

        Steamworks.SteamUserStats.GetAchievement("achiev_04", out achiev_04);
        if (achiev_04 == false) { Steamworks.SteamUserStats.SetAchievement("achiev_04"); }
#endif
    }

    void RunSpeedRunnerCounter()
    {
        if (speedrunner_counter > 0)
        {
            speedrunner_counter -= Time.fixedDeltaTime;
        }
    }

    void ReviveCooldown()
    {

        if (players_stats.avenger == true) {
            if (players_stats.revive_count < 1) {
                if (players_stats.revive_cd > 0)
                {
                    players_stats.revive_cd--;
                }
                else if (players_stats.revive_cd == 0) {
                    players_stats.revive_count++;
                    players_stats.revive_cd = 5;
                }
            }
        }

    }
}
Leave a Comment