react-native-game-engine-ex.../game/entities/entities.ts

119 lines
3.0 KiB
TypeScript
Raw Permalink Normal View History

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 ".";
import { GameEngineEntities } from "@types";
2022-06-25 00:29:43 -05:00
export function entities(): GameEngineEntities {
const engine = Matter.Engine.create({
2022-06-25 00:29:43 -05:00
enableSleeping: false,
gravity: { x: 0, y: 1.75 },
2022-06-25 00:29:43 -05:00
} as Matter.IEngineDefinition);
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,
y: 200,
2022-06-25 00:29:43 -05:00
},
{ width: 40, height: 50 }
2022-06-25 00:29:43 -05:00
);
const entities = {
2022-06-25 00:29:43 -05:00
physics: { engine, world },
Balloon: newBalloon(),
// 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 },
{ 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",
{ 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-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 00:29:43 -05:00
Matter.Events.on(
engine,
"collisionStart",
({ 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
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;
// Remove balloon if it hits the floor
2022-06-25 00:29:43 -05:00
Matter.World.remove(world, balloonBody, true);
// Subtract a point from the score
const gameEngine = global.gameEngine!;
2022-06-25 00:29:43 -05:00
gameEngine.dispatch({
type: "subtractFromScore",
2022-06-25 00:29:43 -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;
}
2022-06-25 00:29:43 -05:00
export const useEntities = () => {
return {
2022-06-26 21:45:01 -05:00
entities,
};
};