PlayerInteract.cs 4.77 KB
Newer Older
1
using System;
2
using UnityEngine;
Sebastian Frey's avatar
Sebastian Frey committed
3
using _Game.Scripts.Elements;
4
5
6
7
using _Game.Scripts.Player.PlayerAttacks.Air;
using _Game.Scripts.Player.PlayerAttacks.Earth;
using _Game.Scripts.Player.PlayerAttacks.Fire;
using _Game.Scripts.Player.PlayerAttacks.Water;
8
using _Game.Scripts.Scriptable_Object;
Sebastian Frey's avatar
Sebastian Frey committed
9
10
11

namespace _Game.Scripts.Player
{
12
13
14
	[RequireComponent(typeof(PlayerAirAttack), typeof(Player_FireAttack))]
	[RequireComponent(typeof(PlayerEarthAttack), typeof(PlayerWaterAttack))]

Sebastian Frey's avatar
Sebastian Frey committed
15
16
17
18
19
	public class PlayerInteract : MonoBehaviour
	{
		private GameObject _currentObject;
		private InteractionObject _targetScript;

20
21
		[SerializeField] private PlayerState state;

22
		[SerializeField] private ElementGameEvent onElementInfused;
23

24
25
		[SerializeField] private ChargeGameEvent onChargeChange;

26
27
		private PlayerAirAttack _airAttack;
		private PlayerEarthAttack _earthAttack;
Sebastian Frey's avatar
Sebastian Frey committed
28
		private Player_FireAttack _fireAttack;
29
30
		private PlayerWaterAttack _waterAttack;

31
32
		private MovementController _controller;

33
34
35
36
		private void Start()
		{
			_airAttack = this.gameObject.GetComponent<PlayerAirAttack>();
			_earthAttack = this.gameObject.GetComponent<PlayerEarthAttack>();
Sebastian Frey's avatar
Sebastian Frey committed
37
			_fireAttack = this.gameObject.GetComponent<Player_FireAttack>();
38
			_waterAttack = this.gameObject.GetComponent<PlayerWaterAttack>();
39
40

			_controller = this.gameObject.GetComponent<MovementController>();
mthiele2's avatar
mthiele2 committed
41
42
43
44
45
46
47

			state.UpdateElement(InfusedElement.None);
			_airAttack.enabled = false;
			_earthAttack.enabled = false;
			_fireAttack.enabled = false;
			_waterAttack.enabled = false;
			_controller.SetMovementParameters(state.GetCurrentMovementState());
48
49
		}

Sebastian Frey's avatar
Sebastian Frey committed
50
51
		private void Update()
		{
52
53
54
55
			if (Input.GetButtonDown("Fire1"))
			{
				switch (state.GetCurrentElement())
				{
mthiele2's avatar
mthiele2 committed
56
					//case InfusedElement.Air:
57
58
59
60
61
//						if(_waterAttack.Attack())
//						{
//							state.ChangeCharges(-1);
//							onChargeChange.Raise();
//						}
mthiele2's avatar
mthiele2 committed
62
						//break;
63
					case InfusedElement.Earth:
mthiele2's avatar
mthiele2 committed
64
65
66
67
68
						if (_earthAttack.Attack())
						{
							state.ChangeCharges(-1);
							onChargeChange.Raise();
						}
69
70
						break;
					case InfusedElement.Fire:
71
72
73
74
75
76
						if (_fireAttack.Attack())
						{
							state.ChangeCharges(-1);
							onChargeChange.Raise();
						}
						break;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
					case InfusedElement.Water:
						print("done");
						if(_waterAttack.Attack())
						{
							state.ChangeCharges(-1);
							onChargeChange.Raise();
						}
						break;
					case InfusedElement.None:
						break;
					default:
						throw new ArgumentOutOfRangeException();
				}
			}

92
93
94
			if (!Input.GetButtonDown("Interact") || !_currentObject) return;

			switch (_currentObject.tag)
Sebastian Frey's avatar
Sebastian Frey committed
95
			{
96
				case "Air":
97
					state.UpdateElement(InfusedElement.Air);
98
					state.ChangeCharges(3);
99
					onElementInfused.Raise(InfusedElement.Air);
100
					onChargeChange.Raise();
Sebastian Frey's avatar
Sebastian Frey committed
101
102
103
104
					_airAttack.enabled = true;
					_earthAttack.enabled = false;
					_fireAttack.enabled = false;
					_waterAttack.enabled = false;
105
106
					break;
				case "Earth":
107
					state.UpdateElement(InfusedElement.Earth);
108
					state.ChangeCharges(3);
109
					onElementInfused.Raise(InfusedElement.Earth);
110
					onChargeChange.Raise();
Sebastian Frey's avatar
Sebastian Frey committed
111
112
113
114
					_airAttack.enabled = false;
					_earthAttack.enabled = true;
					_fireAttack.enabled = false;
					_waterAttack.enabled = false;
115
116
					break;
				case "Fire":
117
					state.UpdateElement(InfusedElement.Fire);
118
					state.ChangeCharges(3);
119
					onElementInfused.Raise(InfusedElement.Fire);
120
					onChargeChange.Raise();
Sebastian Frey's avatar
Sebastian Frey committed
121
122
123
124
					_airAttack.enabled = false;
					_earthAttack.enabled = false;
					_fireAttack.enabled = true;
					_waterAttack.enabled = false;
125
126
					break;
				case "Water":
127
					state.UpdateElement(InfusedElement.Water);
128
					state.ChangeCharges(3);
129
					onElementInfused.Raise(InfusedElement.Water);
130
					onChargeChange.Raise();
Sebastian Frey's avatar
Sebastian Frey committed
131
132
133
134
					_airAttack.enabled = false;
					_earthAttack.enabled = false;
					_fireAttack.enabled = false;
					_waterAttack.enabled = true;
135
136
					break;
				default:
137
					state.UpdateElement(InfusedElement.None);
138
					state.ChangeCharges(3);
139
					onElementInfused.Raise(InfusedElement.None);
140
					onChargeChange.Raise();
Sebastian Frey's avatar
Sebastian Frey committed
141
142
143
144
					_airAttack.enabled = false;
					_earthAttack.enabled = false;
					_fireAttack.enabled = false;
					_waterAttack.enabled = false;
145
					break;
Sebastian Frey's avatar
Sebastian Frey committed
146
			}
147
			_controller.SetMovementParameters(state.GetCurrentMovementState());
148
			_targetScript.InfuseElement();
Sebastian Frey's avatar
Sebastian Frey committed
149
150
151
152
153
		}


		private void OnTriggerEnter2D(Collider2D other)
		{
154
			if (other.gameObject.layer != LayerMask.NameToLayer("Elements")) return;
155

156
			print("Entered Collider of " + other.name);
Sebastian Frey's avatar
Sebastian Frey committed
157
158
159
160
161
162
			_currentObject = other.gameObject;
			_targetScript = _currentObject.GetComponent<InteractionObject>();
		}

		private void OnTriggerExit2D(Collider2D other)
		{
163
			if (other.gameObject.layer != LayerMask.NameToLayer("Elements")) return;
Sebastian Frey's avatar
Sebastian Frey committed
164
			if (other.gameObject != _currentObject) return;
165

166
			print("Exited Collider of " + other.name);
Sebastian Frey's avatar
Sebastian Frey committed
167
168
169
170
171
			_currentObject = null;
			_targetScript = null;
		}
	}
}