aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Traps/TimedTrap.cs
blob: 9413b82ecc0ac697d946020a805446b1aa725b67 (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
using System.Collections;
using UnityEngine;

public class TimedTrap : TrapDamage
{
    [Header("TimedTrap")]
    [SerializeField] protected float activationDelay;
    [SerializeField] protected float activeTime;
    [SerializeField] protected float offset;

    protected Animator animator;
    protected SpriteRenderer sr;
    protected bool triggered;
    protected bool active;
    protected bool isOffset;

    protected void Awake()
    {
        animator = GetComponent<Animator>();
        sr = GetComponent<SpriteRenderer>();
    }

    private void Start()
    {
        StartCoroutine(Offset());
    }

    protected void Update()
    {
        if (!triggered)
        {
            if(isOffset)
                StartCoroutine(Activate());
        }
    }

    protected new void OnTriggerEnter2D(Collider2D collision)
    {
        if (active)
        {
            base.OnTriggerEnter2D(collision);
        }
    }

    protected new void OnTriggerStay2D(Collider2D collision)
    {
        if(active && !stay)
        {
            StartCoroutine(base.StayDamage(collision));
        }
    }

    protected IEnumerator Activate()
    {
        triggered = true;

        yield return new WaitForSeconds(activationDelay);
        active = true;
        animator.SetBool("activated", true);

        yield return new WaitForSeconds(activeTime);
        triggered = false;
        active = false;
        animator.SetBool("activated", false);
    }

    protected IEnumerator Offset()
    {
        yield return new WaitForSeconds(offset);
        isOffset = true;
    }
}