-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.cs
95 lines (86 loc) · 2.5 KB
/
Runner.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
93
94
95
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Runner : Manifestation
{
[SerializeField] private float runnerVelocity;
[SerializeField] private float runnerVelocityIncrement;
[SerializeField] private float pursueTime;
private bool canPursue = true;
private Transform player;
[SerializeField] private float delay;
private List<float> playerPos = new List<float>();
private bool canGo = false;
private bool canPlay;
private Animator animator;
private void Awake()
{
animator = GetComponent<Animator>();
}
protected override void OnEnable()
{
base.OnEnable();
animator.SetBool("PlayerFound", false);
player = null;
canGo = false;
canPursue = true;
canPlay = true;
if (playerPos.Count > 0)
{
playerPos.Clear();
}
EventManager<Transform>.Instance.StartListening("onPlayerDetected", SetPlayer);
EventManager<bool>.Instance.StartListening("onLevelUp", LevelUp);
}
protected override void OnDisable()
{
base.OnDisable();
EventManager<Transform>.Instance.StopListening("onPlayerDetected", SetPlayer);
}
public override void Update()
{
base.Update();
if (player != null)
{
StartCoroutine(PursueCO());
Vector3 pos = transform.position;
if (canPursue)
{
pos.x -= Vector2.right.x * runnerVelocity * Time.deltaTime;
if (canGo)
{
pos.y = playerPos[0];
playerPos.RemoveAt(0);
}
}
else if (!canPursue)
{
animator.SetBool("PlayerFound", true);
if (canPlay)
{
PlayClip();
canPlay = false;
}
pos.x -= Vector2.right.x * runnerVelocity * Time.deltaTime;
}
transform.position = pos;
}
}
private IEnumerator PursueCO()
{
playerPos.Add(player.transform.position.y);
yield return new WaitForSeconds(delay);
canGo = true;
yield return new WaitForSeconds(pursueTime + delay);
canPursue = false;
}
private void SetPlayer(Transform _player)
{
player = _player;
//PlayClip();
}
private void LevelUp(bool value)
{
runnerVelocity += runnerVelocityIncrement;
}
}