Nodejs

No description

fork
loading
Files
  • index.js
  • client
  • classes.js
  • map.json
  • package-lock.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.24684681870396874","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgZXhwcmVzcyA9IHJlcXVpcmUoJ2V4cHJlc3MnKTsKY29uc3QgYXBwID0gZXhwcmVzcygpOwpjb25zdCBzZXJ2ID0gcmVxdWlyZSgnaHR0cCcpLlNlcnZlcihhcHApOwoKYXBwLnVzZSgnLycsIGV4cHJlc3Muc3RhdGljKF9fZGlybmFtZSArICcvY2xpZW50LycpKTsKCnNlcnYubGlzdGVuKDIwMDApOwpjb25zb2xlLmxvZygnU2VydmVyIHN0YXJ0ZWQuJyk7Cgpjb25zdCBpbyA9IHJlcXVpcmUoJ3NvY2tldC5pbycpKHNlcnYpOwoKY29uc3QgZnMgPSByZXF1aXJlKCdmcycpOwoKY29uc3QgbWFwID0gSlNPTi5wYXJzZShmcy5yZWFkRmlsZVN5bmMoIm1hcC5qc29uIikpOwoKCmNvbnN0IHsgRmxvb3IsIFBsYXllciwgQnVsbGV0IH0gPSByZXF1aXJlKCcuL2NsYXNzZXMuanMnKTsKCmNvbnN0IHBsYXllcnMgPSB7fTsKCmNvbnN0IGZsb29ycyA9IFtdOwoKCgpmb3IgKGNvbnN0IGYgb2YgbWFwLmRhdGEpIGZsb29ycy5wdXNoKG5ldyBGbG9vcihmWzBdLCBmWzFdLCBmWzJdLCBmWzNdLCBmWzRdKSk7Cgppby5vbignY29ubmVjdGlvbicsIHNvY2tldCA9PiB7Cgljb25zdCBpZCA9IHNvY2tldC5pZDsKCXBsYXllcnNbaWRdID0gbmV3IFBsYXllcigwLCAwLCAxLCAxKTsKCWNvbnN0IHAgPSBwbGF5ZXJzW2lkXTsKCXNvY2tldC5lbWl0KCdpZCcsIHNvY2tldC5pZCk7Cglzb2NrZXQuZW1pdCgnZmxvb3JzJywgZmxvb3JzKTsKCXNvY2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GV0cy5wdXNoKHsgeDogYi54LCB5OiBiLnksIHo6IGIueiwgaWQ6IGIuaWQgfSk7CgoJCXBhY2sucHVzaCh7CgkJCXg6IHAueCwKCQkJeTogcC55LAoJCQl6OiBwLnosCgkJCWlkOiBpLAogICAgICBocDogcC5ocCwKCQkJYW5nbGU6IHAuYW5nbGUsCgkJCWJ1bGxldHM6IGJ1bGxldHMsCgkJCXNjb3JlOiBwLnNjb3JlCgkJfSk7Cgl9CgoJaW8uZW1pdCgncGxheWVycycsIHBhY2spOwp9CgpzZXRJbnRlcnZhbCgoKSA9PiB7Cgl1cGRhdGUoKTsKCXNlbmQoKTsKfSwgMTAwMCAvIDYwKTsKCgo="},"asBuffer":null},"loaded":true}}
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
const express = require('express');
const app = express();
const serv = require('http').Server(app);

app.use('/', express.static(__dirname + '/client/'));

serv.listen(2000);
console.log('Server started.');

const io = require('socket.io')(serv);

const fs = require('fs');

const map = JSON.parse(fs.readFileSync("map.json"));


const { Floor, Player, Bullet } = require('./classes.js');

const players = {};

const floors = [];



for (const f of map.data) floors.push(new Floor(f[0], f[1], f[2], f[3], f[4]));

io.on('connection', socket => {
	const id = socket.id;
	players[id] = new Player(0, 0, 1, 1);
	const p = players[id];
	socket.emit('id', socket.id);
	socket.emit('floors', floors);
	socket.on('move', data => {
		const x = Math.sin(data.angle) * data.speed;
		const y = Math.cos(data.angle) * data.speed;
		if (p.landed && data.jump) {
			p.sz ++;
			p.landed = false;
		}

		p.sx = x;
		p.sy = y;
	});

	socket.on('angle', a => {
		p.angle = a;
	});

	socket.on('shoot', a => {
    if (!p.ready) return;
		const b = new Bullet(p.x, p.y, p.z);
		p.bullets.push(b);

		b.sx = Math.sin(a) * 1;
		b.sy = Math.cos(a) * 1;
		b.sz = 0;
    p.ready = false;
    setTimeout(() => p.ready = true, 500);
	});

	socket.on('disconnect', () => {
		delete players[id];
	});
});

function update() {
	for (const i in players) {
		const p = players[i];

		p.x += p.sx;
		p.y += p.sy;
		p.z += p.sz;

		if (p.z < -50) {
		  p.reSpawn();
		  io.emit("oof");
		}
		let landed = false;
		for (const f of floors) {
			if (f.collide(p)) {
				landed = true;
				break;
			}
		}
		p.landed = landed;
		if (!p.landed) p.sz -= 0.05;
		else p.sz = 0;

		for (const i in p.bullets) {
			const b = p.bullets[i];
			b.x += b.sx;
			b.y += b.sy;

			if (b.x < -100 || b.x > 100 || b.y < -100 || b.y > 100)
				p.bullets.splice(i, 1);
      
      for (const i in players) {
        const target = players[i];
        if(p != target && b.collide(target)) {
          target.hp -= 20;
          p.bullets.splice(p.bullets.indexOf(b), 1);
          
          if(target.hp <= 0) {
            target.reSpawn();
            io.emit("oof");
            p.score++;
          }
          break;
        }
      }
		}
	}
}


function send(){
  const pack = [];
	for (const i in players) {
		const p = players[i];
		const bullets = [];
		for (const b of p.bullets) bullets.push({ x: b.x, y: b.y, z: b.z, id: b.id });

		pack.push({
			x: p.x,
			y: p.y,
			z: p.z,
			id: i,
      hp: p.hp,
			angle: p.angle,
			bullets: bullets,
			score: p.score
		});
	}

	io.emit('players', pack);
}

setInterval(() => {
	update();
	send();
}, 1000 / 60);


node v10.16.0