From 7f0852bfbe4a5a31a1cda6779f9f8a713cdd2f45 Mon Sep 17 00:00:00 2001 From: Starphee <33707294+Starphee@users.noreply.github.com> Date: Sat, 4 Jan 2025 21:45:54 -0800 Subject: [PATCH] Add basic ship recoil - Implemented rudimentary and simple recoil based on knockback and speed. --- src/game/classes/Ship.mjs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/game/classes/Ship.mjs b/src/game/classes/Ship.mjs index 37476c5..386a87e 100644 --- a/src/game/classes/Ship.mjs +++ b/src/game/classes/Ship.mjs @@ -154,15 +154,30 @@ export class Ship extends DynamicObject { switch (fireCount) { case 1: this.addProjectile(this.pos.d, weaponId, callbacks); + this.applyRecoil(weaponId); // NEW! Apply recoil on fire break; case 3: this.addProjectile(this.pos.d - 10, weaponId, callbacks); this.addProjectile(this.pos.d, weaponId, callbacks); this.addProjectile(this.pos.d + 10, weaponId, callbacks); + this.applyRecoil(weaponId); // NEW! Apply recoil on fire break; } } + applyRecoil(weaponId) { + if (this.exploding) return; + + const { knockBackForce, speed } = this.config.weapons[weaponId].data; + const recoilForce = (knockBackForce * speed) / (1000 * 1000); + const recoilDirection = this.pos.d + 180; // Opposite to firing direction + + this.knockBack = { + angle: recoilDirection, + amount: recoilForce * 0.5, // 0.5 dampening factor + }; + } + // FUNCTION projectile hit/collision callback hit({ type, weapon, source }) { const target = this;