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

implemented improved wall drawing

parent 438a7f6a
function MainAssistant(set){
this.levels = [];
this.level = [];
this.levelSet = set;
this.undoData = [];
this.levelSettings = null;
this.moves = 0;
this.levelSettings = null;
this.moves = 0;
this.currentLevel = -1;
this.width = 0;
this.height = 0;
this.keys = {
up: 113, // q
down: 97, // a
......@@ -18,8 +19,8 @@ function MainAssistant(set){
next: 0x6e, // n
prev: 0x70, // p
}
this.position = {
this.position = {
x: 0,
y: 0
};
......@@ -27,8 +28,8 @@ function MainAssistant(set){
}
MainAssistant.prototype.setup = function(){
this.levelSettings = new Mojo.Model.Cookie(this.levelSet);
this.levelSettings = new Mojo.Model.Cookie(this.levelSet.replace(" ", "_"));
this.menuItems = {
items: [{
label: $L('Restart Level'),
......@@ -41,7 +42,7 @@ MainAssistant.prototype.setup = function(){
command: 'previousLevel'
}, StageAssistant.appMenuModel.items[0]]
}
this.controller.setupWidget(Mojo.Menu.appMenu, {
omitDefaultItems: true
}, this.menuItems);
......@@ -50,8 +51,8 @@ MainAssistant.prototype.setup = function(){
this.ctx = this.canvas.getContext('2d');
this.ctx.save();
this.keyPressEventHandler = this.handleKeyPress.bind(this);
this.keyPressEventHandler = this.handleKeyPress.bind(this);
this.controller.listen(this.controller.document, 'keypress', this.keyPressEventHandler);
this.readLevels();
}
......@@ -61,8 +62,12 @@ MainAssistant.prototype.activate = function(event){
MainAssistant.prototype.deactivate = function(event){
Mojo.Event.stopListening(this.controller.document, 'keypress', this.keyPressEventHandler);
this.levelSettings.put({"level": this.currentLevel});
Mojo.Log.info("saving game state! level: %d", this.currentLevel);
this.levelSettings.remove();
this.levelSettings.put({
"level": this.currentLevel
});
var t = this.levelSettings.get();
Mojo.Log.info("saving game state! level: %d - %d", t.level, this.currentLevel);
}
MainAssistant.prototype.cleanup = function(event){
......@@ -83,18 +88,18 @@ MainAssistant.prototype.initLevel = function(l){
this.targets = [];
this.undoData = [];
this.moves = 0;
this.width = 0;
this.width = 0;
this.height = 0;
this.readLevel(l);
this.calculateBoxSize();
this.drawBackground();
this.updateStats();
this.updateStats();
this.ctx.restore();
this.ctx.save();
Mojo.Log.info("width: %d, blockSize: %d", this.width, this.blockSize);
Mojo.Log.info("width: %d, blockSize: %d", this.width, this.blockSize);
this.ctx.translate((320 - this.width * this.blockSize) / 2, (460 - (this.height * this.blockSize)) / 2);
this.drawLevel();
......@@ -132,13 +137,11 @@ MainAssistant.prototype.handleKeyPress = function(event){
break;
case this.keys.undo:
this.undoMove();
return;
case this.keys.next:
return;case this.keys.next:
Mojo.Controller.stageController.sendEventToCommanders(Mojo.Event.make(Mojo.Event.command, {
command: "nextLevel"
}));
return;
case this.keys.prev:
return;case this.keys.prev:
Mojo.Controller.stageController.sendEventToCommanders(Mojo.Event.make(Mojo.Event.command, {
command: "previousLevel"
}));
......@@ -148,6 +151,16 @@ MainAssistant.prototype.handleKeyPress = function(event){
this.position.x += move.x;
this.position.y += move.y;
if (this.position.x < 0)
this.position.y = 0;
if (this.position.y < 0)
this.position.y = 0;
if (this.position.x > this.width)
this.position.x = this.width;
if (this.position.y >= this.height)
this.position.y = this.height - 1;
if (!this.checkCollision(move, currentPos)) {
this.updateStats(1);
this.drawEmpty(currentPos.x, currentPos.y);
......@@ -177,7 +190,7 @@ MainAssistant.prototype.undoMove = function(){
this.drawEmpty(this.position.x, this.position.y);
this.position = figure;
this.drawFigure();
this.updateStats(-1);
this.updateStats(-1);
if (typeof box.item !== "undefined") {
this.drawEmpty(this.boxes[box.item].x, this.boxes[box.item].y);
......@@ -268,11 +281,11 @@ MainAssistant.prototype.levelFinished = function(){
}
MainAssistant.prototype.readLevel = function(l){
Mojo.Log.info("readLevel %d", l);
var level = this.levels[l];
Mojo.Log.info("level height: %s", level.length);
Mojo.Log.info("readLevel %d", l);
var level = this.levels[l];
Mojo.Log.info("level height: %s", level.length);
for (var i = 0; i < level.length; ++i) {
//Mojo.Log.info("line: %s", level[i]);
var e = level[i].split("");
......@@ -289,7 +302,7 @@ MainAssistant.prototype.calculateBoxSize = function(){
Mojo.Log.info("w: %d, h: %d", w, h);
this.blockSize = Math.floor(w / this.width);
}
......@@ -327,18 +340,18 @@ MainAssistant.prototype.drawLevel = function(){
this.position.y = l;
this.drawFigure();
break;
case "*":
this.level[l][e] = ".";
this.level[l][e] = ".";
this.drawBlock(e, l);
this.boxes.push({
"x": e,
"y": l,
"target": true,
});
break;
break;
case "+":
this.level[l][e] = ".";
this.level[l][e] = ".";
this.drawTarget(e, l);
this.targets.push({
"x": e,
......@@ -367,9 +380,149 @@ MainAssistant.prototype.drawEmpty = function(x, y){
this.ctx.fillRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
}
MainAssistant.prototype.checkLeft = function(x, y){
if (x == 0)
return false;
if (this.level[y][x - 1] == "#")
return true;
return false;
}
MainAssistant.prototype.checkRight = function(x, y){
if (x == this.level[y].length)
return true;
if (this.level[y][x + 1] == "#")
return true;
return false;
}
MainAssistant.prototype.checkUp = function(x, y){
if (y == 0)
return false;
if (this.level[y - 1][x] == "#")
return true;
return false;
}
MainAssistant.prototype.checkDown = function(x, y){
if (y == this.level.length - 1)
return false;
if (this.level[y + 1][x] == "#")
return true;
return false;
}
MainAssistant.prototype.getWallType = function(x, y){
var left = this.checkLeft(x, y);
var right = this.checkRight(x, y);
var up = this.checkUp(x, y);
var down = this.checkDown(x, y);
return (left << 0) + (right << 1) + (up << 2) + (down << 3);
}
MainAssistant.prototype.drawWall = function(x, y){
var t = this.getWallType(x, y);
var bs = this.blockSize;
this.ctx.save();
this.ctx.translate(x * bs, y * bs);
this.ctx.fillStyle = "rgb(0,0,0)";
this.ctx.fillRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
this.ctx.fillRect(0, 0, this.blockSize, this.blockSize);
this.ctx.fillStyle = "rgb(80,80,80)";
this.ctx.strokeStyle = "rgb(50,50,50)";
this.ctx.lineWidth = 4;
var d = Math.ceil(bs/3);
switch (t) {
// alone
case 0:
this.ctx.fillRect(d, d, d, d);
break;
// caps
case 1:
this.ctx.fillRect(0, (d), (d) * 2, d);
break;
case 2:
this.ctx.fillRect(d, d, d * 2, d);
break;
case 4:
this.ctx.fillRect(d, 0, (d), d * 2);
break;
case 8:
this.ctx.fillRect(d, d, d, d * 2)
break;
// straight
case 3:
this.ctx.fillRect(0, (d), bs, d);
break;
case 12:
this.ctx.fillRect((d), 0, d, bs);
break;
// corner
case 5:
this.ctx.fillRect((d), 0, d, d);
this.ctx.fillRect(0, d, (d) * 2, d);
break;
case 9:
this.ctx.fillRect(d, d, d, d * 2);
this.ctx.fillRect(0, d, d * 2, d);
break;
case 10:
this.ctx.fillRect((d), (d), d, d * 2);
this.ctx.fillRect((d), d, d * 2, d);
break;
case 6:
this.ctx.fillRect((d), (d), bs, d);
this.ctx.fillRect((d), 0, d, d);
break;
// intersection
case 7:
this.ctx.fillRect(0, d, bs, d);
this.ctx.fillRect(d, 0, d, d * 2);
break;
case 11:
this.ctx.fillRect(0, (d), bs, d);
this.ctx.fillRect(d, d, (d), d * 2);
break;
case 13:
this.ctx.fillRect(d, 0, (d), bs);
this.ctx.fillRect(0, (d), d * 2, d);
break;
case 14:
this.ctx.fillRect(d, 0, (d), bs);
this.ctx.fillRect(d, (d), d * 2, d);
break;
case 15:
this.ctx.fillRect(d, 0, (d), bs);
this.ctx.fillRect(0, d, bs, d);
break;
default:
}
this.ctx.restore();
//this.ctx.fillStyle = "rgb(0,255,255)";
//this.ctx.textBaseline = "middle";
//this.ctx.strokeText(t, (x * this.blockSize) + (this.blockSize / 2), (y * this.blockSize) + (this.blockSize / 2));
}
MainAssistant.prototype.drawBlock = function(x, y){
......@@ -377,7 +530,18 @@ MainAssistant.prototype.drawBlock = function(x, y){
this.ctx.fillStyle = "rgb(255,0,0)";
else
this.ctx.fillStyle = "rgb(255,255,0)";
this.ctx.fillRect(x * this.blockSize, y * this.blockSize, this.blockSize, this.blockSize);
var bs = this.blockSize;
this.ctx.save();
this.ctx.translate(x * bs, y * bs);
this.ctx.fillRect(0, 0, bs, bs);
this.ctx.lineWidth = 4;
this.ctx.lineCap = "square";
this.ctx.strokeRect(1, 1, bs - 2, bs - 2);
this.ctx.restore();
}
MainAssistant.prototype.drawTarget = function(x, y){
......@@ -386,19 +550,28 @@ MainAssistant.prototype.drawTarget = function(x, y){
}
MainAssistant.prototype.drawFigure = function(){
var bs = this.blockSize;
this.ctx.fillStyle = "rgb(0,0,255)";
this.ctx.fillRect(this.position.x * this.blockSize, this.position.y * this.blockSize, this.blockSize, this.blockSize);
this.drawEmpty(this.position.x, this.position.y);
this.ctx.fillStyle = "rgb(0,0,200)";
this.ctx.beginPath();
this.ctx.arc(this.position.x*bs+Math.ceil(bs/2), this.position.y*bs+Math.ceil(bs/2), bs/4, 0, 2*Math.PI);
this.ctx.fill();
this.ctx.beginPath();
this.ctx.arc(this.position.x*bs+Math.ceil(bs/2), this.position.y*bs+Math.ceil(bs/2), bs/2.5, 0, 2*Math.PI);
this.ctx.stroke();
}
MainAssistant.prototype.showLevelDoneDialog = function(){
var newRecord = this.highscores.set(this.levelSet, this.currentLevel, this.moves);
var SampleDialogAssistant = Class.create({
initialize: function(sceneAssistant, newRecord){
this.sceneAssistant = sceneAssistant;
this.controller = sceneAssistant.controller;
this.newRecord = newRecord;
this.newRecord = newRecord;
},
setup: function(widget){
......@@ -406,11 +579,11 @@ MainAssistant.prototype.showLevelDoneDialog = function(){
this.controller.setupWidget("done", {}, {
label: $L("Next level")
});
if (this.newRecord) {
$("text").innerHTML = "Well done. New highscore set!";
}
if (this.newRecord) {
$("text").innerHTML = "Well done. New highscore set!";
}
this.controller.listen("done", Mojo.Event.tap, this.handleDone.bindAsEventListener(this));
},
......@@ -431,42 +604,43 @@ MainAssistant.prototype.readLevels = function(){
var url = "levels/" + this.levelSet + ".sok"
var request = new Ajax.Request(url, {
method: 'get',
onSuccess: function(r) {
onSuccess: function(r){
var lines = r.responseText.split("\n");
var foundFirst = false;
var level = [];
Mojo.Log.info("lines: %d", lines.length);
for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
if (line.match(/^\s*#+.*#+\s/)) {
//Mojo.Log.info("line %s", lines[i]);
foundFirst = true;
level.push(line.replace(/\r/, ""));
}
else {
if (foundFirst) {
//Mojo.Log.info("split level");
this.levels.push(level.clone());
level.splice(0, level.length);
foundFirst = false;
}
}
}
var ol = this.levelSettings.get();
var l = 0;
Mojo.Log.info("type %s", typeof(ol));
if (typeof ol === "object" && typeof ol.level === "number" && ol.level >= 0) {
l = ol.level;
}
Mojo.Log.info("read %d level from file", this.levels.length);
var foundFirst = false;
var level = [];
Mojo.Log.info("lines: %d", lines.length);
for (var i = 0; i < lines.length; ++i) {
var line = lines[i];
if (line.match(/^\s*#+.*#+\s/)) {
//Mojo.Log.info("line %s", lines[i]);
foundFirst = true;
level.push(line.replace(/\r/, ""));
}
else {
if (foundFirst) {
//Mojo.Log.info("split level");
this.levels.push(level.clone());
level.splice(0, level.length);
foundFirst = false;
}
}
}
var ol = this.levelSettings.get();
var l = 0;
Mojo.Log.info("type %s", typeof(ol));
if (typeof ol === "object" && typeof ol.level === "number" && ol.level >= 0) {
Mojo.Log.info("type %d", ol.level);
l = ol.level;
}
Mojo.Log.info("read %d level from file", this.levels.length);
this.initLevel(l);
}
.bind(this) ,
onFailure: function(r){
......@@ -475,16 +649,16 @@ MainAssistant.prototype.readLevels = function(){
});
}
MainAssistant.prototype.updateStats = function(c) {
if (typeof c === "number") {
this.moves += c;
}
var e = $("stats");
if (e) {
e.innerHTML = "<div>Level: "+(this.currentLevel+1)+" Moves: "+this.moves+" Best: "+this.highscores.get(this.levelSet, this.currentLevel)+"</div>";
}
MainAssistant.prototype.updateStats = function(c){
if (typeof c === "number") {
this.moves += c;
}
var e = $("stats");
if (e) {
e.innerHTML = "<div>Level: " + (this.currentLevel + 1) + " Moves: " + this.moves + " Best: " + this.highscores.get(this.levelSet, this.currentLevel) + "</div>";
}
}
MainAssistant.prototype.handleCommand = function(e){
......
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