-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
292 lines (212 loc) · 8.34 KB
/
App.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import React, { PureComponent } from 'react'
import { Dimensions, Alert } from 'react-native'
import GameView from './src/components/game-view'
import options from './src/config'
const { width, height } = Dimensions.get('window')
export default class App extends PureComponent {
state = {
winner: 0, // 0: nessuno, 1: giocatore, 2: computer
speed: options.startingGameSpeed,
playerXPosition: 0,
lives: options.numberOfLives,
aliens: [],
direction: 1,
down: false,
rockets: [],
explosion: [],
score: 0,
highest: 0
}
componentDidMount() {
this.initGame()
}
componentDidUpdate(prevProps, prevState) {
const { winner, aliens } = this.state
// Un alieno in meno, aumentare la velocità
// Non deve essere chiamato all'init (quando prima c'era un vincitore) o non resetta la velocità!
// Non va chiamato neanche all'ultimo alieno, non serve e rimane un interval attivo
if (!prevState.winner && prevState.aliens.length > 1 && prevState.aliens.length !== aliens.length) {
this.increaseSpeed(prevState.speed)
}
// C'è un vincitore (e solo se prima non ce n'erano altri)
if (!prevState.winner && winner) {
winner === 1 ? this.victory() : this.gameOver()
}
}
initGame() {
const { winner } = this.state
if (this.delay) clearTimeout(this.delay)
if (winner) this.reinitState()
this.generateAliens()
this.gameLoop = setInterval(() => this.renderFrame(), this.state.speed)
}
reinitState() {
const common = {
winner: 0,
speed: options.startingGameSpeed,
direction: 1,
down: false
}
this.setState(this.state.winner === 1 ? { ...common } : { ...common, lives: options.numberOfLives, aliens: [], score: 0 })
}
exit() {
console.log('Exit')
}
victory() {
clearInterval(this.gameLoop)
this.delay = setTimeout(() => this.initGame(), 500)
}
gameOver() {
const { score, highest, playerXPosition } = this.state
clearInterval(this.gameLoop)
this.setState({ highest: Math.max(score, highest), explosion: [playerXPosition, 0] })
Alert.alert(
'GAME OVER',
'Gli alieni hanno vinto!',
[
{ text: 'Esci', onPress: () => this.exit(), style: 'cancel' },
{ text: 'Nuova partita', onPress: () => this.initGame() }
],
{ cancelable: false })
}
increaseSpeed(startingSpeed) {
clearInterval(this.gameLoop)
// Velocità aumenta di x% rispetto al suo ultimo valore
// newSpeed diventa una stringa, crash su Android, ok su IOS (?)
const newSpeed = +(startingSpeed - (startingSpeed * options.speedMultiplier)).toFixed(0)
this.setState({ speed: newSpeed }, () => {
this.gameLoop = setInterval(() => this.renderFrame(), newSpeed)
})
}
// Per clonare array composti da oggetti [{...}, {...}, {...}]
cloneState = source => source.map(el => Object.assign({}, el))
generateAliens() {
const aliens = []
const alienHorSpace = options.alienSize + options.aliensHorDistance
const alienVerSpace = options.alienSize + options.aliensVerDistance
const offsetForCentering = options.aliensHorDistance / 2
const xOffset = offsetForCentering + (width - alienHorSpace * Math.max(...options.aliensInit)) / 2 // Per centrare gli alieni
const yOffset = alienVerSpace + (alienVerSpace * 0.4)
options.aliensInit.map((el, ind) => {
for (let i = 0; i < el; i++) {
const type = ind + 1
const num = i + 1
// Un alieno è { id: 't1n1', t: 1, x: 120, y: 40 }
aliens.push(
{
id: `t${type}n${num}`,
t: type,
x: xOffset + (alienHorSpace * i),
y: height - (alienVerSpace * (ind + 1)) - yOffset
}
)
}
})
aliens.reverse() // I primi nell'array devono essere quelli più vicini al basso
this.setState({ aliens })
}
moveAliens(dX, dY) {
const { aliens, direction, playerXPosition } = this.state
// Con alieni su più file, l'inversione al limite è ripetuta per ogni fila, causando un bug sulla direzione, che viene invertita più volte
// Il controllo è effettuato quindi solo una volta, se bisogna invertire inutile controllare ancora
let inversionTrue = false
// Resetta lo stato down, di default gli alieni non devono scendere
this.setState({ down: false })
const clonedAliens = this.cloneState(aliens)
clonedAliens.forEach(el => {
el.x += dX
el.y -= dY
if (inversionTrue) return // Bisogna invertire? Fermarsi, controllare altri alieni non serve
// Fuori dallo schermo? Invertire! Controllo solo se gli alieni vanno nella stessa direzione del limite
// per evitare un bug in renderFrame quando si muovono solo verso il basso
if (direction === 1 && el.x + (options.alienSize + 16) > width || direction === -1 && el.x < 16) {
this.setState(prevState => ({ direction: prevState.direction *= -1, down: true }))
inversionTrue = true
}
if (el.y <= options.cannonSize) this.setState({ winner: 2, explosion: [playerXPosition, 0] })
})
return clonedAliens
}
renderFrame() {
const { direction, down } = this.state
const dX = down ? 0 : options.aliensHorStep * direction
const dY = down ? options.aliensVerStep : 0
const aliens = this.moveAliens(dX, dY)
const doesShoot = Math.random() < options.shootingProbability
if (doesShoot) {
const randomAlien = aliens[Math.floor(Math.random() * aliens.length)]
this.fire({ x: randomAlien.x, y: randomAlien.y }, 2)
}
// Ritorna un nuovo array degli alieni, come FRAME FINALE
this.setState({ aliens })
}
removeAlien = id => {
const { aliens } = this.state
const clonedAliens = this.cloneState(aliens)
const killedAlienInd = clonedAliens.findIndex(el => el.id === id)
const killedAlienType = clonedAliens[killedAlienInd].t
// Trova gli alieni successivi nella stessa fila (stesso type di quello morto e index inferiore perché l'array è invertito)
const nextAliens = clonedAliens.filter((el, ind) => el.id !== id && el.t === killedAlienType && ind < killedAlienInd)
// Sposta la fila di alieni successivi di un offset pari allo spazio occupato da un alieno
nextAliens.forEach(el => el.x + options.aliensHorSpace)
const commonState = {
aliens: clonedAliens,
// Coordinate per renderizzare l'esplosione
explosion: [clonedAliens[killedAlienInd].x, clonedAliens[killedAlienInd].y]
}
clonedAliens.splice(killedAlienInd, 1)
this.setState(clonedAliens.length === 0 ? { ...commonState, winner: 1 } : commonState)
}
fire = (launchPos, player = 1) => {
// Razzi diversi per giocatore, serve sapere chi ha sparato
const { rockets: stateRockets } = this.state
if (stateRockets.length === options.maxRocketsOnScreen) return
const id = Math.random().toString(36).substring(2, 8)
const rockets = [
...stateRockets,
{ id, player, ...launchPos }
]
this.setState({ rockets })
}
removeRocket = id => {
const { rockets: stateRockets } = this.state
const rockets = this.cloneState(stateRockets)
const rocketInd = rockets.findIndex(el => el.id === id)
rockets.splice(rocketInd, 1)
this.setState({ rockets })
}
updateScore = () => this.setState(prevState => ({ score: prevState.score + 1 }))
updatePlayerPosition = value => this.setState({ playerXPosition: value })
updateLives = () => {
this.setState(prevState => {
const lives = prevState.lives - 1
return lives === 0 ? { lives, winner: 2 } : { lives }
})
}
clearExplosion = () => this.setState({ explosion: [] })
render() {
const { score, highest, rockets, aliens, explosion, playerXPosition, lives, winner } = this.state
return (
<GameView
width={width} // screen width
height={height} // screen height
score={score}
updateScore={this.updateScore}
highest={highest}
fire={this.fire}
rockets={rockets}
removeRocket={this.removeRocket}
aliens={aliens}
removeAlien={this.removeAlien}
explosion={explosion}
clearExplosion={this.clearExplosion}
playerXPosition={playerXPosition}
updatePlayerPosition={this.updatePlayerPosition}
lives={lives}
updateLives={this.updateLives}
winner={winner}
exit={this.exit}
/>
)
}
}