Skip to content

Commit 9214a94

Browse files
committedJan 29, 2013
Updated TouchControls and ShipControls with mobile fixes.
1 parent b7ea7a2 commit 9214a94

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed
 

‎bkcore.coffee/TouchController.coffee

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
###
88
class TouchController
99

10+
@isTouchable: ->
11+
return ('ontouchstart' of document.documentElement);
12+
1013
###
1114
Creates a new TouchController
1215
1316
@param dom DOMElement The element that will listen to touch events
1417
@param stickMargin int The left margin in px for stick detection
1518
@param buttonCallback function Callback for non-stick touches
1619
###
17-
constructor: (@dom, @stickMargin, @buttonCallback) ->
20+
constructor: (@dom, @stickMargin=200, @buttonCallback=null) ->
1821
@active = true
1922
@touches = null
2023
@stickID = -1
@@ -39,7 +42,7 @@ class TouchController
3942
@stickVector.set(0, 0)
4043
continue
4144
else
42-
@buttonCallback?(touch, event)
45+
@buttonCallback?(on, touch, event)
4346
@touches = event.touches
4447
false
4548

@@ -68,6 +71,8 @@ class TouchController
6871
@stickID = -1
6972
@stickVector.set(0, 0)
7073
break
74+
else
75+
@buttonCallback?(off, touch, event)
7176
false
7277

7378
###

‎bkcore.coffee/TouchController.js

+13-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎bkcore/Timer.js

-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ bkcore.Timer.prototype.update = function()
7171

7272
/*!
7373
* Returns a formatted version of the current elapsed time using msToTime().
74-
*
75-
*
7674
*/
7775
bkcore.Timer.prototype.getElapsedTime = function()
7876
{

‎bkcore/hexgl/Gameplay.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bkcore.hexgl.Gameplay = function(opts)
5757
{
5858
self.raceData.tick(this.timer.time.elapsed);
5959

60-
self.hud.updateTime(self.timer.getElapsedTime());
60+
self.hud != null && self.hud.updateTime(self.timer.getElapsedTime());
6161
var cp = self.checkPoint();
6262

6363
if(cp == self.track.checkpoints.start && self.previousCheckPoint == self.track.checkpoints.last)
@@ -74,10 +74,10 @@ bkcore.hexgl.Gameplay = function(opts)
7474
else
7575
{
7676
self.lap++;
77-
self.hud.updateLap(self.lap, self.maxLaps);
77+
self.hud != null && self.hud.updateLap(self.lap, self.maxLaps);
7878

7979
if(self.lap == self.maxLaps)
80-
self.hud.display("Final lap", 0.5);
80+
self.hud != null && self.hud.display("Final lap", 0.5);
8181
}
8282
}
8383
else if(cp != -1 && cp != self.previousCheckPoint)
@@ -107,7 +107,7 @@ bkcore.hexgl.Gameplay.prototype.simu = function()
107107
{
108108
this.lapTimes = [92300, 91250, 90365];
109109
this.finishTime = this.lapTimes[0]+this.lapTimes[1]+this.lapTimes[2];
110-
this.hud.display("Finish");
110+
this.hud != null && this.hud.display("Finish");
111111
this.step = 100;
112112
this.result = this.results.FINISH;
113113
this.shipControls.active = false;
@@ -128,7 +128,7 @@ bkcore.hexgl.Gameplay.prototype.start = function(opts)
128128
if(this.mode == 'replay')
129129
{
130130
this.cameraControls.mode = this.cameraControls.modes.ORBIT;
131-
this.hud.messageOnly = true;
131+
this.hud != null && this.hud.messageOnly = true;
132132

133133
try {
134134
var d = localStorage['race-'+this.track.name+'-replay'];
@@ -147,9 +147,12 @@ bkcore.hexgl.Gameplay.prototype.start = function(opts)
147147
this.active = true;
148148
this.step = 0;
149149
this.timer.start();
150-
this.hud.resetTime();
151-
this.hud.display("Get ready", 1);
152-
this.hud.updateLap(this.lap, this.maxLaps);
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+
}
153156
}
154157

155158
bkcore.hexgl.Gameplay.prototype.end = function(result)
@@ -163,12 +166,12 @@ bkcore.hexgl.Gameplay.prototype.end = function(result)
163166

164167
if(result == this.results.FINISH)
165168
{
166-
this.hud.display("Finish");
169+
this.hud != null && this.hud.display("Finish");
167170
this.step = 100;
168171
}
169172
else if(result == this.results.DESTROYED)
170173
{
171-
this.hud.display("Destroyed");
174+
this.hud != null && this.hud.display("Destroyed");
172175
this.step = 100;
173176
}
174177
}
@@ -181,22 +184,22 @@ bkcore.hexgl.Gameplay.prototype.update = function()
181184

182185
if(this.step == 0 && this.timer.time.elapsed >= this.countDownDelay+this.startDelay)
183186
{
184-
this.hud.display("3");
187+
this.hud != null && this.hud.display("3");
185188
this.step = 1;
186189
}
187190
else if(this.step == 1 && this.timer.time.elapsed >= 2*this.countDownDelay+this.startDelay)
188191
{
189-
this.hud.display("2");
192+
this.hud != null && this.hud.display("2");
190193
this.step = 2;
191194
}
192195
else if(this.step == 2 && this.timer.time.elapsed >= 3*this.countDownDelay+this.startDelay)
193196
{
194-
this.hud.display("1");
197+
this.hud != null && this.hud.display("1");
195198
this.step = 3;
196199
}
197200
else if(this.step == 3 && this.timer.time.elapsed >= 4*this.countDownDelay+this.startDelay)
198201
{
199-
this.hud.display("Go", 0.5);
202+
this.hud != null && this.hud.display("Go", 0.5);
200203
this.step = 4;
201204
this.timer.start();
202205

‎bkcore/hexgl/HexGL.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bkcore.hexgl.HexGL = function(opts)
2020
this.a = window.location.href;
2121

2222
this.active = true;
23-
23+
this.displayHUD = opts.hud == undefined ? true : opts.hud;
2424
this.width = opts.width == undefined ? window.innerWidth : opts.width;
2525
this.height = opts.height == undefined ? window.innerHeight : opts.height;
2626

@@ -265,12 +265,14 @@ bkcore.hexgl.HexGL.prototype.initRenderer = function()
265265
renderer.domElement.style.position = "relative";
266266

267267
this.containers.main.appendChild( renderer.domElement );
268+
this.canvas = renderer.domElement;
268269
this.renderer = renderer;
269270
this.manager = new bkcore.threejs.RenderManager(renderer);
270271
}
271272

272273
bkcore.hexgl.HexGL.prototype.initHUD = function()
273274
{
275+
if(!this.displayHUD) return;
274276
this.hud = new bkcore.hexgl.HUD({
275277
width: this.width,
276278
height: this.height,

‎bkcore/hexgl/ShipControls.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
var bkcore = bkcore || {};
99
bkcore.hexgl = bkcore.hexgl || {};
1010

11-
bkcore.hexgl.ShipControls = function(domElement)
11+
bkcore.hexgl.ShipControls = function(ctx)
1212
{
1313
var self = this;
14+
var domElement = ctx.document;
1415

1516
this.active = true;
1617
this.destroyed = false;
@@ -114,6 +115,17 @@ bkcore.hexgl.ShipControls = function(domElement)
114115
right: false
115116
};
116117

118+
this.touchController = bkcore.TouchController.isTouchable() ? new bkcore.TouchController(
119+
domElement, ctx.width/2,
120+
function(state, touch, event){
121+
if(event.touches.length <= 1)
122+
self.key.forward = false;
123+
else
124+
self.key.forward = true;
125+
console.log(event.touches.length);
126+
console.log(self.key.forward);
127+
}) : null;
128+
117129
function onKeyDown(event)
118130
{
119131
switch(event.keyCode)
@@ -136,7 +148,7 @@ bkcore.hexgl.ShipControls = function(domElement)
136148

137149
function onKeyUp(event)
138150
{
139-
switch(event.keyCode)
151+
switch(event.keyCode)
140152
{
141153
case 38: /*up*/ self.key.forward = false; break;
142154

@@ -212,6 +224,12 @@ bkcore.hexgl.ShipControls.prototype.update = function(dt)
212224
var rollAmount = 0.0;
213225
var angularAmount = 0.0;
214226

227+
if(this.touchController != null)
228+
{
229+
angularAmount -= this.touchController.stickVector.x/100 * this.angularSpeed * dt;
230+
rollAmount += this.touchController.stickVector.x/100 * this.rollAngle;
231+
}
232+
215233
if(this.key.forward)
216234
this.speed += this.thrust * dt;
217235
else

‎bkcore/hexgl/tracks/Cityscape.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ bkcore.hexgl.tracks.Cityscape = {
350350
ship.add(boosterLight);
351351

352352
// SHIP CONTROLS
353-
var shipControls = new bkcore.hexgl.ShipControls(ctx.document);
353+
var shipControls = new bkcore.hexgl.ShipControls(ctx);
354354
shipControls.collisionMap = this.lib.get("analysers", "track.cityscape.collision");
355355
shipControls.collisionPixelRatio = 2048.0 / 6000.0;
356356
shipControls.collisionDetection = true;
@@ -423,7 +423,7 @@ bkcore.hexgl.tracks.Cityscape = {
423423
this.objects.components.cameraChase.cameraCube.rotation.copy(c.rotation);*/
424424

425425
this.objects.composers.game.render(dt);
426-
this.objects.hud.update(
426+
if(this.objects.hud) this.objects.hud.update(
427427
this.objects.components.shipControls.getRealSpeed(100),
428428
this.objects.components.shipControls.getRealSpeedRatio(),
429429
this.objects.components.shipControls.getShield(100),

0 commit comments

Comments
 (0)
Please sign in to comment.