Battle Scenes

Battle.cs

controls the battle system

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

public class Battle : MonoBehaviour {
	public int howManyEnemies; //To count how many enemies join in the battle.

	public bool jasperJoined; //To judge he's dead in the battle or not joined from the start.

	public float enemyHP; //can be stronger later.

	public AudioClip spraySE;
	public AudioClip gustSE;
	public AudioClip mistSE;
	public AudioClip fireSE;
	public AudioClip explosionSE;
	public AudioClip healSE;
	public AudioClip poisonSE;
	public AudioClip rockSE;
	public AudioClip lavaSE;
	public AudioClip attackSE;
	public AudioClip gunshotSE;

	public string nextScene; //put next scene file's name

	private GameObject[] partyObj; //how many member in Amber's side alive.
	private GameObject[] enemiesObj; //how many enemies alive.

	private int partyNo;
	private int enemiesNo;

	private string amberMissed; //set common texts for some skills
	private string jasperAttack;
	private string jasperMissed;

	private int amberInput; //Amber's cast spells are converted to int. Dont touch
	private int a;
	private int b;
	private int c;

	private List<JoinedCharacter> character;
	private int turnID; //to pick "who's turn" from joined characters.
	private List<int> dead;

	private float jasperPW; //affects on jasper's attack dmg
	private float enemyAC; //affects on enemies' accuracy
	private float playerAC; //affects on party's accuracy
	private float damage; //to temporarily contain random dmg
	private float accuracy; //to temporarily contain random accuracy

	private int spray; //145 WPC, 20dmg all enemies + acc down
	private int gust; //245 APC, jasperAT up
	private int mist; //12 FWA, acc down
	private int fire; //45 FPC, 50 dmg to one enemy
	private int explosion; //25 FAP, random dmg to all enemies
	private int heal; //456 HPC, heal the lowest-health party member
	private int poison; //356 EPC, random turn-dmg to one enemy for 5 turns
	private int rock; //235 EAP, 100 dmg to one enemy with low accuracy
	private int lava; //34 FEC, 50 dmg to all enemies after 7 turns

	private bool damaging; //turn-dmg on/off
	private bool lavaComing; //lava is coming or not

	private int turn; //how many turns have passed.
	private int damEnd; //when the turn-dmg will end
	private JoinedCharacter damWho; //who is the turn-dmg target
	private int lavaOn; //when lava comes

	private int phase; //to control mouse click and text proceeding. Don't touch.
	private bool craft; //Amber's casting on/off

	private Book book;
	private GameObject bui;
	private Text bt;
	private SoundEffect se;
	private SceneTrans st;

	void Awake () {
		//register joined characters' status to lists
		enemiesNo = howManyEnemies;
		partyNo = 1;

		character = new List<JoinedCharacter> ();
		dead = new List<int> ();

		phase = 0;
		craft = false;
		turnID = 0;
		jasperPW = 1f;
		enemyAC = 1f;
		playerAC = 1f;
		turn = 0;
		damaging = false;

		book = GameObject.Find ("Book").GetComponent<Book>();
		bui = GameObject.Find ("BattleUI");
		bt = GameObject.Find ("BT").GetComponent<Text>();
		se = GameObject.Find ("SE").GetComponent<SoundEffect> ();
		st = GameObject.Find ("trans").GetComponent<SceneTrans> ();

		amberMissed = "Amber missed her attack.";
		jasperAttack = "Jasper attacked an enemy!";
		jasperMissed = "Jasper missed his attack.";

		spray = 145;
		gust = 245;
		mist = 12;
		fire = 25;
		explosion = 45;
		heal = 456;
		poison = 356;
		rock = 235;
		lava = 34;
		}

	void Start(){

		///
		/// Characters' ID is used to choose correct AI for him.
		/// 0: Amber
		/// 1: Jasper
		/// 2: Enemy1
		/// 3: Enemy2
		/// 4: Enemy3
		///

		character.Add (GameObject.Find ("Amber").GetComponent<JoinedCharacter> ());
		character [0].Set (150f, 0); //character[n].Set(health, charaID)

		if (jasperJoined == true) {
			partyNo = 2;
			character.Add (GameObject.Find ("Jasper").GetComponent<JoinedCharacter> ());
			character [1].Set (150f, 1);
		}

		if (enemiesNo == 2) {
			character.Add (GameObject.Find ("Enemy1").GetComponent<JoinedCharacter> ());
			character [partyNo].Set (enemyHP, 2);

			character.Add (GameObject.Find ("Enemy2").GetComponent<JoinedCharacter> ());
			character [partyNo + 1].Set (enemyHP, 3);
		} else if (enemiesNo == 3) {
			character.Add (GameObject.Find ("Enemy1").GetComponent<JoinedCharacter> ());
			character [partyNo].Set (enemyHP, 2);

			character.Add (GameObject.Find ("Enemy2").GetComponent<JoinedCharacter> ());
			character [partyNo + 1].Set (enemyHP, 3);

			character.Add (GameObject.Find ("Enemy3").GetComponent<JoinedCharacter> ());
			character [partyNo + 2].Set (enemyHP, 4);
		}

		AliveMembers ();
		Text ("Battle starts!");
	}

	void Text (string txt) {
		book.Close();
		bui.SetActive (true);
		bt.text = txt;
	}

	void Damage () { //poison dmg
		if(damWho != character [partyNo + enemiesNo - 1]) { //if the target is no more alive
			damaging = false;
		} else if (damWho == character [partyNo + enemiesNo - 1]) {
			Text ("Poison damages the enemy!");
			se.Sound (poisonSE);
			character [partyNo + enemiesNo - 1].Health (-20f);
		} else {
			damaging = false;
		}

		if (damEnd == turn) {
			damaging = false;
		}
	}

	void Lava(){
		if (turn == lavaOn) {
			Text ("Lava burns the enemies!");
			se.Sound (lavaSE);
			if (enemiesNo == 3) {
				character [partyNo + 2].Health (-100f);
				character [partyNo + 1].Health (-100f);
			} else if (enemiesNo == 2) {
				character [partyNo + 1].Health (-100f);
			}
			character [partyNo].Health (-100f);
			lavaComing = false;
		} else {
			Text ("Lava is getting closer to the enemies.");
			se.Sound (lavaSE);
		}
	}

	void AliveMembers(){
		enemiesObj = GameObject.FindGameObjectsWithTag("Enemy");
		partyObj = GameObject.FindGameObjectsWithTag ("Player");
		enemiesNo = enemiesObj.Length;
		partyNo = partyObj.Length;
	}

	void Turn (int chara) { //chara = characterID, not turnID!

		Debug.Log ("Turn ID = " + turnID.ToString () + ", Turn no. = " + turn.ToString());

		if (chara == 0) { //Amber's turn
			bui.SetActive (false);
			book.Open(0);
			craft = true;
		} else if (chara == 1) { //Jasper's turn: 50dmg * acc to the lowest-health enemy
			accuracy = Random.Range (0f, 99f) * playerAC;
			if (accuracy > 9f)
			{
				Text (jasperAttack);
				se.Sound (attackSE);
				character [partyNo + enemiesNo - 1].Health (-50f * jasperPW);
			} else {
				Text (jasperMissed);
			}

			jasperPW = 1f;
			phase = 0;

		} else if (chara >= 2) {
			accuracy = Random.Range (0f, 99f) * enemyAC;

			if (accuracy > 19f) {
				Text ("An enemy attacked " + character[partyNo - 1].charaName + "!");
				se.Sound (gunshotSE);
				character[partyNo - 1].Health(-30f);
			} else {
				Text ("The enemy missed his attack.");
			}

			phase = 0;
		}
		turnID = turnID + 1;

	}

	void FixedUpdate(){
		int deadppl = dead.Count;
		if (deadppl > 0) {
			Destroy(character[dead[deadppl - 1]].gameObject);
			Debug.Log (character[dead[deadppl - 1]].gameObject.name.ToString() + " is destroyed.");
			character.RemoveAt (dead[deadppl - 1]);
			dead.RemoveAt (deadppl - 1);

			//enemy or player?
			//here No - 1

			Debug.Log ("dead[deadppl - 1] contains " + dead[deadppl - 1].ToString());
		}
	}

	void Update(){
		if (craft == true){
			if (GameControl.casting == true) {

				a = GameControl.circle [0];
				b = GameControl.circle [1];
				c = GameControl.circle [2];
				//Debug.Log (a.ToString() + b.ToString() + c.ToString());

				amberInput = a * 100 + b * 10 + c;

				if (amberInput == spray) {
					Text ("Amber shot water spray to the enemies!");
					se.Sound (spraySE);
					enemyAC = Random.Range (0.2f, 0.8f);
					if (enemiesNo == 3) {
						character [partyNo + 2].Health (-20f);
						character [partyNo + 1].Health (-20f);
					} else if (enemiesNo == 2) {
						character [partyNo + 1].Health (-20f);
					}
					character [partyNo].Health (-20f);
				} else if (amberInput == gust) {
					Text ("Amber created gust to support Jasper!");
					se.Sound (gustSE);
					jasperPW = 1.5f;
				} else if (amberInput == mist) {
					Text ("Amber created mist!");
					se.Sound (mistSE);
					enemyAC = Random.Range (0.5f, 0.8f);
					playerAC = Random.Range (0.5f, 0.8f);
				} else if (amberInput == fire) {
					Text ("Amber created fire!");
					se.Sound (fireSE);
					character [partyNo + enemiesNo - 1].Health (-50f);
				} else if (amberInput == explosion) {
					Text ("Amber caused a fire explosion!");
					se.Sound (explosionSE);
					if (enemiesNo == 3) {
						damage = Random.Range (-75f, -20f);
						//Debug.Log (damage.ToString () + " dmg to Enemy3.");
						character [partyNo + 2].Health (damage);
						damage = Random.Range (-75f, -20f);
						//Debug.Log (damage.ToString () + " dmg to Enemy2.");
						character [partyNo + 1].Health (damage);
					} else if (enemiesNo == 2) {
						damage = Random.Range (-75f, -20f);
						//Debug.Log (damage.ToString () + " dmg to Enemy2.");
						character [partyNo + 1].Health (damage);
					}
					damage = Random.Range (-75f, -20f);
					//Debug.Log (damage.ToString () + " dmg to Enemy1.");
					character [partyNo].Health (damage);
				} else if (amberInput == heal) {
					if (character [partyNo - 1].health < character [partyNo - 1].maxHealth) { 						Text ("Amber cast healing magic!"); 						se.Sound (healSE); 						character [partyNo - 1].Health (9999f); 					} else { 						Text ("Nothing happened..."); 					} 				} else if (amberInput == poison) { 					Text ("Amber poisoned one enemy!"); 					se.Sound (poisonSE); 					damaging = true; 					Debug.Log ("damaging enabled " + damaging); 					damEnd = turn + 5; 					Debug.Log (damEnd); 					damWho = character [partyNo + enemiesNo - 1]; 				} else if (amberInput == rock) { 					accuracy = Random.Range (0f, 99f) * playerAC; 					if (accuracy > 30f)
					{
						character [partyNo + enemiesNo - 1].Health (-100f);
						Text ("Amber projected a rock to an enemy!");
						se.Sound (rockSE);
					} else {
						Text ("Amber missed rock projectile!");
					}
				} else if (amberInput == lava) {
					Text ("Amber created lava around the enemies!");
					se.Sound (lavaSE);
					lavaComing = true;
					lavaOn = turn + 7;
				} else {
					Text (amberMissed);
				}

				GameControl.circle = new int[] { 7, 7, 7 };
				GameControl.casting = false;
				phase = 0;
			}
		}
	}

	void LateUpdate(){
		foreach (JoinedCharacter j in character) {
			if (j.health == 0f) {
				Debug.Log (j.gameObject.name.ToString () + " is dead.");
				int ind = character.IndexOf (j);
				dead.Add (ind);
				Debug.Log (j.gameObject.name.ToString() + " will be destroyed.");
				Debug.Log ("dead[dead.Count - 1] contains " + dead[dead.Count - 1].ToString());
			}
		}
	}

	void OnMouseDown () {
		AliveMembers ();

		if (enemiesNo == 0 && phase < 3) {
			Text ("Amber won the battle!");
			phase = 3;
		} else if (partyNo == 0 && phase < 4) { 			Text ("Amber lost the battle."); 			phase = 4; 		} else if (phase == 0) { 			if (turnID >= partyNo + enemiesNo) {
				turnID = 0;
				turn = turn + 1;
				if (damaging == false && lavaComing == false) {
					Turn (character [turnID].charaID);
				} else if (damaging == false && lavaComing == true) {
					Lava ();
					phase = 2;
				} else if (damaging == true && lavaComing == false) {
					//Debug.Log ("damaging " + damaging);
					Damage ();
					phase = 2;
				} else {
					Lava ();
					phase = 1;
				}
			} else {
				Turn (character[turnID].charaID);
			}
		} else if (phase == 1) {
			//Debug.Log ("damaging " + damaging);
			Damage ();
			phase = 2;
		} else if (phase == 2) {
			Turn (character[turnID].charaID);
		} else if (phase == 3) {
			Destroy (this.GetComponent<BoxCollider2D>() );
			st.Fade (nextScene);
		} else if (phase == 4) {
			Destroy (this.GetComponent<BoxCollider2D>() );
			st.Dead();
		}

	}
}

JoinedCharacter.cs

manages each character’s information

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

public class JoinedCharacter : MonoBehaviour {

	public float health;
	public float maxHealth;
	public int charaID;
	public string charaName;
	private Slider hp;

	public void Set (float h, int id){
		health = maxHealth =  h;
		charaID = id;
	}

	void Awake(){
		charaName = this.gameObject.name;
		hp = GameObject.Find(charaName + "HP").GetComponent<Slider>();
	}

	public void Health(float val){
		health = health + val;
	}

	void FixedUpdate(){
		if (health < 0f) { 			health = 0f; 		} else if (health > maxHealth) {
			health = maxHealth;
		}
	}
	void Update(){
		hp.value = health;
	}
}

Enemy.cs

changes enemies’ sprites to “hurt” when health < 50

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

public class Enemy : MonoBehaviour {

	public Sprite normal;
	public Sprite hurt;

	private SpriteRenderer spriteRenderer;
	private Slider hpbar;
	private bool hurted;
	private float hp;

	void Awake () {
		spriteRenderer = this.GetComponentInChildren<SpriteRenderer> ();
		spriteRenderer.sprite = normal;
		hpbar = GameObject.Find(this.gameObject.name + "HP").GetComponent<Slider>();
	}

	void Update () {
		hp = hpbar.value;
		if (hp <= 50f && hurted == false) { 			spriteRenderer.sprite = hurt; 			hurted = true; 		} else if (hurted == true && hp > 50f) {
			spriteRenderer.sprite = normal;
			hurted = false;
		}
	}
}

Advertisements