-
Notifications
You must be signed in to change notification settings - Fork 0
/
Physics.js
167 lines (131 loc) · 4.76 KB
/
Physics.js
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint-disable no-undef */
/* eslint-disable react-native/no-inline-styles */
/* eslint-disable semi */
/* eslint-disable prettier/prettier */
/* eslint-disable no-unused-vars */
import Matter from "matter-js";
import Pipe from './Pipe';
import PipeTop from './PipeTop';
let tick = 0;
let pose = 1;
let pipes = 0;
export const randomBetween = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min);
}
export const resetPipes = () => {
pipes = 0;
}
export const generatePipes = () => {
let topPipeHeight = randomBetween(100, (Constants.MAX_HEIGHT / 2) - 100);
let bottomPipeHeight = Constants.MAX_HEIGHT - topPipeHeight - Constants.GAP_SIZE;
let sizes = [topPipeHeight, bottomPipeHeight]
if (Math.random() < 0.5) {
sizes = sizes.reverse();
}
return sizes;
}
export const addPipesAtLocation = (x, world, entities) => {
let [pipe1Height, pipe2Height] = generatePipes();
let pipeTopWidth = Constants.PIPE_WIDTH + 20;
let pipeTopHeight = (pipeTopWidth / 205) * 95;
pipe1Height = pipe1Height - pipeTopHeight;
let pipe1Top = Matter.Bodies.rectangle(
x,
pipe1Height + (pipeTopHeight / 2),
pipeTopWidth,
pipeTopHeight,
{ isStatic: true}
);
let pipe1 = Matter.Bodies.rectangle(
x,
pipe1Height / 2,
Constants.PIPE_WIDTH,
pipe1Height,
{ isStatic: true}
);
pipe2Height = pipe2Height - pipeTopHeight;
let pipe2Top = Matter.Bodies.rectangle(
x,
Constants.MAX_HEIGHT - 50 - pipe2Height - (pipeTopHeight / 2),
pipeTopWidth,
pipeTopHeight,
{ isStatic: true}
);
let pipe2 = Matter.Bodies.rectangle(
x,
Constants.MAX_HEIGHT - 50 - (pipe2Height / 2),
Constants.PIPE_WIDTH,
pipe2Height,
{ isStatic: true}
);
Matter.World.add(world, [pipe1, pipe1Top, pipe2, pipe2Top]);
entities["pipe" + (pipes + 1)] = {
body: pipe1, renderer: Pipe, scored: false
}
entities["pipe" + (pipes + 2)] = {
body: pipe2, renderer: Pipe, scored: false
}
entities["pipe" + (pipes + 1) + "Top"] = {
body: pipe1Top, renderer: PipeTop, scored: false
}
entities["pipe" + (pipes + 2) + "Top"] = {
body: pipe2Top, renderer: PipeTop, scored: false
}
pipes += 2;
}
const Physics = (entities, { touches, time, dispatch }) => {
let engine = entities.physics.engine;
let world = entities.physics.world;
let bird = entities.bird.body;
let hadTouches = false;
touches.filter(t => t.type === "press").forEach(t => {
if (!hadTouches){
if (world.gravity.y === 0.0){
world.gravity.y = 1.2;
addPipesAtLocation((Constants.MAX_WIDTH * 2) - (Constants.PIPE_WIDTH / 2), world, entities);
addPipesAtLocation((Constants.MAX_WIDTH * 3) - (Constants.PIPE_WIDTH / 2), world, entities);
}
hadTouches = true;
Matter.Body.setVelocity( bird, {
x: bird.velocity.x,
y: -10
});
}
});
Matter.Engine.update(engine, time.delta);
Object.keys(entities).forEach(key => {
if (key.indexOf("pipe") === 0 && entities.hasOwnProperty(key)){
Matter.Body.translate(entities[key].body, {x: -2, y: 0});
if (key.indexOf("Top") !== -1 && parseInt(key.replace("pipe", "")) % 2 === 0){
if (entities[key].body.position.x <= bird.position.x && !entities[key].scored){
entities[key].scored = true;
dispatch({ type: "score" });
}
if (entities[key].body.position.x <= -1 * (Constants.PIPE_WIDTH / 2)){
let pipeIndex = parseInt(key.replace("pipe", ""));
delete(entities["pipe" + (pipeIndex - 1) + "Top"]);
delete(entities["pipe" + (pipeIndex - 1)]);
delete(entities["pipe" + pipeIndex + "Top"]);
delete(entities["pipe" + pipeIndex]);
addPipesAtLocation((Constants.MAX_WIDTH * 2) - (Constants.PIPE_WIDTH / 2), world, entities);
}
}
} else if (key.indexOf("floor") === 0){
if (entities[key].body.position.x <= -1 * Constants.MAX_WIDTH / 2){
Matter.Body.setPosition(entities[key].body, { x: Constants.MAX_WIDTH + (Constants.MAX_WIDTH / 2), y: entities[key].body.position.y})
} else {
Matter.Body.translate(entities[key].body, {x: -2, y: 0});
}
}
})
tick += 1;
if (tick % 5 === 0){
pose = pose + 1;
if (pose > 3){
pose = 1;
}
entities.bird.pose = pose;
}
return entities;
};
export default Physics;