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

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

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

19 20
		private Rigidbody2D _rigidbody2D;

21
		[SerializeField] private PlayerState state;
22
		[SerializeField] private float infusingTime;
23

24
		private PlayerEarthAttack _earthAttack;
Sebastian Frey's avatar
Sebastian Frey committed
25
		private Player_FireAttack _fireAttack;
26 27
		private PlayerWaterAttack _waterAttack;

28 29
		private MovementController _controller;

30 31 32
		private string _elementType;
		private InteractionObject _elementScript;

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

			_controller = this.gameObject.GetComponent<MovementController>();
mthiele2's avatar
mthiele2 committed
40

41 42
			_rigidbody2D = this.gameObject.GetComponent<Rigidbody2D>();

mthiele2's avatar
mthiele2 committed
43 44 45
			_earthAttack.enabled = false;
			_fireAttack.enabled = false;
			_waterAttack.enabled = false;
46 47
		}

Sebastian's avatar
Sebastian committed
48 49 50 51 52
		private void OnDisable()
		{
			state.UpdateElement(InfusedElement.None);
		}

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

85
			if (Input.GetButtonDown("Interact") && _currentObject && _targetScript.GetIsReady())
86
			{
87 88 89
				_rigidbody2D.bodyType = RigidbodyType2D.Static;
				_elementType = _currentObject.tag;
				_elementScript = _targetScript;
90
				state.isCharging = true;
91
				Invoke("SuckIt", infusingTime);
92
			}
93

94
			if (Input.GetButtonDown("Cancel Element"))
Sebastian Frey's avatar
Sebastian Frey committed
95
			{
96 97 98 99 100
				state.UpdateElement(InfusedElement.None);
				state.ChangeCharges(-5);
				_earthAttack.enabled = false;
				_fireAttack.enabled = false;
				_waterAttack.enabled = false;
Sebastian Frey's avatar
Sebastian Frey committed
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
		private void SuckIt()
		{
			switch (_elementType)
			{
				case "Air":
					state.UpdateElement(InfusedElement.Air);
					_earthAttack.enabled = false;
					_fireAttack.enabled = false;
					_waterAttack.enabled = false;
					break;
				case "Earth":
					state.UpdateElement(InfusedElement.Earth);
					_earthAttack.enabled = true;
					_fireAttack.enabled = false;
					_waterAttack.enabled = false;
					break;
				case "Fire":
					state.UpdateElement(InfusedElement.Fire);
					_earthAttack.enabled = false;
					_fireAttack.enabled = true;
					_waterAttack.enabled = false;
					break;
				case "Water":
					state.UpdateElement(InfusedElement.Water);
					_earthAttack.enabled = false;
					_fireAttack.enabled = false;
					_waterAttack.enabled = true;
					break;
				default:
					throw new ArgumentOutOfRangeException();
			}
			_elementScript.InfuseElement();

			_rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
138 139

			state.isCharging = false;
140
		}
Sebastian Frey's avatar
Sebastian Frey committed
141 142 143

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

Sebastian Frey's avatar
Sebastian Frey committed
146 147 148 149 150 151
			_currentObject = other.gameObject;
			_targetScript = _currentObject.GetComponent<InteractionObject>();
		}

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

Sebastian Frey's avatar
Sebastian Frey committed
155 156 157 158 159
			_currentObject = null;
			_targetScript = null;
		}
	}
}