Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
refactor: move showNextThrowable to weaponManager
Browse files Browse the repository at this point in the history
  • Loading branch information
leia-uwu committed Mar 18, 2024
1 parent 5d47d8f commit 3e5313b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
29 changes: 0 additions & 29 deletions server/src/objects/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,6 @@ export class Player extends BaseGameObject {
this.dirty.inventory = true;
}

/**
* switch weapons slot throwable to the next one in the throwables array
* only call this method after the inventory state has been updated accordingly, this function only changes the weaponManager.weapons' state
*/
showNextThrowable(): void {
const throwables = ["frag", "smoke", "strobe", "mirv", "snowball", "potato"];
const startingIndex = throwables.indexOf(this.weapons[3].type) + 1;
for (let i = startingIndex; i < startingIndex + throwables.length; i++) {
const arrayIndex = i % throwables.length;
const type = throwables[arrayIndex];
const amount = this.inventory[type];

if (amount != 0) {
this.weapons[3].type = type;
this.weapons[3].ammo = amount;
this.dirty.weapons = true;
this.setDirty();
return;
}
}

this.weapons[3].type = "";
this.weapons[3].ammo = 0;
this.weapons[3].cooldown = 0;
if (this.curWeapIdx == 3) { // set weapon index to melee if run out of grenades
this.curWeapIdx = 2;
}
}

inventory: Record<string, number> = {};

get curWeapIdx() {
Expand Down
30 changes: 30 additions & 0 deletions server/src/utils/weaponManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,34 @@ export class WeaponManager {
}
}
}

/**
* switch weapons slot throwable to the next one in the throwables array
* only call this method after the inventory state has been updated accordingly, this function only changes the weaponManager.weapons' state
*/
showNextThrowable(): void {
// TODO: use throwable def inventory order
const throwables = ["frag", "smoke", "strobe", "mirv", "snowball", "potato"];
const startingIndex = throwables.indexOf(this.weapons[3].type) + 1;
for (let i = startingIndex; i < startingIndex + throwables.length; i++) {
const arrayIndex = i % throwables.length;
const type = throwables[arrayIndex];
const amount = this.player.inventory[type];

if (amount != 0) {
this.weapons[3].type = type;
this.weapons[3].ammo = amount;
this.player.dirty.weapons = true;
this.player.setDirty();
return;
}
}

this.weapons[3].type = "";
this.weapons[3].ammo = 0;
this.weapons[3].cooldown = 0;
if (this.curWeapIdx == 3) { // set weapon index to melee if run out of grenades
this.curWeapIdx = 2;
}
}
}

0 comments on commit 3e5313b

Please sign in to comment.