From 13c5dc317abe0fb4458e703f2ab9397a7c24c859 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Mon, 16 Nov 2015 16:58:18 +0200 Subject: Rengöring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/player.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/player.js (limited to 'src/player.js') diff --git a/src/player.js b/src/player.js new file mode 100644 index 0000000..64f00f8 --- /dev/null +++ b/src/player.js @@ -0,0 +1,67 @@ +function Player(texture) { + GameObject.call(this, texture); + this.active = false; + this.acceleration = 18; + this.boost_velocity = -70; + this.velocity = 0.0; + this.terminal_velocity = 60; + + this.score = 0; + + this.initPosition(); + + // bind space bar to boost up + //k_space.press = this.boost.bind(this); +} + +Player.prototype = Object.create(GameObject.prototype); +Player.prototype.constructor = Player; + +Player.prototype.initPosition = function() { + GameObject.prototype.initPosition.call(this); + + this.sprite.position.x = Settings.STAGE_WIDTH * 0.1; + this.sprite.position.y = Settings.STAGE_HEIGHT * 0.6; + this.sprite.rotation = 0; +} + +Player.prototype.updatePhysics = function() { + // update velocity + this.velocity += 0.01 * this.acceleration * dt; + if (this.velocity >= this.terminal_velocity) { + this.velocity = this.terminal_velocity; + } + + // update position + this.sprite.position.y += 0.01 * this.velocity * dt; + + // limit going upwards + this.sprite.position.y = Math.max(0, this.sprite.position.y); +} + +Player.prototype.updateControls = function() { + // TODO +} + +Player.prototype.boost = function() { + this.velocity = this.boost_velocity; + engine.sound.play("jump"); +} + +Player.prototype.outOfScreen = function() { + return this.sprite.position.y > Settings.STAGE_HEIGHT; +} + +Player.prototype.update = function() { + GameObject.prototype.update.call(this); + + this.sprite.rotation += 0.1; + + if (this.active) { + this.score += 1; + this.updatePhysics(); + this.updateControls(); + } +} + +var player = new Player("player"); \ No newline at end of file -- cgit v1.3