2024-01-09 02:26:26 -06:00
|
|
|
import { GameEngineEntities, GameEntity } from "@types";
|
|
|
|
import Matter 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
|
|
|
|
|
|
|
export const GameLoop = (
|
2024-01-09 02:26:26 -06:00
|
|
|
entities: GameEngineEntities,
|
2022-06-25 00:29:43 -05:00
|
|
|
{ touches, time, dispatch }: GameEngineUpdateEventOptionType
|
|
|
|
) => {
|
2024-01-09 02:26:26 -06:00
|
|
|
const engine = entities.physics.engine;
|
2022-06-25 00:29:43 -05:00
|
|
|
|
|
|
|
touches
|
|
|
|
.filter((t: TouchEvent) => t.type === "press")
|
|
|
|
.forEach((t: TouchEvent) => {
|
2024-01-09 02:26:26 -06:00
|
|
|
const balloonBody = (entities.Balloon as GameEntity).body;
|
2022-06-26 22:03:36 -05:00
|
|
|
const balloonPos = balloonBody.position as Matter.Vector;
|
2024-01-09 02:26:26 -06:00
|
|
|
|
2022-06-26 22:03:36 -05:00
|
|
|
const { pageX, pageY } = t.event;
|
2024-01-09 02:26:26 -06:00
|
|
|
if (
|
|
|
|
Math.abs(pageX - balloonPos.x) < 100 &&
|
|
|
|
Math.abs(pageY - balloonPos.y) < 100
|
|
|
|
) {
|
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;
|
|
|
|
};
|