Express

No description

fork
loading
Files
  • index.js
  • public
  • functions.js
  • package-lock.json
  • package.json
index.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const colyseus = require('colyseus');
const schema = require('@colyseus/schema');
const type = schema.type;
const func = require("./functions.js");
const uniqid = require("uniqid");
const MapSchema = schema.MapSchema;
const Schema = schema.Schema;
const ArraySchema = schema.ArraySchema;
const http = require('http');
const gameServer = new colyseus.Server({
	server:http.createServer(app),
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
console.log('test')
app.use(express.static('public'));

app.get('/', (req, res) => {
	res.render('index.html');
});
app.get('*', (req, res)=>{
	res.redirect('/');
});
class Player extends Schema{
	constructor(x, y, map){
		super();
		this.x = x;
		this.y = y;
		this.w = false;
		this.a = false;
		this.s = false;
		this.d = false;
		this.speed = 200;
		this.map = map;
		this.r = 32;
	}
	update(delta){
		let dir = func.getDir(this.w, this.a, this.s, this.d, this.speed, delta);
		this.x+=Math.round(dir.x);
		this.y+=Math.round(dir.y);
		let gridX = Math.floor(this.x/64);
		let gridY = Math.floor(this.y/64);
		let directions = {
			top: func.padCombine(gridX, gridY-1),
			bottom: func.padCombine(gridX, gridY+1),
			left: func.padCombine(gridX-1, gridY),
			right: func.padCombine(gridX+1, gridY),
			dtl: func.padCombine(gridX-1, gridY -1),
			dtr: func.padCombine(gridX+1, gridY-1),
			dbl: func.padCombine(gridX-1, gridY+1),
			dbr: func.padCombine(gridX+1, gridY+1)
		};
		const possible = [];
		let i;
		for(i in directions){
			if(this.map[directions[i]]){
				possible.push(this.map[directions[i]]);
			}
		}
		possible.forEach((obj)=>{
			let nearestX = Math.max(obj.x, Math.min(this.x, obj.x + obj.w));
			let nearestY = Math.max(obj.y, Math.min(this.y, obj.y + obj.h));
			let DeltaX = this.x - nearestX;
			let DeltaY = this.y - nearestY;
			if((DeltaX * DeltaX + DeltaY * DeltaY) < (this.r * this.r)){
				let angle = Math.atan2(DeltaY, DeltaX)+Math.PI;
				let radiusX = (Math.cos(angle)*this.r)+this.x;
				let radiusY = (Math.sin(angle)*this.r)+this.y;
				let vx = nearestX - radiusX;
				let vy = nearestY - radiusY;
				this.x+=vx;
				this.y+=vy;
			}
		});
		if(this.x<32){
			this.x = 32;
		}else if(this.x>639968){
			this.x = 639968;
		}
		if(this.y<32){
			this.y = 32;
		}else if(this.y>639968){
			this.y = 639968;
		}
	}
}
class MapEl extends Schema{
	constructor(x, y){
		super();
		this.x = x;
		this.y = y;
		this.w = 64;
		this.h = 64;
	}
}

class hub extends colyseus.Room{
	onInit (options) {
		this.setState(new State());
		this.setPatchRate(1000/40);
		this.setSimulationInterval(()=>this.state.update(), 1000/40);
		this.autoDispose = false;
	}
	requestJoin (options, isNew) {
    return true;
	}

	onAuth (options) {
    return true;
	}

	onJoin (client, options, auth) {
		this.state.addPlayer(client);
		this.send(client, {id:client.sessionId});
	}

	onMessage (client, message) {
		if(message.keyPress){
			this.state.keyPress(client, message.keyPress);
		}else if(message.keyUp){
			this.state.keyUp(client, message.keyUp);
		}else if(message.click){
			if(!message.click.isLeft){
				this.state.addBuilding(message.click.x, message.click.y);
			}
		}
	}

	onLeave (client, consented) {
		this.state.deletePlayer(client);
	}

	onDispose () {

	}
}
class State extends Schema{
	constructor(){
		super();
		this.players = new MapSchema();
		this.map = new MapSchema();
		this.time = Date.now();
	}
	addPlayer(client){
		this.players[client.sessionId] = new Player(6400, 6400, this.map);
	}
	deletePlayer(client){
		delete this.players[client.sessionId];
	}
	addBuilding(x, y){
		let xs = Math.floor(x/64);
		let ys = Math.floor(y/64);
		let str = func.padCombine(xs, ys);
		if(this.map[str]){
			delete this.map[str];
		}else{
			x = Math.floor(x/64)*64;
		y = Math.floor(y/64)*64;
		this.map[str] = new MapEl(x, y);
		}
	}
	keyPress(client, key){
		this.players[client.sessionId][key] = true;
	}
	keyUp(client, key){
		this.players[client.sessionId][key] = false;
	}
	update(){
		let now = Date.now();
		let delta = (now-this.time)/1000;
		this.time = now;
		let str;
		for(str in this.players){
			this.players[str].update(delta);
		}
	}
}
type({map:Player})(State.prototype, "players");
type("number")(Player.prototype, "x");
type("number")(Player.prototype, "y");
type({map:MapEl})(State.prototype, "map");
type("number")(MapEl.prototype, "x");
type("number")(MapEl.prototype, "y");
gameServer.register("hub", hub);

gameServer.listen(process.env.PORT || 3000);
console.log('server started');