Website Summary Files Commits Branches @commit @diff @stats
path: root / scripts / gameObjects
diff options
context:
space:
mode:
Commit86df07a5daa6aa1545bdf17e4661af0cb80bc48b (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:43:06 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:43:06 +0200
Filesf3bb4a842fc199b48bcdc4c9de6dbd2770d02c55 /scripts/gameObjects
pusg-front-end-86df07a5daa6aa1545bdf17e4661af0cb80bc48b.tar
pusg-front-end-86df07a5daa6aa1545bdf17e4661af0cb80bc48b.tar.gz
pusg-front-end-86df07a5daa6aa1545bdf17e4661af0cb80bc48b.zip
Parentd95945a4c1f567fd479d68c3a6ecff02320f6d5c (diff)
Major Game Mechanics implementation
3 files changed, 86 insertions, 30 deletions
-rw-r--r--scripts/gameObjects/baseGameObject.js29
-rw-r--r--scripts/gameObjects/tank.js55
-rw-r--r--scripts/gameObjects/tankShell.js32
diff --git a/scripts/gameObjects/baseGameObject.js b/scripts/gameObjects/baseGameObject.js
new file mode 100644
index 0000000..91ec11f
--- /dev/null
+++ b/scripts/gameObjects/baseGameObject.js
@@ -0,0 +1,29 @@
+var app = app || {};
+
+class BaseGameObject {
+ constructor(id, sprite, size, position) {
+ this.id = id;
+ this.sprite = sprite;
+ this.container = new PIXI.Container();
+ this.sprite.width = size.width;
+ this.sprite.height = size.height;
+ this.container.width = size.width;
+ this.container.height = size.height;
+ this.container.x = position.x;
+ this.container.y = position.y;
+ this.container.addChild(this.sprite);
+ this.container.pivot.x = this.container.width / 2;
+ this.container.pivot.y = this.container.height / 2;
+ }
+
+ update() {}
+
+ draw() {
+ return this.container;
+ }
+
+ destroy() {
+ this.container.parent.removeChild(this.container);
+ this.container.destroy({children: false, texture: false, baseTexture: false});
+ }
+}
diff --git a/scripts/gameObjects/tank.js b/scripts/gameObjects/tank.js
index 2cd3b1c..7469ba1 100644
--- a/scripts/gameObjects/tank.js
+++ b/scripts/gameObjects/tank.js
@@ -1,18 +1,57 @@
var app = app || {};
-class Tank {
- constructor(color, type, sprite, position, direction) {
+const DIRECTION_STATE = {
+ 'up': {
+ next: 'right',
+ previous: 'left',
+ },
+ 'right': {
+ next: 'down',
+ previous: 'up',
+ },
+ 'down': {
+ next: 'left',
+ previous: 'right',
+ },
+ 'left': {
+ next: 'up',
+ previous: 'down',
+ },
+};
+
+class Tank extends BaseGameObject {
+ constructor(color, type, sprite, size, position, direction) {
+ super(null, sprite, size, position);
this.color = color;
this.type = type;
- this.sprite = sprite;
- this.sprite.x = position.x;
- this.sprite.y = position.y;
this.direction = direction;
}
- update() { }
+ get x (){
+ return this.container.x;
+ }
+
+ get y (){
+ return this.container.y;
+ }
+
+ set x (value) {
+ this.container.x = value;
+ }
+
+ set y (value) {
+ this.container.y = value;
+ }
+
+ rotateLeft() {
+ this.direction = DIRECTION_STATE[this.direction].previous;
+
+ this.container.rotation -= 1.57079633;
+ }
+
+ rotateRight() {
+ this.direction = DIRECTION_STATE[this.direction].next;
- draw() {
- app.pixiApplication.stage.addChild(this.sprite);
+ this.container.rotation += 1.57079633;
}
}
diff --git a/scripts/gameObjects/tankShell.js b/scripts/gameObjects/tankShell.js
index 5f87143..1fffa23 100644
--- a/scripts/gameObjects/tankShell.js
+++ b/scripts/gameObjects/tankShell.js
@@ -1,31 +1,19 @@
var app = app || {};
-class TankShell {
- constructor(sprite, position, direction) {
- this.sprite = sprite;
- this.sprite.x = position.x;
- this.sprite.y = position.y;
+class TankShell extends BaseGameObject {
+ constructor(id, sprite, size, position, direction) {
+ super(id, sprite, size, position);
this.direction = direction;
+ this.isDestroyed = false;
}
- update() {
- switch(this.direction) {
- case "up":
- this.sprite.y--;
- break;
- case "right":
- this.sprite.x++;
- break;
- case "down":
- this.sprite.y++;
- break;
- case "left":
- this.sprite.x--;
- break;
- }
+ set x (value) {
+ this.container.x = value;
}
- draw() {
- app.pixiApplication.stage.addChild(this.sprite);
+ set y (value) {
+ this.container.y = value;
}
+
+ update() {}
}