PlayerWaterAttack.cs 1.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections;
using UnityEngine;

namespace _Game.Scripts.Player.PlayerAttacks.Water
{
	public class PlayerWaterAttack : MonoBehaviour
	{
		private float _nextFire;
		private Rigidbody2D _body;

		public float cooldown;
		public int volleySize;
		public float launchPower;
		public float attackSpeed;

		public Transform shotSpawn;
		public GameObject elementalAttack;

19
20
21
		private bool _ready;
		private bool _activated;

22
23
24
		private void Start()
		{
			_nextFire = 0.0f;
25
26
			_ready = true;
			_activated = false;
27
28
29
30
		}

		private void Update()
		{
31
32
33
			_ready = Time.time > _nextFire;

			if (!_activated) return;
34

35
36
			print("Water_Attack");
			_nextFire = Time.time + cooldown;
37
38
39
40

			StartCoroutine(Fire());
		}

41
42
43
44
45
46
47
48
49
50
		public bool Attack()
		{
			if (_ready)
			{
				_activated = true;
			}

			return _ready;
		}

51
52
53
54
55
56
		private IEnumerator Fire()
		{
			for (var i = 0; i < volleySize; i++)
			{
				var position = shotSpawn.position;
				var projectile = Instantiate(elementalAttack, position, shotSpawn.rotation);
Sebastian Frey's avatar
Sebastian Frey committed
57
58
59
				var direction = new Vector3(position.x - transform.position.x, 0.0f, 0.0f);
				if (direction.x < 0)
				{
60
61
62
					var localScale = projectile.transform.localScale;
					localScale = new Vector3(-localScale.x, localScale.y, 0.0f);
					projectile.transform.localScale = localScale;
Sebastian Frey's avatar
Sebastian Frey committed
63
				}
64
65
66
				projectile.GetComponent<Rigidbody2D>().velocity = direction * launchPower;
				yield return new WaitForSeconds(attackSpeed);
			}
67
68

			_activated = false;
69
70
71
		}
	}
}