Spell Library

SpellLibrary.cs

keeps information of learnt spells over the scenes and show only the learnt spell on the library page.

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

public class SpellLibrary : MonoBehaviour {

	private static bool spray;
	private static bool gust;
	private static bool mist;
	private static bool fire;
	private static bool explosion;
	private static bool heal;
	private static bool poison;
	private static bool rock;
	private static bool lava;

	private bool[] learnt; // kept within a scene
	private GameObject[] spells;
	private int[] id;

	private void Renew (){
		foreach (int n in id) {
			if (learnt [n] == true) {
				spells [n].SetActive (true);
			} else {
				spells [n].SetActive (false);
			}
		}
	}

	private void Listing (){
		spray = learnt [0];
		gust = learnt [1];
		mist = learnt [2];
		fire = learnt [3];
		explosion = learnt [4];
		heal = learnt [5];
		poison = learnt [6];
		rock = learnt [7];
		lava = learnt [8];

		Renew ();
	}

	void Awake () {
		id = new int[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8 };
		learnt = new bool[] {spray, gust, mist, fire, explosion, heal, poison, rock, lava};
		spells = new GameObject[]{GameObject.Find("Spray"), GameObject.Find("Gust"), GameObject.Find("Mist"), GameObject.Find("Fire"), GameObject.Find("Explosion"), GameObject.Find("Heal"), GameObject.Find("Poison"), GameObject.Find("Rock"), GameObject.Find("Lava")};
	}

	void Start(){
		Renew();
	}

	public void Learn(int s){
		if (learnt [s] == false) {
			learnt [s] = true;
			Listing ();
		}
	}

	public static void NewSpells(){
		if(poison == false){
			poison = true;
		}
		if(rock == false){
			rock = true;
		}
		if(lava == false){
			lava = true;
		}
	}

	public static void Empty (){
		spray = false;
		gust = false;
		mist = false;
		fire = false;
		explosion = false;
		heal = false;
	    poison = false;
		rock = false;
		lava = false;
	}
}

LibFade.cs

 

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

public class LibFade : MonoBehaviour {

	private CanvasGroup cg;
	private float newAlpha;

	private bool fading;

	void Awake () {
		cg = this.gameObject.GetComponent<CanvasGroup> ();
		newAlpha = 0f;
		cg.alpha = newAlpha;
	}
	
	void OnEnable (){
		cg.alpha = newAlpha;
		fading = true;
	}

	void FixedUpdate (){
		if (fading == true) {
			newAlpha = newAlpha + 0.2f * Time.deltaTime;
			cg.alpha = newAlpha;
		} else if (newAlpha >= 1f) {
			newAlpha = 0f;
			fading = false;
		}
	}

	void OnDisable () {
		newAlpha = 0f;
		cg.alpha = newAlpha;
	}
}

Advertisements