-
Notifications
You must be signed in to change notification settings - Fork 70
/
app.js
224 lines (187 loc) · 7.13 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
/*
Javascript Space Game
By Frank Force 2021
*/
'use strict';
const clampCamera = !debug;
const lowGraphicsSettings = glOverlay = !window['chrome']; // only chromium uses high settings
const startCameraScale = 4*16;
const defaultCameraScale = 4*16;
const maxPlayers = 4;
const team_none = 0;
const team_player = 1;
const team_enemy = 2;
let updateWindowSize, renderWindowSize, gameplayWindowSize;
engineInit(
///////////////////////////////////////////////////////////////////////////////
()=> // appInit
{
resetGame();
cameraScale = startCameraScale;
},
///////////////////////////////////////////////////////////////////////////////
()=> // appUpdate
{
const cameraSize = vec2(mainCanvas.width, mainCanvas.height).scale(1/cameraScale);
renderWindowSize = cameraSize.add(vec2(5));
gameplayWindowSize = vec2(mainCanvas.width, mainCanvas.height).scale(1/defaultCameraScale);
updateWindowSize = gameplayWindowSize.add(vec2(30));
//debugRect(cameraPos, maxGameplayCameraSize);
//debugRect(cameraPos, updateWindowSize);
if (debug)
{
randSeeded(randSeeded(randSeeded(randSeed = Date.now()))); // set random seed for debug mode stuf
if (keyWasPressed(81))
new Enemy(mousePosWorld);
if (keyWasPressed(84))
{
//for(let i=30;i--;)
new Prop(mousePosWorld);
}
if (keyWasPressed(69))
explosion(mousePosWorld);
if (keyIsDown(89))
{
let e = new ParticleEmitter(mousePosWorld);
// test
e.collideTiles = 1;
//e.tileIndex=7;
e.emitSize = 2;
e.colorStartA = new Color(1,1,1,1);
e.colorStartB = new Color(0,1,1,1);
e.colorEndA = new Color(0,0,1,0);
e.colorEndB = new Color(0,.5,1,0);
e.emitConeAngle = .1;
e.particleTime = 1
e.speed = .3
e.elasticity = .1
e.gravityScale = 1;
//e.additive = 1;
e.angle = -PI;
}
if (mouseWheel) // mouse zoom
cameraScale = clamp(cameraScale*(1-mouseWheel/10), defaultTileSize.x*16, defaultTileSize.x/16);
//if (keyWasPressed(77))
// playSong([[[,0,219,,,,,1.1,,-.1,-50,-.05,-.01,1],[2,0,84,,,.1,,.7,,,,.5,,6.7,1,.05]],[[[0,-1,1,0,5,0],[1,1,8,8,0,3]]],[0,0,0,0],90]) // music test
if (keyWasPressed(77))
players[0].pos = mousePosWorld;
/*if (keyWasPressed(32))
{
skyParticles && skyParticles.destroy();
tileLayer.destroy();
tileBackgroundLayer.destroy();
tileParallaxLayers.forEach((tileParallaxLayer)=>tileParallaxLayer.destroy());
randomizeLevelParams();
applyArtToLevel();
}*/
if (keyWasPressed(78))
nextLevel();
}
// restart if no lives left
let minDeadTime = 1e3;
for(const player of players)
minDeadTime = min(minDeadTime, player && player.isDead() ? player.deadTimer.get() : 0);
if (minDeadTime > 3 && (keyWasPressed(90) || keyWasPressed(32) || gamepadWasPressed(0)) || keyWasPressed(82))
resetGame();
if (levelEndTimer.get() > 3)
nextLevel();
},
///////////////////////////////////////////////////////////////////////////////
()=> // appUpdatePost
{
if (players.length == 1)
{
const player = players[0];
if (!player.isDead())
cameraPos = cameraPos.lerp(player.pos, clamp(player.getAliveTime()/2));
}
else
{
// camera follows average pos of living players
let posTotal = vec2();
let playerCount = 0;
let cameraOffset = 1;
for(const player of players)
{
if (player && !player.isDead())
{
++playerCount;
posTotal = posTotal.add(player.pos.add(vec2(0,cameraOffset)));
}
}
if (playerCount)
cameraPos = cameraPos.lerp(posTotal.scale(1/playerCount), .2);
}
// spawn players if they don't exist
for(let i = maxPlayers;i--;)
{
if (!players[i] && (gamepadWasPressed(0, i)||gamepadWasPressed(1, i)))
{
++playerLives;
new Player(checkpointPos, i);
}
}
// clamp to bottom and sides of level
if (clampCamera)
{
const w = mainCanvas.width/2/cameraScale+1;
const h = mainCanvas.height/2/cameraScale+2;
cameraPos.y = max(cameraPos.y, h);
if (w*2 < tileCollisionSize.x)
cameraPos.x = clamp(cameraPos.x, tileCollisionSize.x - w, w);
}
updateParallaxLayers();
updateSky();
},
///////////////////////////////////////////////////////////////////////////////
()=> // appRender
{
const gradient = mainContext.createLinearGradient(0,0,0,mainCanvas.height);
gradient.addColorStop(0,levelSkyColor.rgba());
gradient.addColorStop(1,levelSkyHorizonColor.rgba());
mainContext.fillStyle = gradient;
//mainContext.fillStyle = levelSkyColor.rgba();
mainContext.fillRect(0,0,mainCanvas.width, mainCanvas.height);
drawStars();
},
///////////////////////////////////////////////////////////////////////////////
()=> // appRenderPost
{
//let minAliveTime = 9;
//for(const player of players)
// minAliveTime = min(minAliveTime, player.getAliveTime());
//const livesPercent = percent(minAliveTime, 5, 4)
//const s = 8;
//const offset = 100*livesPercent;
//mainContext.drawImage(tileImage, 32, 8, s, s, 32, mainCanvas.height-90, s*9, s*9);
mainContext.textAlign = 'center';
const p = percent(gameTimer.get(), 8, 10);
//mainContext.globalCompositeOperation = 'difference';
mainContext.fillStyle = new Color(0,0,0,p).rgba();
if (p > 0)
{
//mainContext.fillStyle = (new Color).setHSLA(time/3,1,.5,p).rgba();
mainContext.font = '1.5in impact';
mainContext.fillText('SPACE HUGGERS', mainCanvas.width/2, 140);
}
mainContext.font = '.5in impact';
p > 0 && mainContext.fillText('A JS13K Game by Frank Force',mainCanvas.width/2, 210);
// check if any enemies left
let enemiesCount = 0;
for (const o of engineCollideObjects)
{
if (o.isCharacter && o.team == team_enemy)
{
++enemiesCount;
const pos = vec2(mainCanvas.width/2 + (o.pos.x - cameraPos.x)*30,mainCanvas.height-20);
drawRectScreenSpace(pos, o.size.scale(20), o.color.scale(1,.6));
}
}
if (!enemiesCount && !levelEndTimer.isSet())
levelEndTimer.set();
mainContext.fillStyle = new Color(0,0,0).rgba();
mainContext.fillText('Level ' + level + ' Lives ' + playerLives + ' Enemies ' + enemiesCount, mainCanvas.width/2, mainCanvas.height-40);
// fade in level transition
const fade = levelEndTimer.isSet() ? percent(levelEndTimer.get(), 3, 1) : percent(levelTimer.get(), .5, 2);
drawRect(cameraPos, vec2(1e3), new Color(0,0,0,fade))
});