Skip to content

Commit bf9d5d7

Browse files
committed
Don't know why these wern't commited before
1 parent 119b9f5 commit bf9d5d7

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

Client/multiplayer.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ function initMultiplayer() {
2626
});
2727

2828
// Handle game state updates
29-
socket.on('gameStateUpdate', (gameState) => {
29+
socket.on('gameStateUpdate', setGameState);
30+
function setGameState(gameState) {
3031
// Initialize local racer ID if not set
3132
if (!localRacerId) {
3233
const localRacerData = gameState.racers.find(r => r.id === clientId);
@@ -139,7 +140,7 @@ function initMultiplayer() {
139140
position: localRacer.position
140141
});
141142
}
142-
});
143+
}
143144

144145
// Handle game start
145146
socket.on('gameStart', (racers) => {
@@ -167,6 +168,23 @@ function initMultiplayer() {
167168
});
168169
}
169170
});
171+
172+
socket.on('replay', (replayStates) => {
173+
debug('Replay received', {
174+
replayStates: replayStates.length
175+
});
176+
replay = replayStates;
177+
clearInterval(raceloop);
178+
clearInter
179+
setTimeout(renderGameState, 0,0);
180+
function renderGameState(frame) {
181+
setGameState(replay[frame].gameState);
182+
render(tracks[trackNr - 1], racers, items, itemBoxes);
183+
if (frame < replay.length - 1) {
184+
setTimeout(renderGameState, replay[frame+1].time-replay[frame].time, frame+1);
185+
}
186+
}
187+
});
170188
}
171189
initMultiplayer();
172190

app.js

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,16 @@ function createRoomGameContext() {
190190
function createGameRoom(roomId) {
191191
const room = {
192192
id: roomId,
193+
clientUpdates: [],
193194
gameStarted: false,
194195
lastUpdate: Date.now(),
196+
replayStates: [],
195197
gameContext: createRoomGameContext(),
196198
update: function(){
199+
this.clientUpdates.forEach(update => {
200+
update.function(update.args);
201+
})
202+
this.clientUpdates = [];
197203
this.gameContext.items.forEach(function(item){
198204
baseGameContext.Items[item.type].update(item);
199205
});
@@ -321,41 +327,62 @@ io.on('connection', (socket) => {
321327
socket.on('playerUpdate', (data) => {
322328
const room = gameRooms.get(data.roomId);
323329
if (!room || !room.gameContext.racers.has(socket.id)) return;
324-
325-
// Update racer state
326-
const racer = room.gameContext.racers.get(socket.id);
327-
Object.assign(racer, data);
328-
room.lastUpdate = Date.now();
330+
room.clientUpdates.push({
331+
function: function(args){
332+
// Update racer state
333+
const racer = args.room.gameContext.racers.get(args.socketId);
334+
Object.assign(racer, args.data);
335+
if (racer.finished) {
336+
socket.emit('replay', args.room.replayStates);
337+
}
338+
},
339+
args: {
340+
room: room,
341+
socketId: socket.id,
342+
data: data
343+
}
344+
});
329345
});
330346

331347
// Handle item events
332348
socket.on('itemEvent', (data) => {
333349
const room = gameRooms.get(data.roomId);
334350
if (!room || !room.gameContext.racers.has(socket.id)) return;
335351

336-
debug('Item event', {
337-
roomId: data.roomId,
338-
playerId: socket.id,
339-
itemType: data.item.type,
340-
itemPosition: data.item.position,
341-
itemState: data.state,
342-
ownerId: data.item.ownerId,
343-
itemId: data.item.id
352+
room.clientUpdates.push({
353+
function: function(args){
354+
const newItem = new args.room.gameContext.Item(
355+
args.data.item.type,
356+
new args.room.gameContext.Point(args.data.item.position.x, args.data.item.position.y),
357+
args.data.item.target,
358+
args.data.item.ownerId,
359+
args.data.item.duration,
360+
new args.room.gameContext.Point(args.data.item.velocity.x, args.data.item.velocity.y),
361+
args.data.item.direction,
362+
args.data.item.state
363+
);
364+
newItem.id = data.item.id;
365+
room.gameContext.items.push(newItem);
366+
},
367+
args: {
368+
room: room,
369+
data: data
370+
}
344371
});
345-
const newItem = new room.gameContext.Item(data.item.type, new room.gameContext.Point(data.item.position.x, data.item.position.y), data.item.target, data.item.ownerId, data.item.duration, new room.gameContext.Point(data.item.velocity.x, data.item.velocity.y), data.item.direction, data.item.state);
346-
newItem.id = data.item.id;
347-
room.gameContext.items.push(newItem);
348372
});
349373
socket.on('itemUpdate', (data) => {
350374
const room = gameRooms.get(data.roomId);
351375
if (!room || !room.gameContext.racers.has(socket.id)) return;
352-
debug('Item update', {
353-
roomId: data.roomId,
354-
playerId: socket.id,
355-
itemId: data.itemId,
356-
dataToUpdate: data.dataToUpdate
376+
room.clientUpdates.push({
377+
function: function(args){
378+
const item = args.room.gameContext.items.find(item => item.id === args.data.itemId);
379+
Object.assign(item, args.data.dataToUpdate);
380+
},
381+
args: {
382+
room: room,
383+
data: data
384+
}
357385
});
358-
Object.assign(room.gameContext.items.find(item => item.id === data.itemId), data.dataToUpdate);
359386
});
360387

361388
// Handle disconnection
@@ -427,6 +454,7 @@ setInterval(() => {
427454
}
428455
// Send game state to all players
429456
io.to(roomId).emit('gameStateUpdate', gameState);
457+
room.replayStates.push({ time: now, gameState: gameState });
430458
}
431459
});
432460
}, 1000/10); // 10 times per second

0 commit comments

Comments
 (0)