summaryrefslogtreecommitdiff
path: root/GameCode/Sawblade.cs
blob: 19038dfc2df5dce701cde18a9e2e5ea71cb6d84b (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
using System.Collections.Generic;
using UnityEngine;

public class Sawblade : Projectile
{
	private Pathfinder targetPathfinder;

	private Waypoint nextWaypoint;

	private bool pathMode;

	private HashSet<Collider> targetsHit = new HashSet<Collider>();

	public override 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)
	{
		base.SetStats(whoShotMe, _target, spd, dmg, healthDmg, armorDmg, shieldDmg, slow, bleed, burn, poison, crit, stun);
		targetPathfinder = target.GetComponent<Pathfinder>();
		nextWaypoint = targetPathfinder.currentWaypoint;
	}

	protected override void AlterCourse()
	{
		if (!pathMode && targetPathfinder != null)
		{
			nextWaypoint = targetPathfinder.currentWaypoint;
		}
		if (!pathMode && (target == null || Vector3.SqrMagnitude(target.transform.position - base.transform.position) < 0.125f))
		{
			nextWaypoint = GetPreviousWaypoint();
			pathMode = true;
		}
		Vector3 position;
		if (!pathMode)
		{
			position = target.transform.position;
		}
		else
		{
			if (Vector3.SqrMagnitude(nextWaypoint.transform.position - base.transform.position) < 0.125f)
			{
				nextWaypoint = GetPreviousWaypoint();
			}
			position = nextWaypoint.transform.position;
		}
		Quaternion rotation = Quaternion.LookRotation(position - base.transform.position, Vector3.up);
		base.transform.rotation = rotation;
	}

	protected override void CheckForHits()
	{
		Collider[] array = Physics.OverlapBox(base.transform.position, Vector3.one * 0.25f, Quaternion.identity, layermask, QueryTriggerInteraction.Collide);
		foreach (Collider collider in array)
		{
			if (!pathMode && collider.gameObject == target)
			{
				nextWaypoint = GetPreviousWaypoint();
				pathMode = true;
			}
			if (targetsHit.Contains(collider))
			{
				continue;
			}
			IDamageable component = collider.GetComponent<IDamageable>();
			if (component != null)
			{
				DealDamage(component);
				damage--;
				if (damage <= 0)
				{
					Object.Destroy(base.gameObject);
				}
			}
			targetsHit.Add(collider);
		}
	}

	private Waypoint GetPreviousWaypoint()
	{
		Waypoint[] previousWaypoints = nextWaypoint.GetPreviousWaypoints();
		if (previousWaypoints.Length == 0)
		{
			Object.Destroy(base.gameObject);
			return nextWaypoint;
		}
		return previousWaypoints[Random.Range(0, previousWaypoints.Length)];
	}
}