« get me outta code hell

Sprite dummy test - csb-game - Pixelly spin-off of the Command Synergy Battle system used in Final Fantasy XIII
summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-08-09 21:06:30 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-09 21:06:30 -0300
commitec47074f8cc2503de3eada5247e483d0d02c561e (patch)
tree2f5307eeb4e3fcbee8d66cf6d436dfc15b71da26
parent53744b647a6f1b889f63b6feb31e325236ba1348 (diff)
Sprite dummy test
-rw-r--r--img/cat1.pngbin0 -> 867 bytes
-rw-r--r--img/cat2.pngbin0 -> 793 bytes
-rw-r--r--img/weird-woods.pngbin0 -> 140931 bytes
-rw-r--r--index.js71
4 files changed, 69 insertions, 2 deletions
diff --git a/img/cat1.png b/img/cat1.png
new file mode 100644
index 0000000..226f272
--- /dev/null
+++ b/img/cat1.png
Binary files differdiff --git a/img/cat2.png b/img/cat2.png
new file mode 100644
index 0000000..8695825
--- /dev/null
+++ b/img/cat2.png
Binary files differdiff --git a/img/weird-woods.png b/img/weird-woods.png
new file mode 100644
index 0000000..528acf0
--- /dev/null
+++ b/img/weird-woods.png
Binary files differdiff --git a/index.js b/index.js
index 137fd44..e9cbfc7 100644
--- a/index.js
+++ b/index.js
@@ -2,6 +2,31 @@
 
 const canvas = document.getElementById('canvas')
 
+class Sprite {
+  constructor() {
+    this.x = 0
+    this.y = 0
+    this.canvas = document.createElement('canvas')
+    this.image = document.createElement('img')
+  }
+
+  update(dt) {}
+
+  draw() {
+    this.canvas.width = this.image.width
+    this.canvas.height = this.image.height
+    const ctx = this.canvas.getContext('2d')
+    ctx.drawImage(this.image, 0, 0)
+  }
+}
+
+class Backdrop extends Sprite {
+  constructor() {
+    super()
+    this.image.src = 'img/weird-woods.png'
+  }
+}
+
 class ATBBar {
   constructor(battleCharacter) {
     // Parts
@@ -140,7 +165,7 @@ class ActionMenu {
       ['Stun', 'Stunra', 'Stunga']
     ]
 
-    this.currentOptionIndex = 3
+    this.currentOptionIndex = 0
     this.uiLevel = 1 // 1-3 -- which of "fire", "fira", "firaga" is selected.
 
     // Drawing
@@ -245,11 +270,14 @@ class ActionMenu {
   }
 }
 
-class BattleCharacter {
+class BattleCharacter extends Sprite {
   constructor() {
+    super()
+
     // Parts
 
     this.atbBar = new ATBBar(this)
+    this.image.src = 'img/cat1.png'
 
     // State
 
@@ -286,13 +314,43 @@ class BattleCharacter {
   }
 }
 
+class Camera extends Sprite {
+  constructor() {
+    super()
+
+    this.spriteToFollow = null
+    this.width = 200
+    this.height = 200
+  }
+
+  update(dt) {
+    if (this.spriteToFollow) {
+      this.x = this.spriteToFollow.x - this.width / 2
+      this.y = this.spriteToFollow.y - this.height / 2
+    }
+  }
+
+  follow(sprite) {
+    this.spriteToFollow = sprite
+  }
+}
+
 const battleCharacter = new BattleCharacter()
+battleCharacter.x = 80
+battleCharacter.y = 80
 
 const atbBar = battleCharacter.atbBar
 //atbBar.queuedActions = [{label: 'Fire', size: 1}, {label: 'Blizz', size: 1}, {label: 'Zap', size: 1}, {label: 'Firaga', size: 3}]
 
 const actionMenu = new ActionMenu()
 
+const camera = new Camera()
+camera.width = canvas.width
+camera.height = canvas.height
+camera.follow(battleCharacter)
+
+const backdrop = new Backdrop()
+
 let lastTime = Date.now()
 
 function drawLoop() {
@@ -300,12 +358,21 @@ function drawLoop() {
   lastTime = Date.now()
 
   battleCharacter.update(dt)
+  camera.update(dt)
 
   const ctx = canvas.getContext('2d')
   ctx.clearRect(0, 0, canvas.width, canvas.height)
   ctx.fillStyle = '#EEE'
   ctx.fillRect(0, 0, canvas.width, canvas.height)
 
+  ctx.save()
+  ctx.translate(-camera.x, -camera.y)
+  for (const sprite of [backdrop, battleCharacter]) {
+    sprite.draw()
+    ctx.drawImage(sprite.canvas, sprite.x - sprite.canvas.width / 2, sprite.y - sprite.canvas.height / 2)
+  }
+  ctx.restore()
+
   let y = canvas.height - 20
   if (!battleCharacter.isExecutingChain) {
     y -= actionMenu.canvas.height