Skip to content

Instantly share code, notes, and snippets.

@ancestral
Created July 28, 2012 08:26
Show Gist options
  • Save ancestral/3192417 to your computer and use it in GitHub Desktop.
Save ancestral/3192417 to your computer and use it in GitHub Desktop.
Wesnoth Game Object: Game state in JavaScript
/*
* Wesnoth Game Object (WGO)
* by ancestral
*
*
* This is an experiment with writing an entire Wesnoth game, stored in an object.
*
* Yes, if you combine this with Wesnoth Canvas, I guess this means Wesnoth for the
* browser.
*
*
* The motto is “Start lean, write clean, be mean.”
*
* Currently, you can create a side and add a unit. As you can see, there’s a lot of
* fallback for missing information. The idea is WGOs need to be extensible and
* flexible, which is why you see objects being passed around a lot.
*
* For debugging and troubleshooting purposes you might want to make use of a console
* in your favorite modern browser to view the WGO.
*
* Remember, this is experimental. I may abandon it if I lose interest. Or I might
* continue to expand upon it and add features slowly. Maybe there will even be a
* documented API one day.
*
*
* “Being crazy isn’t enough.” — Dr. Seuss
*
*/
var Wesnoth = Wesnoth || (function() {
var currentHex = [ 4, 10 ]; // TODO: "get current hex" function
var counter = 0;
// Integer from 0 to num
function randomInt(num) {
return Math.round(Math.random() * num);
}
function chooseName(params) {
return "Bob"; // TODO: name generator
}
function chooseTraits(params) {
var a = [];
var t = [ "strong", "dextrous", "quick", "intelligent", "resilient", "healthy", "fearless" ];
for (var i = 0; i < params.number; i++) {
a.push(t.splice([randomInt(t.length-1)], 1));
}
return a;
}
function chooseUnit(params) {
return "Elvish Archer"; // TODO: Branch on params, return random unit id
}
return {
"faction": [ "Drakes", "Knalgan Alliance", "Loyalists", "Northerners", "Rebels", "Undead" ],
"history": [],
"meta": {},
"side": [],
addSide: function(params) {
var s = { "unit": [] };
for (var arg in params) {
s[arg] = params[arg];
}
if (!s.gold) { s.gold = 0 }
if (!s.name) { s.name = "Untitled" }
if (!s.controller) { s.controller = "human" }
if (!s.faction) { s.faction = Wesnoth.faction[randomInt(5)] }
s.addUnit = function(params) {
var u = {};
u.hex = currentHex;
u.gender = [ "male", "female" ][randomInt(1)];
for (var arg in params) {
u[arg] = params[arg];
}
if (!u.id) {
var c = {};
if (u.faction) { c.faction = u.faction }
if (u.race) { c.race = u.race }
if (u.alignment) { c.alignment = u.alignment }
if (u.usage) { c.usage = u.usage }
u.id = chooseUnit(c);
}
// TODO: lookup missing faction, race, alignnent and usage from knowing the unit id
if (!u.name) { u.name = chooseName({ "race": u.race, "gender": u.gender }) }
if (!u.traits) { u.traits = chooseTraits({ "race": u.race, "number": 2 }) }
u.wuid = ++counter; // Wesnoth unique id
s.unit.push(u);
};
Wesnoth.side.push(s);
},
removeSide: function(params) {
if (typeof params === "number") { Wesnoth.side.splice(params, 1) }
else {
for (var arg in params) {
// if all params are met on a side, that’s the side we want to remove
}
}
},
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment