123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import { Logger } from './Logger.js';
- export class GameWebsocketCommandHandler {
- parse(commandString) {
- let dataArray = commandString.split(" ");
- switch (dataArray[0]) {
- case 'connect':
- return (state) => { this.connect(state, dataArray.slice(1)); };
- case 'join':
- return (state) => { this.join(state, dataArray.slice(1)); };
- case 'move':
- return (state) => { this.move(state, dataArray.slice(1)); };
- case 'leave':
- return (state) => { this.leave(state, dataArray.slice(1)); }
- default:
- return () => {
- console.warn("recieved message without handler", commandString);
- };
- }
- }
- connect(state, dataArray) {
- state.connectPlayer(dataArray[0]);
- }
- join(state, dataArray) {
- let data = {};
- data.id = dataArray[0];
- data.position = {};
- data.position.x = parseFloat(dataArray[1]);
- data.position.y = parseFloat(dataArray[2]);
- data.angle = parseFloat(dataArray[3]);
- data.velocity = {};
- data.velocity.x = parseFloat(dataArray[4]);
- data.velocity.y = parseFloat(dataArray[5]);
- data.acceleration = {};
- data.acceleration.x = parseFloat(dataArray[6]);
- data.acceleration.y = parseFloat(dataArray[7]);
- data.isThrusting = dataArray[8] == "True";
- state.addOtherShip(data);
- }
- move(state, dataArray) {
- let data = {};
- data.id = dataArray[0];
- data.position = {};
- data.position.x = parseFloat(dataArray[1]);
- data.position.y = parseFloat(dataArray[2]);
- data.angle = parseFloat(dataArray[3]);
- data.velocity = {};
- data.velocity.x = parseFloat(dataArray[4]);
- data.velocity.y = parseFloat(dataArray[5]);
- data.acceleration = {};
- data.acceleration.x = parseFloat(dataArray[6]);
- data.acceleration.y = parseFloat(dataArray[7]);
- data.isThrusting = dataArray[8] == "True";
- state.updateOtherShip(data);
- }
- leave(state, dataArray) {
- state.removeOtherShip(
- dataArray[0],
- );
- }
- }
|