Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

simpel spaceship movement.. next step 2.5d

  • Loading branch information...
commit d247f5906ed2b5a86a497dd4de837d5d2a60eb83 1 parent 2ebcea2
@subrise authored
View
101 tron/custom.js 100644 → 100755
@@ -11,19 +11,116 @@
var game = new SR.Game(),
// Defining classes
Ship = function () {
+ var texture = new Image(),
+ imagesLoaded = false,
+ position = SR.Point(),
+ velocity = SR.Point(),
+ isDescending = false,
+ isAscending = false,
+ isRollingLeft = false,
+ isRollingRight = false,
+ width = 246,
+ height = 70,
+ halfWidth = width / 2,
+ halfHeight = height / 2,
+ speed = 1,
+ maxSpeed = 25,
+ drag = 0.9;
+
+ texture.src = './img/spaceship_top_view.png';
+ texture.addEventListener('load', function () {
+ imagesLoaded = true;
+ }, false);
+
this.gameComponent = new SR.GameComponent();
+ this.onKeyDown = function (e) {
+ if (e.keyCode === 40) {
+ isDescending = true;
+ }
+ if (e.keyCode === 38) {
+ isAscending = true;
+ }
+ if (e.keyCode === 37) {
+ isRollingLeft = true;
+ }
+ if (e.keyCode === 39) {
+ isRollingRight = true;
+ }
+ };
+
+ this.onKeyUp = function (e) {
+ if (e.keyCode === 40) {
+ isDescending = false;
+ }
+ if (e.keyCode === 38) {
+ isAscending = false;
+ }
+ if (e.keyCode === 37) {
+ isRollingLeft = false;
+ }
+ if (e.keyCode === 39) {
+ isRollingRight = false;
+ }
+ };
+
this.gameComponent.update = function (e) {
+ if (isAscending && ! isDescending) {
+ if (velocity.y > maxSpeed * -1) {
+ velocity.y -= speed;
+ }
+ } else if (isDescending && ! isAscending) {
+ if (velocity.y < maxSpeed) {
+ velocity.y += speed;
+ }
+ } else {
+ velocity.y *= drag;
+ }
+ if (isRollingLeft && ! isRollingRight) {
+ if (velocity.x > maxSpeed * -1) {
+ velocity.x -= speed;
+ }
+ } else if (isRollingRight && ! isRollingLeft) {
+ if (velocity.x < maxSpeed) {
+ velocity.x += speed;
+ }
+ } else {
+ velocity.x *= drag;
+ }
+
+ position.x += velocity.x;
+ position.y += velocity.y;
+
+ // Set boundaries, so ship won't fly of screen
+ if (position.x < 0) {
+ position.x = 0;
+ } else if (position.x > 539) {
+ position.x = 539;
+ }
+ if (position.y < 0) {
+ position.y = 0;
+ } else if (position.y > 299) {
+ position.y = 299;
+ }
};
this.gameComponent.draw = function (c) {
+ if (imagesLoaded) {
+ c.drawImage(texture, position.x - halfWidth, position.y - halfHeight);
+ }
};
},
Rock = function () {
this.gameComponent = new SR.GameComponent();
},
// Declaring game variables
- ship;
+ ship,
+ onKeyDown = function (e) {
+ ship.onKeyDown(e);
+ },
+ onKeyUp = function (e) {
+ ship.onKeyUp(e);
+ };
@@ -32,6 +129,8 @@
game.init(539, 299);
ship = new Ship();
game.addGameComponent(ship.gameComponent);
+ document.body.addEventListener('keydown', onKeyDown, false);
+ document.body.addEventListener('keyup', onKeyUp, false);
}, false);
}(this, this.document, this.SUBRISE));
View
BIN  tron/img/spaceship_top_view.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
0  tron/index.html 100644 → 100755
File mode changed
View
2  tron/style.css 100644 → 100755
@@ -14,6 +14,6 @@ body {
#gameCanvas {
display:block;
- margin:100px auto;
+ margin:0 auto;
border:1px solid rgba(32, 32, 32, 0.5);
}
Please sign in to comment.
Something went wrong with that request. Please try again.