Skip to content
Browse files

Good AWSD movement of ship

  • Loading branch information...
1 parent 91bb162 commit 9f5d17550b01e8e45ad662a08275cf103a3cc4c3 @huljas committed Sep 21, 2011
Showing with 67 additions and 64 deletions.
  1. +67 −64 gamejs/javascript/main.js
View
131 gamejs/javascript/main.js
@@ -14,83 +14,86 @@ var gamejs = require('gamejs');
var Ship = function(rect) {
// call superconstructor
Ship.superConstructor.apply(this, arguments);
- this.speed = 20 + (40 * Math.random());
- // ever ship has its own scale
+ this.speed = 100;
+ this.rotationSpeed = 360;
this.originalImage = gamejs.image.load("images/ship.png");
- var dims = this.originalImage.getSize();
- this.originalImage = gamejs.transform.scale(
- this.originalImage,
- [dims[0] * (0.5 + Math.random()), dims[1] * (0.5 + Math.random())]
- );
- this.rotation = 50 + parseInt(120*Math.random());
+ this.rotation = parseInt(360*Math.random());
this.image = gamejs.transform.rotate(this.originalImage, this.rotation);
this.rect = new gamejs.Rect(rect);
+ this.acceleration = 0;
+ this.movementDirection = 0;
+ this.rotationDirection = 0;
+ gamejs.log("New ship ", this.rotation, this.speed);
return this;
};
// inherit (actually: set prototype)
gamejs.utils.objects.extend(Ship, gamejs.sprite.Sprite);
Ship.prototype.update = function(msDuration) {
- // moveIp = move in place
- var directionV = gamejs.utils.vectors.rotate([0,1], gamejs.utils.math.radians(this.rotation));
- var speedV = gamejs.utils.vectors.multiply(directionV, this.speed);
- this.rect.moveIp(speedV[0] * (msDuration/1000), speedV[1] * (msDuration/1000));
- if (this.rect.top > 600) {
- this.speed *= -1;
- this.image = gamejs.transform.rotate(this.originalImage, this.rotation + 180);
- } else if (this.rect.top < 0 ) {
- this.speed *= -1;
- this.image = gamejs.transform.rotate(this.originalImage, this.rotation);
- } else if (this.rect.left > 800) {
- this.speed *= -1;
- } else if (this.rect.left < 0) {
- this.speed *= -1;
- }
+ var timePassedSeconds = msDuration / 1000;
+ this.rotation += this.rotationDirection * this.rotationSpeed * timePassedSeconds;
+ var heading = gamejs.utils.vectors.rotate([1,0], gamejs.utils.math.radians(this.rotation));
+ heading = gamejs.utils.vectors.multiply(heading, this.movementDirection);
+ var moveV = gamejs.utils.vectors.multiply(heading, this.speed * timePassedSeconds);
+ var oldSize = this.image.getSize();
+ this.image = gamejs.transform.rotate(this.originalImage, this.rotation);
+ var newSize = this.image.getSize();
+ this.rect.moveIp([moveV[0] + (oldSize[0] - newSize[0])/2, moveV[1] + (oldSize[1] - newSize[1])/2]);
+ this.movementDirection = 0;
+ this.rotationDirection = 0;
};
function main() {
- // screen setup
- gamejs.display.setMode([800, 600]);
- gamejs.display.setCaption("Example Sprites");
- // create some ship sprites and put them in a group
- var ship = new Ship([100, 100]);
- var gShips = new gamejs.sprite.Group();
- for (var j=0;j<4;j++) {
- for (var i=0; i<3; i++) {
- gShips.add(new Ship([10 + i*20, j * 20]));
- }
- }
- // game loop
- var mainSurface = gamejs.display.getSurface();
- // msDuration = time since last tick() call
- var tick = function(msDuration) {
- mainSurface.fill("#FFFFFF");
- // update and draw the ships
- gShips.update(msDuration);
- gShips.draw(mainSurface);
- ship.update(msDuration);
- ship.draw(mainSurface);
-
- var events = gamejs.event.get()
- events.forEach(function(event) {
- if (event.type === gamejs.event.MOUSE_DOWN) {
- var pos = event.pos;
- var directionV = gamejs.utils.vectors.subtract(event.pos, ship.rect.center);
- var angle = Math.atan(directionV[0] / directionV[1]);
- var angleD = gamejs.utils.math.normaliseDegrees(gamejs.utils.math.degrees(angle));
- ship.rotation = angleD;
- gamejs.log("angle", angle, angleD);
- gamejs.log("new direction", directionV);
- gamejs.draw.line(mainSurface, "#ff0000", ship.rect.center, event.pos, 1);
- }
- if (event.type === gamejs.event.MOUSE_UP) {
- } else if (event.type === gamejs.event.KEY_UP) {
- }
- });
-
- };
- gamejs.time.fpsCallback(tick, this, 20);
+ // screen setup
+ gamejs.display.setMode([800, 600]);
+ gamejs.display.setCaption("Example Sprites");
+ // create some ship sprites and put them in a group
+ var ship = new Ship([100, 100]);
+ var gShips = new gamejs.sprite.Group();
+ for (var j=0;j<4;j++) {
+ for (var i=0; i<3; i++) {
+ //gShips.add(new Ship([10 + i*20, j * 20]));
+ }
+ }
+ var keysDown = {};
+ // game loop
+ var mainSurface = gamejs.display.getSurface();
+ // msDuration = time since last tick() call
+ var tick = function(msDuration) {
+ mainSurface.fill("#000000");
+ // update and draw the ships
+ gShips.update(msDuration);
+ gShips.draw(mainSurface);
+ ship.update(msDuration);
+ ship.draw(mainSurface);
+
+ var events = gamejs.event.get();
+ events.forEach(function(event) {
+ if (event.type === gamejs.event.KEY_DOWN) {
+ gamejs.log("key down", event.key);
+ keysDown[event.key] = true;
+ }
+ if (event.type === gamejs.event.KEY_UP) {
+ gamejs.log("key up", event.key);
+ keysDown[event.key] = false;
+ }
+ });
+ if (keysDown[gamejs.event.K_a]) {
+ ship.rotationDirection = -1;
+ }
+ if (keysDown[gamejs.event.K_d]) {
+ ship.rotationDirection = 1;
+ }
+ if (keysDown[gamejs.event.K_w]) {
+ ship.movementDirection = 1;
+ }
+ if (keysDown[gamejs.event.K_s]) {
+ ship.movementDirection = -1;
+ }
+ };
+ gamejs.time.fpsCallback(tick, this, 60);
}
+
/**
* M A I N
*/

0 comments on commit 9f5d175

Please sign in to comment.
Something went wrong with that request. Please try again.