summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Walls/XTrigger.cs
blob: cea27e259bd6c0fb7ede797ac46ff3faeacffd3d (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
using UnityEngine;
using XUtliPoolLib;

public abstract class XTrigger : MonoBehaviour
{
    protected IXPlayerAction _interface;
    private CapsuleCollider _cap = null;

    // Use this for initialization
    void Awake()
    {
        _cap = GetComponent<CapsuleCollider>();
        _cap.enabled = false;
    }

    void Update()
    {
        if (_interface == null || _interface.Deprecated) _interface = XInterfaceMgr.singleton.GetInterface<IXPlayerAction>(1);

        if (_interface != null && _interface.IsValid)
        {
            Vector3 pos = _interface.PlayerPosition(true);
            Vector3 last_pos = _interface.PlayerLastPosition(true);

            if ((last_pos - pos).sqrMagnitude > 0)
            {
                CollisionDetected(pos);
            }
        }
    }

    private void CollisionDetected(Vector3 pos)
    {
        Vector3 delta = (pos - (_cap.transform.position + _cap.center)); delta.y = 0;
        if (delta.sqrMagnitude < _cap.radius * _cap.radius)
        {
            OnTriggered();
        }
    }

    protected abstract void OnTriggered();
}