This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
114 lines (95 loc) · 2.73 KB
/
main.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
import { AlienGroup } from './scripts/enemy.js'
import GameObject from './scripts/engine.js'
import Player from './scripts/player.js'
import config from './config.js'
const pressedKeys = {}
const levelLogger = document.getElementById('level')
const player = new Player()
window.onkeydown = (e) => {
if (e.repeat) return
pressedKeys[e.key] = true
GameObject.currentObjects.forEach((obj) => {
obj.onkeydown(e, pressedKeys)
})
}
window.onkeyup = (e) => {
pressedKeys[e.key] = false
GameObject.currentObjects.forEach((obj) => {
obj.onkeyup(e, pressedKeys)
})
}
const alienGroupBaseSpeed = 300
var currentLevel = 1
var direction = !!Math.round(Math.random())
var alienGoups = [new AlienGroup(alienGroupBaseSpeed, !direction)]
var nextLevel = true
var lastRender = 0
/**
* @param {number} timestamp animattion frame tiks between frames
*/
function loop(timestamp) {
let deltaTime = (timestamp - lastRender) / 1000
update(deltaTime)
draw()
if (player.gameover) {
gameover()
}
for (let i = 0; i < alienGoups.length; i++) {
const group = alienGoups[i];
if (group.aliens.length > 0) {
nextLevel = false
break
}
}
if (nextLevel) {
currentLevel++
let nGroups = alienGoups.length + 1
alienGoups.forEach((group) => {
group.destroy()
})
alienGoups = []
if (currentLevel <= config.gioco.ultimolivello) {
for (let i = 0; i < nGroups; i++) {
alienGoups.push(new AlienGroup(alienGroupBaseSpeed + (100 * i), direction))
direction = !direction
}
player.upgrade()
levelLogger.innerText = `LIVELLO ${currentLevel}/${config.gioco.ultimolivello}`
}
else {
win()
}
}
nextLevel = true
lastRender = timestamp
window.requestAnimationFrame(loop)
}
function win() {
levelLogger.innerText = 'Vittoria'
levelLogger.style.color = 'lime'
levelLogger.classList.add('center')
win = () => { }
}
function gameover() {
levelLogger.innerText = 'Game Over'
levelLogger.style.color = 'red'
levelLogger.classList.add('center')
gameover = () => { }
}
/**
* @param {number} deltaTime
*/
function update(deltaTime) {
for (let i = 0; i < GameObject.currentObjects.length; i++) {
const gameobj = GameObject.currentObjects[i];
gameobj.update(deltaTime)
}
}
function draw() {
for (let i = 0; i < GameObject.currentObjects.length; i++) {
const gameobj = GameObject.currentObjects[i];
gameobj.draw()
}
}
levelLogger.innerText = `LIVELLO ${currentLevel}/${config.gioco.ultimolivello}`
window.requestAnimationFrame(loop)