Skip to content

Commit

Permalink
Add in day structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mrspeaker committed Apr 27, 2014
1 parent 51fa3e2 commit 5d0b790
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 18 deletions.
17 changes: 13 additions & 4 deletions lib/Ω/screens/Screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var Screen = Ω.Class.extend({

loaded: true, // Set to false if you want to do async stuff
autoTick: true, // Tick the children by magic?
frame: 0,

bodies: null, // Holds new bodies to be added next tick
Expand All @@ -15,12 +16,10 @@

tick: function () {},

_tick: function () {
tickBodies: function () {

var self = this;

this.frame++;

if (this._bodies) {

// Erfph... make this all nicer, yo.
Expand Down Expand Up @@ -60,7 +59,7 @@
this._bodies[tag] = this._bodies[tag].filter(function (body) {

// Automagically remove any "remove"d entities
var stillAlive = body.tick(self.camera) && !(body.remove);
var stillAlive = body.tick() && !(body.remove);

// Add any children bodies
if (body.bodies) {
Expand All @@ -76,6 +75,16 @@
}
}

},

_tick: function () {

this.frame++;

if (this.autoTick) {
this.tickBodies();
}

this.tick();

},
Expand Down
10 changes: 6 additions & 4 deletions scripts/levels/Beach.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
hoverCell: null,
walkableSandCells: null,

init: function (width, length) {var this$0 = this;
init: function (width, length, env) {var this$0 = this;

this.walkableSandCells = this.sheet.cellW * 2;

Expand Down Expand Up @@ -37,6 +37,8 @@
this$0.map.cells[cell[1] + 1][cell[0]] = this$0.walkableSandCells + this$0.sheet.cellW + 1;
});

this.env = env;

},

setPlayer: function (player) {
Expand Down Expand Up @@ -76,7 +78,7 @@

//tick: function () { return true; },

tick: function (camera) {
tick: function () {

var player = this.player;

Expand All @@ -87,8 +89,8 @@
this.pos.x = player.x - (Ω.env.w / 2) + 24;
this.pos.y = player.y - (Ω.env.h / 2);

this.pos.x = Math.min(Math.max(0, this.pos.x), this.map.w - camera.w);
this.pos.y = Math.min(Math.max(-80, this.pos.y), this.map.h - camera.h);
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);

return true;

Expand Down
52 changes: 49 additions & 3 deletions scripts/screens/MainScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@

font: new Ω.Font("res/images/mamefont.png", 16, 16, "abcdefghijklmnopqrstuvwxyz0123456789 .,:!?'\"&<>$"),

state: null,
autoTick: false,
curTime: 0,

init: function () {

this.beach = this.add(new window.Beach(40, 10));
var env = {
x:0,
y:0,
w:Ω.env.w,
h:Ω.env.h - 100
};

this.beach = this.add(new window.Beach(40, 10, env));
this.add(this.beach.map, "map", 1);
this.player = this.add(new window.Player(Ω.env.w * 0.5, Ω.env.h * 0.2, this));
this.camera = new Ω.Camera(0, 0, Ω.env.w, Ω.env.h - 100);
Expand All @@ -17,10 +28,45 @@

this.cop = this.add(new window.SurfPatrol(Ω.env.w, 10, this));

this.state = new Ω.utils.State("BORN");

},

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");
break;
case "DAY":
this.curTime++;
if (this.curTime / 30 > 8) {
// Day over.
this.state.set("SUNSET");
} else {
this.tick_DAY();
}
break;
case "SUNSET":
this.state.set("DAYBREAK");
break;
case "GAMEOVER":
window.game.reset();
break;
}

},

tick_DAY: function () {

this.tickBodies();

this.camera.moveTo(
this.beach.pos.x,
this.beach.pos.y
Expand All @@ -29,11 +75,10 @@
if (this.cop.state.is("CHASE")) {
Ω.Physics.checkCollision(this.player, [this.cop]);
}

},

captured: function () {
window.game.reset();
this.state.set("GAMEOVER");
},

render: function (gfx) {
Expand All @@ -51,6 +96,7 @@
c.fillRect(0, top, Ω.env.w, 100);

this.font.render(gfx, "cash: $" + this.player.cash, 16, top + 16);
this.font.render(gfx, "time: " + (this.curTime | 0), 16, top + 36);

}

Expand Down
10 changes: 6 additions & 4 deletions src/levels/Beach.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
hoverCell: null,
walkableSandCells: null,

init: function (width, length) {
init: function (width, length, env) {

this.walkableSandCells = this.sheet.cellW * 2;

Expand Down Expand Up @@ -37,6 +37,8 @@
this.map.cells[cell[1] + 1][cell[0]] = this.walkableSandCells + this.sheet.cellW + 1;
});

this.env = env;

},

setPlayer: function (player) {
Expand Down Expand Up @@ -76,7 +78,7 @@

//tick: function () { return true; },

tick: function (camera) {
tick: function () {

let player = this.player;

Expand All @@ -87,8 +89,8 @@
this.pos.x = player.x - (Ω.env.w / 2) + 24;
this.pos.y = player.y - (Ω.env.h / 2);

this.pos.x = Math.min(Math.max(0, this.pos.x), this.map.w - camera.w);
this.pos.y = Math.min(Math.max(-80, this.pos.y), this.map.h - camera.h);
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);

return true;

Expand Down
52 changes: 49 additions & 3 deletions src/screens/MainScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@

font: new Ω.Font("res/images/mamefont.png", 16, 16, "abcdefghijklmnopqrstuvwxyz0123456789 .,:!?'\"&<>$"),

state: null,
autoTick: false,
curTime: 0,

init: function () {

this.beach = this.add(new window.Beach(40, 10));
var env = {
x:0,
y:0,
w:Ω.env.w,
h:Ω.env.h - 100
};

this.beach = this.add(new window.Beach(40, 10, env));
this.add(this.beach.map, "map", 1);
this.player = this.add(new window.Player(Ω.env.w * 0.5, Ω.env.h * 0.2, this));
this.camera = new Ω.Camera(0, 0, Ω.env.w, Ω.env.h - 100);
Expand All @@ -17,10 +28,45 @@

this.cop = this.add(new window.SurfPatrol(Ω.env.w, 10, this));

this.state = new Ω.utils.State("BORN");

},

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");
break;
case "DAY":
this.curTime++;
if (this.curTime / 30 > 8) {
// Day over.
this.state.set("SUNSET");
} else {
this.tick_DAY();
}
break;
case "SUNSET":
this.state.set("DAYBREAK");
break;
case "GAMEOVER":
window.game.reset();
break;
}

},

tick_DAY: function () {

this.tickBodies();

this.camera.moveTo(
this.beach.pos.x,
this.beach.pos.y
Expand All @@ -29,11 +75,10 @@
if (this.cop.state.is("CHASE")) {
Ω.Physics.checkCollision(this.player, [this.cop]);
}

},

captured: function () {
window.game.reset();
this.state.set("GAMEOVER");
},

render: function (gfx) {
Expand All @@ -51,6 +96,7 @@
c.fillRect(0, top, Ω.env.w, 100);

this.font.render(gfx, "cash: $" + this.player.cash, 16, top + 16);
this.font.render(gfx, "time: " + (this.curTime | 0), 16, top + 36);

}

Expand Down

0 comments on commit 5d0b790

Please sign in to comment.