loading gameInfo now from server, instead of hard-coding from browser

This commit is contained in:
Richard Harrington 2013-08-30 00:03:43 -04:00
parent 367aaa8c3f
commit 537d31b99b
3 changed files with 31 additions and 24 deletions

View File

@ -12,15 +12,6 @@
// TODO: Get rid of this.
var worlds;
// TODO: This game info should probably come from the server
// in a preliminary ajax call.
var GAME_INFO = {
robotRadius: 7,
robotRangeX: 256.0,
robotRangeY: 256.0,
gameSecondsPerTick: 0.0333333333333333333333333
}
var Geom = (function() {
var degreesToRadians = function(angle) {
return angle * Math.PI / 180;
@ -78,11 +69,16 @@
$.getJSON('init?programs=' + encodeURIComponent(programs))
.done(function(data) {
gameId = data['id'];
var gameInfo = data['game-info'];
fetch(function() {
currentWorld = queue.peek();
// TODO: Pass into this callback actual game info from the server.
// This returning of local constants is only temporary.
constructorCallback(GAME_INFO);
console.log(gameInfo)
constructorCallback({
robotRadius: gameInfo["ROBOT-RADIUS"],
robotRangeX: gameInfo["ROBOT-RANGE-X"],
robotRangeY: gameInfo["ROBOT-RANGE-Y"],
gameSecondsPerTick: gameInfo["*GAME-SECONDS-PER-TICK*"]
});
});
});
@ -98,9 +94,9 @@
var Animation = function(el, sounds, gameInfo) {
var width = parseInt(el.width);
var height = parseInt(el.height);
var roomForRobots = GAME_INFO.robotRadius * 2;
var arenaWidth = GAME_INFO.robotRangeX + roomForRobots;
var arenaHeight = GAME_INFO.robotRangeY + roomForRobots;
var roomForRobots = gameInfo.robotRadius * 2;
var arenaWidth = gameInfo.robotRangeX + roomForRobots;
var arenaHeight = gameInfo.robotRangeY + roomForRobots;
var scaleFactorX = width / arenaWidth;
var scaleFactorY = height / arenaHeight;
var scaleX = function(x) {
@ -110,10 +106,10 @@
return Math.round(y * scaleFactorY);
}
var offsetX = function(x) {
return scaleX(GAME_INFO.robotRadius + x);
return scaleX(gameInfo.robotRadius + x);
}
var offsetY = function(y) {
return scaleY(GAME_INFO.robotRadius + y);
return scaleY(gameInfo.robotRadius + y);
}
// TODO: regularize this here and on the server so that
@ -121,10 +117,10 @@
// like why are we using scaleFactorX here and don't need
// scaleFactorY?
var robotDisplayRadius = scaleX(GAME_INFO.robotRadius);
var shellDisplayRadius = scaleX(GAME_INFO.robotRadius * 0.3);
var gunDisplayLength = scaleX(GAME_INFO.robotRadius * 1.4);
var gunDisplayWidth = scaleY(GAME_INFO.robotRadius * 0.5);
var robotDisplayRadius = scaleX(gameInfo.robotRadius);
var shellDisplayRadius = scaleX(gameInfo.robotRadius * 0.3);
var gunDisplayLength = scaleX(gameInfo.robotRadius * 1.4);
var gunDisplayWidth = scaleY(gameInfo.robotRadius * 0.5);
var ctx = el.getContext('2d');
ctx.lineCap = 'square';
@ -296,6 +292,7 @@
setTimeout(function() {
$('#canvas').css({opacity: 1});
setTimeout(function() {
console.log(gameInfo);
startGame(gameInfo);
}, 500);
}, 500);

View File

@ -2,7 +2,7 @@
; MAX_ACCEL is in decimeters per second per second.
(def MAX-ACCEL 4.0)
(def ^:dynamic *GAME-SECONDS-PER-TICK* 0.03)
(def ^:dynamic *GAME-SECONDS-PER-TICK* 0.033)
; ROBOT-RANGE-X and -Y are in meters
(def ROBOT-RANGE-X 256.0)
@ -12,3 +12,6 @@
; SHELL-SPEED is in meters per second
(def SHELL-SPEED 25.0)
; Robot-radius is in meters.
(def ROBOT-RADIUS 7.0)

View File

@ -1,6 +1,7 @@
(ns robotwar.handler
(:use [compojure.core]
[clojure.string :only [split]])
[clojure.string :only [split]]
[robotwar.constants])
(:require [compojure.handler :as handler]
[ring.middleware.json :as middleware]
[ring.util.response :as response]
@ -9,6 +10,11 @@
[robotwar.world :as world]
[robotwar.browser :as browser]))
(def game-info {:ROBOT-RADIUS ROBOT-RADIUS
:ROBOT-RANGE-X ROBOT-RANGE-X
:ROBOT-RANGE-Y ROBOT-RANGE-Y
:*GAME-SECONDS-PER-TICK* *GAME-SECONDS-PER-TICK*})
(defn parse-program-names
"takes a string parameter from the browser and returns a seqence
of program keys"
@ -53,7 +59,8 @@
{:names (map name (keys source-programs/programs))}))
(GET "/init" [programs] (let [next-id (:next-id @games-store)]
(swap! games-store add-game programs)
(response/response {:id next-id})))
(response/response {:id next-id
:game-info game-info})))
(GET "/worlds/:id/:n" [id n] (response/response (take-drop-send
games-store
(Integer/parseInt id)