summaryrefslogtreecommitdiff
path: root/GameCode/Projectile.cs
blob: 3c15fe4f688f4bc5c89ab2191061844912a3c623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using UnityEngine;

public class Projectile : MonoBehaviour
{
	[SerializeField]
	protected Sound launchSound;

	[SerializeField]
	protected Sound hitSound;

	protected TowerType shotBy;

	[SerializeField]
	protected LayerMask layermask;

	[SerializeField]
	protected float speed;

	[SerializeField]
	protected bool homing;

	[SerializeField]
	protected float maximumLifeTime = 10f;

	[SerializeField]
	public GameObject detachOnDestruction;

	[SerializeField]
	public ProjectileFX extraFX;

	protected GameObject target;

	protected int damage;

	protected int healthDamage;

	protected int armorDamage;

	protected int shieldDamage;

	protected float slowPercent;

	protected float bleedPercent;

	protected float burnPercent;

	protected float poisonPercent;

	protected float critChance;

	protected float stunChance;

	protected virtual void Start()
	{
		if (launchSound != 0)
		{
			SFXManager.instance.PlaySound(launchSound, base.transform.position);
		}
	}

	protected virtual void FixedUpdate()
	{
		maximumLifeTime -= Time.fixedDeltaTime;
		if (maximumLifeTime < 0f)
		{
			Object.Destroy(base.gameObject);
		}
		CheckForHits();
		MoveProjectile();
		if (homing)
		{
			AlterCourse();
		}
	}

	public virtual void SetStats(TowerType whoShotMe, GameObject _target, float spd, int dmg, int healthDmg, int armorDmg, int shieldDmg, float slow, float bleed, float burn, float poison, float crit, float stun)
	{
		shotBy = whoShotMe;
		target = _target;
		speed = spd;
		damage = dmg;
		healthDamage = healthDmg;
		armorDamage = armorDmg;
		shieldDamage = shieldDmg;
		slowPercent = slow;
		bleedPercent = bleed;
		burnPercent = burn;
		poisonPercent = poison;
		critChance = crit;
		stunChance = stun;
	}

	protected virtual void MoveProjectile()
	{
		base.transform.Translate(Vector3.forward * speed * Time.fixedDeltaTime);
	}

	protected virtual void AlterCourse()
	{
		if (!(target == null))
		{
			Quaternion rotation = Quaternion.LookRotation(0.25f * Vector3.up + target.transform.position - base.transform.position, Vector3.up);
			base.transform.rotation = rotation;
		}
	}

	protected virtual void CheckForHits()
	{
		if (Physics.Raycast(base.transform.position, base.transform.forward, out var hitInfo, speed * Time.fixedDeltaTime, layermask, QueryTriggerInteraction.Collide))
		{
			OnHit(hitInfo);
		}
	}

	protected virtual void OnHit(RaycastHit hit)
	{
		IDamageable component = hit.transform.GetComponent<IDamageable>();
		if (component != null)
		{
			DealDamage(component);
		}
		if (detachOnDestruction != null)
		{
			detachOnDestruction.transform.parent = null;
		}
		if (extraFX != null)
		{
			extraFX.OnDetach();
		}
		Object.Destroy(base.gameObject);
	}

	protected virtual void DealDamage(IDamageable target)
	{
		target.TakeDamage(shotBy, damage, healthDamage, armorDamage, shieldDamage, slowPercent, bleedPercent, burnPercent, poisonPercent, critChance, stunChance);
		if (hitSound != 0)
		{
			SFXManager.instance.PlaySound(hitSound, base.transform.position);
		}
	}
}