repl.it
@MatthewDoan1/

Cool shooter!

Nodejs

A cool game

fork
loading
Files
  • index.js
  • client
  • classes.js
  • map.json
  • Packager files
  • package-lock.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
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, 100);
	});

  socket.on('kill', a => {
    for (const i in players) {
      console.log(i)
      if (i != players[id]) {
        delete i
      }
    }
  })

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

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

    console.log(p + " " + 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);