-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
160 lines (131 loc) · 8.48 KB
/
server.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
// Dependencies
var http = require("http");
var path = require("path");
var express = require("express");
var socketIO = require("socket.io");
var types = require("./ObjectTypes");
var prefabs = require("./Prefabs/Prefabs");
var collisions = require("./Collisions");
var app = express();
var server = http.Server(app);
var io = socketIO(server);
app.use("/dist", express.static(__dirname + "/dist"));
const PORT = process.env.PORT || 5000;
const INDEX = path.join(__dirname, "dist/index.html");
app.use((req, res) => res.sendFile(INDEX) );
// Start the server
server.listen(PORT, () => {
console.log(`Starting server on port ${ PORT }`);
});
var renderSize = prefabs.renderSize;
var prevTime = Date.now();
var delta = 0;
var objects = {};
// Adds starting resources to the map
initializeMap(objects);
// Listen for connection on IO
io.on("connection", (socket) => {
// Handle connection
socket.on("new-player", () => {
prefabs.generateNew(objects, socket.id, 0, 0, types.ObjectTypes.PLAYER, types.Player.HUMAN);
socket.emit("handshake", {
id: socket.id,
cubeSize: renderSize,
});
});
// Handle player input event
socket.on("playerInput", (playerInput) => {
if (objects[socket.id]) {
objects[socket.id].onPlayerInput(objects, socket.id, playerInput);
}
});
// Handle mouse down event from player
socket.on("mouseDown", (object) => {
if (objects[object.sourceId]) {
objects[object.sourceId].mouseDown(objects, object);
}
});
// Handle player disconnect - Clean up resources
socket.on("disconnect", () => {
delete objects[socket.id];
});
});
// Main update loop at 60fps
setInterval(() => {
const time = Date.now();
delta = time - prevTime;
prevTime = time;
for (var id in objects) {
if (objects[id]) {
objects[id].update(objects, id, delta);
}
}
io.sockets.emit("state", objects);
}, 1000 / 60);
// Initializes starting map resources
function initializeMap(obs) {
prefabs.generateNew(obs, "init", 75, -25, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 180, 210, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -150, 200, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 220, -205, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -195, -175, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -104, 125, types.ObjectTypes.TERRAIN, types.Terrain.WALL_HORIZ);
prefabs.generateNew(obs, "init", 0, 150, types.ObjectTypes.TERRAIN, types.Terrain.WALL_HORIZ);
prefabs.generateNew(obs, "init", 104, 125, types.ObjectTypes.TERRAIN, types.Terrain.WALL_HORIZ);
prefabs.generateNew(obs, "init", -100, 0, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", -124, 0, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", -148, 0, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", -100, 32, types.ObjectTypes.INTERACTABLE, types.Interactable.PLAYER_TYPE_CHANGER, { newType: types.Player.HUMAN });
prefabs.generateNew(obs, "init", -124, 32, types.ObjectTypes.INTERACTABLE, types.Interactable.PLAYER_TYPE_CHANGER, { newType: types.Player.FIRE_MAGE });
prefabs.generateNew(obs, "init", -148, 32, types.ObjectTypes.INTERACTABLE, types.Interactable.PLAYER_TYPE_CHANGER, { newType: types.Player.GOD });
prefabs.generateNew(obs, "init", 150, 0, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 150, 24, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 150, 48, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 170, 12, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 170, 36, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 190, 0, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 190, 24, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", 190, 48, types.ObjectTypes.TRIGGER, types.Trigger.SPIKE_TRAP);
prefabs.generateNew(obs, "init", -300, 0, types.ObjectTypes.VEHICLE, types.Vehicle.CAR);
prefabs.generateNew(obs, "init", -300, 74, types.ObjectTypes.VEHICLE, types.Vehicle.CAR);
prefabs.generateNew(obs, "init", -350, 0, types.ObjectTypes.VEHICLE, types.Vehicle.CAR);
prefabs.generateNew(obs, "init", -350, 74, types.ObjectTypes.VEHICLE, types.Vehicle.CAR);
prefabs.generateNew(obs, "init", -25, -100, types.ObjectTypes.ENEMY, types.Enemy.TARGET_DUMMY);
prefabs.generateNew(obs, "init", 25, -100, types.ObjectTypes.ENEMY, types.Enemy.TARGET_DUMMY);
prefabs.generateNew(obs, "init", 0, 0, types.ObjectTypes.TRIGGER, types.Trigger.INVULN_PLATFORM);
prefabs.generateNew(obs, "init", 0, 75, types.ObjectTypes.INTERACTABLE, types.Interactable.TELEPORTER, { destX: 0, destY: 1985 });
// Castle
prefabs.generateNew(obs, "init", 0, 1985, types.ObjectTypes.DECORATION, types.Decoration.CASTLE_FLOOR);
prefabs.generateNew(obs, "init", 0, 1613, types.ObjectTypes.TERRAIN, types.Terrain.CASTLE_WALL_HORIZ);
prefabs.generateNew(obs, "init", 0, 2413, types.ObjectTypes.TERRAIN, types.Terrain.CASTLE_WALL_HORIZ);
prefabs.generateNew(obs, "init", 400, 1500, types.ObjectTypes.DECORATION, types.Decoration.WATCH_TOWER);
prefabs.generateNew(obs, "init", -400, 1500, types.ObjectTypes.DECORATION, types.Decoration.WATCH_TOWER);
prefabs.generateNew(obs, "init", 400, 1985, types.ObjectTypes.TERRAIN, types.Terrain.CASTLE_WALL_VERT);
prefabs.generateNew(obs, "init", -400, 1985, types.ObjectTypes.TERRAIN, types.Terrain.CASTLE_WALL_VERT);
prefabs.generateNew(obs, "init", 400, 2300, types.ObjectTypes.DECORATION, types.Decoration.WATCH_TOWER);
prefabs.generateNew(obs, "init", -400, 2300, types.ObjectTypes.DECORATION, types.Decoration.WATCH_TOWER);
prefabs.generateNew(obs, "init", 200, 1785, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", -200, 1785, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", 200, 2185, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", -200, 2185, types.ObjectTypes.INTERACTABLE, types.Interactable.HEALTH_PICKUP);
prefabs.generateNew(obs, "init", 600, 1400, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 634, 1550, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 720, 1800, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 550, 2100, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -600, 2100, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -634, 1800, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -720, 1400, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -550, 1500, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -350, 1450, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -50, 1400, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 150, 1450, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 230, 1300, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -350, 2500, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -50, 2700, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 150, 2650, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 230, 2550, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -580, 2650, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", -500, 2550, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 450, 2500, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
prefabs.generateNew(obs, "init", 630, 2700, types.ObjectTypes.TERRAIN, types.Terrain.TREE);
}