-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
156 lines (129 loc) · 4.07 KB
/
server.js
File metadata and controls
156 lines (129 loc) · 4.07 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
// Constants
const WALK_SPEED = 4;
const FPS = 30;
// Globals
let id = 0;
let map;
let mapSize = { x: 0, y: 0 };
let entities = [];
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const fs = require('fs');
const _ = require('./public/underscore.js')._;
fs.readFile('public/common.js', 'utf8', (err, data) => {
if (err) {
throw err;
}
eval(data);
});
io.on('connection', (socket) => {
socket.emit('map', map);
socket.emit('players', _.map(entities, (entity) => entity.id));
let player = new Entity({ x: 110, y: 135 }, 'blue', false, socket.id);
entities.push(player);
io.emit('new-player', player.id);
socket.on('direction-update', (data) => {
player.action = data ? 'walk' : 'stationary';
switch (data) {
case 'N': player.orientation = 'up'; break;
case 'S': player.orientation = 'down'; break;
case 'W': player.orientation = 'left'; break;
case 'E': player.orientation = 'right'; break;
default: break; // Keep existing orientation if none of the cases match
}
});
socket.emit('show-content', 'welcome');
});
class Entity {
constructor(pos, colour, npc, socketId) {
this.id = id++;
this.pos = pos;
this.colour = colour;
this.socketId = socketId;
this.isViewingPage = true;
this.action = 'stationary';
this.orientation = 'down';
}
update() {
if (this.action === 'walk') {
this.walk();
}
}
walk() {
let direction = { x: 0, y: 0 };
switch (this.orientation) {
case 'left': this._walk({ x: -WALK_SPEED, y: 0 }); break;
case 'right': this._walk({ x: WALK_SPEED, y: 0 }); break;
case 'up': this._walk({ x: 0, y: -WALK_SPEED }); break;
case 'down': this._walk({ x: 0, y: WALK_SPEED }); break;
}
let page = this.getObjectsAt(this.pos, "Page");
if (this.isViewingPage) {
if (page.length === 0) {
io.to(this.socketId).emit('hide-content');
this.isViewingPage = false;
}
} else if (page.length === 1) {
this.isViewingPage = true;
io.to(this.socketId).emit('show-content', page[0].properties.content);
}
}
_walk(direction) {
let newPos = {
x: this.pos.x + direction.x,
y: this.pos.y + direction.y
};
if (this.canMoveTo(newPos)) {
this.pos = newPos;
}
}
canMoveTo(pos) {
let impassableObjects = this.getObjectsAt(pos, "Impassable");
// Can't move outside the map boundary or through impassable terrain
if (pos.x > mapSize.x || pos.y > mapSize.y || pos.x < 0 || pos.y < 0 || impassableObjects.length !== 0) {
return false;
}
return true;
}
getObjectsAt(pos, layerName) {
let layer = getLayer(map, layerName);
return layer.objects.filter((object) => {
return object.x <= pos.x && pos.x <= object.x + object.width &&
object.y <= pos.y && pos.y <= object.y + object.height;
});
}
}
var getLayer = function(map, name) {
for (key in map.layers) {
if (name == map.layers[key].name) {
return map.layers[key];
}
}
return null;
}
function updateEntities() {
entities.forEach((entity) => entity.update());
}
function gameLoop() {
updateEntities();
io.emit('update', entities);
setTimeout(gameLoop, 1000 / FPS);
}
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
fs.readFile(__dirname + '/public/map.json', 'utf8', (err, data) => {
if (err) {
throw err;
}
map = JSON.parse(data);
mapSize.x = map.width * map.tilewidth - map.tilewidth;
mapSize.y = map.height * map.tileheight - 16;
});
http.listen(8080, () => {
console.log('Server listening on *:8080');
});
gameLoop();