Compare commits
3 Commits
990419c351
...
aae9d27b31
Author | SHA1 | Date | |
---|---|---|---|
|
aae9d27b31 | ||
|
57db26a38b | ||
|
7ecdabd0b9 |
|
@ -11,4 +11,3 @@ export default class LocalClient extends Client {
|
||||||
this.worker.postMessage(message);
|
this.worker.postMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,7 @@ export default class RemoteClient extends Client {
|
||||||
async connect() {
|
async connect() {
|
||||||
this.socket = new WebSocket(`ws://${window.location.host}/ws`);
|
this.socket = new WebSocket(`ws://${window.location.host}/ws`);
|
||||||
this.socket.onmessage = (event) => {
|
this.socket.onmessage = (event) => {
|
||||||
for (const i in this.listeners) {
|
this.accept(JSON.parse(event.data));
|
||||||
this.listeners[i](JSON.parse(event.data));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
await new Promise((resolve) => {
|
await new Promise((resolve) => {
|
||||||
this.socket.onopen = resolve;
|
this.socket.onopen = resolve;
|
||||||
|
|
8
src/components/configuration.jsx
Normal file
8
src/components/configuration.jsx
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
export default function Configuration() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>Configuration</h1>
|
||||||
|
<p>This is the configuration page.</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
0
src/components/configuration.module.css
Normal file
0
src/components/configuration.module.css
Normal file
|
@ -4,7 +4,7 @@ export default function Entities({entities}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{
|
{
|
||||||
Object.values(entities)
|
entities
|
||||||
.map(({image, position: [x, y]}, i) => (
|
.map(({image, position: [x, y]}, i) => (
|
||||||
<Sprite
|
<Sprite
|
||||||
image={image}
|
image={image}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import styles from './pixi.module.css';
|
||||||
|
|
||||||
export default function Pixi() {
|
export default function Pixi() {
|
||||||
const client = useContext(ClientContext);
|
const client = useContext(ClientContext);
|
||||||
const [entities, setEntities] = useState({});
|
const [entities, setEntities] = useState([]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function onMessage(message) {
|
function onMessage(message) {
|
||||||
const {type, payload} = message;
|
const {type, payload} = message;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const SPEED = 100;
|
const SPEED = 100;
|
||||||
const TPS = 60;
|
const TPS = 20;
|
||||||
|
|
||||||
const MOVE_MAP = {
|
const MOVE_MAP = {
|
||||||
'moveUp': 0,
|
'moveUp': 0,
|
||||||
|
@ -11,24 +11,23 @@ const MOVE_MAP = {
|
||||||
export default class Server {
|
export default class Server {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.dude = {
|
this.connections = [];
|
||||||
image: './assets/bunny.png',
|
this.connectedPlayers = new Map();
|
||||||
movement: [0, 0, 0, 0],
|
this.entities = [];
|
||||||
position: [50, 50],
|
|
||||||
};
|
|
||||||
this.frame = 0;
|
this.frame = 0;
|
||||||
this.last = Date.now();
|
this.last = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
accept({type, payload}) {
|
accept(connection, {type, payload}) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'connect': {
|
case 'connect': {
|
||||||
this.send({type: 'connected', payload: {dude: this.dude}});
|
this.send(connection, {type: 'connected', payload: Array.from(this.connectedPlayers.values())});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'action': {
|
case 'action': {
|
||||||
|
const connectedPlayer = this.connectedPlayers.get(connection);
|
||||||
if (payload.type in MOVE_MAP) {
|
if (payload.type in MOVE_MAP) {
|
||||||
this.dude.movement[MOVE_MAP[payload.type]] = payload.value;
|
connectedPlayer.movement[MOVE_MAP[payload.type]] = payload.value;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -36,6 +35,17 @@ export default class Server {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connectPlayer(connection) {
|
||||||
|
this.connections.push(connection);
|
||||||
|
const entity = {
|
||||||
|
image: './assets/bunny.png',
|
||||||
|
movement: [0, 0, 0, 0],
|
||||||
|
position: [50, 50],
|
||||||
|
};
|
||||||
|
this.entities.push(entity);
|
||||||
|
this.connectedPlayers.set(connection, entity);
|
||||||
|
}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
return setInterval(() => {
|
return setInterval(() => {
|
||||||
const elapsed = (Date.now() - this.last) / 1000;
|
const elapsed = (Date.now() - this.last) / 1000;
|
||||||
|
@ -45,16 +55,24 @@ export default class Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
tick(elapsed) {
|
tick(elapsed) {
|
||||||
this.dude.position[0] += SPEED * elapsed * (this.dude.movement[1] - this.dude.movement[3]);
|
for (const connection of this.connections) {
|
||||||
this.dude.position[1] += SPEED * elapsed * (this.dude.movement[2] - this.dude.movement[0]);
|
const {movement, position} = this.connectedPlayers.get(connection);
|
||||||
this.send({
|
position[0] += SPEED * elapsed * (movement[1] - movement[3]);
|
||||||
type: 'tick',
|
position[1] += SPEED * elapsed * (movement[2] - movement[0]);
|
||||||
payload: {
|
}
|
||||||
entities: {dude: this.dude},
|
for (const connection of this.connections) {
|
||||||
elapsed,
|
this.send(
|
||||||
frame: this.frame,
|
connection,
|
||||||
},
|
{
|
||||||
});
|
type: 'tick',
|
||||||
|
payload: {
|
||||||
|
entities: this.entities,
|
||||||
|
elapsed,
|
||||||
|
frame: this.frame,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
this.frame += 1;
|
this.frame += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,15 @@ const {
|
||||||
|
|
||||||
const wss = new WebSocketServer({port: SILPHIUS_WEBSOCKET_PORT});
|
const wss = new WebSocketServer({port: SILPHIUS_WEBSOCKET_PORT});
|
||||||
|
|
||||||
|
const server = new class SocketServer extends Server {
|
||||||
|
send(ws, data) { ws.send(JSON.stringify(data)); }
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start();
|
||||||
|
|
||||||
wss.on('connection', function connection(ws) {
|
wss.on('connection', function connection(ws) {
|
||||||
const server = new class SocketServer extends Server {
|
server.connectPlayer(ws);
|
||||||
send(data) { ws.send(JSON.stringify(data)); }
|
|
||||||
}
|
|
||||||
ws.on('message', function message(data) {
|
ws.on('message', function message(data) {
|
||||||
server.accept(JSON.parse(data));
|
server.accept(ws, JSON.parse(data));
|
||||||
});
|
});
|
||||||
server.start();
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
import Server from './server.js';
|
import Server from './server.js';
|
||||||
|
|
||||||
const server = new class WorkerServer extends Server {
|
const server = new class WorkerServer extends Server {
|
||||||
send(data) { postMessage(data); }
|
send(connection, data) { postMessage(data); }
|
||||||
}
|
}
|
||||||
|
|
||||||
onmessage = ({data}) => { server.accept(data); };
|
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
|
server.connectPlayer(undefined);
|
||||||
|
|
||||||
|
onmessage = ({data}) => { server.accept(undefined, data); };
|
||||||
|
|
Loading…
Reference in New Issue
Block a user