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

This commit is contained in:
Josh Guyette 2022-06-25 21:47:29 -05:00
parent 55fdfcb7d7
commit e66166c30c
4 changed files with 71 additions and 20 deletions

View File

@ -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 }}
>
<TouchableOpacity
onPress={() => {
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,
}}
>
<Text
@ -59,7 +79,7 @@ export default function AppView() {
marginTop: topInset / 2,
}}
>
{score}
{isRunning ? score : "Press to Resume"}
</Text>
</TouchableOpacity>
</GameEngine>

View File

@ -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: {

View File

@ -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<any>) => {
// 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);
}

View File

@ -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);