From db480929aa9e300a89f7877c53f3948eb70d32b0 Mon Sep 17 00:00:00 2001 From: Mr Speaker Date: Sun, 27 Apr 2014 11:07:49 -0400 Subject: [PATCH] Reset over days --- index.html | 1 + "lib/\316\251/screens/Screen.js" | 6 +++ res/images/tiles.png | Bin 11371 -> 11371 bytes res/images/tiles.pxm | Bin 179738 -> 179738 bytes scripts/entities/Player.js | 4 ++ scripts/entities/SurfPatrol.js | 4 ++ scripts/levels/Beach.js | 77 ++++++++++++++++++------------- scripts/screens/MainScreen.js | 31 +++++++++++-- scripts/screens/PopupDialog.js | 44 ++++++++++++++++++ src/entities/Player.js | 4 ++ src/entities/SurfPatrol.js | 4 ++ src/levels/Beach.js | 75 ++++++++++++++++++------------ src/screens/MainScreen.js | 31 +++++++++++-- src/screens/PopupDialog.js | 44 ++++++++++++++++++ 14 files changed, 254 insertions(+), 71 deletions(-) create mode 100644 scripts/screens/PopupDialog.js create mode 100644 src/screens/PopupDialog.js diff --git a/index.html b/index.html index 97fc722..563ebe2 100644 --- a/index.html +++ b/index.html @@ -71,6 +71,7 @@ + diff --git "a/lib/\316\251/screens/Screen.js" "b/lib/\316\251/screens/Screen.js" index 6883d63..4c4a3f4 100644 --- "a/lib/\316\251/screens/Screen.js" +++ "b/lib/\316\251/screens/Screen.js" @@ -104,6 +104,12 @@ return body; }, + removeAll: function () { + if (this._bodies) { + this._bodies = null; + } + }, + get: function (tag) { return this._bodies[tag] || []; diff --git a/res/images/tiles.png b/res/images/tiles.png index f078575a8a2bd132f315dbc367be7fd31053155d..7240ef4af628b577905c38039ca4d8dea6e00a93 100644 GIT binary patch delta 25 hcmaDI@j7CI7!Q+?`DO_oX(pBpe6G7UoAa5-0RVHx2gCpX delta 25 hcmaDI@j7CI7!Q-B*=7kIX(pBnzGqT5oAa5-0RVXN2wDID diff --git a/res/images/tiles.pxm b/res/images/tiles.pxm index 9688ba9b53423f7b528b46267a4d66ecc4a4b3da..d5827ba2c094df9390e9d9d7f152daf06cc5117e 100644 GIT binary patch delta 134 zcmbRBhild!t__n}SeA=lytjEe%R@mXBlFEwV*ld7beXLTtJ|@_=D^K+CfzYsF>p08 zH+QnM)HO0Nbkj9)a>QG&M5NwRChdH8e7GakO+a+Pvr3o%hOSCN7Q^CI(KrPUfx# lx+cy>j=B~WmM*%^md0jIu1*FPPKNEfw=r(ty^SfJ9RR7PEO7t; delta 134 zcmbRBhild!t__n}SpG^*o3eR2%R@mXOS8>YV*ld7beXLTtMmTa#;=?AOuA#N;^yjN zZ02I(s%z}# -1) { - treasure = this.treasure[blockCellPos[1]][blockCellPos[0]] - if (treasure && removeIfFound) { - this.treasure[blockCellPos[1]][blockCellPos[0]] = 0; - } - } - return treasure; + treasure = this.treasure[blockCellPos[1]][blockCellPos[0]] + if (treasure && removeIfFound) { + this.treasure[blockCellPos[1]][blockCellPos[0]] = 0; + } + } + return treasure; }, dig: function (player, stage) { @@ -87,7 +102,7 @@ this.hoverCell = targetPixels; this.pos.x = player.x - (Ω.env.w / 2) + 24; - this.pos.y = player.y - (Ω.env.h / 2); + this.pos.y = player.y - (Ω.env.h / 2); this.pos.x = Math.min(Math.max(0, this.pos.x), this.map.w - this.env.w); this.pos.y = Math.min(Math.max(-80, this.pos.y), this.map.h - this.env.h); diff --git a/scripts/screens/MainScreen.js b/scripts/screens/MainScreen.js index 8370b51..062a093 100644 --- a/scripts/screens/MainScreen.js +++ b/scripts/screens/MainScreen.js @@ -12,6 +12,14 @@ init: function () { + this.reset(); + + }, + + reset: function () { + + this.removeAll(); + var env = { x:0, y:0, @@ -35,18 +43,24 @@ tick: function () { this.state.tick(); - console.log(this.state.get()); + switch (this.state.get()) { case "BORN": this.state.set("DAYBREAK"); break; case "DAYBREAK": - this.curTime = 0; - this.state.set("DAY"); + if (this.state.first()) { + this.tick_DAY(); + } + if (this.state.count === 30) { + this.curTime = 0; + window.game.setDialog(new window.PopupDialog("day break!")); + this.state.set("DAY"); + } break; case "DAY": this.curTime++; - if (this.curTime / 30 > 8) { + if (this.curTime / 100 > 8) { // Day over. this.state.set("SUNSET"); } else { @@ -54,7 +68,14 @@ } break; case "SUNSET": - this.state.set("DAYBREAK"); + if (this.state.first()) { + window.game.setDialog(new window.PopupDialog("Day is done.")); + + } + if (this.state.count > 10) { + this.state.set("DAYBREAK"); + this.reset(); + } break; case "GAMEOVER": window.game.reset(); diff --git a/scripts/screens/PopupDialog.js b/scripts/screens/PopupDialog.js new file mode 100644 index 0000000..c5b8b13 --- /dev/null +++ b/scripts/screens/PopupDialog.js @@ -0,0 +1,44 @@ +(function (Ω) { + + "use strict"; + + var PopupDialog = Ω.Dialog.extend({ + + init: function (msg, key, cb) { + + this._super(key, cb); + + this.msg = msg; + + }, + + tick: function (delta) { + + this.time += delta; + + if (this.time > 1.5 || (this.killKey && Ω.input.pressed(this.killKey))) { + Ω.input.release(this.killKey); + this.done(); + } + + }, + + render: function (gfx) { + + var c = gfx.ctx; + + c.fillStyle = "hsl(0, 60%, 60%)"; + c.strokeStyle = "#fff"; + c.fillRect(gfx.w * 0.15, gfx.h * 0.25, gfx.w * 0.7, gfx.h * 0.5); + c.strokeRect(gfx.w * 0.15, gfx.h * 0.25, gfx.w * 0.7, gfx.h * 0.5); + + c.fillStyle = "#fff"; + c.fillText(this.msg, gfx.w * 0.3, gfx.h * 0.5); + + } + + }); + + window.PopupDialog = PopupDialog; + +}(window.Ω)); diff --git a/src/entities/Player.js b/src/entities/Player.js index 91ac56d..7311b88 100644 --- a/src/entities/Player.js +++ b/src/entities/Player.js @@ -37,6 +37,10 @@ }, + reset: function () { + this.state.set("BORN"); + }, + tick: function () { this.state.tick(); diff --git a/src/entities/SurfPatrol.js b/src/entities/SurfPatrol.js index 4192827..92441ce 100644 --- a/src/entities/SurfPatrol.js +++ b/src/entities/SurfPatrol.js @@ -30,6 +30,10 @@ }, + reset: function () { + this.state.set("BORN"); + }, + tick: function () { this.state.tick(); diff --git a/src/levels/Beach.js b/src/levels/Beach.js index 855ac61..6bda334 100644 --- a/src/levels/Beach.js +++ b/src/levels/Beach.js @@ -13,7 +13,23 @@ this.walkableSandCells = this.sheet.cellW * 2; - var map = this.generateMap(width, length); + this.env = env; + this.cellW = width; + this.cellL = length; + + this.reset(); + + }, + + reset: function () { + + if (this.sunbathers) { + this.sunbathers.forEach((sb) => { + sb.remove = true; + }); + } + + var map = this.generateMap(this.cellW, this.cellL); this.map = map.map; this.treasure = map.treasure; @@ -21,23 +37,21 @@ this.h = this.map.h; this.pos = { - x: Ω.env.w / 2, - y: 0 - }; - - var bb = [this.add(new window.BeachBum( - Ω.math.snap(Ω.utils.rand(this.w), 32), - Ω.math.snap(Ω.utils.rand(this.h - 96), 32) + 32, - Ω.utils.rand(2) - ), "extras", 2) for (x of [1,2,3,4,5,6,7,8,9])]; + x: Ω.env.w / 2, + y: 0 + }; - bb.forEach((b) => { - var cell = this.map.getBlockCell([b.x, b.y]); - this.map.cells[cell[1]][cell[0]] = this.walkableSandCells + this.sheet.cellW + 1; - this.map.cells[cell[1] + 1][cell[0]] = this.walkableSandCells + this.sheet.cellW + 1; - }); + this.sunbathers = [this.add(new window.BeachBum( + Ω.math.snap(Ω.utils.rand(this.w), 32), + Ω.math.snap(Ω.utils.rand(this.h - 96), 32) + 32, + Ω.utils.rand(2) + ), "extras", 2) for (x of [1,2,3,4,5,6,7,8,9])]; - this.env = env; + this.sunbathers.forEach((b) => { + var cell = this.map.getBlockCell([b.x, b.y]); + this.map.cells[cell[1]][cell[0]] = this.walkableSandCells + this.sheet.cellW + 1; + this.map.cells[cell[1] + 1][cell[0]] = this.walkableSandCells + this.sheet.cellW + 1; + }); }, @@ -46,23 +60,24 @@ }, search: function (player, removeIfFound) { - var blockPixPos = [player.x, player.y + 24]; - var blockCellPos = this.map.getBlockCell(blockPixPos); - var blockType = this.map.getBlock(blockPixPos); - // Dodgy hack: if on first line == not searched - if (!removeIfFound && blockType < this.sheet.cellW) { - this.map.setBlock(blockPixPos, blockType + this.sheet.cellW); - } + var blockPixPos = [player.x, player.y + 24]; + var blockCellPos = this.map.getBlockCell(blockPixPos); + var blockType = this.map.getBlock(blockPixPos); + // Dodgy hack: if on first line == not searched + + if (!removeIfFound && blockType < this.sheet.cellW) { + this.map.setBlock(blockPixPos, blockType + this.sheet.cellW); + } // Any treasure? let treasure = 0; if (blockCellPos[1] > -1) { - treasure = this.treasure[blockCellPos[1]][blockCellPos[0]] - if (treasure && removeIfFound) { - this.treasure[blockCellPos[1]][blockCellPos[0]] = 0; - } - } - return treasure; + treasure = this.treasure[blockCellPos[1]][blockCellPos[0]] + if (treasure && removeIfFound) { + this.treasure[blockCellPos[1]][blockCellPos[0]] = 0; + } + } + return treasure; }, dig: function (player, stage) { @@ -87,7 +102,7 @@ this.hoverCell = targetPixels; this.pos.x = player.x - (Ω.env.w / 2) + 24; - this.pos.y = player.y - (Ω.env.h / 2); + this.pos.y = player.y - (Ω.env.h / 2); this.pos.x = Math.min(Math.max(0, this.pos.x), this.map.w - this.env.w); this.pos.y = Math.min(Math.max(-80, this.pos.y), this.map.h - this.env.h); diff --git a/src/screens/MainScreen.js b/src/screens/MainScreen.js index 8370b51..062a093 100644 --- a/src/screens/MainScreen.js +++ b/src/screens/MainScreen.js @@ -12,6 +12,14 @@ init: function () { + this.reset(); + + }, + + reset: function () { + + this.removeAll(); + var env = { x:0, y:0, @@ -35,18 +43,24 @@ tick: function () { this.state.tick(); - console.log(this.state.get()); + switch (this.state.get()) { case "BORN": this.state.set("DAYBREAK"); break; case "DAYBREAK": - this.curTime = 0; - this.state.set("DAY"); + if (this.state.first()) { + this.tick_DAY(); + } + if (this.state.count === 30) { + this.curTime = 0; + window.game.setDialog(new window.PopupDialog("day break!")); + this.state.set("DAY"); + } break; case "DAY": this.curTime++; - if (this.curTime / 30 > 8) { + if (this.curTime / 100 > 8) { // Day over. this.state.set("SUNSET"); } else { @@ -54,7 +68,14 @@ } break; case "SUNSET": - this.state.set("DAYBREAK"); + if (this.state.first()) { + window.game.setDialog(new window.PopupDialog("Day is done.")); + + } + if (this.state.count > 10) { + this.state.set("DAYBREAK"); + this.reset(); + } break; case "GAMEOVER": window.game.reset(); diff --git a/src/screens/PopupDialog.js b/src/screens/PopupDialog.js new file mode 100644 index 0000000..c5b8b13 --- /dev/null +++ b/src/screens/PopupDialog.js @@ -0,0 +1,44 @@ +(function (Ω) { + + "use strict"; + + var PopupDialog = Ω.Dialog.extend({ + + init: function (msg, key, cb) { + + this._super(key, cb); + + this.msg = msg; + + }, + + tick: function (delta) { + + this.time += delta; + + if (this.time > 1.5 || (this.killKey && Ω.input.pressed(this.killKey))) { + Ω.input.release(this.killKey); + this.done(); + } + + }, + + render: function (gfx) { + + var c = gfx.ctx; + + c.fillStyle = "hsl(0, 60%, 60%)"; + c.strokeStyle = "#fff"; + c.fillRect(gfx.w * 0.15, gfx.h * 0.25, gfx.w * 0.7, gfx.h * 0.5); + c.strokeRect(gfx.w * 0.15, gfx.h * 0.25, gfx.w * 0.7, gfx.h * 0.5); + + c.fillStyle = "#fff"; + c.fillText(this.msg, gfx.w * 0.3, gfx.h * 0.5); + + } + + }); + + window.PopupDialog = PopupDialog; + +}(window.Ω));