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

Commit

Permalink
Merge pull request #14 from MamounKolovos/master
Browse files Browse the repository at this point in the history
added gun and throwable dropping, added scope selection, refactored p…
  • Loading branch information
leia-uwu authored Mar 17, 2024
2 parents e0171dc + 57a37dd commit 2112dd7
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 153 deletions.
2 changes: 1 addition & 1 deletion client/src/objects/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class Player {
this.netData.oe = v2.copy(e.dir);
if (t) {
this.netData.se = e.outfit;
this.netData.ne = e.pack;
this.netData.ne = e.backpack;
this.netData.le = e.helmet;
this.netData.ce = e.chest;
this.netData.me = e.activeWeapon;
Expand Down
150 changes: 102 additions & 48 deletions server/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import net from "../../shared/net";
import { type Explosion } from "./objects/explosion";
import { type Msg } from "../../shared/netTypings";
import { EmotesDefs } from "../../shared/defs/gameObjects/emoteDefs";
import { type AmmoDef, type BoostDef, type ChestDef, type HealDef, type HelmetDef, type ScopeDef } from "../../shared/defs/objectsTypings";
import { type GunDef, type AmmoDef, type BoostDef, type ChestDef, type HealDef, type HelmetDef, type ScopeDef, type ThrowableDef, type MeleeDef } from "../../shared/defs/objectsTypings";

export class Emote {
playerId: number;
Expand Down Expand Up @@ -294,69 +294,123 @@ export class Game {
dropMsg.deserialize(stream);

// @TODO: move me somewhere
if (player.weapons.some(weapon => weapon.type === dropMsg.item)) {
this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player.setDirty();
} else {
const item = GameObjectDefs[dropMsg.item] as ScopeDef | HelmetDef | ChestDef | HealDef | BoostDef | AmmoDef;
switch (item.type) {
case "ammo": {
const inventoryCount = player.inventory[dropMsg.item];
const item = GameObjectDefs[dropMsg.item] as ScopeDef | HelmetDef | ChestDef | HealDef | BoostDef | AmmoDef | GunDef | ThrowableDef | MeleeDef;
switch (item.type) {
case "ammo": {
const inventoryCount = player.inventory[dropMsg.item];

if (inventoryCount === 0) return;

if (inventoryCount === 0) return;
let amountToDrop = Math.max(1, Math.floor(inventoryCount / 2));

if (item.minStackSize && inventoryCount <= item.minStackSize) {
amountToDrop = Math.min(item.minStackSize, inventoryCount);
} else if (inventoryCount <= 5) {
amountToDrop = Math.min(5, inventoryCount);
}

let amountToDrop = Math.max(1, Math.floor(inventoryCount / 2));
splitUpLoot(this, player, dropMsg.item, amountToDrop);
player.inventory[dropMsg.item] -= amountToDrop;
player.dirty.inventory = true;
break;
}
case "scope": {
const level = item.level;
if (level === 1) break;

const availableScopeLevels = [15, 8, 4, 2, 1];
let targetScopeLevel = availableScopeLevels.indexOf(level);

this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player.inventory[`${level}xscope`] = 0;

if (item.minStackSize && inventoryCount <= item.minStackSize) {
amountToDrop = Math.min(item.minStackSize, inventoryCount);
} else if (inventoryCount <= 5) {
amountToDrop = Math.min(5, inventoryCount);
for (let i = 0; i < availableScopeLevels.length; i++) {
if (player.inventory[`${availableScopeLevels[i]}xscope`]) {
targetScopeLevel = availableScopeLevels[i];
break;
}
}

splitUpLoot(this, player, dropMsg.item, amountToDrop);
player.inventory[dropMsg.item] -= amountToDrop;
player.scope = `${targetScopeLevel}xscope`;
break;
}
case "chest":
case "helmet": {
if (item.noDrop) break;
this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player[item.type] = "";
player.setDirty();
break;
}
case "heal":
case "boost": {
if (player.inventory[dropMsg.item] === 0) break;
player.inventory[dropMsg.item]--;
// @TODO: drop more than one?
this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player.dirty.inventory = true;
break;
}
case "gun": {
const weaponAmmoType = (GameObjectDefs[player.weapons[dropMsg.weapIdx].type] as GunDef).ammo;
const weaponAmmoCount = player.weapons[dropMsg.weapIdx].ammo;

player.weapons[dropMsg.weapIdx].type = "";
player.weapons[dropMsg.weapIdx].ammo = 0;
if (player.curWeapIdx == dropMsg.weapIdx) {
player.curWeapIdx = 2;
}

const backpackLevel = Number(player.backpack.at(-1));// backpack00, backpack01, etc ------- at(-1) => 0, 1, etc
const bagSpace = GameConfig.bagSizes[weaponAmmoType][backpackLevel];
if (player.inventory[weaponAmmoType] + weaponAmmoCount <= bagSpace) {
player.inventory[weaponAmmoType] += weaponAmmoCount;
player.dirty.inventory = true;
} else {
const spaceLeft = bagSpace - player.inventory[weaponAmmoType];
const amountToAdd = spaceLeft;

player.inventory[weaponAmmoType] += amountToAdd;
player.dirty.inventory = true;
break;

const amountToDrop = weaponAmmoCount - amountToAdd;

this.addLoot(weaponAmmoType, player.pos, player.layer, amountToDrop);
}
case "scope": {
const level = item.level;
if (level === 1) break;

const availableScopeLevels = [15, 8, 4, 2, 1];
let targetScopeLevel = availableScopeLevels.indexOf(level);
this.addGun(dropMsg.item, player.pos, player.layer, 1);
player.dirty.weapons = true;
player.setDirty();
break;
}
case "throwable": {
const inventoryCount = player.inventory[dropMsg.item];

this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player.inventory[`${level}xscope`] = 0;
if (inventoryCount === 0) return;

for (let i = 0; i < availableScopeLevels.length; i++) {
if (player.inventory[`${availableScopeLevels[i]}xscope`]) {
targetScopeLevel = availableScopeLevels[i];
break;
}
}
const amountToDrop = Math.max(1, Math.floor(inventoryCount / 2));

splitUpLoot(this, player, dropMsg.item, amountToDrop);
player.inventory[dropMsg.item] -= amountToDrop;
player.weapons[3].ammo -= amountToDrop;

player.scope = `${targetScopeLevel}xscope`;
break;
if (player.inventory[dropMsg.item] == 0) {
player.showNextThrowable();
}
case "chest":
case "helmet": {
if (item.noDrop) break;
player.dirty.inventory = true;
player.dirty.weapons = true;
break;
}
case "melee": {
if (player.weapons[2].type != "fists") {
this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player[item.type] = "";
player.weapons[2].type = "fists";
player.weapons[2].ammo = 0;
player.dirty.weapons = true;
player.setDirty();
break;
}
case "heal":
case "boost": {
if (player.inventory[dropMsg.item] === 0) break;
player.inventory[dropMsg.item]--;
// @TODO: drop more than one?
this.addLoot(dropMsg.item, player.pos, player.layer, 1);
player.dirty.inventory = true;
break;
}
}
}
}

player.cancelAction();
}
Expand Down
Loading

0 comments on commit 2112dd7

Please sign in to comment.