-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspacegame.js
More file actions
365 lines (277 loc) · 10.6 KB
/
Copy pathspacegame.js
File metadata and controls
365 lines (277 loc) · 10.6 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
var framecount = 0;
var hoverEntity = null; // entity that the mouse is hovering over
var selectedEntity = null; // entity which the mouse has clicked on
var selectedMission = null; // mission selected in the menus
var singletouchtimer = 0;
var cursorAbsX; var cursorAbsY;
var bypassGameClick = false; // gui boolean for when a gui element is clicked, not to trigger anything in game world
var pathPredictEnabled = true; var buildingDrawEnabled = true;
var trajPredictor = new Entity(0,0,0);
trajPredictor.boostForce = new ForceVector(0,0);
var trajectory = [[],[]];
var dir_history = [];
var trajectoryBuffer = [[],[]];
var traj_pointer = 0;
var drawEnabled = true;
var FONT = null;
CHUNK_DIM = 524288; // both width and height of the chunks are equal. this could technically be very large.
MAX_ZOOM = 100;
MIN_CITY_TEXT_ZOOM = 0.04; // anything smaller than this will not render city label names
MAX_INTERPLANETARY_ZOOM = 0.5; // anything larger than this will only render a single planet (the planet the player is nearest to/in the gravity radius of)
MAX_INTERSTELLAR_ZOOM = 0.001; // anything larger than this will render a whole star system and its planets but no buildings/small details(TODO)
// anything smaller than this will only render stars (no planets)
MIN_ZOOM = 0.001;
function loopyMod(x, m) {
return (x % m + m) % m;
}
function windowResized() {
var outerw = window.innerWidth;
var outerh = window.innerHeight;
var window_aspect_ratio = outerh/outerw
bodydiv = document.getElementById("bodydiv");
var cw = bodydiv.offsetWidth - 30;
var ch = cw * (window_aspect_ratio)
resizeCanvas(windowWidth, windowHeight);
}
function preload(){
//FONT = loadImage("font8drawn.png");
}
function setup(){
document.documentElement.style.overflow = 'hidden'; // firefox, chrome
document.body.scroll = "no"; // ie only
createCanvas(windowWidth, windowHeight);
frameRate(60);
textFont("Courier"); textSize(32);
pixelDensity(1);
noSmooth();
server = new Server();
server.init(); server.world.init();
client = new Client();
update(); update(); update(); // I guess it takes three ticks to position everything correctly (including the camera and player)
/* var ship = new EntityTruck(7500, 8192, 0); var n = server.world.getPlayer().getNation();
ship.nationUUID = n.uuid;
ship.groundedBodyUUID = n.getHomePlanet().uuid; ship.grounded = true;
ship.targetIndex = n.getCapitalCity().centerIndex;
ship.moveToSpawnPoint(); server.world.spawnEntity(ship); */
GuiHandler.init();
let canvasElement = createCanvas(windowWidth, windowHeight).elt;
let context = canvasElement.getContext('2d');
context.mozImageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.imageSmoothingEnabled = false;
//settings = QuickSettings.create(0, 0, "Space Game 0.0.1 2021-04-02", mainelement);
}
function draw(){
if (GROUP_INFOBAR.active && !(selectedTextEntry)){
update();
}
framecount++;
cam_zoom = Math.min(cam_zoom, MAX_ZOOM);
cam_zoom = Math.max(cam_zoom, MIN_ZOOM);
// The level of detail (LOD) is used to tell how many times to invoke a resampling function which each time reduces the number of vertices drawn on a planet by a factor of 2
//
lod = 10 / cam_zoom;
lod = Math.ceil(Math.log(lod)/Math.log(2))
lod = Math.max( 0, lod );
lod = Math.min( 8, lod );
//background(128,128,240);
background(6,0,6);
if ( drawEnabled ){
// chunk boundary lines are behind everything
var chunk = client.world.getPlayer().getChunk();
stroke(128);
noFill();
beginShape();
var cx = chunk.x * CHUNK_DIM; var cy = chunk.y * CHUNK_DIM
vertex( tra_rot_x( cx, cy ), tra_rot_y( cx, cy ) );
vertex( tra_rot_x( cx + CHUNK_DIM, cy ), tra_rot_y( cx + CHUNK_DIM, cy ) );
vertex( tra_rot_x( cx + CHUNK_DIM, cy + CHUNK_DIM ), tra_rot_y( cx + CHUNK_DIM, cy + CHUNK_DIM ) );
vertex( tra_rot_x( cx, cy + CHUNK_DIM ), tra_rot_y( cx, cy + CHUNK_DIM ) );
vertex( tra_rot_x( cx, cy ), tra_rot_y( cx, cy ) );
endShape();
var bodies = 0;
var buildings = 0;
var particles = 0;
for ( var i = 0; i < 6; i++ ){
// entities not confined to chunk
for ( var uuid in client.world.entities ){
var e = client.world.entities[uuid];
if (e.isOnScreen() && e.renderPriority == i){
e.render();
if (e instanceof EntityBuilding){ buildings++ }
if (e instanceof EntityParticle){ particles++ }
}
}
// entities confined to chunk
for ( var uuid in chunk.bodies ){
var b = chunk.bodies[uuid];
if (b.isOnScreen() && b.renderPriority == i){
//if (b.renderPriority == i){
b.render();
bodies++;
}
}
}
GuiHandler.drawBuildingGhost();
fill(255,0,0);
circle(tra_rot_x(cursorAbsX, cursorAbsY), tra_rot_y(cursorAbsX, cursorAbsY), 5);
if (PLANET_CAM){
translate(width/2, height/2);
rotate(-cam_rot);
rotate(-HALF_PI);
translate(-width/2, -height/2);
}
GuiHandler.drawCityLabels();
resetMatrix();
}
GuiHandler.drawSpaceportTooltips();
GuiHandler.update();
GuiHandler.render();
GuiHandler.handleTouches();
stroke(255); fill(255);
textSize(16 * GUI_SCALE);
text("FPS: " + Math.round(frameRate()), width - ( 75 * GUI_SCALE ), 16 * GUI_SCALE);
if (VERBOSE_DEBUG_TEXT){
text("bod: " + bodies, width - ( 75 * GUI_SCALE ), 32 * GUI_SCALE);
text("bld: " + buildings, width - ( 75 * GUI_SCALE ), 48 * GUI_SCALE);
text("prt: " + particles, width - ( 75 * GUI_SCALE ), 64 * GUI_SCALE);
}
textSize(16);
//text(Math.round(tra_rot_x(cursorAbsX, cursorAbsY)) + " " + Math.round(tra_rot_y(cursorAbsX, cursorAbsY)), width - 225, 32);
}
function touchStarted() {
mousePressed(); TOUCH_MODE = true;
return false;
}
function keyPressed() {
if (selectedTextEntry){
if (keyCode === 8){
selectedTextEntry.setting = selectedTextEntry.setting.slice(0, -1);
backspaceTimer = BACKSPACE_TIMER_AMT;
}else if (keyCode === 13){
selectedTextEntry.commit();
}else if((keyCode >= 48 && keyCode <= 90) || keyCode == 32) {
var ltr = String.fromCharCode(keyCode);
if (!shiftDown){
ltr = ltr.toLowerCase();
}
if (selectedTextEntry.setting.length < 27){
selectedTextEntry.setting += ltr;
}
}
}else{
if (keyCode === 70){
//fullscreen(!fullscreen());
//}else if (keyCode === 80){
//pathPredictEnabled = !pathPredictEnabled;
}else if (keyCode === 80){
PLANET_CAM = !PLANET_CAM;
}else if (keyCode === 86){ // V
VERBOSE_DEBUG_TEXT = !VERBOSE_DEBUG_TEXT;
}else if (keyCode === 66){ // B
//buildingDrawEnabled = !buildingDrawEnabled;
}else if (keyCode === 69){ // E
if (GROUP_INFOBAR.active){
if (MissionHandler.inPlaceForDelivery){
var planet = client.world.getPlayer().getGroundedBody();
var index = client.world.getPlayer().terrainIndex;
var building = planet.tiles[ index ].getBuilding(); selectedEntity = building;
server.world.getPlayer().currentMission.onSuccess();
} else if (MissionHandler.inPlaceForMission){
var planet = client.world.getPlayer().getGroundedBody();
var index = client.world.getPlayer().terrainIndex;
var building = planet.tiles[ index ].getBuilding(); selectedEntity = building;
GuiHandler.openWindow( GROUP_MISSION_SELECT );
}
}
} else if (keyCode === 27){ // esc
for ( var q = GuiHandler.elements.length - 1; q >= 0; q-- ){
var e = GuiHandler.elements[q];
if (e.active && e.BTN_BACK){
e.BTN_BACK.onClick(); return;
}
}
// Hotbar
}else if (keyCode >= 48 && keyCode <= 57){
server.onUpdateRequest( keyCode - 49, "world", "getPlayer", "inventory", "selection" );
}
}
}
function mouseMoved() {
}
function mousePressed() {
GuiHandler.onClick(mouseX, mouseY);
if (bypassGameClick){ bypassGameClick = false; return; }
if (!GROUP_INFOBAR.active){ return; };
if (GuiHandler.getBuildingGhost()){
buildingToPlace = GuiHandler.getBuildingGhost();
GuiHandler.openWindow(GROUP_CITY_FOUND); return;
}
if (hoverEntity){
selectedEntity = hoverEntity;
}else{
selectedEntity = null;
}
MissionHandler.onPlayerSelectEntity( client.world.getPlayer(), selectedEntity );
//if (selectedEntity != null){ return; }
//if (client.world.getPlayer().dead){ return; }
}
function mouseWheel(e) {
//console.log(e.delta);
//cam_zoom -= (cam_zoom / 25) * (e.delta / 25);
cam_zoom -= (cam_zoom / 25) * (e.delta * MOUSE_SENSITIVITY / 25);
cam_zoom = Math.min(cam_zoom, MAX_ZOOM);
cam_zoom = Math.max(cam_zoom, MIN_ZOOM);
return false;
}
var lod;
// This is truly a double-duty function, doing both serverside and clientside calls. However, the backend behavior is almost entirely relegated to the corresponding objects (World, Chunk, Entity) whereas the clientside behavior is mostly in here (or the GuiHandler and its constituents)
// For multiplayer test, the server side calls will be moved and all the rest will stay put!
var update = function(){
server.update();
var player = client.world.getPlayer();
// KEYBOARD HANDLING
if (player){
if (keyIsDown(87)) { // up
server.onUpdateRequest( player.boostForce.magnitude + 0.005, "world", "getPlayer", "boostForce", "magnitude" );
}
else if (keyIsDown(83)) { // down
server.onUpdateRequest( player.boostForce.magnitude - 0.005, "world", "getPlayer", "boostForce", "magnitude" );
}
if (keyIsDown(65)) { // left
server.onUpdateRequest( player.dir - 0.1, "world", "getPlayer", "dir" );
}
if (keyIsDown(68)) { // right
server.onUpdateRequest( player.dir + 0.1, "world", "getPlayer", "dir" );
}
}
// MOUSE HANDLING
cursorAbsX = untra_x( mouseX, mouseY ); cursorAbsY = untra_y( mouseX, mouseY );
hoverEntity = null;
cursorChunkX = Math.floor(cursorAbsX / CHUNK_DIM); cursorChunkY = Math.floor(cursorAbsY / CHUNK_DIM);
cursorEntity = new Entity(cursorAbsX, cursorAbsY, 0);
var cc = client.world.getChunk(cursorChunkX,cursorChunkY);
if (cc){
for (var uuid in cc.bodies) {
var body = cc.bodies[uuid];
if (CollisionUtil.isColliding(cursorEntity, body) && body.canEntitiesCollide && !(body instanceof BodyOcean)){
hoverEntity = body; break;
}
if (body.hasDynamicScale && cam_zoom < MAX_INTERPLANETARY_ZOOM){
var dist = CollisionUtil.euclideanDistance(tra_rot_x(body.x,body.y), tra_rot_y(body.x,body.y), mouseX, mouseY);
if (dist < body.dispradius){
hoverEntity = body; break;
}
}
}
}
for (var uuid in client.world.entities){
entity = client.world.entities[uuid];
if (entity.isOnScreen()){
if (!(entity instanceof EntityPlayer || entity instanceof EntityParticle) && CollisionUtil.isEntityCollidingWithEntity(cursorEntity, entity)){
hoverEntity = entity; break;
}
}
}
}