Skip to content

Commit

Permalink
fix: Improve detection/placement when slices are falling and landing
Browse files Browse the repository at this point in the history
In high CPU utilization situations (like level 4), slices would fall too
far or "clump" together while falling. By reducing floor precision,
floors are detected even in higher CPU utilization situations.

We also optimize slice-bumps-slice detection by only have one slice bit
(the first one) check for bumping into other slice bits, instead of all
four of them, which just causes multiple collisions.

Then, to counter the reduced precision, we snap the slices to the exact
position of the floor so that placement is pixel-perfect.
  • Loading branch information
joshuacurtiss committed Aug 6, 2024
1 parent 5e9b270 commit 9435bfd
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/objects/Slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function addSlice(options: Partial<SliceCompOpt> = {}): Slice {
return add([
anchor('center'),
pos(opt.pos),
canDetect({ floorPrecision: 0.5 }),
canDetect({ floorPrecision: 1.25 }),
slice(opt),
]);
}
Expand Down Expand Up @@ -120,7 +120,7 @@ export function slice(options: Partial<SliceCompOpt> = {}): SliceComp {
},
fall(newFallCount=0) {
if (this.isFalling || this.isOnPlate) return;
this.pos = this.pos.add(0, 1);
this.pos = this.pos.add(0, 1.3);
dir.y = FALL_SPEED;
fallCount = newFallCount<0 ? 0 : newFallCount;
play('burger_drop', { volume: getVol(DATA_SFX_VOL) });
Expand All @@ -129,6 +129,12 @@ export function slice(options: Partial<SliceCompOpt> = {}): SliceComp {
land() {
if (dir.isZero()) return;
dir.y = 0;
// Snap to the closest floor for exact placement
const closestFloor = this.getClosestObject(this.isOnPlate ? 'plates' : 'floors');
if (closestFloor && Math.abs(closestFloor.pos.y-this.pos.y)<1.3) {
this.moveTo(closestFloor.pos);
}
// Reset slice bits to non-trampled position
this.children.forEach(child=>child.pos.y = Y_NORMAL);
play('burger_floor', { volume: getVol(DATA_SFX_VOL) });
enemies.forEach(enemy=>{
Expand All @@ -155,7 +161,7 @@ export function slice(options: Partial<SliceCompOpt> = {}): SliceComp {
"slice-bit"
]);
}
this.children.forEach(child=>{
this.children.forEach((child, idx)=>{
// Player can trample slices
child.onCollide('player', ()=>{
if (child.pos.y !== Y_TRAMPLED) play('burger_step', { volume: getVol(DATA_SFX_VOL) });
Expand All @@ -169,18 +175,18 @@ export function slice(options: Partial<SliceCompOpt> = {}): SliceComp {
enemy.squash();
});
// Slices will bump other slices while falling
child.onCollide('slice-bit', (sliceBit)=>{
if (idx===0) child.onCollide('slice-bit', (sliceBit)=>{
if (!sliceBit.isOverlapping(child)) return;
const otherSlice = sliceBit.parent as Slice;
if (otherSlice.isOnPlate) {
this.isOnPlate = true;
this.pos = this.pos.add(0, -4);
this.land();
this.pos = this.pos.add(0, -1);
return;
}
sliceBit.parent.fall(fallCount);
this.pos.y-=1.5
enemies.forEach(enemy=>enemy.pos.y-=1.5);
this.pos.y-=3;
enemies.forEach(enemy=>enemy.pos.y-=3);
});
});
},
Expand Down

0 comments on commit 9435bfd

Please sign in to comment.