This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updatePhysics() { | |
// 1: updates self position and direction | |
if (!pongR.updateTimestamp) { // The first time this loop runs, pongR.updateTimestamp will be undefined | |
pongR.updateTimestamp = new Date().getTime(); | |
} | |
else { | |
var now = new Date().getTime(); | |
pongR.deltaTime = (now - pongR.updateTimestamp) / 1000; | |
//console.log("Delta time: " + pongR.deltaTime + ". Now: " + now + ", previous update: " + pongR.updateTimestamp); | |
pongR.updateTimestamp = now; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//process_input(player) : number // returns the increment on the y axis | |
test("Test process_input", function () { | |
// Test 1 | |
var commands1 = ["up", "up"]; | |
var inputs = [{ commands: commands1, sequenceNumber: 0}]; | |
var player = { inputs: inputs, lastProcessedInputId: -1 }; | |
var expectedIncrement = -20; | |
var observedIncrement = PongR.UnitTestPrototype.process_input(player, 10, 1); | |
deepEqual(observedIncrement, expectedIncrement); | |
deepEqual(player.lastProcessedInputId, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A single step of the client update loop (a frame) | |
function updateLoopStep() { | |
// Step 1: Clear canvas | |
pongR.canvasContext.clearRect(0, 0, pongR.settings.viewport.width, pongR.settings.viewport.height); | |
// Step 2: Handle user inputs (update internal model) | |
var playerInput = handleClientInputs(pongR.me); | |
if (playerInput !== null) { | |
// Step 3: Send the just processed input batch to the server. | |
sendInput(pongR.game.gameId, pongR.me.user.connectionId, playerInput); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setupMatch(opts) { | |
$("#waiter").remove(); | |
initMatch(opts); | |
performCountdown(3, startGame); | |
}; | |
// Initial setup of the match state | |
function initMatch(opts) { | |
pongR.game = new Game(opts.PlayRoomId, opts.Player1, opts.Player2, opts.BallDirection); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function () { | |
var username = @Html.Raw(Json.Encode(Model)); | |
PongR.createInstance(1200,600,username); | |
PongR.connect(); | |
}); | |
// Public methods | |
pongR.PublicPrototype.createInstance = function (width, height, username) { | |
pongR.settings = new Settings(width, height); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Starts the client update loop | |
function startUpdateLoop() { | |
if (pongR.goalTimestamp) { | |
var now = new Date().getTime(); | |
var timelapse = (now - pongR.goalTimestamp) / 1000; // in seconds | |
if (timelapse > pongR.settings.PAUSE_AFTER_GOAL) { // TODO Change here to adjust with network latency | |
pongR.goalTimestamp = undefined; | |
updateLoopStep(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Compute the next state for this game, based on player inputs and ball position | |
/// </summary> | |
/// <param name="game"></param> | |
private static void ProcessTick(Game game) | |
{ | |
// 1: Apply inputs received from players (and progressively remove them from the buffer) | |
// 2: Update ball position | |
// 3: Check for collisions and if collision, update ball status | |
// 4: If no collision, check for a goal condition and update status if goal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Run the physics update loop for each game registered in _games | |
/// </summary> | |
private static void ProcessGamesTick() | |
{ | |
DateTime timestamp; | |
foreach(var game in _games.Values) | |
{ | |
// If in one of the previous rounds we had a goal condition | |
if (_goalTimestamps.TryGetValue(game.GameId, out timestamp)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Notifier | |
{ | |
// Synchronize clients with the new authoritative game status | |
public static void UpdateClients(Game game) | |
{ | |
var packet = new UpdatePacket() | |
{ | |
Game = game, | |
//Timestamp = DateTime.UtcNow.Subtract(new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc)).TotalMilliseconds | |
Timestamp = DateTime.UtcNow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Invoked when a new client joins the system | |
/// </summary> | |
public void Joined() | |
{ | |
// 1: Add user to list of connected users | |
// 2: If waiting list is empty add user to waiting list | |
// 3: Else find an opponent (first in the waiting list) and remove him from the waiting list | |
// 4: Create room and assign both users | |
// 5: Create a group for this room |