So I have doubts about my code and if it is any good. I have a parent class Spell:
using UnityEngine;
using System.Collections;
public class Spell : MonoBehaviour {
public enum SpellType { AttackType, AreaType, BuffType, DebuffType };
public float damage;
public float speed;
public float cooldowntime;
public SpellType type;
public Spell()
{
}
void Start () {
}
protected void Update () {
if(type == SpellType.AttackType)
{
updateSpell();
}
}
protected virtual void updateSpell()
{
}
protected void deleteSpell()
{
Destroy(gameObject);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Monster")
{
Interface.IMonster inter = other.GetComponent<Interface.IMonster>();
inter.takeDamage(damage);
deleteSpell();
}
}
}
Then one of the child classes:
using UnityEngine;
using System.Collections;
class WaterBolt : Spell
{
private Vector3 mouseposition;
private Vector3 screenpoint;
private Vector2 offset;
private float angle;
void Start()
{
mouseposition = Input.mousePosition;
screenpoint = Camera.main.WorldToScreenPoint(transform.localPosition);
offset = new Vector2(mouseposition.x - screenpoint.x, mouseposition.y - screenpoint.y);
angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
Invoke("deleteSpell", 5f);
}
protected override void updateSpell()
{
transform.position += transform.right * speed * Time.deltaTime;
}
}
Then this is what pretty much displays and drives the spell system with cooldown. Its a bit long and I would like to shorten but I can't think of something that would help. using UnityEngine; using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class GuiSpellHandler : MonoBehaviour {
public GameObject player;
public int activenumber = 0;
public GameObject spell1, spell2, spell3, spell4, spell5, spell6, spell7, spell8;
public float spellcooldown1, spellcooldown2, spellcooldown3, spellcooldown4, spellcooldown5,
spellcooldown6, spellcooldown7, spellcooldown8;
float spellcooldownnumber1, spellcooldownnumber2, spellcooldownnumber3, spellcooldownnumber4, spellcooldownnumber5,
spellcooldownnumber6, spellcooldownnumber7, spellcooldownnumber8;
public Image spell1cooldownimage, spell2cooldownimage, spell3cooldownimage, spell4cooldownimage, spell5cooldownimage,
spell6cooldownimage, spell7cooldownimage, spell8cooldownimage;
void Start () {
player = GameObject.FindWithTag("Player");
}
void Update () {
scrollSpells();
displayCooldowns();
}
public void activateSpell(Transform trans)
{
switch (activenumber)
{
case 0:
if (spellcooldown1 == 0 || spellcooldown1 < Time.time)
{
Instantiate(spell1, trans.position, trans.rotation);
Spell spell = spell1.GetComponent<Spell>();
spellcooldown1 = Time.time + spell.cooldowntime;
spellcooldownnumber1 = spell.cooldowntime;
}
break;
case 1:
if (spellcooldown2 == 0 || spellcooldown2 < Time.time)
{
Instantiate(spell2, trans.position, trans.rotation);
Spell spell = spell2.GetComponent<Spell>();
spellcooldown2 = Time.time + spell.cooldowntime;
spellcooldownnumber2 = spell.cooldowntime;
}
break;
case 2:
if (spellcooldown3 == 0 || spellcooldown3 < Time.time)
{
Instantiate(spell3, trans.position, trans.rotation);
Spell spell = spell3.GetComponent<Spell>();
spellcooldown3 = Time.time + spell.cooldowntime;
spellcooldownnumber3 = spell.cooldowntime;
}
break;
case 3:
if (spellcooldown4 == 0 || spellcooldown4 < Time.time)
{
Instantiate(spell4, trans.position, trans.rotation);
Spell spell = spell4.GetComponent<Spell>();
spellcooldown4 = Time.time + spell.cooldowntime;
spellcooldownnumber4 = spell.cooldowntime;
}
break;
case 4:
if (spellcooldown5 == 0 || spellcooldown5 < Time.time)
{
Instantiate(spell5, trans.position, trans.rotation);
Spell spell = spell5.GetComponent<Spell>();
spellcooldown5 = Time.time + spell.cooldowntime;
spellcooldownnumber5 = spell.cooldowntime;
}
break;
case 5:
if (spellcooldown6 == 0 || spellcooldown6 < Time.time)
{
Instantiate(spell6, trans.position, trans.rotation);
Spell spell = spell6.GetComponent<Spell>();
spellcooldown6 = Time.time + spell.cooldowntime;
spellcooldownnumber6 = spell.cooldowntime;
}
break;
case 6:
if (spellcooldown7 == 0 || spellcooldown7 < Time.time)
{
Instantiate(spell7, trans.position, trans.rotation);
Spell spell = spell7.GetComponent<Spell>();
spellcooldown7 = Time.time + spell.cooldowntime;
spellcooldownnumber7 = spell.cooldowntime;
}
break;
case 7:
if (spellcooldown8 == 0 || spellcooldown8 < Time.time)
{
Instantiate(spell8, trans.position, trans.rotation);
Spell spell = spell8.GetComponent<Spell>();
spellcooldown8 = Time.time + spell.cooldowntime;
spellcooldownnumber8 = spell.cooldowntime;
}
break;
default:
break;
}
}
void scrollSpells()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0f) // forward
{
if (activenumber == 7)
activenumber = 0;
else
activenumber++;
}
else if (Input.GetAxis("Mouse ScrollWheel") < 0f) // backwards
{
if (activenumber == 0)
activenumber = 7;
else
activenumber--;
}
}
void displayCooldowns()
{
spell1cooldownimage.fillAmount = (spellcooldown1- Time.time) / spellcooldownnumber1;
spell2cooldownimage.fillAmount = (spellcooldown2 - Time.time) / spellcooldownnumber2;
spell3cooldownimage.fillAmount = (spellcooldown3 - Time.time) / spellcooldownnumber3;
spell4cooldownimage.fillAmount = (spellcooldown4 - Time.time) / spellcooldownnumber4;
spell5cooldownimage.fillAmount = (spellcooldown5 - Time.time) / spellcooldownnumber5;
spell6cooldownimage.fillAmount = (spellcooldown6 - Time.time) / spellcooldownnumber6;
spell7cooldownimage.fillAmount = (spellcooldown7 - Time.time) / spellcooldownnumber7;
spell8cooldownimage.fillAmount = (spellcooldown8 - Time.time) / spellcooldownnumber8;
}
}
I hope someone could review this code for me and give me tips about how I use inheritance or how my SpellHandlerGUI can be improved!