Skip to content

Commit d389178

Browse files
GianlucaGianluca
Gianluca
authored and
Gianluca
committed
Initial commit
0 parents  commit d389178

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+7731
-0
lines changed

Diff for: .expo-shared/assets.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"f9155ac790fd02fadcdeca367b02581c04a353aa6d5aa84409a59f6804c87acd": true,
3+
"89ed26367cdb9b771858e026f2eb95bfdb90e5ae943e716575327ec325f39c44": true
4+
}

Diff for: .gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
node_modules/**/*
2+
.expo/*
3+
npm-debug.*
4+
*.jks
5+
*.p8
6+
*.p12
7+
*.key
8+
*.mobileprovision
9+
*.orig.*
10+
web-build/
11+
web-report/

Diff for: .watchmanconfig

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Diff for: App.js

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
import React, { PureComponent } from 'react'
2+
import { Dimensions } from 'react-native'
3+
import GameView from './src/components/game-view'
4+
import options from './src/config'
5+
6+
const { width, height } = Dimensions.get('window')
7+
8+
export default class App extends PureComponent {
9+
state = {
10+
winner: 0, // 0: nessuno, 1: giocatore, 2: computer
11+
speed: options.defaultSpeed,
12+
score: 0,
13+
highest: 0,
14+
fire: false,
15+
source: {},
16+
aliens: [],
17+
explosion: [],
18+
direction: 1,
19+
down: false
20+
}
21+
22+
23+
componentDidMount() {
24+
this.initGame()
25+
}
26+
27+
28+
componentDidUpdate(prevProps, prevState) {
29+
const { winner, aliens } = this.state
30+
31+
// Un alieno in meno, aumentare la velocità
32+
if (prevState.aliens.length !== 0 && prevState.aliens.length !== aliens.length) {
33+
clearInterval(this.run)
34+
// Velocità aumenta di x% rispetto al suo ultimo valore
35+
const newSpeed = (prevState.speed - (prevState.speed * options.speedMultiplier)).toFixed(0)
36+
this.setState({ speed: newSpeed }, () => this.run = setInterval(() => this.renderFrame(), newSpeed))
37+
}
38+
39+
// C'è un vincitore
40+
if (winner && prevState.winner !== winner) {
41+
winner === 1 ? this.victory() : this.gameOver()
42+
}
43+
44+
}
45+
46+
47+
initGame() {
48+
this.generateAliens()
49+
this.run = setInterval(() => this.renderFrame(), this.state.speed)
50+
//this.intervalTimeout = setTimeout(() => this.dynamicInterval(), this.state.speed)
51+
}
52+
53+
54+
dynamicInterval() {
55+
const { speed, winner } = this.state
56+
57+
if (winner) return
58+
if (this.intervalTimeout) clearTimeout(this.intervalTimeout)
59+
60+
this.renderFrame()
61+
this.intervalTimeout = setTimeout(() => this.dynamicInterval(), speed)
62+
}
63+
64+
65+
victory() {
66+
alert('HAI VINTO')
67+
//clearTimeout(this.intervalTimeout)
68+
clearInterval(this.run)
69+
}
70+
71+
72+
gameOver() {
73+
const { score, highest } = this.state
74+
//clearTimeout(this.intervalTimeout)
75+
clearInterval(this.run)
76+
alert('Game over')
77+
this.setState({ highest: Math.max(score, highest) })
78+
}
79+
80+
81+
cloneAliens(source) {
82+
const destination = []
83+
source.map(el => {
84+
//destination.push({ ...el })
85+
destination.push(Object.assign({}, el))
86+
})
87+
return destination
88+
}
89+
90+
91+
generateAliens() {
92+
const aliens = []
93+
94+
const xOffset = (width - options.aliensHorSpace * 5) / 2 // per centrare gli alieni
95+
const yOffset = 80
96+
97+
options.aliensInit.map((el, ind) => {
98+
for (let i = 0; i < el; i++) {
99+
const type = ind + 1
100+
const num = i + 1
101+
// Un alieno è { id: 't1n1', t: 1, x: 120, y: 40, inactive: false }
102+
aliens.push(
103+
{
104+
id: `t${type}n${num}`,
105+
t: type,
106+
x: xOffset + (options.aliensHorSpace * i),
107+
y: height - (options.aliensVerSpace * (ind + 1)) - yOffset,
108+
inactive: false
109+
}
110+
)
111+
}
112+
})
113+
aliens.reverse() // I primi nell'array devono essere quelli più vicini al basso
114+
115+
this.setState({ aliens })
116+
}
117+
118+
119+
moveAliens(dX, dY) {
120+
const { aliens, direction } = this.state
121+
122+
// Con alieni su più file, l'inversione al limite è ripetuta per ogni fila, causando un bug sulla direzione, che viene invertita più volte
123+
// Il controllo è effettuato quindi solo una volta, se bisogna invertire inutile controllare ancora
124+
let inversionTrue = false
125+
126+
// Resetta lo stato down, di default gli alieni non devono scendere
127+
this.setState({ down: false })
128+
129+
const clonedAliens = this.cloneAliens(aliens)
130+
131+
clonedAliens.forEach(el => {
132+
el.x += dX
133+
el.y -= dY
134+
135+
if (inversionTrue) return // Bisogna invertire? Fermarsi, controllare altri alieni non serve
136+
137+
// Fuori dallo schermo? Invertire! Controllo solo se gli alieni vanno nella stessa direzione del limite
138+
// per evitare un bug in renderFrame quando si muovono solo verso il basso
139+
if (direction === 1 && el.x + 60 > width || direction === -1 && el.x < 10) {
140+
this.setState(prevState => ({ direction: prevState.direction *= -1, down: true }))
141+
inversionTrue = true
142+
}
143+
144+
if (el.y <= 50) this.setState({ winner: 2 })
145+
})
146+
147+
return clonedAliens
148+
}
149+
150+
151+
renderFrame() {
152+
const { direction, down } = this.state
153+
const dX = down ? 0 : options.aliensHorStep * direction
154+
const dY = down ? options.aliensVerStep : 0
155+
156+
const aliens = this.moveAliens(dX, dY)
157+
158+
// Ritorna un nuovo array degli alieni, come FRAME FINALE
159+
this.setState({ aliens })
160+
}
161+
162+
163+
updateScore = () => this.setState(prevState => ({ score: prevState.score + 1 }))
164+
165+
166+
removeAlien = id => {
167+
const { aliens } = this.state
168+
169+
const clonedAliens = this.cloneAliens(aliens)
170+
171+
const killedAlienInd = clonedAliens.findIndex(el => el.id === id)
172+
const killedAlienType = clonedAliens[killedAlienInd].t
173+
174+
// Trova gli alieni successivi nella stessa fila (stesso type di quello morto e index inferiore perché l'array è invertito)
175+
const nextAliens = clonedAliens.filter((el, ind) => el.id !== id && el.t === killedAlienType && ind < killedAlienInd)
176+
177+
// Sposta la fila di alieni successivi di un offset pari allo spazio occupato da un alieno
178+
nextAliens.forEach(el => el.x + options.aliensHorSpace)
179+
180+
const commonState = {
181+
aliens: clonedAliens,
182+
// Coordinate per renderizzare l'esplosione
183+
explosion: [clonedAliens[killedAlienInd].x, clonedAliens[killedAlienInd].y]
184+
}
185+
186+
clonedAliens.splice(killedAlienInd, 1)
187+
188+
this.setState(clonedAliens.length === 0 ? { ...commonState, winner: 1 } : commonState)
189+
}
190+
191+
192+
fire = source => this.setState({ fire: true, source })
193+
194+
195+
animationEnded = () => this.setState({ fire: false })
196+
197+
198+
clearExplosion = () => this.setState({ explosion: [] })
199+
200+
render() {
201+
const { score, highest, source, fire, aliens, explosion } = this.state
202+
203+
return (
204+
<GameView
205+
width={width} // screen width
206+
height={height} // screen height
207+
score={score}
208+
highest={highest}
209+
fire={this.fire}
210+
aliens={aliens}
211+
fireStatus={fire}
212+
source={source}
213+
animationEnded={this.animationEnded}
214+
updateScore={this.updateScore}
215+
removeAlien={this.removeAlien}
216+
explosion={explosion}
217+
clearExplosion={this.clearExplosion}
218+
/>
219+
)
220+
}
221+
}

Diff for: app.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"expo": {
3+
"name": "Space Invaders",
4+
"slug": "SpaceInvaders",
5+
"privacy": "public",
6+
"sdkVersion": "34.0.0",
7+
"platforms": [
8+
"ios",
9+
"android",
10+
"web"
11+
],
12+
"version": "1.0.0",
13+
"orientation": "portrait",
14+
"icon": "./assets/icon.png",
15+
"splash": {
16+
"image": "./assets/splash.png",
17+
"resizeMode": "contain",
18+
"backgroundColor": "#ffffff"
19+
},
20+
"updates": {
21+
"fallbackToCacheTimeout": 0
22+
},
23+
"assetBundlePatterns": [
24+
"**/*"
25+
],
26+
"ios": {
27+
"supportsTablet": true
28+
}
29+
}
30+
}

Diff for: assets/512/alien1_1.png

2.15 KB
Loading

Diff for: assets/512/alien1_2.png

2.14 KB
Loading

Diff for: assets/512/alien2_1.png

2.03 KB
Loading

Diff for: assets/512/alien2_2.png

2 KB
Loading

Diff for: assets/512/alien3_1.png

1.9 KB
Loading

Diff for: assets/512/alien3_2.png

1.87 KB
Loading

Diff for: assets/512/cannon.png

1.92 KB
Loading

Diff for: assets/512/explosion1.png

2.17 KB
Loading

Diff for: assets/512/explosion2.png

2.07 KB
Loading

Diff for: assets/alien1_1.png

1.3 KB
Loading

Diff for: assets/alien1_2.png

1.29 KB
Loading

Diff for: assets/alien2_1.png

1.41 KB
Loading

Diff for: assets/alien2_2.png

1.41 KB
Loading

Diff for: assets/alien3_1.png

1.38 KB
Loading

Diff for: assets/alien3_2.png

1.37 KB
Loading

Diff for: assets/cannon.png

1.25 KB
Loading

Diff for: assets/explosion1.png

1.66 KB
Loading

Diff for: assets/explosion2.png

4.47 KB
Loading

Diff for: assets/icon.png

1.07 KB
Loading

Diff for: assets/splash.png

7.01 KB
Loading

Diff for: babel.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = function(api) {
2+
api.cache(true);
3+
return {
4+
presets: ['babel-preset-expo'],
5+
};
6+
};

0 commit comments

Comments
 (0)