summaryrefslogtreecommitdiff
path: root/src/entities
diff options
context:
space:
mode:
Diffstat (limited to 'src/entities')
-rw-r--r--src/entities/boshy.js65
-rw-r--r--src/entities/boshybullet.js30
-rw-r--r--src/entities/entity.js23
3 files changed, 118 insertions, 0 deletions
diff --git a/src/entities/boshy.js b/src/entities/boshy.js
new file mode 100644
index 0000000..ea6068d
--- /dev/null
+++ b/src/entities/boshy.js
@@ -0,0 +1,65 @@
+import { BOSHY } from "../assets.js"
+import { Vector } from "../engine/vector.js"
+import { Engine } from "../engine.js"
+import { BoshyBullet } from "./boshybullet.js"
+import { Entity } from "./entity.js"
+
+export class Boshy extends Entity {
+ reloadTime = 6
+ lastShot = 0
+ speed = 5
+
+ constructor() {
+ super({
+ size: new Vector(30, 30),
+ position: new Vector(325, 400),
+ sprite: BOSHY.NORMAL,
+ })
+
+ Engine.playSound(BOSHY.SOUNDS.INTRO)
+ }
+
+ update() {
+ let keys = Engine.keys
+ let viewport = Engine.screen.viewport
+
+ /* get movement direction */
+ let move = new Vector()
+ let speed = this.speed
+ if (keys.right) move.x += 1
+ if (keys.left) move.x -= 1
+ if (keys.up) move.y -= 1
+ if (keys.down) move.y += 1
+ if (keys.shoot) this.shoot()
+
+ /* apply movement */
+ move.normalize()
+ move.multiply(speed)
+ this.position.add(move)
+
+ /* check limits */
+ if (this.position.x < 0) this.position.x = 0
+ if (this.position.y < 0) this.position.y = 0
+ if (this.position.x > viewport.x) this.position.x = viewport.x
+ if (this.position.y > viewport.y) this.position.y = viewport.y
+ }
+
+ shoot() {
+ /* check if it can shoot */
+ if (Engine.frame < this.lastShot + this.reloadTime) {
+ return
+ }
+
+ this.lastShot = Engine.frame /*(reset reload)*/
+
+ let bullet_a_position = this.position.clone()
+ let bullet_b_position = this.position.clone()
+
+ bullet_a_position.x = bullet_a_position.x + 5
+ bullet_b_position.x = bullet_b_position.x - 5
+
+ new BoshyBullet(bullet_a_position)
+ new BoshyBullet(bullet_b_position)
+ Engine.playSound(BOSHY.SOUNDS.SHOOT)
+ }
+}
diff --git a/src/entities/boshybullet.js b/src/entities/boshybullet.js
new file mode 100644
index 0000000..ed91ede
--- /dev/null
+++ b/src/entities/boshybullet.js
@@ -0,0 +1,30 @@
+import { BOSHY } from "../assets.js"
+import { Vector } from "../engine/vector.js"
+import { bullets } from "../main.js"
+import { Entity } from "./entity.js"
+
+export class BoshyBullet extends Entity {
+ speed = 20
+
+ constructor(position = new Vector(0,0)) {
+ super({
+ size: new Vector(5, 5),
+ position: position.floor(),
+ sprite: BOSHY.BULLET,
+ })
+
+ bullets.list.push(this)
+ }
+
+ update() {
+ this.position.y -= this.speed
+ if (this.position.y < 0) {
+ this.span.remove()
+ bullets.remove(this)
+ }
+ }
+
+ remove() {
+ this.span.remove()
+ }
+}
diff --git a/src/entities/entity.js b/src/entities/entity.js
new file mode 100644
index 0000000..ac14d4b
--- /dev/null
+++ b/src/entities/entity.js
@@ -0,0 +1,23 @@
+import { Vector } from "../engine/vector.js"
+import { Engine } from "../engine.js"
+
+export class Entity {
+ constructor({
+ size,
+ hitbox = size,
+ sprite,
+ position = new Vector(0,0),
+ }) {
+ this.size = size
+ this.hitbox = hitbox
+ this.position = position
+
+ this.span = document.createElement("img")
+ this.span.src = sprite
+ Engine.screen.append(this.span)
+ }
+
+ draw() {
+ Engine.screen.draw(this.span, this.position, this.size)
+ }
+}