-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.js
182 lines (143 loc) · 5.71 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
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
/*
Space Fighter Game
*/
var gamejs = require('gamejs');
var draw = gamejs.draw;
var screen = require('./screen');
var input = require('./input');
var animate = require('./animate');
var stars = require('./stars');
var player = require('./player');
var enemy = require('./enemy');
var SIZE = [800, 600];
function main() {
// screen setup
gamejs.display.setMode(SIZE);
gamejs.display.setCaption("Fighter");
var intro = gamejs.image.load('images/intro.png');
var font = new gamejs.font.Font('48px ubuntu, sans-serif');
var paused = font.render("Paused", "#08c");
var gameOver = font.render("Game Over", "#000");
var background = gamejs.image.load("images/nebula1.jpg");
var backgroundPos = SIZE[1] - background.getSize()[1];
var starMap = new stars.StarMap(SIZE);
starMap.seed();
var powerups = new gamejs.sprite.Group();
var enemies = [];
// create some ship sprites and put them in a group
var ship = new player.Ship([SIZE[0] / 2 - 28, SIZE[1] - 100, 57, 26], SIZE, enemies);
var controls = new input.UserControls(SIZE, ship);
var ai = new enemy.AiManager(SIZE, ship);
enemies.push(ai.ships);
var hud = screen.Hud(SIZE);
var pausedDrawn = false;
// game loop
var mainSurface = gamejs.display.getSurface();
// msDuration = time since last tick() call
var tick = function(msDuration) {
gamejs.event.get().forEach(function(event) {
controls.handle(event);
});
if (controls.paused) {
if (!pausedDrawn) {
pausedDrawn = true;
// Draw paused overlay
gamejs.draw.rect(mainSurface, "rgba(0, 0, 0, 0.5)", new gamejs.Rect([0, 0], SIZE), 0)
mainSurface.blit(paused, [SIZE[0] / 2 - paused.getSize()[0] / 2, SIZE[1] / 2 - paused.getSize()[1] / 2]);
}
return;
}
pausedDrawn = false;
if (!controls.paused) {
backgroundPos += 10 * (msDuration / 1000);
if (backgroundPos >= SIZE[1] - (background.getSize()[1] / 2)) {
backgroundPos = SIZE[1] - background.getSize()[1];
}
starMap.update(msDuration);
if (controls.initialClick && !ship.isDead()) {
powerups.update(msDuration);
ship.update(msDuration);
ship.clipMotion();
ai.update(msDuration);
}
}
mainSurface.blit(background, [(SIZE[0] / 2) - (background.getSize()[0] / 2), backgroundPos]);
starMap.draw(mainSurface);
if (controls.initialClick) {
if (!ship.isDead()) {
powerups.draw(mainSurface);
ship.angle = controls.angle();
ship.firing = controls.fire;
ship.draw(mainSurface);
ship.weapons.forEach(function (weapon) {
weapon.draw(mainSurface)
});
if (ship.damaged) {
gamejs.draw.rect(mainSurface, "rgba(255, 0, 0, " + (ship.damaged / 150) + ")", new gamejs.Rect([0, 0], SIZE), 0)
ship.damaged = Math.max(0, ship.damaged - msDuration);
}
if (ship.clearAllEnemies) {
if (ship.clearAllEnemies == 150) {
ai.weapons.empty();
}
gamejs.draw.rect(mainSurface, "rgba(255, 255, 255, " + (ship.clearAllEnemies / 150) + ")", new gamejs.Rect([0, 0], SIZE), 0)
ship.clearAllEnemies = Math.max(0, ship.clearAllEnemies - msDuration);
}
ai.draw(mainSurface);
} else {
gamejs.draw.rect(mainSurface, "rgba(255, 0, 0, 0.5)", new gamejs.Rect([0, 0], SIZE), 0)
mainSurface.blit(gameOver, [SIZE[0] / 2 - gameOver.getSize()[0] / 2, SIZE[1] / 2 - gameOver.getSize()[1] / 2]);
}
} else {
mainSurface.blit(intro, [(SIZE[0] / 2) - (intro.getSize()[0] / 2), (SIZE[1] / 2) - (intro.getSize()[1] / 2)]);
}
hud.health = ship.health;
hud.level = ai.level;
hud.draw(mainSurface);
};
gamejs.time.fpsCallback(tick, this, 45);
var collisionTick = function (msDuration) {
var collisions;
if (controls.paused || ship.isDead()) {
return;
}
collisions = gamejs.sprite.groupCollide(ship.weapons, ai.ships, true);
collisions.forEach(function (collision) {
var weapon = collision.a;
var enemy = collision.b;
enemy.damage(weapon.strength);
hud.score += weapon.strength;
if (enemy.isDead() && Math.random() < 0.1) {
powerups.add(new player.Powerup([enemy.rect.left + (enemy.rect.width / 2) - 12, enemy.rect.top + (enemy.rect.height / 2) - 12], enemy.speed));
}
});
collisions = gamejs.sprite.spriteCollide(ship, powerups, true);
collisions.forEach(function (powerup) {
powerup.engage(ship);
hud.score += 100;
});
ai.collide();
};
gamejs.time.fpsCallback(collisionTick, this, 45);
}
/**
* M A I N
*/
gamejs.preload([
'images/intro.png',
'images/nebula1.jpg',
'images/ship_1.png',
'images/ship_2.png',
'images/missile1_1.png',
'images/missile1_2.png',
'images/asteroid1.png',
'images/weapon-upgrade.png',
'images/speed-upgrade.png',
'images/health-upgrade.png',
'images/health-hud.png',
'images/scout.png',
'images/drone.png',
'images/heavy-drone.png',
'images/porcupine.png',
]);
gamejs.ready(main);