Created
November 15, 2016 20:34
-
-
Save timetocode/6b146c49406850b80625d76e60684dc4 to your computer and use it in GitHub Desktop.
Scene management, loading assets, beginning a game loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Client = require('../Client') | |
function GameScene() { | |
this.client = new Client() | |
} | |
GameScene.prototype.enterScene = function() { | |
console.log('GameScene entered') | |
this.client.connect() | |
} | |
GameScene.prototype.update = function(delta, tick, now) { | |
// call the game client's update each tick | |
this.client.update(delta, tick, now) | |
} | |
module.exports = GameScene |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GameScene = require('./GameScene') | |
function LoadingScene() { | |
// TODO load more stuffz? all this game needs so far is a sprite sheet | |
} | |
LoadingScene.prototype.enterScene = function() { | |
/* I would display a loading screen but everything loads so quickly */ | |
PIXI.SCALE_MODES.DEFAULT = PIXI.SCALE_MODES.NEAREST | |
PIXI.loader.add('spritesheet', '/images/spritesheet.json') | |
PIXI.loader.load((loader, resource) => { | |
console.log('spritesheet loaded') | |
// start the game | |
this.sceneManager.gotoScene(new GameScene()) | |
}) | |
} | |
module.exports = LoadingScene |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SceneManager = require('../../game/client/scene/SceneManager') | |
var LoadingScene = require('../../game/client/scene/LoadingScene') | |
var scenes = new SceneManager() | |
window.onload = function() { | |
console.log('window loaded') | |
scenes.gotoScene(new LoadingScene() | |
var loop = function() { | |
scenes.update() | |
window.requestAnimationFrame(loop) | |
} | |
loop() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function SceneManager() { | |
this.currentScene = null | |
} | |
SceneManager.prototype.gotoScene = function(scene) { | |
if (this.currentScene) { | |
if (typeof this.currentScene.exitScene === 'function') { | |
this.currentScene.exitScene() | |
} | |
} | |
this.currentScene = scene | |
scene.sceneManager = this | |
scene.enterScene() | |
} | |
var prev = Date.now() | |
var now = Date.now() | |
var tick = 0 | |
SceneManager.prototype.update = function() { | |
now = Date.now() | |
var deltaTime = (now - prev) / 1000 | |
prev = now | |
if (this.currentScene) { | |
if (typeof this.currentScene.update === 'function') { | |
this.currentScene.update(deltaTime, tick, now) | |
} | |
} | |
tick++ | |
} | |
module.exports = SceneManager |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment