diff --git a/AppView.tsx b/AppView.tsx index bb1850c..18c51f9 100644 --- a/AppView.tsx +++ b/AppView.tsx @@ -42,13 +42,33 @@ export default function AppView() { setScore(score => score + 1); break; } + case "subtractFromScore": { + setScore(score => score - 1); + break; + } } }} style={{ position: "absolute", top: 0, left: 0, right: 0, bottom: 0 }} > { - gameEngine?.swap(entities()); + // gameEngine?.swap(entities()); + if (isRunning) { + gameEngine?.stop(); + setIsRunning(false); + } else { + gameEngine?.start(); + setIsRunning(true); + } + }} + style={{ + position: "absolute", + top: 0, + left: 0, + height: 55, + width: "100%", + borderColor: "red", + borderWidth: 1, }} > - {score} + {isRunning ? score : "Press to Resume"} diff --git a/game/entities/Balloon.tsx b/game/entities/Balloon.tsx index a4be021..727d6ae 100644 --- a/game/entities/Balloon.tsx +++ b/game/entities/Balloon.tsx @@ -38,7 +38,7 @@ const Balloon = ({ body, color }: any) => { isStatic: false, // restitution: 0.4, // friction: 1, - frictionAir: 0.2, + frictionAir: 0.5, // mass: 0.1, // inverseMass: 0.1, // bounds: { diff --git a/game/entities/entities.ts b/game/entities/entities.ts index 7e26ab4..4197e42 100644 --- a/game/entities/entities.ts +++ b/game/entities/entities.ts @@ -7,7 +7,7 @@ import { Balloon, Finger, Wall } from "."; export const entities = (restart: boolean = false) => { let engine = Matter.Engine.create(undefined, { enableSleeping: false, - gravity: { x: 0, y: 0.0005 }, + gravity: { x: 0, y: 0.000005 }, } as Matter.IEngineDefinition); let world = engine.world; @@ -27,13 +27,13 @@ export const entities = (restart: boolean = false) => { let entities = { physics: { engine, world }, - fingers: { - 1: { position: [40, 200], renderer: Finger }, - 2: { position: [100, 200], renderer: Finger }, - 3: { position: [160, 200], renderer: Finger }, - 4: { position: [220, 200], renderer: Finger }, - 5: { position: [280, 200], renderer: Finger }, - }, + // fingers: { + // 1: { position: [40, 200], renderer: Finger }, + // 2: { position: [100, 200], renderer: Finger }, + // 3: { position: [160, 200], renderer: Finger }, + // 4: { position: [220, 200], renderer: Finger }, + // 5: { position: [280, 200], renderer: Finger }, + // }, Balloon: newBalloon(), LeftWall: Wall( world, @@ -61,6 +61,26 @@ export const entities = (restart: boolean = false) => { ), }; + Matter.Events.on( + engine, + "removeBalloon", + ({ pairs }: Matter.IEventCollision) => { + // 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); + // }); + }); + Matter.Events.on( engine, "collisionStart", @@ -72,19 +92,20 @@ export const entities = (restart: boolean = false) => { "collisionStart between " + bodyA.label + " - " + bodyB.label ); const balloonBody = entities.Balloon.body; - Matter.Body.scale(balloonBody, 0.0, 0.0, { - x: balloonBody.bounds.min.x - 1000, - y: balloonBody.bounds.max.y, - }); + const floorBody = entities.Floor.body; + + // Remove balloon if it hits the floor Matter.World.remove(world, balloonBody, true); + // Subtract a point from the score const gameEngine = (global as any).gameEngine; gameEngine.dispatch({ - type: "addToScore", + type: "subtractFromScore", }); entities.Balloon = newBalloon(); + // Add new Balloon // @ts-ignore Matter.World.add(world, entities.Balloon); } diff --git a/game/systems/GameLoop.ts b/game/systems/GameLoop.ts index 491077c..dbeee2b 100644 --- a/game/systems/GameLoop.ts +++ b/game/systems/GameLoop.ts @@ -1,7 +1,11 @@ import Matter, { Vector } from "matter-js"; -import { GameEngineUpdateEventOptionType, TouchEvent } from "react-native-game-engine"; +import { + GameEngineUpdateEventOptionType, + TouchEvent, +} from "react-native-game-engine"; import { windowHeight, windowWidth } from "@game"; +import { Balloon } from "@entities"; export const GameLoop = ( entities: any, @@ -13,9 +17,15 @@ export const GameLoop = ( touches .filter((t: TouchEvent) => t.type === "press") .forEach((t: TouchEvent) => { - let balloonPos = entities.Balloon.body.position; - console.log('Touch:', t, balloonPos); - // Matter.Body.setVelocity(something, { x: something.velocity.x + 20, y: something.velocity.y - 20 }); + const balloonBody = entities.Balloon.body; + const balloonPos = balloonBody.position; + const { locationX, locationY } = t.event; + if (locationX < 50 && locationY < 50) { + dispatch({ + type: "addToScore", + }); + Matter.Events.trigger(engine, "removeBalloon"); + } }); Matter.Engine.update(engine, time.delta);