-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.cs
175 lines (146 loc) · 3.87 KB
/
car.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Car : MonoBehaviour
{
[HideInInspector] public int value = 0xFFFFFF;/// x[2] | z[2] | sharp[8] | smooth[8] | forward[4]
public int sensor_detected = 0b000;
int sharpTurnValue;
int smoothTurnValue;
int backTurnValue;
float speed = 5f;
Rigidbody rb;
Transform tr;
public bool dead = true;
bool halfLine = false;
bool finish = false;
public float time = 0f;
public float record = 0f;
bool[] scoreLines;
int timeLimit = 15;
void Start()
{
rb = GetComponent<Rigidbody>();
tr = GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
if (!dead)
{
MoveCar();
sensor_detected = 0b000;
time += Time.deltaTime;
}
else
{
// Car is dead -> stop
}
}
public void Initialize()
{
/// x[2] | z[2] | sharp[8] | smooth[8] | forward[4]
int vX = ((value & 0xC00000) >> 22);
int vZ = ((value & 0x300000) >> 20);
int startingX = 2 - (vX * 2);
int startingZ = vZ * -4;
sharpTurnValue = (value & 0x0FF0000) >> 16;
smoothTurnValue = (value & 0x000FF0)>>4;
backTurnValue = value & 0x000F;
this.transform.position = new Vector3(startingX, 0, startingZ);
this.transform.eulerAngles = new Vector3(0, 180, 0);
record = 0f;
time = 0f;
sensor_detected = 0;
dead = false;
halfLine = false;
scoreLines = new bool[6] { false, false, false, false, false, false };
}
private void Drive()
{
tr.Translate(new Vector3(0, 0, 1) * speed / 10);
}
private void Turn(int value_value)
{
tr.Rotate(new Vector3(0, 1, 0) * value_value);
}
private void MoveCar()
{
switch (sensor_detected)
{
case 0b000:
Drive();
break;
case 0b100:
Turn(sharpTurnValue);
break;
case 0b110:
Turn(smoothTurnValue);
break;
case 0b011:
Turn(-smoothTurnValue);
break;
case 0b001:
Turn(-sharpTurnValue);
break;
case 0b111:
case 0b010:
case 0b101:
Turn(backTurnValue);
break;
case 0b1111:
//body sensor detected fence
incomplete();
break;
}
if (time > timeLimit) // to kill zombie car
{
incomplete();
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == "FinishLine")
{
if (halfLine)
{
complete();
}
else
{
incomplete();
}
}
else if (other.name == "HalfLine")
{
this.halfLine = true;
}
if(other.tag == "scoreLine")
{
scoreLines[int.Parse(other.name[9].ToString()) - 1] = !scoreLines[int.Parse(other.name[9].ToString()) - 1];
}
}
private void complete()
{
this.finish = true;
ScoreCheck();
record += (100-time);
this.dead = true;
}
private void incomplete()
{
this.finish = true;
ScoreCheck();
this.dead = true;
}
private void ScoreCheck()
{
foreach(bool b in scoreLines)
{
if (b == true)
{
record += 10;
}
}
}
}