react-native-game-engine-ex.../game/systems/GameLoop.ts

35 lines
933 B
TypeScript
Raw Permalink Normal View History

import { GameEngineEntities, GameEntity } from "@types";
import Matter from "matter-js";
import {
GameEngineUpdateEventOptionType,
TouchEvent,
} from "react-native-game-engine";
2022-06-25 00:29:43 -05:00
export const GameLoop = (
entities: GameEngineEntities,
2022-06-25 00:29:43 -05:00
{ touches, time, dispatch }: GameEngineUpdateEventOptionType
) => {
const engine = entities.physics.engine;
2022-06-25 00:29:43 -05:00
touches
.filter((t: TouchEvent) => t.type === "press")
.forEach((t: TouchEvent) => {
const balloonBody = (entities.Balloon as GameEntity).body;
2022-06-26 22:03:36 -05:00
const balloonPos = balloonBody.position as Matter.Vector;
2022-06-26 22:03:36 -05:00
const { pageX, pageY } = t.event;
if (
Math.abs(pageX - balloonPos.x) < 100 &&
Math.abs(pageY - balloonPos.y) < 100
) {
dispatch({
type: "addToScore",
});
Matter.Events.trigger(engine, "removeBalloon");
}
2022-06-25 00:29:43 -05:00
});
Matter.Engine.update(engine, time.delta);
return entities;
};