summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Decompile/PlayerInteraction.cs
blob: 184045bc923c43ba9bbcaecd86bdacf434632dce (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
88
89
90
91
92
93
94
95
96
97
98
99
100
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Rewired;
using UnityEngine;
using UnityEngine.Events;

public class PlayerInteraction : MonoBehaviour
{
	private int balance;

	public float coinMagnetRadius = 10f;

	public LayerMask coinLayer;

	public float interactionRadius = 3f;

	public LayerMask interactorLayer;

	private ManualAttack manualAttack;

	private int networth;

	public PlayerCharacterAudio playerAudio;

	public GameObject coinSpendFX;

	public Transform coinSpendFXOrigin;

	private TagManager tagManager;

	[HideInInspector]
	public UnityEvent<int> onBalanceGain = new UnityEvent<int>();

	[HideInInspector]
	public UnityEvent<int> onBalanceSpend = new UnityEvent<int>();

	[HideInInspector]
	public UnityEvent onFocusPaymentInteraction = new UnityEvent();

	[HideInInspector]
	public UnityEvent onUnfocusPaymentInteraction = new UnityEvent();

	public static PlayerInteraction instance;

	private InteractorBase focussedInteractor;

	private Player input;

	public int Networth => networth;

	public ManualAttack EquippedWeapon => manualAttack;

	public bool IsFreeToCallNight
	{
		get
		{
			if (!LocalGamestate.Instance.PlayerFrozen && focussedInteractor == null && tagManager.CountObjectsWithTag(TagManager.ETag.CastleCenter) > 0 && TutorialManager.AllowStartingTheNight && !EnemySpawner.instance.MatchOver)
			{
				return LocalGamestate.Instance.CurrentState == LocalGamestate.State.InMatch;
			}
			return false;
		}
	}

	public int Balance => balance;

	public int TrueBalance => balance + TagManager.instance.coins.Count + CoinSpawner.AllCoinsLeftToBeSpawned;

	public InteractorBase FocussedInteractor => focussedInteractor;

	private void Awake()
	{
		instance = this;
	}

	public void EquipWeapon(ManualAttack _manualAttack)
	{
		manualAttack = _manualAttack;
		playerAudio.OnWeaponEquip(manualAttack);
	}

	private void Start()
	{
		input = ReInput.players.GetPlayer(0);
		tagManager = TagManager.instance;
	}

	private void Update()
	{
		FetchCoins();
		FetchInteractors();
		RunInteraction();
	}

	private void RunInteraction()
	{
		if (LocalGamestate.Instance.PlayerFrozen)
		{
			return;
		}
		if (input.GetButtonDown("Interact"))
		{
			if ((bool)focussedInteractor)
			{
				focussedInteractor.InteractionBegin(this);
			}
			else if ((bool)manualAttack)
			{
				manualAttack.TryToAttack();
			}
		}
		if (input.GetButton("Interact") && (bool)focussedInteractor)
		{
			focussedInteractor.InteractionHold(this);
		}
		if (input.GetButtonUp("Interact") && (bool)focussedInteractor)
		{
			focussedInteractor.InteractionEnd(this);
		}
		if (input.GetButton("Preview Build Options"))
		{
			BuildingInteractor.displayAllBuildPreviews = true;
		}
		else
		{
			BuildingInteractor.displayAllBuildPreviews = false;
		}
	}

	private void FetchInteractors()
	{
		if (input.GetButton("Call Night") || input.GetButton("Interact") || ((bool)focussedInteractor && focussedInteractor is BuildingInteractor && (focussedInteractor as BuildingInteractor).IsWaitingForChoice))
		{
			return;
		}
		Collider[] array = Physics.OverlapSphere(base.transform.position, interactionRadius, interactorLayer);
		InteractorBase interactorBase = null;
		float num = float.PositiveInfinity;
		Collider[] array2 = array;
		foreach (Collider collider in array2)
		{
			InteractorBase component = collider.GetComponent<InteractorBase>();
			if ((bool)component && component.CanBeInteractedWith)
			{
				float num2 = Vector3.Distance(base.transform.position, collider.ClosestPoint(base.transform.position));
				if (num2 < num)
				{
					interactorBase = component;
					num = num2;
				}
			}
		}
		if ((bool)focussedInteractor && focussedInteractor != interactorBase)
		{
			if (focussedInteractor is BuildingInteractor)
			{
				onUnfocusPaymentInteraction.Invoke();
			}
			focussedInteractor.Unfocus(this);
			focussedInteractor = null;
		}
		if ((bool)interactorBase && interactorBase != focussedInteractor)
		{
			if (interactorBase is BuildingInteractor)
			{
				onFocusPaymentInteraction.Invoke();
			}
			interactorBase.Focus(this);
			focussedInteractor = interactorBase;
		}
	}

	private void FetchCoins()
	{
		Collider[] array = Physics.OverlapSphere(base.transform.position, coinMagnetRadius, coinLayer);
		for (int i = 0; i < array.Length; i++)
		{
			Coin component = array[i].GetComponent<Coin>();
			if (component != null && component.IsFree)
			{
				component.SetTarget(this);
			}
		}
	}

	public void AddCoin(int amount = 1)
	{
		balance += amount;
		networth += amount;
		onBalanceGain.Invoke(amount);
	}

	public void SpendCoins(int amount)
	{
		balance -= amount;
		onBalanceSpend.Invoke(amount);
		Object.Instantiate(coinSpendFX, coinSpendFXOrigin.position, coinSpendFXOrigin.rotation, base.transform);
	}

	private void OnDrawGizmosSelected()
	{
		Gizmos.color = Color.blue;
		Gizmos.DrawWireSphere(base.transform.position, interactionRadius);
		Gizmos.color = Color.yellow;
		Gizmos.DrawWireSphere(base.transform.position, coinMagnetRadius);
	}
}