Skip to content

Commit

Permalink
Reorganization of Game,Player,Map,DynamicObject
Browse files Browse the repository at this point in the history
As discussed in #24.

Private variables now prefixed with __ and methods and variables not
exposed to the player are prefixed with _. '_' is verboten, preventing
the player from accessing any non-revealed methods within Game, Player,
Map, and DynamicObject.

The remaining classes remain as-is.
  • Loading branch information
AlexNisnevich committed Jan 2, 2014
1 parent d8ab48f commit 405c9c5
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 411 deletions.
2 changes: 1 addition & 1 deletion levels/08_intoTheWoods.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"commandsIntroduced":
["map.getObjectTypeAt", "player.getX", "player.getY",
"output.write"],
"map.refresh"],
"mapProperties": {
"allowOverwrite": true
}
Expand Down
85 changes: 46 additions & 39 deletions levels/13_robotMaze.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getRandomInt(min, max) {
}

function startLevel(map) {
map.placePlayer(map.getWidth()-2, map.getHeight()-2);
map.placePlayer(map.getWidth()-1, map.getHeight()-1);

map.defineObject('robot', {
'type': 'dynamic',
Expand All @@ -29,44 +29,51 @@ function startLevel(map) {
},
'behavior': function (me) {
#BEGIN_EDITABLE#
if (me.pathFound) {
if (me.pathFound.length > 0) {
me.move(me.pathFound.shift());
} else {
me.move('down');
}
} else {
me.pathFound = [];
exploredSet = [];
frontier = [[[1, 1], []]];
frontierSet = ["1,1"];
for (var j=0; j<500; j++) {
node = frontier.shift(); frontierSet.shift();
state = node[0];
exploredSet.push(state.toString());
var moves = map.getAdjacentEmptyCells(state[0], state[1]);
for (var i = 0; i < moves.length; i++) {
var move = moves[i];
var child = move[0];
var direction = move[1];
if (exploredSet.indexOf(child.toString()) == -1 &&
frontierSet.indexOf(child.toString()) == -1) {
path = node[1].slice(0);
path.push(direction);
frontier.push([child, path.slice(0)]);
frontierSet.push(child.toString());
if (child[0] == map.getWidth() - 2 && child[1] == 7) {
me.pathFound = path;
me.done = true;
break;
}
}
}
if (me.done) {
break;
}
}
}
















































Expand Down
1 change: 1 addition & 0 deletions levels/16_pointers.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#BEGIN_PROPERTIES#
{
"commandsIntroduced": ["map.getDynamicObjects"],
"music": "Various_Artists_-_15_-_Slimeball_vomit"
}
#END_PROPERTIES#
Expand Down
7 changes: 4 additions & 3 deletions levels/17_superDrEvalBros.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#BEGIN_PROPERTIES#
{
"commandsIntroduced": ["player.move"],
"music": "Rolemusic_-_07_-_Beach_Wedding_Dance",
"mapProperties": {
"keyDelay": 25
Expand All @@ -24,13 +25,13 @@ function startLevel(map) {

map.placeObject(w-1, fl(h/2)-1, 'exit');

for (var x = 0; x < fl(w/2) - 3; x++) {
for (var x = 0; x < fl(w/2) - 5; x++) {
for (var y = fl(h/2); y < h; y++) {
map.placeObject(x, y, 'block');
}
}

for (var x = fl(w/2)+3; x <= w; x++) {
for (var x = fl(w/2) + 5; x <= w; x++) {
for (var y = fl(h/2); y < h; y++) {
map.placeObject(x, y, 'block');
}
Expand All @@ -50,7 +51,7 @@ function startLevel(map) {
}

}
map.startTimer(gravity, 40);
map.startTimer(gravity, 50);

function jump() {
#BEGIN_EDITABLE#
Expand Down
2 changes: 1 addition & 1 deletion levels/99_credits.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function startLevel(map) {

for (var i = 0; i < credits.length; i++) {
setTimeout(function (i) {
map.display.drawText(credits[i][0], credits[i][1], credits[i][2]);
map._display.drawText(credits[i][0], credits[i][1], credits[i][2]);
}, (i + 2) * 2000, i);
}
}
48 changes: 24 additions & 24 deletions scripts/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ ROT.Display.prototype.setupEventHandlers = function() {
// contentEditable is required for canvas elements to detect keyboard events
$(this.getContainer()).attr("contentEditable", "true");
this.getContainer().addEventListener("keydown", function(e) {
if (display.intro == true) {
game.start();
display.intro = false;
if (display._intro == true) {
game._start();
display._intro = false;
} else if (keys[e.keyCode] && game.map.getPlayer()) {
game.map.getPlayer().move(keys[e.keyCode], true);
}
Expand All @@ -53,7 +53,7 @@ ROT.Display.prototype.setupEventHandlers = function() {
// according to name (NOT according to the actual object literal!)
ROT.Display.prototype.drawObject = function (map, x, y, object) {
var type = object.type;
var definition = map.getObjectDefinition(type) || this.savedDefinitions[type];
var definition = map._getObjectDefinition(type) || this.savedDefinitions[type];

var symbol = definition.symbol;
var color = object.color || definition.color || "#fff";
Expand All @@ -67,11 +67,11 @@ ROT.Display.prototype.drawAll = function(map) {

var game = this.game;

// initialize grid
var grid = new Array(game.dimensions.width);
for (var x = 0; x < game.dimensions.width; x++) {
grid[x] = new Array(game.dimensions.height);
for (var y = 0; y < game.dimensions.height; y++) {
// _initialize grid
var grid = new Array(game._dimensions.width);
for (var x = 0; x < game._dimensions.width; x++) {
grid[x] = new Array(game._dimensions.height);
for (var y = 0; y < game._dimensions.height; y++) {
grid[x][y] = {
type: 'empty',
bgColor: 'black'
Expand All @@ -80,11 +80,11 @@ ROT.Display.prototype.drawAll = function(map) {
}

// place static objects
for (var x = 0; x < game.dimensions.width; x++) {
for (var y = 0; y < game.dimensions.height; y++) {
for (var x = 0; x < game._dimensions.width; x++) {
for (var y = 0; y < game._dimensions.height; y++) {
grid[x][y] = {
type: map.getGrid()[x][y].type,
bgColor: map.getGrid()[x][y].bgColor
type: map._getGrid()[x][y].type,
bgColor: map._getGrid()[x][y].bgColor
};
}
}
Expand All @@ -94,7 +94,7 @@ ROT.Display.prototype.drawAll = function(map) {
var obj = map.getDynamicObjects()[i];
grid[obj.getX()][obj.getY()] = {
type: obj.getType(),
bgColor: map.getGrid()[obj.getX()][obj.getY()].bgColor
bgColor: map._getGrid()[obj.getX()][obj.getY()].bgColor
};
}

Expand All @@ -104,21 +104,21 @@ ROT.Display.prototype.drawAll = function(map) {
grid[player.getX()][player.getY()] = {
type: 'player',
color: player.getColor(),
bgColor: map.getGrid()[player.getX()][player.getY()].bgColor
bgColor: map._getGrid()[player.getX()][player.getY()].bgColor
}
}

// draw grid
for (var x = 0; x < game.dimensions.width; x++) {
for (var y = Math.max(0, this.offset - map.getHeight()); y < game.dimensions.height; y++) {
for (var x = 0; x < game._dimensions.width; x++) {
for (var y = Math.max(0, this.offset - map.getHeight()); y < game._dimensions.height; y++) {
this.drawObject(map, x, y + this.offset, grid[x][y]);
}
}

// write error messages, if any
if (this.errors && this.errors.length > 0) {
for (var i = 0; i < this.errors.length; i++) {
var y = this.game.dimensions.height - this.errors.length + i;
var y = this.game._dimensions.height - this.errors.length + i;
this.drawText(0, y, this.errors[i]);
}
}
Expand All @@ -134,8 +134,8 @@ ROT.Display.prototype.drawPreviousLevel = function(map, offset) {
var grid = this.savedGrid;

if (grid) {
for (var x = 0; x < game.dimensions.width; x++) {
for (var y = 0; y < game.dimensions.height; y++) {
for (var x = 0; x < game._dimensions.width; x++) {
for (var y = 0; y < game._dimensions.height; y++) {
this.drawObject(map, x, y + offset, grid[x][y]);
}
}
Expand All @@ -144,14 +144,14 @@ ROT.Display.prototype.drawPreviousLevel = function(map, offset) {

ROT.Display.prototype.saveGrid = function (map) {
this.savedGrid = this.grid;
this.savedDefinitions = map.getObjectDefinitions();
this.savedDefinitions = map._getObjectDefinitions();
}

ROT.Display.prototype.playIntro = function (map, i) {
display = this;

if (i < 0) {
this.intro = true;
this._intro = true;
} else {
if (typeof i === 'undefined') { i = map.getHeight(); }
this.clear();
Expand All @@ -169,7 +169,7 @@ ROT.Display.prototype.playIntro = function (map, i) {
ROT.Display.prototype.fadeIn = function (map, speed, callback, i) {
var display = this;
var game = this.game;
var command = "%c{#0f0}> run " + game.levelFileNames[game.currentLevel - 1];
var command = "%c{#0f0}> run " + game._levelFileNames[game._currentLevel - 1];

if (i < -3) {
if (callback) { callback(); }
Expand Down Expand Up @@ -212,7 +212,7 @@ ROT.Display.prototype.writeStatus = function(text) {
ROT.Display.prototype.appendError = function(errorText, command) {
var map = this.game.map;
if (!command) {
command = "%c{#0f0}> run " + this.game.levelFileNames[this.game.currentLevel - 1];
command = "%c{#0f0}> run " + this.game._levelFileNames[this.game._currentLevel - 1];
}

this.offset -= 3;
Expand Down
Loading

0 comments on commit 405c9c5

Please sign in to comment.