Skip to content

Commit

Permalink
Integrate tic into engine
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Jun 4, 2013
1 parent 8024cd9 commit a363507
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,30 @@ this should be generated by something like this:
return {voxels: voxels, dimensions: [length, width, height], position: start}
```

### Timing
`setTimeout` and `setInterval` work fine when timing things against the computer's clock but what about staying in sync with the game's clock? When the game lags, is paused or hasn't begun you probably don't want your timed operations firing.

`game.setInterval(fn, duration[, args])` and `game.setTimeout(fn, duration[, args])` are available and similar to the built in interval functions but stay in sync with the game's clock.

An example is we want our object to jump every 2 seconds. Normally we'd just do:

```js
setInterval(function() {
jump()
}, 2000)
```

But if the game's frame rate drops or the game hasn't begun the computer will still fire the `jump()` function every 2 seconds. Thus causing the object to fly way up into the air.

```js
var clearInterval = game.setInterval(function() {
jump()
}, 2000)
// later we can stop the interval by calling clearInterval()
```

Will achieve the same thing although now when the game's frame rate drops the `jump()` funciton won't be called repeatedly and the object will jump at the desired frequency.

## style guide

basically https://github.com/felixge/node-style-guide#nodejs-style-guide with a couple of minor changes:
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var regionChange = require('voxel-region-change')
var kb = require('kb-controls')
var physical = require('voxel-physical')
var pin = require('pin-it')
var tic = require('tic')()

module.exports = Game

Expand Down Expand Up @@ -594,6 +595,9 @@ Game.prototype.onFire = function(state) {
this.emit('fire', this.controlling, state)
}

Game.prototype.setInterval = tic.interval.bind(tic)
Game.prototype.setTimeout = tic.timeout.bind(tic)

Game.prototype.tick = function(delta) {
for(var i = 0, len = this.items.length; i < len; ++i) {
this.items[i].tick(delta)
Expand All @@ -604,6 +608,8 @@ Game.prototype.tick = function(delta) {
if (this.pendingChunks.length) this.loadPendingChunks()
if (Object.keys(this.chunksNeedsUpdate).length > 0) this.updateDirtyChunks()

tic.tick(delta)

this.emit('tick', delta)

if (!this.controls) return
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"kb-controls": "0.0.2",
"spatial-events": "0.0.1",
"spatial-trigger": "0.0.0",
"collide-3d-tilemap": "0.0.1"
"collide-3d-tilemap": "0.0.1",
"tic": "0.2.1"
},
"devDependencies": {
"tape": "0.2.2"
Expand Down
10 changes: 9 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,12 @@ test('infinite terrain', function gravity(t) {
t.equal(!!game.voxels.chunks['1|1|3'], true)
game.destroy()
}, 300)
})
})

gameTest(function tic(game, t) {
t.plan(3)
game.setInterval(function() { t.ok(true) }, 500)
game.setTimeout(function() { t.ok(true) }, 500)
var interval = setInterval(function() { game.tick(100) }, 100)
setTimeout(function() { clearInterval(interval) }, 1200)
})

0 comments on commit a363507

Please sign in to comment.