Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class Battle : MonoBehaviour
- {
- AudioManager AM;
- public GameObject BattleSystem;
- public GameObject UIHandle;
- public GameObject PlayerControls;
- public GameObject BattlePlayer;
- public TextMeshProUGUI PlayerHealthBox;
- public TextMeshProUGUI DamageBox;
- public Sprite MorningBattle;
- public Sprite EveningBattle;
- public List<CombatEnemy> Enemies = new List<CombatEnemy>();
- public static bool inbattle = false;
- public Transform[] SpawnPoints;
- List<CombatEnemy> Spawned = new List<CombatEnemy>();
- List<TurnScheduleItem> turnSchedule = new List<TurnScheduleItem>();
- PlayerMovement player;
- HeartSystem PH;
- bool IsAttacking = false;
- int AttackingTarget;
- int Attacker;
- float AttackTimer;
- bool HasTakenDmg;
- public Camera BattleCam;
- void Start()
- {
- AM = AudioManager.instance;
- player = GameObject.FindObjectOfType<PlayerMovement>();
- PH = player.GetComponent<HeartSystem>();
- }
- // Update is called once per frame
- void Update()
- {
- AttackTimer += Time.deltaTime;
- if (Input.GetKeyDown(KeyCode.L))
- {
- Encounter(0);
- }
- PlayerHealthBox.text = PH.curHealth.ToString();
- if (!inbattle)
- {
- return;
- }
- if(turnSchedule.Count <= 0)
- {
- BattleSchedule();
- }
- if (!IsAttacking)
- {
- TurnScheduleItem currentTurn = turnSchedule[0];
- if (currentTurn.index != -1)
- {
- PlayerControls.SetActive(false);
- StartAttack(currentTurn.index,-1);
- }
- else
- {
- PlayerControls.SetActive(true);
- }
- }
- if(AttackTimer > 2 && !HasTakenDmg && IsAttacking)
- {
- HasTakenDmg = true;
- Debug.Log("Attacking " + AttackingTarget);
- if (AttackingTarget == -1)
- {
- PH.TakeDamage(1);
- DamageBox.text = "-1";
- DamageBox.gameObject.SetActive(true);
- DamageBox.transform.position = BattleCam.WorldToScreenPoint(BattlePlayer.transform.position + new Vector3(0,0.5f,0));
- }
- else
- {
- Spawned[AttackingTarget].HP -= 5;
- Debug.Log("Enemy down to " + Spawned[AttackingTarget].HP);
- DamageBox.text = "-5";
- DamageBox.gameObject.SetActive(true);
- DamageBox.transform.position = BattleCam.WorldToScreenPoint(Spawned[AttackingTarget].transform.position + new Vector3(0, 0.5f, 0));
- if (Spawned[AttackingTarget].HP <= 0)
- {
- Destroy(Spawned[AttackingTarget].gameObject);
- Spawned.RemoveAt(AttackingTarget);
- foreach(var foo in turnSchedule)
- {
- if(foo.index == AttackingTarget)
- {
- turnSchedule.Remove(foo);
- break;
- }
- }
- Debug.Log("Enemy Died");
- if (Spawned.Count <= 0)
- {
- EndBattle();
- }
- }
- }
- }
- DamageBox.color = new Color(1, 1, 1, 3 - AttackTimer);
- if(AttackTimer > 5 && IsAttacking)
- {
- Debug.Log("Attack Over " + Attacker);
- turnSchedule.RemoveAt(0);
- IsAttacking = false;
- }
- }
- public void Encounter(int kind)
- {
- UIHandle.SetActive(false);
- inbattle = true;
- BattleSystem.SetActive(true);
- int enemyCount = Random.Range(1, 3);
- for (int j = 0; j < enemyCount; j++)
- {
- Spawned.Add(Instantiate(Enemies[kind], SpawnPoints[j].position, Quaternion.identity));
- }
- BattleSchedule();
- }
- //Enemies.Add(Instantiate(Enemies[Type], SpawnPoints[0].position, Quaternion.identity));
- //for (int i = 0; i < SpawnPoints.Length - 1; i++)
- //{
- // for (int j = 1; j < Random.Range(1, 2); j++)
- // {
- // //Vector3 position = SpawnPoints[i].position + j * (SpawnPoints[i + 1].position - SpawnPoints[i].position) / j;
- // }
- //}
- private void StartAttack(int Attackerin, int AttackTargetin)
- {
- Attacker = Attackerin;
- AttackingTarget = AttackTargetin;
- AttackTimer = 0;
- IsAttacking = true;
- HasTakenDmg = false;
- PlayerControls.SetActive(false);
- Debug.Log("Attack started " + Attacker + " " + AttackingTarget);
- }
- private void EndBattle()
- {
- inbattle = false;
- BattleSystem.SetActive(false);
- UIHandle.SetActive(true);
- }
- internal void CloseUI()
- {
- BattleSystem.SetActive(false);
- }
- public void Attack()
- {
- if (turnSchedule[0].index != -1)
- {
- return;
- }
- if (IsAttacking)
- return;
- int AttackIndex = Random.Range(0, Spawned.Count);
- StartAttack(-1, AttackIndex);
- Debug.Log("You struck the enemy.");
- }
- private void Abilities()
- {
- }
- private void Item()
- {
- }
- private void Escape()
- {
- EndBattle();
- }
- public void BattleSchedule()
- {
- turnSchedule.Clear();
- turnSchedule.Add(new TurnScheduleItem(-1,player.combatSpeed));
- for(int i = 0; i < Spawned.Count; i++)
- {
- turnSchedule.Add(new TurnScheduleItem(i, Spawned[i].Speed));
- }
- turnSchedule.Sort(new TurnSort());
- }
- }
- class TurnScheduleItem
- {
- public int index;
- public float speed;
- public TurnScheduleItem(int i, float s)
- {
- index = i;
- speed = s;
- }
- }
- class TurnSort : IComparer<TurnScheduleItem>
- {
- public int Compare(TurnScheduleItem x, TurnScheduleItem y)
- {
- if (x.speed > y.speed)
- return -1;
- if (x.speed < y.speed)
- return 1;
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement