From b4783ab327f290c4e4441a26a2ec2edfdb721175 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 30 Jul 2015 23:19:36 +0300 Subject: Add js-platformer. --- js-platformer/src/engine.js | 26 ++++++++++++++++++++++++++ js-platformer/src/gameobject.js | 28 ++++++++++++++++++++++++++++ js-platformer/src/platformer.js | 9 +++++++++ js-platformer/src/player.js | 18 ++++++++++++++++++ js-platformer/src/settings.js | 8 ++++++++ 5 files changed, 89 insertions(+) create mode 100644 js-platformer/src/engine.js create mode 100644 js-platformer/src/gameobject.js create mode 100644 js-platformer/src/platformer.js create mode 100644 js-platformer/src/player.js create mode 100644 js-platformer/src/settings.js (limited to 'js-platformer/src') diff --git a/js-platformer/src/engine.js b/js-platformer/src/engine.js new file mode 100644 index 0000000..7dc9ddc --- /dev/null +++ b/js-platformer/src/engine.js @@ -0,0 +1,26 @@ +var Engine = function() { + + this.stage = new PIXI.Stage(settings.STAGE_BGCOLOR); + this.renderer = PIXI.autoDetectRenderer(settings.STAGE_WIDTH, settings.STAGE_HEIGHT); + + this.gameObjectList = []; + + this.update = function() { + for (var i = 0; i < this.gameObjectList.length; i++) { + this.gameObjectList[i].update(); + } + + + this.renderer.render(this.stage); + } + + this.addGameObject = function(gameObject) { + this.gameObjectList.push(gameObject); + this.stage.addChild(gameObject.sprite); + } + + return this; +} + +var engine = new Engine(); +Engine.instance = engine; \ No newline at end of file diff --git a/js-platformer/src/gameobject.js b/js-platformer/src/gameobject.js new file mode 100644 index 0000000..c7e47b4 --- /dev/null +++ b/js-platformer/src/gameobject.js @@ -0,0 +1,28 @@ +var GameObjectPrototype = function() { + this.texture = PIXI.Texture.fromImage("res/red.png"); + this.sprite = new PIXI.Sprite(this.texture); + + this.initPosition = function() { + this.sprite.anchor.x = 0.5; + this.sprite.anchor.y = 0.5; + + this.sprite.position.x = settings.STAGE_WIDTH * 0.5; + this.sprite.position.y = settings.STAGE_HEIGHT * 0.5; + } + + return this; +} + +var GameObject = function() { + + this.initPosition(); + Engine.instance.addGameObject(this); + + this.update = function() { + + } + + return this; +} + +GameObject.prototype = new GameObjectPrototype(); \ No newline at end of file diff --git a/js-platformer/src/platformer.js b/js-platformer/src/platformer.js new file mode 100644 index 0000000..45ede21 --- /dev/null +++ b/js-platformer/src/platformer.js @@ -0,0 +1,9 @@ +document.body.appendChild(Engine.instance.renderer.view); + +requestAnimFrame(animate); + +function animate() { + requestAnimFrame(animate); + + Engine.instance.update(); +} \ No newline at end of file diff --git a/js-platformer/src/player.js b/js-platformer/src/player.js new file mode 100644 index 0000000..e3520a5 --- /dev/null +++ b/js-platformer/src/player.js @@ -0,0 +1,18 @@ +var Player = function() { + this.texture = PIXI.Texture.fromImage("res/player.png"); + this.sprite = new PIXI.Sprite(this.texture); + + Engine.instance.addGameObject(this); + this.initPosition(); + + this.update = function() { + //this.prototype.update.call(this); + } + + return this; +} + +Player.prototype = new GameObjectPrototype(); + +//create a player instance +Engine.player = new Player(); \ No newline at end of file diff --git a/js-platformer/src/settings.js b/js-platformer/src/settings.js new file mode 100644 index 0000000..230003c --- /dev/null +++ b/js-platformer/src/settings.js @@ -0,0 +1,8 @@ +var Settings = function() { + // stage constants + this.STAGE_WIDTH = 800; + this.STAGE_HEIGHT = 600; + this.STAGE_BGCOLOR = 0x111111; +} + +var settings = new Settings(); \ No newline at end of file -- cgit v1.3