pistons
This commit is contained in:
parent
196773c363
commit
78797aa175
@ -8,7 +8,5 @@ export class RadialObserve extends Component {
|
||||
public radius: number = 0,
|
||||
) {
|
||||
super(ComponentNames.RadialObserve);
|
||||
|
||||
this.onObservation = onObservation;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import {
|
||||
LambdaTerm,
|
||||
Modal,
|
||||
Pushable,
|
||||
RadialObserve,
|
||||
Sprite,
|
||||
} from "../components";
|
||||
import { Coord2D } from "../interfaces";
|
||||
@ -40,6 +41,8 @@ export class FunctionBox extends Entity {
|
||||
|
||||
this.addComponent(new Pushable());
|
||||
|
||||
this.addComponent(new RadialObserve());
|
||||
|
||||
this.addComponent(new Grid(gridPosition));
|
||||
|
||||
this.addComponent(
|
||||
|
@ -1,5 +1,11 @@
|
||||
import { Entity, EntityNames } from ".";
|
||||
import { Colliding, ComponentNames, FacingDirection, Grid, RadialObserve } from "../components";
|
||||
import {
|
||||
Colliding,
|
||||
ComponentNames,
|
||||
FacingDirection,
|
||||
Grid,
|
||||
RadialObserve,
|
||||
} from "../components";
|
||||
import { Game } from "../Game";
|
||||
import { Coord2D, Direction } from "../interfaces";
|
||||
|
||||
@ -7,7 +13,7 @@ export class Piston extends Entity {
|
||||
constructor(gridPosition: Coord2D, direction: Direction) {
|
||||
super(EntityNames.Piston);
|
||||
|
||||
const radius = 1 + .001;
|
||||
const radius = 1;
|
||||
this.addComponent(new RadialObserve(this.onObservation.bind(this), radius));
|
||||
|
||||
this.addComponent(new FacingDirection(direction));
|
||||
@ -18,24 +24,31 @@ export class Piston extends Entity {
|
||||
}
|
||||
|
||||
private onObservation(_game: Game, entity: Entity) {
|
||||
const facingDirection = this.getComponent<FacingDirection>(ComponentNames.FacingDirection);
|
||||
const facingDirection = this.getComponent<FacingDirection>(
|
||||
ComponentNames.FacingDirection,
|
||||
);
|
||||
|
||||
const myPosition = this.getComponent<Grid>(ComponentNames.Grid).gridPosition;
|
||||
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 [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,
|
||||
[dy]: Direction.RIGHT,
|
||||
},
|
||||
[1]: {
|
||||
[dy]: Direction.LEFT,
|
||||
[dy]: Direction.LEFT,
|
||||
},
|
||||
[0]: {
|
||||
[-1]: Direction.UP,
|
||||
[1]: Direction.DOWN,
|
||||
}
|
||||
[-1]: Direction.UP,
|
||||
[1]: Direction.DOWN,
|
||||
},
|
||||
};
|
||||
|
||||
if (facingDirection.currentDirection !== v[dx][dy]) {
|
||||
|
@ -60,7 +60,6 @@ export class CarCadr extends Level {
|
||||
new Wall({ x: 10, y: 5 }),
|
||||
new Wall({ x: 12, y: 5 }),
|
||||
new Wall({ x: 12, y: 3 }),
|
||||
new Piston({ x: 10, y: 6 }, Direction.RIGHT),
|
||||
new FunctionApplication({ x: 12, y: 6 }, "(_INPUT _KEY)"),
|
||||
new Wall({ x: 10, y: 7 }),
|
||||
new Wall({ x: 11, y: 7 }),
|
||||
|
@ -27,34 +27,38 @@ export class FacingDirection extends System {
|
||||
game.forEachEntityWithComponent(
|
||||
ComponentNames.FacingDirection,
|
||||
(entity) => {
|
||||
if (!entity.hasComponent(ComponentNames.BoundingBox)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const boundingBox = entity.getComponent<BoundingBox>(
|
||||
ComponentNames.BoundingBox,
|
||||
)!;
|
||||
const facingDirection = entity.getComponent<FacingDirectionComponent>(
|
||||
ComponentNames.FacingDirection,
|
||||
);
|
||||
if (!entity.hasComponent(ComponentNames.Sprite)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { center } = boundingBox;
|
||||
const angle = Math.atan2(
|
||||
mousePosition.y - center.y,
|
||||
mousePosition.x - center.x,
|
||||
);
|
||||
if (entity.hasComponent(ComponentNames.Control)) {
|
||||
const boundingBox = entity.getComponent<BoundingBox>(
|
||||
ComponentNames.BoundingBox,
|
||||
)!;
|
||||
|
||||
const mouseInBoundingBox =
|
||||
boundingBox.isCollidingWith(mouseBoundingBox);
|
||||
const direction = mouseInBoundingBox
|
||||
? Direction.NONE
|
||||
: angleToDirection(angle);
|
||||
const { center } = boundingBox;
|
||||
const angle = Math.atan2(
|
||||
mousePosition.y - center.y,
|
||||
mousePosition.x - center.x,
|
||||
);
|
||||
|
||||
facingDirection.setDirection(direction);
|
||||
entity.addComponent(facingDirection);
|
||||
const mouseInBoundingBox =
|
||||
boundingBox.isCollidingWith(mouseBoundingBox);
|
||||
const direction = mouseInBoundingBox
|
||||
? Direction.NONE
|
||||
: angleToDirection(angle);
|
||||
|
||||
facingDirection.setDirection(direction);
|
||||
entity.addComponent(facingDirection);
|
||||
}
|
||||
|
||||
const oldSprite = entity.getComponent<Sprite>(ComponentNames.Sprite);
|
||||
const sprite = facingDirection.directionSprites.get(direction)!;
|
||||
const sprite = facingDirection.directionSprites.get(
|
||||
facingDirection.currentDirection,
|
||||
)!;
|
||||
sprite.fillTimingsFromSprite(oldSprite);
|
||||
|
||||
entity.addComponent(sprite);
|
||||
|
@ -54,6 +54,9 @@ export class Grid extends System {
|
||||
if (!entity.hasComponent(ComponentNames.Grid)) {
|
||||
return;
|
||||
}
|
||||
if (!entity.hasComponent(ComponentNames.Control)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const grid = entity.getComponent<GridComponent>(ComponentNames.Grid)!;
|
||||
const facingDirection = entity.getComponent<FacingDirection>(
|
||||
|
@ -1,5 +1,9 @@
|
||||
import { System, SystemNames } from ".";
|
||||
import { ComponentNames, Grid, RadialObserve as RadialObserveComponent } from "../components";
|
||||
import {
|
||||
ComponentNames,
|
||||
Grid,
|
||||
RadialObserve as RadialObserveComponent,
|
||||
} from "../components";
|
||||
import { Entity, EntityNames } from "../entities";
|
||||
import { Game } from "../Game";
|
||||
import { cartesianDistance } from "../interfaces";
|
||||
@ -24,7 +28,7 @@ export class RadialObserve extends System {
|
||||
ComponentNames.RadialObserve,
|
||||
);
|
||||
if (!entityObserve.onObservation) {
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
const entityPosition = entity.getComponent<Grid>(
|
||||
@ -54,7 +58,7 @@ export class RadialObserve extends System {
|
||||
});
|
||||
|
||||
for (const observation of observations) {
|
||||
entityObserve.onObservation!(game, observation);
|
||||
entityObserve.onObservation!(game, observation);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user