add a piston

This commit is contained in:
Elizabeth Hunt 2025-03-02 18:49:51 -07:00
parent 491a1d05a5
commit 196773c363
Signed by: simponic
GPG Key ID: 2909B9A7FF6213EE
7 changed files with 61 additions and 5 deletions

View File

@ -12,6 +12,7 @@ import {
Music, Music,
Level, Level,
Modal, Modal,
RadialObserve,
} from "./systems"; } from "./systems";
export class TheAbstractionEngine { export class TheAbstractionEngine {
@ -35,6 +36,7 @@ export class TheAbstractionEngine {
const isDev = document.location.hostname.includes("localhost"); const isDev = document.location.hostname.includes("localhost");
[ [
new RadialObserve(),
new Modal(), new Modal(),
new Level(isDev ? LevelNames.CarCadr : LevelNames.LevelSelection), new Level(isDev ? LevelNames.CarCadr : LevelNames.LevelSelection),
inputSystem, inputSystem,

View File

@ -11,4 +11,5 @@ export namespace EntityNames {
export const Portal = "Portal"; export const Portal = "Portal";
export const Grass = "Grass"; export const Grass = "Grass";
export const Sign = "Sign"; export const Sign = "Sign";
export const Piston = "Piston";
} }

View File

@ -0,0 +1,48 @@
import { Entity, EntityNames } from ".";
import { Colliding, ComponentNames, FacingDirection, Grid, RadialObserve } from "../components";
import { Game } from "../Game";
import { Coord2D, Direction } from "../interfaces";
export class Piston extends Entity {
constructor(gridPosition: Coord2D, direction: Direction) {
super(EntityNames.Piston);
const radius = 1 + .001;
this.addComponent(new RadialObserve(this.onObservation.bind(this), radius));
this.addComponent(new FacingDirection(direction));
this.addComponent(new Grid(gridPosition));
this.addComponent(new Colliding());
}
private onObservation(_game: Game, entity: Entity) {
const facingDirection = this.getComponent<FacingDirection>(ComponentNames.FacingDirection);
const myPosition = this.getComponent<Grid>(ComponentNames.Grid).gridPosition;
const observingGrid = entity.getComponent<Grid>(ComponentNames.Grid);
const observingPosition = observingGrid.gridPosition;
const [dx, dy] = [myPosition.x - observingPosition.x, myPosition.y - observingPosition.y].map(x => Math.round(x));
const v: Record<typeof dx, Record<typeof dy, Direction>> = {
[-1]: {
[dy]: Direction.RIGHT,
},
[1]: {
[dy]: Direction.LEFT,
},
[0]: {
[-1]: Direction.UP,
[1]: Direction.DOWN,
}
};
if (facingDirection.currentDirection !== v[dx][dy]) {
return;
}
observingGrid.movingDirection = facingDirection.currentDirection;
entity.addComponent(observingGrid);
}
}

View File

@ -9,6 +9,8 @@ import {
Player, Player,
Wall, Wall,
} from "../entities"; } from "../entities";
import { Piston } from "../entities/Piston";
import { Direction } from "../interfaces";
import { Grid, SystemNames } from "../systems"; import { Grid, SystemNames } from "../systems";
import { normalRandom } from "../utils"; import { normalRandom } from "../utils";
@ -58,6 +60,7 @@ export class CarCadr extends Level {
new Wall({ x: 10, y: 5 }), new Wall({ x: 10, y: 5 }),
new Wall({ x: 12, y: 5 }), new Wall({ x: 12, y: 5 }),
new Wall({ x: 12, y: 3 }), new Wall({ x: 12, y: 3 }),
new Piston({ x: 10, y: 6 }, Direction.RIGHT),
new FunctionApplication({ x: 12, y: 6 }, "(_INPUT _KEY)"), new FunctionApplication({ x: 12, y: 6 }, "(_INPUT _KEY)"),
new Wall({ x: 10, y: 7 }), new Wall({ x: 10, y: 7 }),
new Wall({ x: 11, y: 7 }), new Wall({ x: 11, y: 7 }),

View File

@ -1,16 +1,16 @@
import { System, SystemNames } from "."; import { System, SystemNames } from ".";
import { ComponentNames, Grid, RadialObserve } from "../components"; import { ComponentNames, Grid, RadialObserve as RadialObserveComponent } from "../components";
import { Entity, EntityNames } from "../entities"; import { Entity, EntityNames } from "../entities";
import { Game } from "../Game"; import { Game } from "../Game";
import { cartesianDistance } from "../interfaces"; import { cartesianDistance } from "../interfaces";
const radialObservations: Record<string, Set<string>> = { const radialObservations: Record<string, Set<string>> = {
TODO: new Set([]), [EntityNames.Piston]: new Set([EntityNames.FunctionBox]),
}; };
export class RadialObserver extends System { export class RadialObserve extends System {
constructor() { constructor() {
super(SystemNames.RadialObserver); super(SystemNames.RadialObserve);
} }
public update(_dt: number, game: Game) { public update(_dt: number, game: Game) {
@ -20,7 +20,7 @@ export class RadialObserver extends System {
} }
const observable = radialObservations[entity.name]; const observable = radialObservations[entity.name];
const entityObserve = entity.getComponent<RadialObserve>( const entityObserve = entity.getComponent<RadialObserveComponent>(
ComponentNames.RadialObserve, ComponentNames.RadialObserve,
); );
if (!entityObserve.onObservation) { if (!entityObserve.onObservation) {

View File

@ -9,4 +9,5 @@ export namespace SystemNames {
export const Music = "Music"; export const Music = "Music";
export const Level = "Level"; export const Level = "Level";
export const Modal = "Modal"; export const Modal = "Modal";
export const RadialObserve = "RadialObserve";
} }

View File

@ -10,3 +10,4 @@ export * from "./Life";
export * from "./Music"; export * from "./Music";
export * from "./Level"; export * from "./Level";
export * from "./Modal"; export * from "./Modal";
export * from "./RadialObserve";