Skip to content

Commit

Permalink
feat: weapon and melee firing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
leia-uwu committed Mar 18, 2024
1 parent fe84f8d commit db3d9c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
14 changes: 6 additions & 8 deletions server/src/objects/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export class Player extends BaseGameObject {
this.inventory["8xscope"] = 1;
this.scope = "8xscope";
this.game.addLoot("katana", this.pos, this.layer, 1);
this.game.addLoot("katana", this.pos, this.layer, 1);
this.game.addLoot("hook", this.pos, this.layer, 1);
this.game.addLoot("outfitPrisoner", this.pos, this.layer, 1);
this.game.addLoot("outfitPrisoner", this.pos, this.layer, 1);
this.game.addLoot("9mm", this.pos, this.layer, 100);
Expand All @@ -381,7 +381,7 @@ export class Player extends BaseGameObject {
this.game.addLoot("frag", this.pos, this.layer, 10);
this.game.addLoot("smoke", this.pos, this.layer, 3);
this.game.addLoot("mirv", this.pos, this.layer, 3);
this.game.addLoot("m870", this.pos, this.layer, 1);
this.game.addLoot("deagle_dual", this.pos, this.layer, 1);
this.game.addLoot("pkp", this.pos, this.layer, 1);
this.game.addLoot("backpack03", this.pos, this.layer, 1);
this.game.addLoot("helmet03", this.pos, this.layer, 1);
Expand Down Expand Up @@ -631,12 +631,6 @@ export class Player extends BaseGameObject {
if (this.shotSlowdownTimer - Date.now() <= 0) {
this.shotSlowdownTimer = -1;
}
if (this.shootStart) {
this.weaponManager.shootStart();
}
if (this.shootHold) {
this.weaponManager.shootHold();
}
}

private _firstUpdate = true;
Expand Down Expand Up @@ -949,6 +943,10 @@ export class Player extends BaseGameObject {
this.shootStart = msg.shootStart;
this.toMouseLen = msg.toMouseLen;

if (this.shootStart) {
this.weaponManager.shootStart();
}

for (const input of msg.inputs) {
switch (input) {
case GameConfig.Input.StowWeapons:
Expand Down
41 changes: 27 additions & 14 deletions server/src/utils/weaponManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ export class WeaponManager {
if (def) {
switch (def.type) {
case "melee": {
if (this.player.game.now > this.meleeCooldown) {
this.meleeAttack();
}
this.meleeAttack();
break;
}
case "gun": {
if (this.weapons[this.curWeapIdx].ammo != 0) {
this.fireWeapon(false, this.activeWeapon);
}
this.fireWeapon();
break;
}
}
Expand Down Expand Up @@ -93,16 +89,25 @@ export class WeaponManager {
this.player.doAction(this.activeWeapon, GameConfig.Action.Reload, duration);
}

// TODO: proper firing delays and stuff
fireDelay = 0;
fireWeapon(offhand: boolean, type: string) {
// if (this.fireDelay > this.player.game.now) return;
offHand = false;

fireWeapon(skipDelayCheck = false) {
if (this.weapons[this.curWeapIdx].cooldown > this.player.game.now) return;
const itemDef = GameObjectDefs[type] as GunDef;
if (this.fireDelay > this.player.game.now && !skipDelayCheck) return;
if (this.weapons[this.curWeapIdx].ammo <= 0) return;

const itemDef = GameObjectDefs[this.activeWeapon] as GunDef;

this.weapons[this.curWeapIdx].cooldown = this.player.game.now + (itemDef.fireDelay * 1000);
// this.fireDelay = this.player.game.now + (itemDef.fireDelay * 1000);

if (this.player.shootHold && itemDef.fireMode === "auto") {
this.timeouts.push(
setTimeout(() => this.fireWeapon(this.player.shootHold), itemDef.fireDelay * 1000)
);
}

// Check firing location
if (itemDef.outsideOnly && this.player.indoors) {
const msg = new net.PickupMsg();
Expand All @@ -124,7 +129,7 @@ export class WeaponManager {
const collisionLayer = util.toGroundLayer(this.player.layer);
const bulletLayer = this.player.aimLayer;

const gunOff = itemDef.isDual ? itemDef.dualOffset! * (offhand ? 1.0 : -1.0) : itemDef.barrelOffset;
const gunOff = itemDef.isDual ? itemDef.dualOffset! * (this.offHand ? 1.0 : -1.0) : itemDef.barrelOffset;
const gunPos = v2.add(this.player.pos, v2.mul(v2.perp(direction), gunOff));
const gunLen = itemDef.barrelLength;

Expand Down Expand Up @@ -222,7 +227,7 @@ export class WeaponManager {
const params: BulletParams = {
playerId: this.player.id,
bulletType: itemDef.bulletType,
sourceType: type,
sourceType: this.activeWeapon,
damageType: GameConfig.DamageType.Player,
pos: shotPos,
dir: shotDir,
Expand All @@ -231,7 +236,7 @@ export class WeaponManager {
variance: 1,
damageMult,
shotFx: i === 0,
shotOffhand: offhand,
shotOffhand: this.offHand,
trailSmall: false,
reflectCount: 0,
splinter: hasSplinter,
Expand Down Expand Up @@ -269,6 +274,7 @@ export class WeaponManager {
if (this.weapons[this.curWeapIdx].ammo == 0) {
this.player.scheduleAction(this.activeWeapon, GameConfig.Action.Reload);
}
this.offHand = !this.offHand;
}

getMeleeCollider() {
Expand All @@ -284,7 +290,9 @@ export class WeaponManager {
return collider.createCircle(rotated, rad, 0);
}

meleeAttack(): void {
meleeAttack(skipCooldownCheck = false): void {
if (this.player.game.now < this.meleeCooldown && !skipCooldownCheck) return;

const meleeDef = GameObjectDefs[this.player.activeWeapon] as MeleeDef;

this.player.animType = GameConfig.Anim.Melee;
Expand All @@ -301,6 +309,11 @@ export class WeaponManager {
this.meleeDamage();
}, damageTime * 1000));
}
if (meleeDef.autoAttack && this.player.shootHold) {
this.timeouts.push(setTimeout(() => {
if (this.player.shootHold) { this.meleeAttack(true); }
}, meleeDef.attack.cooldownTime * 1000));
}
}

meleeDamage(): void {
Expand Down

0 comments on commit db3d9c4

Please sign in to comment.