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

114 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-06-25 00:29:43 -05:00
import { Dimensions } from "react-native";
import Matter from "matter-js";
import { windowHeight, windowWidth } from "@game";
2022-06-26 21:45:01 -05:00
import { Balloon, Wall } from ".";
2022-06-25 00:29:43 -05:00
export const entities = (restart: boolean = false) => {
let engine = Matter.Engine.create(undefined, {
enableSleeping: false,
2022-06-26 00:29:24 -05:00
gravity: { x: 0, y: 0.001 },
2022-06-25 00:29:43 -05:00
} as Matter.IEngineDefinition);
let world = engine.world;
const topInset = (global as any).topInset; // for notch handling
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: 150,
},
{ width: 50, height: 50 }
);
let entities = {
physics: { engine, world },
Balloon: newBalloon(),
LeftWall: Wall(
2022-06-26 21:45:01 -05:00
"LeftWall",
2022-06-25 00:29:43 -05:00
world,
"orange",
{ x: 0 - 25, y: windowHeight / 2 },
{ height: windowHeight, width: 50 }
),
RightWall: Wall(
2022-06-26 21:45:01 -05:00
"RightWall",
2022-06-25 00:29:43 -05:00
world,
"orange",
{ x: windowWidth + 25, y: windowHeight / 2 },
{ height: windowHeight, width: 50 }
),
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 + topInset, width: windowWidth * 2 }
),
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 + 60 },
{ height: 60, width: windowWidth }
),
};
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",
2022-06-26 21:45:01 -05:00
({ pairs, name, source, timestamp }: Matter.IEventCollision<any>) => {
2022-06-25 00:29:43 -05:00
for (var i = 0, j = pairs.length; i != j; ++i) {
const bodyA = pairs[i].bodyA;
const bodyB = pairs[i].bodyB;
2022-06-26 21:45:01 -05:00
console.log(bodyA.label, bodyB.label);
// We only want collisions between the balloon and the floor
if ((bodyA.label !== "Balloon" && bodyB.label !== "Balloon") || (bodyA.label !== "Floor" && bodyB.label !== "Floor")) {
continue;
}
2022-06-25 00:29:43 -05:00
const balloonBody = entities.Balloon.body;
const floorBody = entities.Floor.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
2022-06-25 00:29:43 -05:00
const gameEngine = (global as any).gameEngine;
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;
};
export const useEntities = () => {
return {
2022-06-26 21:45:01 -05:00
entities,
};
};