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

Commit

Permalink
refactor: clean up loot.js update method
Browse files Browse the repository at this point in the history
  • Loading branch information
leia-uwu committed Mar 5, 2024
1 parent c4b235d commit 12d298f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 44 deletions.
4 changes: 2 additions & 2 deletions client/src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { PlaneBarn } from "./objects/plane";
import { PlayerBarn } from "./objects/player";
import { ShotBarn } from "./objects/shot";
import { ProjectileBarn } from "./objects/projectile";
import { SmokeBarn } from "./objects/Smoke";
import { SmokeBarn } from "./objects/smoke";
import { Renderer } from "./renderer";
import { Touch } from "./ui/touch";
import { UiManager } from "./ui/ui";
Expand Down Expand Up @@ -686,7 +686,7 @@ export class Game {
smokeParticles,
debug
);
this.lootBarn.m(dt, this.m_activePlayer, this.map, this.audioManager, this.camera, debug);
this.lootBarn.update(dt, this.m_activePlayer, this.map, this.audioManager, this.camera, debug);
this.bulletBarn.m(
dt,
this.playerBarn,
Expand Down
86 changes: 46 additions & 40 deletions client/src/objects/loot.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,61 +110,67 @@ class Loot {
export class LootBarn {
constructor() {
this.sr = new Pool(Loot);
this.Dr = null;
this.closestLoot = null;
}

m(e, t, r, a, i, o) {
this.Dr = null;
for (
let p = Number.MAX_VALUE, d = this.sr.p(), u = 0;
u < d.length;
u++
) {
const g = d[u];
if (g.active) {
/**
* @param {number} dt
* @param {import("./player").Player} activePlayer
* @param {import("../map").Map} map
* @param {import("../audioManager").AudioManager} audioManager
* @param {import("../camera").Camera} camera
*/
update(dt, activePlayer, map, audioManager, camera, debug) {
this.closestLoot = null;
let closestDist = Number.MAX_VALUE;
const loots = this.sr.p();
for (let i = 0; i < loots.length; i++) {
/** @type {Loot} */
const loot = loots[i];
if (loot.active) {
if (
util.sameLayer(g.layer, t.layer) &&
!t.netData.he &&
(g.ownerId == 0 || g.ownerId == t.__id)
util.sameLayer(loot.layer, activePlayer.layer) &&
!activePlayer.netData.he &&
(loot.ownerId == 0 || loot.ownerId == activePlayer.__id)
) {
const y = g.pos;
const w = device.touch
? t.rad + g.rad * GameConfig.player.touchLootRadMult
: g.rad;
const f = v2.sub(t.pos, y);
const _ = v2.lengthSqr(f);
if (_ < w * w && _ < p) {
p = _;
this.Dr = g;
const pos = loot.pos;
const rad = device.touch
? activePlayer.rad + loot.rad * GameConfig.player.touchLootRadMult
: loot.rad;
const toPlayer = v2.sub(activePlayer.pos, pos);
const distSq = v2.lengthSqr(toPlayer);
if (distSq < rad * rad && distSq < closestDist) {
closestDist = distSq;
this.closestLoot = loot;
}
}
g.ticker += e;
if (g.playDropSfx) {
r.lootDropSfxIds.push(g.__id);
g.playDropSfx = false;
const b = GameObjectDefs[g.type];
a.playSound(b.sound.drop, {
loot.ticker += dt;
if (loot.playDropSfx) {
map.lootDropSfxIds.push(loot.__id);
loot.playDropSfx = false;
const b = GameObjectDefs[loot.type];
audioManager.playSound(b.sound.drop, {
channel: "sfx",
soundPos: g.pos,
layer: g.layer,
soundPos: loot.pos,
layer: loot.layer,
filter: "muffled"
});
}
if (g.emitter) {
g.emitter.pos = v2.add(g.pos, v2.create(0, 0.1));
g.emitter.layer = g.layer;
if (loot.emitter) {
loot.emitter.pos = v2.add(loot.pos, v2.create(0, 0.1));
loot.emitter.layer = loot.layer;
}
const x = math.delerp(g.ticker, 0, 1);
const x = math.delerp(loot.ticker, 0, 1);
const S = math.easeOutElastic(x, 0.75);
const v = i.pointToScreen(g.pos);
const k = i.pixels(g.imgScale * S);
g.container.position.set(v.x, v.y);
g.container.scale.set(k, k);
const v = camera.pointToScreen(loot.pos);
const k = camera.pixels(loot.imgScale * S);
loot.container.position.set(v.x, v.y);
loot.container.scale.set(k, k);
}
}
}

Er() {
return this.Dr;
getClosestLoot() {
return this.closestLoot;
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion client/src/ui/opponentDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ParticleBarn } from "../objects/particles";
import { Renderer } from "../renderer";
import { GameObjectDefs } from "../../../shared/defs/gameObjectDefs";
import { PlayerBarn } from "../objects/player";
import { SmokeBarn } from "../objects/Smoke";
import { SmokeBarn } from "../objects/smoke";
import { Creator } from "../objects/objectPool";

class LoadoutDisplay {
Expand Down
2 changes: 1 addition & 1 deletion client/src/ui/ui2.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ export class UiManager2 {
R = q;
L = true;
}
const W = lootBarn.Er();
const W = lootBarn.getClosestLoot();
if (W && !activePlayer.netData.ue) {
const G = GameObjectDefs[W.type];
const X = activePlayer.Wr(GameConfig.WeaponSlot.Primary);
Expand Down

0 comments on commit 12d298f

Please sign in to comment.