self.load = function(path, universe) { // Load game replay file, 'path' should be either an object or a path (file or http) to a json file self.reset(); try { numberOfAlertsShown = 0; storm = gamejs.http.load("replay/addon.json"); var json = gamejs.http.load(path); self.maxShips = 0; fleetUniqueIdCounter = 1; json.players.forEach(function(player) { player.turn = 0; player.color = playerColors[player.id]; player.ships = 0; player.planets = 0; player.fleets = 0; self.players.set(player.id, new PlayerTurnInfo(player)); }); json.planets.forEach(function(planet) { planet.turn = 0; var pinfo = new PlanetTurnInfo(planet); pinfo.updatePlanet(); self.planets.set(pinfo.id, pinfo); }); universe.projectCoordinates(self.planets); self.turns = []; // Generate one turn info object per turn, to be able to show game at any turn without recalculating stuff all the time var currentInfo = new TurnInfo(self); self.turns.push(currentInfo); json.events.forEach(function(event) { event.turn++; while (currentInfo.number < event.turn) { self.maxShips = Math.max(self.maxShips, currentInfo.updateTurnState()); currentInfo = currentInfo.next(); self.turns.push(currentInfo); } if (event.turn == currentInfo.number) currentInfo.consumeEvent(event); }); self.maxShips = Math.max(self.maxShips, currentInfo.updateTurnState()); // Add one last turn to show correct final player ship counts currentInfo = currentInfo.next(); self.turns.push(currentInfo); self.maxShips = Math.max(self.maxShips, currentInfo.updateTurnState()); self.currentTurn = self.turns[0]; self.totalTurns = currentInfo.number; currentInfo.players.forEach(function(player) { if (!self.winningPlayer || self.winningPlayer.ships < player.ships) self.winningPlayer = player; }); self.turns.forEach(function(turnInfo) { turnInfo.players.forEach(function(player) { if (player.id != self.winningPlayer.id) self.loosingPlayersMaxShips = Math.max(self.loosingPlayersMaxShips, player.ships); }); }); } catch (err) { self.reset(); self.error = "Can't load game replay:\n" + err; } };
this.die = function(killer){ var name = null; while(!name){ name = prompt("Enter your name",""); } gamejs.http.post("save", { "name": name, "scores": self.scores }); window.location = "/over"; };
function main() { var display = gamejs.display.setMode([800, 450]); var font = new gamejs.font.Font('30px monospace'); var response = gamejs.http.load('foobar'); var text = 'Server says: ' + response.text; display.blit(font.render(text), [100, 100]); };
exports.init=function(){ if(!exports.all.length){ var lname=''; for(var i=0; i<resources.levels.length;i++){ lname=resources.levels[i]; var level; if(compiled[level]) level = compiled[level]; else level = gamejs.http.load('/levels/'+lname+'.json'); level.id = lname; exports[lname] = level; exports.all.push(level); } } }
var YWorld = exports.YWorld = function() { var self = this; this.agents = gamejs.http.load('./data/agents.json'); this.continents = gamejs.http.load('./data/continents.json'); this.connections = gamejs.http.load('./data/connections.json'); // FIXME store adjacent in extra 'connections' and resolve it here // at load time for easy indexOf() lookup var rawCities = gamejs.http.load('./data/cities.json'); this.cities = {}; this.populations = {}; $o.keys(this.continents).forEach(function(continent) { this.populations[continent] = 10000000; }, this); var $ = function(title) { return self.cities[title] || self.agents[title] || self.continents[title]; }; // convert raw cities into this.cities object $o.keys(rawCities).forEach(function(cityKey) { var $c = rawCities[cityKey]; $c.continent = $o.keys(this.continents).filter(function(continentKey) { return $(continentKey).indexOf(cityKey) > -1; })[0]; $c.infections = {}; $c.adjacent = []; $o.keys(this.continents).forEach(function(continent) { $c.infections[continent] = 0; }); this.cities[cityKey] = $c; }, this); // resolve connections this.connections.forEach(function(con) { var a = con[0]; var b = con[1]; if (!$(a) || !$(b)) return; $(a).adjacent.push(b); $(b).adjacent.push(a); }); /** * the resolves, create by the server, modify the world */ var APPLY = { setagentlocation: function(data) { $(data.agent).city = data.city; }, increaseinfection: function(data) { var $city = $(data.city); $city.infections[data.strain]++; }, decreaseinfection: function(data) { var $city = $(data.city); $city.infections[data.strain]--; if ($city.infections[data.strain] < 0) { gamejs.log('[World.apply] warning! infections < 0', data.strain,$city.infections[data.strain]); $city.infections[data.strain] = 0; } }, setpopulation: function(data) { this.populations[data.continent] = data.count; }, } /** * uses resolves to modify the world */ this.handle = function(event) { if (event.type === server.RESULT) { APPLY[event.data.type].apply(this, [event.data]); gamejs.info('[YWorld.handle]', event); if (event.data.agent) { $(event.data.agent).currentAction = null; } } else if (event.type === server.ACTION) { if (event.data.agent) { $(event.data.agent).currentAction = event.data; } } } return this; };