add basic game classes
This commit is contained in:
parent
ebcee954bf
commit
bb9a493450
11 changed files with 137 additions and 6 deletions
|
@ -1,10 +1,23 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const GameNet = require('./net');
|
const GameNet = require('./net');
|
||||||
|
const Room = require('./room');
|
||||||
|
|
||||||
class GameServer {
|
class GameServer {
|
||||||
constructor(webServer) {
|
constructor(webServer) {
|
||||||
this.net = new GameNet();
|
this.net = new GameNet(this);
|
||||||
|
|
||||||
|
this.rooms = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
assignRoom(player) {
|
||||||
|
let room = Array.from(this.rooms.values()).sort((a, b) => {
|
||||||
|
return a.players - b.players;
|
||||||
|
})[0];
|
||||||
|
|
||||||
|
if (!room || room.full) room = new Room();
|
||||||
|
|
||||||
|
room.add(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const Player = require('../player.js');
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
constructor(net, socket) {
|
constructor(net, socket) {
|
||||||
this.net = net;
|
this.net = net;
|
||||||
|
this.server = net.server;
|
||||||
this.connections = net.connections;
|
this.connections = net.connections;
|
||||||
this.io = net.io;
|
this.io = net.io;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
|
||||||
this.player = false;
|
this.player = new Player(this);
|
||||||
this._room = false;
|
this._room = false;
|
||||||
this.name = '';
|
|
||||||
this.chatCooldown = 0;
|
this.chatCooldown = 0;
|
||||||
|
|
||||||
socket.on('chat', data => {
|
socket.on('chat', data => {
|
||||||
|
@ -17,10 +19,10 @@ class Connection {
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('setName', data => {
|
socket.on('setName', data => {
|
||||||
this.player.name = data.name;
|
this.player.name = data.name.slice(0, 20) || 'Fish';
|
||||||
});
|
});
|
||||||
|
|
||||||
this.room = 'egg';
|
this.server.assignRoom(this.player);
|
||||||
}
|
}
|
||||||
|
|
||||||
chat(data) {
|
chat(data) {
|
||||||
|
@ -47,6 +49,10 @@ class Connection {
|
||||||
this.socket.join(str);
|
this.socket.join(str);
|
||||||
this._room = str;
|
this._room = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get name() {
|
||||||
|
return this.player.name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Connection;
|
module.exports = Connection;
|
||||||
|
|
|
@ -5,8 +5,9 @@ const socketio = require('socket.io');
|
||||||
const Connection = require('./connection.js');
|
const Connection = require('./connection.js');
|
||||||
|
|
||||||
class GameNet {
|
class GameNet {
|
||||||
constructor() {
|
constructor(server) {
|
||||||
this.io = socketio(starbugs.webServer.appServer);
|
this.io = socketio(starbugs.webServer.appServer);
|
||||||
|
this.server = server;
|
||||||
|
|
||||||
this.connections = new Map();
|
this.connections = new Map();
|
||||||
}
|
}
|
||||||
|
|
16
server/game/player.js
Normal file
16
server/game/player.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const fruit = ['Apple', 'Banana', 'Pear', 'Plum', 'Pineapple', 'Peach', 'Apricot', 'Orange', 'Triangle', 'Kiwi', 'Mango', 'Strawberry', 'Lemon', 'Blueberry', 'Raspberry', 'Grape', 'Dragonfruit', 'Watermelon', 'Honeymelon', 'Pomegranate', 'Cherry', 'Avocado'];
|
||||||
|
|
||||||
|
class Player {
|
||||||
|
constructor(connection) {
|
||||||
|
this.room = false;
|
||||||
|
this.ship = false;
|
||||||
|
this.team = false;
|
||||||
|
this.kickCount = 0;
|
||||||
|
this.connection = connection;
|
||||||
|
this.name = `Stupid $(fruit[Math.random() * fruit.length | 0])`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Player;
|
|
@ -0,0 +1,38 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const World = require('./world');
|
||||||
|
|
||||||
|
class Room {
|
||||||
|
constructor() {
|
||||||
|
this.players = new Set();
|
||||||
|
this.teamA = new Set();
|
||||||
|
this.teamB = new Set();
|
||||||
|
this.world = new World();
|
||||||
|
this.name = (Math.random() * 100000 | 0).toString(36);
|
||||||
|
}
|
||||||
|
|
||||||
|
add(player) {
|
||||||
|
player.room = this;
|
||||||
|
player.connection.room = this.name;
|
||||||
|
this.players.add(player);
|
||||||
|
this.setTeam(player, this.teamA.size > this.teamB.size ? 'b' : 'a');
|
||||||
|
this.world.addPlayer(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
setTeam(player, team) {
|
||||||
|
this.teamA.delete(player);
|
||||||
|
this.teamB.delete(player);
|
||||||
|
(team == 'a' ? this.teamA : this.teamB).add(player);
|
||||||
|
player.team = team;
|
||||||
|
}
|
||||||
|
|
||||||
|
get playerCount() {
|
||||||
|
return this.players.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
get full() {
|
||||||
|
return this.playerCount >= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Room;
|
0
server/game/room/world/asteroid.js
Normal file
0
server/game/room/world/asteroid.js
Normal file
9
server/game/room/world/body.js
Normal file
9
server/game/room/world/body.js
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
class Body {
|
||||||
|
constructor() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Body;
|
35
server/game/room/world/index.js
Normal file
35
server/game/room/world/index.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Ship = require('./ship.js');
|
||||||
|
|
||||||
|
class World {
|
||||||
|
constructor() {
|
||||||
|
this.box2d = false;
|
||||||
|
this.bodies = new Set();
|
||||||
|
this.structures = new Set();
|
||||||
|
this.asteroids = new Set();
|
||||||
|
this.ships = new Map();
|
||||||
|
this.players = new Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
addPlayer(player) {
|
||||||
|
this.players.add(player);
|
||||||
|
this.ships.set(player, new Ship(player));
|
||||||
|
}
|
||||||
|
|
||||||
|
removePlayer(player) {
|
||||||
|
removeBody(player.ship);
|
||||||
|
this.ships.delete(player);
|
||||||
|
this.players.delete(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeBody(body) {
|
||||||
|
// Remove from Box2d.
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = World;
|
0
server/game/room/world/physics.js
Normal file
0
server/game/room/world/physics.js
Normal file
13
server/game/room/world/ship.js
Normal file
13
server/game/room/world/ship.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const Body = require('./body.js');
|
||||||
|
|
||||||
|
class Ship extends Body {
|
||||||
|
constructor(player) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.player = player;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Ship;
|
0
server/game/room/world/structure.js
Normal file
0
server/game/room/world/structure.js
Normal file
Loading…
Add table
Add a link
Reference in a new issue