forked from enriquesanchezb/game_for_testing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
115 lines (110 loc) · 4.11 KB
/
main.js
File metadata and controls
115 lines (110 loc) · 4.11 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
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
import { Player } from './player.js'
import { InputHandler } from './input.js'
import { Background } from './background.js';
import { FlyingEnemy, GroundEnemy, ClimbingEnemy } from './enemies.js';
import { UI } from './ui.js'
window.addEventListener('load', function(){
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = 900;
canvas.height = 500;
class Game {
constructor(width, height){
this.width = width;
this.height = height;
this.groundMargin = 80;
this.speed = 0;
this.maxSpeed = 4;
this.background = new Background(this);
this.player = new Player(this);
this.input = new InputHandler(this);
this.UI = new UI(this);
this.enemies = [];
this.particles = [];
this.collisions = [];
this.floatingMessages = [];
this.enemyTimer = 0;
this.enemyInterval = 1500;
this.debug = false;
this.score = 0;
this.winningScore = 100;
this.fontColor = 'black';
this.time = 0;
this.maxTime = 30000;
this.gameOver = false;
this.lives = 5;
this.player.currentState = this.player.states[0];
this.player.currentState.enter();
this.maxParticles = 50;
}
update(deltaTime){
this.time += deltaTime;
if(this.time > this.maxTime) this.gameOver = true;
this.background.update();
this.player.update(this.input.keys,deltaTime);
// Handle enemies
if(this.enemyTimer > this.enemyInterval){
this.addEnemy();
this.enemyTimer = 0;
} else {
this.enemyTimer += deltaTime;
}
this.enemies.forEach(enemy => {
enemy.update(deltaTime);
})
// Handle message
this.floatingMessages.forEach(message => {
message.update();
});
// Handle particles
this.particles.forEach((particle, index) => {
particle.update();
});
if (this.particles.length > this.maxParticles){
this.particles.length = this.maxParticles;
}
// Handle collision sprites
this.collisions.forEach((collision, index) => {
collision.update(deltaTime);
});
this.enemies = this.enemies.filter(enemy => !enemy.markedForDeletion);
this.particles = this.particles.filter(particle => !particle.markedForDeletion);
this.collisions = this.collisions.filter(collision => !collision.markedForDeletion);
this.floatingMessages = this.floatingMessages.filter(message => !message.markedForDeletion);
}
draw(context){
this.background.draw(context);
this.player.draw(context);
this.enemies.forEach(enemy => {
enemy.draw(context);
});
this.particles.forEach(particle => {
particle.draw(context);
});
this.collisions.forEach(collision => {
collision.draw(context);
});
this.floatingMessages.forEach(message => {
message.draw(context);
});
this.UI.draw(context);
}
addEnemy(){
if (this.speed > 0 && Math.random() < 0.5) this.enemies.push(new GroundEnemy(this));
else if (this.speed > 0) this.enemies.push(new ClimbingEnemy(this));
this.enemies.push(new FlyingEnemy(this));
}
}
const game = new Game(canvas.width, canvas.height);
console.log(game);
let lastTime = 0;
function animate(timeStamp) {
const deltaTime = timeStamp - lastTime;
lastTime = timeStamp;
ctx.clearRect(0, 0, canvas.width, canvas.height);
game.update(deltaTime);
game.draw(ctx);
if (!game.gameOver) requestAnimationFrame(animate);
}
animate(0);
});