Commit a6baa7c9 authored by Christof Musik's avatar Christof Musik
Browse files

implemented undo move

parent 97a7857b
function MainAssistant(){
this.levels = [];
this.levelSet = "Sokoban";
this.undoData = [];
}
MainAssistant.prototype.setup = function() {
MainAssistant.prototype.setup = function(){
this.controller.setupWidget(Mojo.Menu.appMenu, {
omitDefaultItems: true
}, StageAssistant.appMenuModel);
this.canvas = document.getElementById('playfield');
this.ctx = this.canvas.getContext('2d');
this.ctx.save();
this.ctx.save();
this.controller.listen(this.controller.document, 'keypress', this.handleKeyPress.bind(this));
this.readLevels();
this.levelSettings = new Mojo.Model.Cookie("level");
this.levelSettings = new Mojo.Model.Cookie("level");
}
MainAssistant.prototype.activate = function(event){
......@@ -28,14 +30,14 @@ MainAssistant.prototype.cleanup = function(event){
}
MainAssistant.prototype.initLevel = function(set, l){
if (l < 0)
l = 0;
if (l > this.levels.length)
l = this.levels.length-1;
this.currentLevel = l;
this.levelSettings.put(l);
if (l < 0)
l = 0;
if (l > this.levels.length)
l = this.levels.length - 1;
this.currentLevel = l;
this.levelSettings.put(l);
this.levelDone = false;
this.level = [];
......@@ -55,17 +57,18 @@ MainAssistant.prototype.initLevel = function(set, l){
down: 97, // a
left: 107, // k
right: 108, // l
undo: 117, // u
}
this.readLevel(this.levels[l]);
this.calculateBoxSize();
this.drawBackground();
this.ctx.restore();
this.ctx.save();
this.ctx.translate((320-this.width*this.blockSize)/2, (460-(this.height*this.blockSize))/2);
this.ctx.translate((320 - this.width * this.blockSize) / 2, (460 - (this.height * this.blockSize)) / 2);
this.drawLevel();
}
......@@ -99,12 +102,17 @@ MainAssistant.prototype.handleKeyPress = function(event){
case this.keys.up:
move.y = -1;
break;
case this.keys.undo:
this.undoMove();
return;
default:
return;
}
this.position.x += move.x;
this.position.y += move.y;
if (!this.checkCollision(move)) {
if (!this.checkCollision(move, currentPos)) {
this.drawEmpty(currentPos.x, currentPos.y);
this.drawFigure();
}
......@@ -123,9 +131,29 @@ MainAssistant.prototype.handleKeyPress = function(event){
//Mojo.Log.info("x: %d, y: %d", this.position.x, this.position.y);
}
MainAssistant.prototype.checkCollision = function(move){
MainAssistant.prototype.undoMove = function() {
if (this.undoData.length) {
var move = this.undoData.pop();
var figure = move.figure;
var box = move.box;
this.drawEmpty(this.position.x, this.position.y);
this.position = figure;
this.drawFigure();
if (box) {
this.drawEmpty(this.boxes[box.item].x, this.boxes[box.item].y);
this.boxes[box.item] = box.pos;
this.drawBlock(this.boxes[box.item].x, this.boxes[box.item].y)
}
}
}
MainAssistant.prototype.checkCollision = function(move, currentPos){
var x = this.position.x;
var y = this.position.y;
var movedBox = null;
Mojo.Log.info("next block would be: %s", this.level[y][x]);
......@@ -155,6 +183,15 @@ MainAssistant.prototype.checkCollision = function(move){
return true;
}
movedBox = {
item: i,
pos: {
"x": this.boxes[i].x,
"y": this.boxes[i].y,
"target": this.boxes[i].target,
}
};
// no collision with other boxes, check if on target
if (this.level[by][bx] == ".") {
this.boxes[i].target = true;
......@@ -163,6 +200,7 @@ MainAssistant.prototype.checkCollision = function(move){
this.boxes[i].target = false;
}
// move box
this.boxes[i].x = bx;
this.boxes[i].y = by;
this.drawBlock(bx, by);
......@@ -170,6 +208,12 @@ MainAssistant.prototype.checkCollision = function(move){
break;
}
}
this.undoData.push({
figure: Object.clone(currentPos),
box: Object.clone(movedBox)
});
return false;
}
......@@ -271,9 +315,9 @@ MainAssistant.prototype.drawWall = function(x, y){
}
MainAssistant.prototype.drawBlock = function(x, y){
if (this.level[y][x] == ".")
if (this.level[y][x] == ".")
this.ctx.fillStyle = "rgb(255,0,0)";
else
else
this.ctx.fillStyle = "rgb(255,255,0)";
this.ctx.fillRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
}
......@@ -309,10 +353,7 @@ MainAssistant.prototype.showLevelDoneDialog = function(){
Mojo.Controller.stageController.sendEventToCommanders(Mojo.Event.make(Mojo.Event.command, {
command: "nextLevel"
}));
}
});
var dialog = this.controller.showDialog({
template: 'Main/leveldone',
......@@ -327,9 +368,9 @@ MainAssistant.prototype.readLevels = function(){
onSuccess: function(r){
this.levels = r.responseText.split(";LEVEL");
this.levels.splice(0, 1);
var l = this.levelSettings.get("level");
if (!(l > 0))
l = 0;
var l = this.levelSettings.get("level");
if (typeof l === "undefined")
l = 0;
this.initLevel("fo", l);
}
.bind(this) ,
......@@ -351,7 +392,7 @@ MainAssistant.prototype.handleCommand = function(e){
break;
case "previousLevel":
this.initLevel("foo", this.currentLevel - 1);
break;
break;
case 'cmdAbout':
this.controller.showAlertDialog({
onChoose: function(value){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment