-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deer.cs
142 lines (131 loc) · 4.69 KB
/
Deer.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
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
using UnityEngine;
using System.Collections;
public class Deer : MonoBehaviour
{
private GameObject player;
private StateManager stateManager;
public AnimationClip[] clips;
private Coroutine coroutine;
private float direction = 0.1f;
private int thinkTimer;
private bool coroutineBusy;
private bool running = true;
private bool startled;
//! Called by unity engine on start up to initialize variables.
public void Start()
{
player = GameObject.Find("Player");
stateManager = GameObject.Find("GameManager").GetComponent<StateManager>();
GetComponent<Animation>()["Run"].speed = 0.75f;
GetComponent<Animation>()["Idle"].speed = 0.25f;
}
//! Called once per frame by unity engine.
public void Update()
{
if (coroutineBusy == false && stateManager.worldLoaded == true && !stateManager.Busy())
{
coroutine = StartCoroutine(Think());
}
}
//! Controls movement, animation and player detection.
public IEnumerator Think()
{
coroutineBusy = true;
float playerDistance = Vector3.Distance(player.transform.position, transform.position);
if (playerDistance < 40)
{
startled = true;
}
NetworkPlayer[] networkPlayers = FindObjectsOfType<NetworkPlayer>();
foreach (NetworkPlayer netWorkPlayer in networkPlayers)
{
float networkPlayerDistance = Vector3.Distance(netWorkPlayer.transform.position, transform.position);
if (networkPlayerDistance < 40)
{
startled = true;
break;
}
}
if (startled == true)
{
direction = Random.Range(0, 1) > 0.5f ? 0.1f : -0.1f;
GetComponent<Animation>().Play("Run");
thinkTimer = 0;
running = true;
yield return new WaitForSeconds(10);
startled = false;
}
else
{
yield return new WaitForSeconds(0.25f);
thinkTimer++;
if (thinkTimer > Random.Range(20, 60))
{
GetComponent<Animation>().Play("Idle");
if (running == true)
{
GetComponent<Rigidbody>().AddForce(transform.right * 12000);
running = false;
}
}
if (thinkTimer > Random.Range(60, 120))
{
direction = Random.Range(0, 1) > 0.5f ? 0.1f : -0.1f;
GetComponent<Animation>().Play("Run");
running = true;
thinkTimer = 0;
}
}
EnforceWorldLimits();
coroutineBusy = false;
}
//! Frame-rate independent physics calculations.
public void FixedUpdate()
{
if (running == true)
{
GetComponent<Rigidbody>().AddForce(-transform.right * 6000);
transform.Rotate(Vector3.up * direction);
}
}
//! For avoiding obstacles.
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag != "Landscape")
{
direction = Random.Range(0, 1) > 0.5f ? 1 : -1;
}
}
//! Enforces world size limitations.
private void EnforceWorldLimits()
{
if (gameObject.transform.position.x > 4500)
{
gameObject.transform.position = new Vector3(4500, gameObject.transform.position.y, gameObject.transform.position.z);
direction = Random.Range(0, 1) > 0.5f ? 1 : -1;
}
if (gameObject.transform.position.z > 4500)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 4500);
direction = Random.Range(0, 1) > 0.5f ? 1 : -1;
}
if (gameObject.transform.position.x < -4500)
{
gameObject.transform.position = new Vector3(-4500, gameObject.transform.position.y, gameObject.transform.position.z);
direction = Random.Range(0, 1) > 0.5f ? 1 : -1;
}
if (gameObject.transform.position.z < -4500)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, -4500);
direction = Random.Range(0, 1) > 0.5f ? 1 : -1;
}
if (gameObject.transform.position.y > 500)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, 500, gameObject.transform.position.z);
}
if (gameObject.transform.position.y < -100)
{
gameObject.transform.position = new Vector3(gameObject.transform.position.x, 500, gameObject.transform.position.z);
}
}
}