PlayerWaterProjectile.cs 2.6 KB
Newer Older
1
2
3
4
5
6
using UnityEngine;

namespace _Game.Scripts.Player.PlayerAttacks.Water
{
	public class PlayerWaterProjectile : MonoBehaviour
	{
Sebastian Frey's avatar
Sebastian Frey committed
7
		public float destroyTime = 2.0f;
Sebastian Frey's avatar
Sebastian Frey committed
8
		public float pushFactor = 1.0f;
9
		public float pushEnemyFactor = 1.0f;
10
11
		private float _startTime;
		private float _deltaTime;
12
13
		private float _newScaleFactor;
		private Vector3 _newScaleVector;
14
		private Vector3 _startingScale;
15
16
		private Rigidbody2D _rigidbody2D;
		private Vector2 _startingVelocity;
17
18
19
20
21

		private void Start()
		{
			_startTime = Time.time;
			_startingScale = this.gameObject.transform.localScale;
22
			_rigidbody2D = GetComponent<Rigidbody2D>();
23
24
25
26
		}

		private void Update()
		{
27
			_deltaTime = (Time.time - _startTime) / destroyTime;
28
29
30
31
32
33
34
35
			if (_deltaTime >= 0.8f)
			{
				_newScaleFactor = (1 - _deltaTime) * 5.0f;
			}
			else
			{
				_newScaleFactor = 1.0f;
			}
36

37
			_newScaleVector = new Vector3(_newScaleFactor * _startingScale.x, _newScaleFactor * _startingScale.y, 0.0f);
38

39
			this.gameObject.transform.localScale = _newScaleVector;
40
		}
41

Sebastian Frey's avatar
Sebastian Frey committed
42
43
44
45
46
47
		public void OnCollisionEnter2D(Collision2D other)
		{
			if (other.gameObject.layer != LayerMask.NameToLayer("Ground"))
			{
				if (other.gameObject.layer == LayerMask.NameToLayer("MovableObject"))
				{
48
					if (Mathf.Abs(_rigidbody2D.velocity.x) > 0.0f)
Sebastian Frey's avatar
Sebastian Frey committed
49
					{
50
51
						var velocity = _rigidbody2D.velocity;
						var projectileVelocityX = velocity.x / Mathf.Abs(velocity.x);
Sebastian Frey's avatar
Sebastian Frey committed
52
						other.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileVelocityX * pushFactor, 0.0f, 0.0f);
53
						Destroy();
Sebastian Frey's avatar
Sebastian Frey committed
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
				else if (other.gameObject.layer == LayerMask.NameToLayer("Enemy"))
				{
					if (other.gameObject.CompareTag("Fire"))
					{
						other.gameObject.GetComponent<Life>().Damage(1);
						if (Mathf.Abs(_rigidbody2D.velocity.x) > 0.0f)
						{
							var velocity = _rigidbody2D.velocity;
							var projectileVelocityX = velocity.x / Mathf.Abs(velocity.x);
							other.gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(projectileVelocityX * pushEnemyFactor, 0.0f));
						}
						Destroy();
					}
					else
					{
						if (Mathf.Abs(_rigidbody2D.velocity.x) > 0.0f)
						{
							var velocity = _rigidbody2D.velocity;
							var projectileVelocityX = velocity.x / Mathf.Abs(velocity.x);
							other.gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(projectileVelocityX * pushEnemyFactor, 0.0f));
						}
						Destroy();
					}
				}
				else if (other.gameObject.CompareTag("DestroyableByFire"))
				{
					Destroy();
				}
Sebastian Frey's avatar
Sebastian Frey committed
85
86
87
			}
		}

88
89
		private void OnEnable()
		{
90
			Invoke(nameof(Destroy), destroyTime);
91
92
93
94
95
96
97
98
		}

		private void Destroy()
		{
			Destroy(gameObject);
		}
	}
}