-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHammerGrabbable.cs
92 lines (73 loc) · 2.84 KB
/
HammerGrabbable.cs
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
using FishNet.Object;
using UnityEngine;
using DerailedDeliveries.Framework.Gameplay.Interactions.Interactables;
using DerailedDeliveries.Framework.DamageRepairManagement;
using DerailedDeliveries.Framework.Gameplay.Player;
namespace DerailedDeliveries.Framework.Gameplay.Interactions.Grabbables
{
/// <summary>
/// A <see cref="UseableGrabbable"/> class that handles logic for the hammer.
/// </summary>
public class HammerGrabbable : UseableGrabbable
{
private protected override bool CheckCollidingType(Interactable interactable)
=> interactable is IRepairable || interactable is ShelfInteractable;
/// <summary>
/// <inheritdoc/>
/// </summary>
/// <param name="interactor"><inheritdoc/></param>
/// <returns><inheritdoc/></returns>
public override bool CheckIfUseable(Interactor interactor)
=> IsInteractable
&& !IsOnCooldown
&& interactor.InteractingTarget == this
&& GetRepairableInteractable(GetCollidingColliders()) != null;
[Server]
private protected override Interactable GetCollidingInteractable(Interactor interactor, bool isUse)
{
if(!isUse)
return base.GetCollidingInteractable(interactor, isUse);
Collider[] colliders = GetCollidingColliders();
Interactable interactable = GetRepairableInteractable(colliders);
if(interactable != null)
return interactable;
foreach(Collider collider in colliders)
{
if(!collider.TryGetComponent(out interactable))
continue;
if(!CheckCollidingType(interactable))
continue;
if(!interactable.CheckIfInteractable(interactor))
continue;
return interactable;
}
return null;
}
private Interactable GetRepairableInteractable(Collider[] colliders)
{
foreach (Collider collider in colliders)
{
if (!collider.TryGetComponent(out Interactable interactable))
continue;
if (!CheckCollidingType(interactable))
continue;
IRepairable repairable = (IRepairable)interactable;
if (!repairable.CanBeRepaired())
continue;
return interactable;
}
return null;
}
[Server]
private protected override bool RunUse(Interactable interactable)
{
IRepairable repairable = (IRepairable)interactable;
if(repairable.CanBeRepaired())
{
repairable.Repair();
return true;
}
return base.RunUse(interactable);
}
}
}