2022-06-25 00:29:43 -05:00
|
|
|
import Matter, { Vector } from "matter-js";
|
2022-06-25 21:47:29 -05:00
|
|
|
import {
|
|
|
|
GameEngineUpdateEventOptionType,
|
|
|
|
TouchEvent,
|
|
|
|
} from "react-native-game-engine";
|
2022-06-25 00:29:43 -05:00
|
|
|
|
|
|
|
import { windowHeight, windowWidth } from "@game";
|
|
|
|
|
|
|
|
export const GameLoop = (
|
|
|
|
entities: any,
|
|
|
|
{ touches, time, dispatch }: GameEngineUpdateEventOptionType
|
|
|
|
) => {
|
|
|
|
let engine = entities.physics.engine;
|
|
|
|
let world = entities.physics.world;
|
|
|
|
|
|
|
|
touches
|
|
|
|
.filter((t: TouchEvent) => t.type === "press")
|
|
|
|
.forEach((t: TouchEvent) => {
|
2022-06-26 22:03:36 -05:00
|
|
|
const balloonBody = entities.Balloon.body as Matter.IBodyDefinition;
|
|
|
|
const balloonPos = balloonBody.position as Matter.Vector;
|
|
|
|
const { pageX, pageY } = t.event;
|
|
|
|
// if (locationX < 50 && locationY < 50) { // for some reason this works, but the line below is more readable.
|
|
|
|
if (Math.abs(pageX - balloonPos.x) < 50 && Math.abs(pageY - balloonPos.y) < 50) {
|
2022-06-25 21:47:29 -05:00
|
|
|
dispatch({
|
|
|
|
type: "addToScore",
|
|
|
|
});
|
|
|
|
Matter.Events.trigger(engine, "removeBalloon");
|
|
|
|
}
|
2022-06-25 00:29:43 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
Matter.Engine.update(engine, time.delta);
|
|
|
|
return entities;
|
|
|
|
};
|