From e66166c30cbccadd5c27a31eb28b2a4f8ea41cce Mon Sep 17 00:00:00 2001 From: Josh Guyette Date: Sat, 25 Jun 2022 21:47:29 -0500 Subject: [PATCH] can touch balloons now, to 'pop them' amd add a new one on top. Also if a balloon hits the floor, you lose a point. You can also pause the game by tapping on the score --- AppView.tsx | 24 ++++++++++++++++++-- game/entities/Balloon.tsx | 2 +- game/entities/entities.ts | 47 ++++++++++++++++++++++++++++----------- game/systems/GameLoop.ts | 18 +++++++++++---- 4 files changed, 71 insertions(+), 20 deletions(-) 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);