-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathphysics.js
More file actions
50 lines (35 loc) · 1.57 KB
/
physics.js
File metadata and controls
50 lines (35 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import Matter from "matter-js";
import { getPipeSizePosPair } from "./utils/random";
import { Dimensions } from 'react-native'
const windowHeight = Dimensions.get('window').height
const windowWidth = Dimensions.get('window').width
const Physics = (entities, { touches, time, dispatch }) => {
let engine = entities.physics.engine
touches.filter(t => t.type === 'press')
.forEach(t => {
Matter.Body.setVelocity(entities.Bird.body, {
x: 0,
y: -8
})
})
Matter.Engine.update(engine, time.delta)
for (let index = 1; index <= 2; index++) {
if (entities[`ObstacleTop${index}`].body.bounds.max.x <= 50 && !entities[`ObstacleTop${index}`].point) {
entities[`ObstacleTop${index}`].point = true
dispatch({ type: 'new_point' })
}
if (entities[`ObstacleTop${index}`].body.bounds.max.x <= 0) {
const pipeSizePos = getPipeSizePosPair(windowWidth * 0.9);
Matter.Body.setPosition(entities[`ObstacleTop${index}`].body, pipeSizePos.pipeTop.pos)
Matter.Body.setPosition(entities[`ObstacleBottom${index}`].body, pipeSizePos.pipeBottom.pos)
entities[`ObstacleTop${index}`].point = false
}
Matter.Body.translate(entities[`ObstacleTop${index}`].body, { x: -3, y: 0 })
Matter.Body.translate(entities[`ObstacleBottom${index}`].body, { x: -3, y: 0 })
}
Matter.Events.on(engine, 'collisionStart', (event) => {
dispatch({ type: 'game_over' })
})
return entities;
}
export default Physics