Player repels walls

This commit is contained in:
Pyrex 2025-02-22 16:40:05 -08:00
parent 92288085b7
commit 4a1f06e6bd
2 changed files with 63 additions and 22 deletions

View File

@ -16,6 +16,8 @@ import { Architecture, LoadedNewMap } from "./newmap.ts";
import { FLOOR_CELL_SIZE, GridArt } from "./gridart.ts"; import { FLOOR_CELL_SIZE, GridArt } from "./gridart.ts";
import { shadowcast } from "./shadowcast.ts"; import { shadowcast } from "./shadowcast.ts";
import { withCamera } from "./layout.ts"; import { withCamera } from "./layout.ts";
import { getCheckModal } from "./checkmodal.ts";
import { CARDINAL_DIRECTIONS } from "./mapgen.ts";
export class HuntMode { export class HuntMode {
map: LoadedNewMap; map: LoadedNewMap;
@ -130,23 +132,38 @@ export class HuntMode {
let dx = this.velocity.x; let dx = this.velocity.x;
let dy = this.velocity.y; let dy = this.velocity.y;
let touched = false;
let amt = 0.006; let amt = 0.006;
if (getPlayerProgress().getBlood() > 0) { if (getPlayerProgress().getBlood() <= 0) {
if (I.isKeyDown("w")) { amt = 0;
dy -= amt;
}
if (I.isKeyDown("s")) {
dy += amt;
}
if (I.isKeyDown("a")) {
dx -= amt;
}
if (I.isKeyDown("d")) {
dx += amt;
}
} }
let mvdx = 0;
let mvdy = 0;
if (I.isKeyDown("w")) {
touched = true;
mvdy -= amt;
}
if (I.isKeyDown("s")) {
touched = true;
mvdy += amt;
}
if (I.isKeyDown("a")) {
touched = true;
mvdx -= amt;
}
if (I.isKeyDown("d")) {
touched = true;
mvdx += amt;
}
if (touched) {
getCheckModal().show(null, null);
}
dx += mvdx;
dy += mvdy;
dx *= 0.87; dx *= 0.87;
dy *= 0.87; dy *= 0.87;
@ -156,18 +173,42 @@ export class HuntMode {
if (Math.abs(dy) < 0.0001) { if (Math.abs(dy) < 0.0001) {
dy = 0; dy = 0;
} }
if (dx < 0) { if (mvdx < 0) {
this.faceLeft = true; this.faceLeft = true;
} }
if (dx > 0) { if (mvdx > 0) {
this.faceLeft = false; this.faceLeft = false;
} }
this.velocity = new Point(dx, dy);
let nSteps = 40; let nSteps = 40;
let szX = 0.5; let szX = 0.75;
let szY = 0.75; let szY = 0.75;
this.velocity = new Point(dx, dy);
// try to push us away from walls if we're close
for (let offset of CARDINAL_DIRECTIONS.values()) {
let bigBbox = new Rect(
this.floatingPlayer
.offset(offset.scale(new Size(0.02, 0.02)))
.offset(new Point(-szX / 2, -szY / 2)),
new Size(szX, szY),
);
let hitsWall = false;
for (let cell of bigBbox.overlappedCells(new Size(1, 1)).values()) {
if (this.#blocksMovement(cell.top)) {
hitsWall = true;
break;
}
}
if (hitsWall) {
this.velocity = this.velocity.offset(
offset.scale(new Point(0.005, 0.005)).negate(),
);
}
}
let initialXy = this.floatingPlayer; let initialXy = this.floatingPlayer;
for (let i = 0; i < nSteps; i++) { for (let i = 0; i < nSteps; i++) {
let oldXy = this.floatingPlayer; let oldXy = this.floatingPlayer;
@ -393,7 +434,7 @@ export class HuntMode {
#drawPlayer(globalOffset: Point) { #drawPlayer(globalOffset: Point) {
let cellOffset = this.pixelPlayer.offset(globalOffset.negate()); let cellOffset = this.pixelPlayer.offset(globalOffset.negate());
this.drawpile.add(0, () => { this.drawpile.add(1024, () => {
D.drawSprite(sprThrallLore, cellOffset, 1, { D.drawSprite(sprThrallLore, cellOffset, 1, {
xScale: this.faceLeft ? -2 : 2, xScale: this.faceLeft ? -2 : 2,
yScale: 2, yScale: 2,

View File

@ -427,7 +427,7 @@ let mergeRects = (a: Rect, b: Rect) => {
return new Rect(new Point(abx0, aby0), new Size(abx1 - abx0, aby1 - aby0)); return new Rect(new Point(abx0, aby0), new Size(abx1 - abx0, aby1 - aby0));
}; };
const _CARDINAL_DIRECTIONS = [ export const CARDINAL_DIRECTIONS = [
new Point(-1, 0), new Point(-1, 0),
new Point(0, -1), new Point(0, -1),
new Point(1, 0), new Point(1, 0),
@ -451,7 +451,7 @@ function connectRegions(knife: Knife) {
} }
let regions = []; let regions = [];
for (let offset of _CARDINAL_DIRECTIONS.values()) { for (let offset of CARDINAL_DIRECTIONS.values()) {
let region = knife.regions.get(pos.offset(offset)); let region = knife.regions.get(pos.offset(offset));
if (region != null) { if (region != null) {
regions.push(region); regions.push(region);
@ -547,7 +547,7 @@ function growMaze(knife: Knife, start: Point) {
let unmadeCells: Point[] = []; let unmadeCells: Point[] = [];
let lastDirOk = false; let lastDirOk = false;
for (let dir of _CARDINAL_DIRECTIONS.values()) { for (let dir of CARDINAL_DIRECTIONS.values()) {
if (canCarve(knife, cell, dir)) { if (canCarve(knife, cell, dir)) {
unmadeCells.push(dir); unmadeCells.push(dir);
if (lastDir != null && dir.equals(lastDir)) { if (lastDir != null && dir.equals(lastDir)) {
@ -603,7 +603,7 @@ function removeDeadEnds(knife: Knife) {
} }
let exits = 0; let exits = 0;
for (let dir of _CARDINAL_DIRECTIONS.values()) { for (let dir of CARDINAL_DIRECTIONS.values()) {
if (knife.map.get(xy.offset(dir)).architecture != Architecture.Wall) { if (knife.map.get(xy.offset(dir)).architecture != Architecture.Wall) {
exits++; exits++;
} }