2022-06-25 00:29:43 -05:00
|
|
|
import { Dimensions } from "react-native";
|
|
|
|
import Matter from "matter-js";
|
|
|
|
|
|
|
|
import { windowHeight, windowWidth } from "@game";
|
|
|
|
import { Balloon, Finger, Wall } from ".";
|
|
|
|
|
|
|
|
export const entities = (restart: boolean = false) => {
|
|
|
|
let engine = Matter.Engine.create(undefined, {
|
|
|
|
enableSleeping: false,
|
2022-06-25 21:47:29 -05:00
|
|
|
gravity: { x: 0, y: 0.000005 },
|
2022-06-25 00:29:43 -05:00
|
|
|
} as Matter.IEngineDefinition);
|
|
|
|
|
|
|
|
let world = engine.world;
|
|
|
|
const topInset = (global as any).topInset; // for notch handling
|
|
|
|
|
|
|
|
const newBalloon = () => {
|
|
|
|
return Balloon(
|
|
|
|
world,
|
|
|
|
"red",
|
|
|
|
{
|
|
|
|
x: Math.random() * (windowWidth - 100) + 50,
|
|
|
|
y: 150,
|
|
|
|
},
|
|
|
|
{ width: 50, height: 50 }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let entities = {
|
|
|
|
physics: { engine, world },
|
|
|
|
Balloon: newBalloon(),
|
|
|
|
LeftWall: Wall(
|
|
|
|
world,
|
|
|
|
"orange",
|
|
|
|
{ x: 0 - 25, y: windowHeight / 2 },
|
|
|
|
{ height: windowHeight, width: 50 }
|
|
|
|
),
|
|
|
|
RightWall: Wall(
|
|
|
|
world,
|
|
|
|
"orange",
|
|
|
|
{ x: windowWidth + 25, y: windowHeight / 2 },
|
|
|
|
{ height: windowHeight, width: 50 }
|
|
|
|
),
|
|
|
|
Ceiling: Wall(
|
|
|
|
world,
|
|
|
|
"orange",
|
|
|
|
{ x: 0, y: 0 },
|
|
|
|
{ height: 110 + topInset, width: windowWidth * 2 }
|
|
|
|
),
|
|
|
|
Floor: Wall(
|
|
|
|
world,
|
|
|
|
"orange",
|
|
|
|
{ x: windowWidth / 2, y: windowHeight + 60 },
|
|
|
|
{ height: 60, width: windowWidth }
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2022-06-25 21:47:29 -05:00
|
|
|
Matter.Events.on(
|
|
|
|
engine,
|
|
|
|
"removeBalloon",
|
|
|
|
({ pairs }: Matter.IEventCollision<any>) => {
|
|
|
|
// pairs.forEach((pair: Matter.IPair) => {
|
|
|
|
// if (pair.bodyA.label === "Balloon" && pair.bodyB.label === "Floor") {
|
|
|
|
// Matter.Events.trigger(engine, "removeBalloon");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Remove old balloon
|
|
|
|
const balloonBody = entities.Balloon.body;
|
|
|
|
Matter.World.remove(world, balloonBody, true);
|
|
|
|
|
|
|
|
// Add new Balloon
|
|
|
|
entities.Balloon = newBalloon();
|
|
|
|
// @ts-ignore
|
|
|
|
Matter.World.add(world, entities.Balloon);
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
|
2022-06-25 00:29:43 -05:00
|
|
|
Matter.Events.on(
|
|
|
|
engine,
|
|
|
|
"collisionStart",
|
|
|
|
({ pairs }: Matter.IEventCollision<any>) => {
|
|
|
|
for (var i = 0, j = pairs.length; i != j; ++i) {
|
|
|
|
const bodyA = pairs[i].bodyA;
|
|
|
|
const bodyB = pairs[i].bodyB;
|
|
|
|
console.log(
|
|
|
|
"collisionStart between " + bodyA.label + " - " + bodyB.label
|
|
|
|
);
|
|
|
|
const balloonBody = entities.Balloon.body;
|
2022-06-25 21:47:29 -05:00
|
|
|
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);
|
|
|
|
|
2022-06-25 21:47:29 -05:00
|
|
|
// Subtract a point from the score
|
2022-06-25 00:29:43 -05:00
|
|
|
const gameEngine = (global as any).gameEngine;
|
|
|
|
gameEngine.dispatch({
|
2022-06-25 21:47:29 -05:00
|
|
|
type: "subtractFromScore",
|
2022-06-25 00:29:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
entities.Balloon = newBalloon();
|
|
|
|
|
2022-06-25 21:47:29 -05:00
|
|
|
// Add new Balloon
|
2022-06-25 00:29:43 -05:00
|
|
|
// @ts-ignore
|
|
|
|
Matter.World.add(world, entities.Balloon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useEntities = () => {
|
|
|
|
return {
|
|
|
|
entities
|
|
|
|
}
|
|
|
|
}
|