Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/HexGL/bkcore/hexgl/Gameplay.js
4627 views
1
/*
2
* HexGL
3
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
4
* @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License.
5
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.
6
*/
7
8
var bkcore = bkcore || {};
9
bkcore.hexgl = bkcore.hexgl || {};
10
11
bkcore.hexgl.Gameplay = function(opts)
12
{
13
var self = this;
14
15
this.startDelay = opts.hud == null ? 0 : 1000;
16
this.countDownDelay = opts.hud == null ? 1000 : 1500;
17
18
this.active = false;
19
this.timer = new bkcore.Timer();
20
this.modes = {
21
'timeattack':null,
22
'survival':null,
23
'replay':null
24
};
25
this.mode = opts.mode == undefined || !(opts.mode in this.modes) ? "timeattack" : opts.mode;
26
this.step = 0;
27
28
this.hud = opts.hud;
29
this.shipControls = opts.shipControls;
30
this.cameraControls = opts.cameraControls;
31
this.track = opts.track;
32
this.analyser = opts.analyser;
33
this.pixelRatio = opts.pixelRatio;
34
35
this.previousCheckPoint = -1;
36
37
this.results = {
38
FINISH: 1,
39
DESTROYED: 2,
40
WRONGWAY: 3,
41
REPLAY: 4,
42
NONE: -1
43
};
44
this.result = this.results.NONE;
45
46
this.lap = 1;
47
this.lapTimes = [];
48
this.lapTimeElapsed = 0;
49
this.maxLaps = 3;
50
this.score = null;
51
this.finishTime = null;
52
this.onFinish = opts.onFinish == undefined ? function(){console.log("FINISH");} : opts.onFinish;
53
54
this.raceData = null;
55
56
this.modes.timeattack = function()
57
{
58
self.raceData.tick(this.timer.time.elapsed);
59
60
self.hud != null && self.hud.updateTime(self.timer.getElapsedTime());
61
var cp = self.checkPoint();
62
63
if(cp == self.track.checkpoints.start && self.previousCheckPoint == self.track.checkpoints.last)
64
{
65
self.previousCheckPoint = cp;
66
var t = self.timer.time.elapsed;
67
self.lapTimes.push(t - self.lapTimeElapsed);
68
self.lapTimeElapsed = t;
69
70
if(self.lap == this.maxLaps)
71
{
72
self.end(self.results.FINISH);
73
}
74
else
75
{
76
self.lap++;
77
self.hud != null && self.hud.updateLap(self.lap, self.maxLaps);
78
79
if(self.lap == self.maxLaps)
80
self.hud != null && self.hud.display("Final lap", 0.5);
81
}
82
}
83
else if(cp != -1 && cp != self.previousCheckPoint)
84
{
85
self.previousCheckPoint = cp;
86
//self.hud.display("Checkpoint", 0.5);
87
}
88
89
if(self.shipControls.destroyed == true)
90
{
91
self.end(self.results.DESTROYED);
92
}
93
};
94
95
this.modes.replay = function()
96
{
97
self.raceData.applyInterpolated(this.timer.time.elapsed);
98
99
if(self.raceData.seek == self.raceData.last)
100
{
101
self.end(self.result.REPLAY);
102
}
103
};
104
}
105
106
bkcore.hexgl.Gameplay.prototype.simu = function()
107
{
108
this.lapTimes = [92300, 91250, 90365];
109
this.finishTime = this.lapTimes[0]+this.lapTimes[1]+this.lapTimes[2];
110
if(this.hud != null) this.hud.display("Finish");
111
this.step = 100;
112
this.result = this.results.FINISH;
113
this.shipControls.active = false;
114
}
115
116
bkcore.hexgl.Gameplay.prototype.start = function(opts)
117
{
118
this.finishTime = null;
119
this.score = null;
120
this.lap = 1;
121
122
this.shipControls.reset(this.track.spawn, this.track.spawnRotation);
123
this.shipControls.active = false;
124
125
this.previousCheckPoint = this.track.checkpoints.start;
126
127
this.raceData = new bkcore.hexgl.RaceData(this.track.name, this.mode, this.shipControls);
128
if(this.mode == 'replay')
129
{
130
this.cameraControls.mode = this.cameraControls.modes.ORBIT;
131
if(this.hud != null) this.hud.messageOnly = true;
132
133
try {
134
var d = localStorage['race-'+this.track.name+'-replay'];
135
if(d == undefined)
136
{
137
console.error('No replay data for '+'race-'+this.track.name+'-replay'+'.');
138
return false;
139
}
140
this.raceData.import(
141
JSON.parse(d)
142
);
143
}
144
catch(e) { console.error('Bad replay format : '+e); return false; }
145
}
146
147
this.active = true;
148
this.step = 0;
149
this.timer.start();
150
if(this.hud != null)
151
{
152
this.hud.resetTime();
153
this.hud.display("Get ready", 1);
154
this.hud.updateLap(this.lap, this.maxLaps);
155
}
156
}
157
158
bkcore.hexgl.Gameplay.prototype.end = function(result)
159
{
160
this.score = this.timer.getElapsedTime();
161
this.finishTime = this.timer.time.elapsed;
162
this.timer.start();
163
this.result = result;
164
165
this.shipControls.active = false;
166
167
if(result == this.results.FINISH)
168
{
169
if(this.hud != null) this.hud.display("Finish");
170
this.step = 100;
171
}
172
else if(result == this.results.DESTROYED)
173
{
174
if(this.hud != null) this.hud.display("Destroyed");
175
this.step = 100;
176
}
177
}
178
179
bkcore.hexgl.Gameplay.prototype.update = function()
180
{
181
if(!this.active) return;
182
183
this.timer.update();
184
185
if(this.step == 0 && this.timer.time.elapsed >= this.countDownDelay+this.startDelay)
186
{
187
if(this.hud != null) this.hud.display("3");
188
this.step = 1;
189
}
190
else if(this.step == 1 && this.timer.time.elapsed >= 2*this.countDownDelay+this.startDelay)
191
{
192
if(this.hud != null) this.hud.display("2");
193
this.step = 2;
194
}
195
else if(this.step == 2 && this.timer.time.elapsed >= 3*this.countDownDelay+this.startDelay)
196
{
197
if(this.hud != null) this.hud.display("1");
198
this.step = 3;
199
}
200
else if(this.step == 3 && this.timer.time.elapsed >= 4*this.countDownDelay+this.startDelay)
201
{
202
if(this.hud != null) this.hud.display("Go", 0.5);
203
this.step = 4;
204
this.timer.start();
205
206
if(this.mode != "replay")
207
this.shipControls.active = true;
208
}
209
else if(this.step == 4)
210
{
211
this.modes[this.mode].call(this);
212
}
213
else if(this.step == 100 && this.timer.time.elapsed >= 2000)
214
{
215
this.active = false;
216
this.onFinish.call(this);
217
}
218
}
219
220
bkcore.hexgl.Gameplay.prototype.checkPoint = function()
221
{
222
var x = Math.round(this.analyser.pixels.width/2 + this.shipControls.dummy.position.x * this.pixelRatio);
223
var z = Math.round(this.analyser.pixels.height/2 + this.shipControls.dummy.position.z * this.pixelRatio);
224
225
var color = this.analyser.getPixel(x, z);
226
227
if(color.r == 255 && color.g == 255 && color.b < 250)
228
return color.b;
229
else
230
return -1;
231
}
232