« get me outta code hell

Slide camera when player character changes - 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-12 18:32:40 -0300
committerFlorrie <towerofnix@gmail.com>2018-08-12 18:32:40 -0300
commit8c7c73ca88b500b700462346527878c80eb72531 (patch)
treef888ba57f4cdf54446b5be1a39dc22928a27763d
parentaa0905c2c1358bc5ee218f9b5a6443c9c8c4501d (diff)
Slide camera when player character changes
-rw-r--r--index.js21
1 files changed, 18 insertions, 3 deletions
diff --git a/index.js b/index.js
index 384f314..a27872e 100644
--- a/index.js
+++ b/index.js
@@ -627,7 +627,7 @@ class Battle {
       this.playerCharacter.targetCharacter)
 
     ctx.save()
-    ctx.translate(-camera.x, -camera.y)
+    ctx.translate(Math.round(-camera.x), Math.round(-camera.y))
     for (const sprite of [this.backdrop, ...this.getAllBattleCharacters(), ...this.animationEntities]) {
       const x = Math.round(sprite.x - sprite.canvas.width / 2)
       const y = Math.round(sprite.y - sprite.canvas.height / 2)
@@ -832,14 +832,28 @@ class Camera extends Sprite {
 
   update(dt) {
     if (this.spriteToFollow) {
-      this.x = this.spriteToFollow.x - this.width / 2
-      this.y = this.spriteToFollow.y - this.height / 2
+      const target = this.getCenteredCoords(this.spriteToFollow)
+      this.x += 5 * dt * (target.x - this.x)
+      this.y += 5 * dt * (target.y - this.y)
     }
   }
 
   follow(sprite) {
     this.spriteToFollow = sprite
   }
+
+  warpTo(sprite) {
+    const target = this.getCenteredCoords(sprite)
+    this.x = target.x
+    this.y = target.y
+  }
+
+  getCenteredCoords(sprite) {
+    return {
+      x: this.spriteToFollow.x - this.width / 2,
+      y: this.spriteToFollow.y - this.height / 2
+    }
+  }
 }
 
 class MagicProjectile {
@@ -905,6 +919,7 @@ for (let ti = 0; ti < battle.teams.length; ti++) {
 const camera = battle.camera
 camera.width = canvas.width
 camera.height = canvas.height
+camera.warpTo(battle.playerCharacter)
 
 battle.canvas.width = canvas.width
 battle.canvas.height = canvas.height