2022-06-25 00:29:43 -05:00
|
|
|
import Matter from "matter-js";
|
|
|
|
|
|
|
|
import { windowHeight, windowWidth } from "@game";
|
2022-06-26 21:45:01 -05:00
|
|
|
import { Balloon, Wall } from ".";
|
2024-01-09 02:26:26 -06:00
|
|
|
import { GameEngineEntities } from "@types";
|
2022-06-25 00:29:43 -05:00
|
|
|
|
2024-01-09 02:26:26 -06:00
|
|
|
export function entities(): GameEngineEntities {
|
|
|
|
const engine = Matter.Engine.create({
|
2022-06-25 00:29:43 -05:00
|
|
|
enableSleeping: false,
|
2024-01-09 02:26:26 -06:00
|
|
|
gravity: { x: 0, y: 1.75 },
|
2022-06-25 00:29:43 -05:00
|
|
|
} as Matter.IEngineDefinition);
|
|
|
|
|
2024-01-09 02:26:26 -06:00
|
|
|
const world = engine.world;
|
|
|
|
const [top, bottom] = [
|
|
|
|
global.topInset,
|
|
|
|
global.bottomInset,
|
|
|
|
global.leftInset,
|
|
|
|
global.rightInset,
|
|
|
|
];
|
2022-06-25 00:29:43 -05:00
|
|
|
|
2022-06-26 21:45:01 -05:00
|
|
|
const newBalloon = () =>
|
|
|
|
Balloon(
|
|
|
|
"Balloon",
|
2022-06-25 00:29:43 -05:00
|
|
|
world,
|
|
|
|
"red",
|
|
|
|
{
|
|
|
|
x: Math.random() * (windowWidth - 100) + 50,
|
2024-01-09 02:26:26 -06:00
|
|
|
y: 200,
|
2022-06-25 00:29:43 -05:00
|
|
|
},
|
2024-01-09 02:26:26 -06:00
|
|
|
{ width: 40, height: 50 }
|
2022-06-25 00:29:43 -05:00
|
|
|
);
|
|
|
|
|
2024-01-09 02:26:26 -06:00
|
|
|
const entities = {
|
2022-06-25 00:29:43 -05:00
|
|
|
physics: { engine, world },
|
|
|
|
Balloon: newBalloon(),
|
2024-01-09 02:26:26 -06:00
|
|
|
// LeftWall: Wall(
|
|
|
|
// "LeftWall",
|
|
|
|
// world,
|
|
|
|
// "orange",
|
|
|
|
// { x: 0 - 25, y: windowHeight / 2 },
|
|
|
|
// { height: windowHeight - topInset - bottomInset + 560, width: 50 }
|
|
|
|
// ),
|
|
|
|
// RightWall: Wall(
|
|
|
|
// "RightWall",
|
|
|
|
// world,
|
|
|
|
// "orange",
|
|
|
|
// { x: windowWidth + 25, y: windowHeight / 2 },
|
|
|
|
// { height: windowHeight - topInset - bottomInset + 560, width: 50 }
|
|
|
|
// ),
|
2022-06-25 00:29:43 -05:00
|
|
|
Ceiling: Wall(
|
2022-06-26 21:45:01 -05:00
|
|
|
"Ceiling",
|
2022-06-25 00:29:43 -05:00
|
|
|
world,
|
|
|
|
"orange",
|
|
|
|
{ x: 0, y: 0 },
|
2024-01-09 02:26:26 -06:00
|
|
|
{ height: 110 + top, width: windowWidth * 2 }
|
2022-06-25 00:29:43 -05:00
|
|
|
),
|
|
|
|
Floor: Wall(
|
2022-06-26 21:45:01 -05:00
|
|
|
"Floor",
|
2022-06-25 00:29:43 -05:00
|
|
|
world,
|
|
|
|
"orange",
|
2024-01-09 02:26:26 -06:00
|
|
|
{ x: windowWidth / 2, y: windowHeight - top },
|
|
|
|
{ height: 60 + bottom, width: windowWidth }
|
2022-06-25 00:29:43 -05:00
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2022-06-26 21:45:01 -05:00
|
|
|
Matter.Events.on(engine, "removeBalloon", () => {
|
|
|
|
// Remove old balloon
|
|
|
|
const balloonBody = entities.Balloon.body;
|
|
|
|
Matter.World.remove(world, balloonBody, true);
|
2022-06-25 21:47:29 -05:00
|
|
|
|
2022-06-26 21:45:01 -05:00
|
|
|
// Add new Balloon
|
|
|
|
entities.Balloon = newBalloon();
|
|
|
|
// @ts-expect-error, for some reason this doesn't work as expected if passed as entities.Balloon.body
|
|
|
|
Matter.World.add(world, entities.Balloon);
|
|
|
|
});
|
2022-06-25 21:47:29 -05:00
|
|
|
|
2022-06-25 00:29:43 -05:00
|
|
|
Matter.Events.on(
|
|
|
|
engine,
|
|
|
|
"collisionStart",
|
2024-01-09 02:26:26 -06:00
|
|
|
({ pairs }: Matter.IEventCollision<object>) => {
|
|
|
|
for (let i = 0, j = pairs.length; i != j; ++i) {
|
2022-06-25 00:29:43 -05:00
|
|
|
const bodyA = pairs[i].bodyA;
|
|
|
|
const bodyB = pairs[i].bodyB;
|
2022-06-26 21:45:01 -05:00
|
|
|
|
|
|
|
// We only want collisions between the balloon and the floor
|
2024-01-09 02:26:26 -06:00
|
|
|
if (
|
|
|
|
(bodyA.label !== "Balloon" && bodyB.label !== "Balloon") ||
|
|
|
|
(bodyA.label !== "Floor" && bodyB.label !== "Floor")
|
|
|
|
) {
|
2022-06-26 21:45:01 -05:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-25 00:29:43 -05:00
|
|
|
const balloonBody = entities.Balloon.body;
|
2022-06-25 21:47:29 -05:00
|
|
|
|
|
|
|
// Remove balloon if it hits the floor
|
2022-06-25 00:29:43 -05:00
|
|
|
Matter.World.remove(world, balloonBody, true);
|
|
|
|
|
2022-06-25 21:47:29 -05:00
|
|
|
// Subtract a point from the score
|
2024-01-09 02:26:26 -06:00
|
|
|
const gameEngine = global.gameEngine!;
|
2022-06-25 00:29:43 -05:00
|
|
|
gameEngine.dispatch({
|
2022-06-25 21:47:29 -05:00
|
|
|
type: "subtractFromScore",
|
2022-06-25 00:29:43 -05:00
|
|
|
});
|
|
|
|
|
2022-06-25 21:47:29 -05:00
|
|
|
// Add new Balloon
|
2022-06-26 21:45:01 -05:00
|
|
|
entities.Balloon = newBalloon();
|
|
|
|
// @ts-expect-error, for some reason this doesn't work if passed as entities.Balloon.body
|
2022-06-25 00:29:43 -05:00
|
|
|
Matter.World.add(world, entities.Balloon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return entities;
|
2024-01-09 02:26:26 -06:00
|
|
|
}
|
2022-06-25 00:29:43 -05:00
|
|
|
|
|
|
|
export const useEntities = () => {
|
|
|
|
return {
|
2022-06-26 21:45:01 -05:00
|
|
|
entities,
|
|
|
|
};
|
|
|
|
};
|