Skip to content

Commit

Permalink
Refactor server game objects
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhou842 committed May 2, 2019
1 parent c12ca7f commit ab9c86f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 16 deletions.
13 changes: 2 additions & 11 deletions src/server/bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@ const Constants = require('../shared/constants');

class Bullet extends ObjectClass {
constructor(parentID, x, y, dir) {
super(x, y, dir, Constants.BULLET_SPEED);
this.id = shortid();
super(shortid(), x, y, dir, Constants.BULLET_SPEED);
this.parentID = parentID;
}

// Returns true if the bullet should be destroyed
update(dt) {
super.update(dt);
return (this.x < 0 || this.x > Constants.MAP_SIZE || this.y < 0 || this.y > Constants.MAP_SIZE);
}

serializeForUpdate() {
return {
id: this.id,
x: this.x,
y: this.y,
};
return this.x < 0 || this.x > Constants.MAP_SIZE || this.y < 0 || this.y > Constants.MAP_SIZE;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/server/object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Object {
constructor(x, y, dir, speed) {
constructor(id, x, y, dir, speed) {
this.id = id;
this.x = x;
this.y = y;
this.direction = dir;
Expand All @@ -23,9 +24,9 @@ class Object {

serializeForUpdate() {
return {
id: this.id,
x: this.x,
y: this.y,
direction: this.direction,
};
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/server/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const Constants = require('../shared/constants');

class Player extends ObjectClass {
constructor(id, username, x, y) {
super(x, y, Math.random() * 2 * Math.PI, Constants.PLAYER_SPEED);
this.id = id;
super(id, x, y, Math.random() * 2 * Math.PI, Constants.PLAYER_SPEED);
this.username = username;
this.hp = Constants.PLAYER_MAX_HP;
this.fireCooldown = 0;
Expand Down Expand Up @@ -44,7 +43,7 @@ class Player extends ObjectClass {
serializeForUpdate() {
return {
...(super.serializeForUpdate()),
id: this.id,
direction: this.direction,
hp: this.hp,
};
}
Expand Down

0 comments on commit ab9c86f

Please sign in to comment.