Skip to content

Commit

Permalink
2nd room, room activation, room sleeping/waking
Browse files Browse the repository at this point in the history
  • Loading branch information
jywarren committed Dec 27, 2015
1 parent 750ddfd commit 9d091c3
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 74 deletions.
2 changes: 2 additions & 0 deletions index.html
Expand Up @@ -84,6 +84,8 @@
world.northRoom.read(map2);
world.northRoom.hide();

world.room.wake();


});

Expand Down
152 changes: 115 additions & 37 deletions pxlqst.js
Expand Up @@ -129,6 +129,21 @@ Pxlqst.Room = Class.extend({
}


room.things = function() {

var things = [];

room.tiles.forEach(function(tile) {

things = things.concat(tile.things);

});

return things;

}


room.opposite = function(direction) {

if (direction == 'n') return 's';
Expand Down Expand Up @@ -157,9 +172,9 @@ Pxlqst.Room = Class.extend({
}


// shift by one pixel width towards room.destination
// this needs to be synchronized with neighboring room...
room.pan = function() {
// Shift by one pixel width towards room.destination;
// execute callback() if arrived.
room.pan = function(callback) {

if (Math.abs(room.destination.x - room.x) > Math.abs(room.destination.y - room.y)) {
if (room.destination.x > room.x) room.x += 1;
Expand All @@ -173,7 +188,10 @@ Pxlqst.Room = Class.extend({
room.el.css('top', room.y * (room.world.roomWidth / world.tilesWide));

if (room.interval && room.destination.x == room.x && room.destination.y == room.y) {

clearInterval(room.interval);
if (callback) callback();

}

}
Expand All @@ -189,7 +207,7 @@ Pxlqst.Room = Class.extend({

room.interval = setInterval(function() {

room.pan();
room.pan(callback);

}, 100);

Expand Down Expand Up @@ -228,9 +246,7 @@ Pxlqst.Room = Class.extend({

var tile = new Pxlqst.Tile(x, y, room);

var ln = room.tiles.length;
room.tiles.push(tile);
console.log(ln, room.tiles.length, room.id);

}

Expand Down Expand Up @@ -262,6 +278,30 @@ console.log(ln, room.tiles.length, room.id);
}


// activate all things
room.wake = function() {

room.things().forEach(function(thing) {

if (thing.wake) thing.wake();

});

}


// deactivate all things
room.sleep = function() {

room.things().forEach(function(thing) {

if (thing.sleep) thing.sleep();

});

}


// create a door to the neighboring room, and a door leading back
// Break out door into subclass
room.addDoor = function(x, y) {
Expand All @@ -284,7 +324,6 @@ console.log(ln, room.tiles.length, room.id);

if (room.neighbors[direction]) {

console.log('room to ',direction);
neighbor = room.neighbors[direction];
counterpart = neighbor.tile(counterpart.x, counterpart.y);
counterpart.remove(counterpart.things[0]);
Expand Down Expand Up @@ -325,17 +364,17 @@ Pxlqst.Thing = Class.extend({

thing.tile = function() {

return room.tile(thing.x, thing.y);
return thing.room.tile(thing.x, thing.y);

}


// move to any tile, removing self from old tile
thing.move = function(_x, _y) {

thing.tile().remove(thing);
room.tile(_x, _y).add(thing);
thing.tile().remove(thing);

thing.room.tile(_x, _y).add(thing);

}

Expand Down Expand Up @@ -512,7 +551,6 @@ Pxlqst.World = Class.extend({
init: function() {

var world = this;
console.log('world created');


world.resize = function() {
Expand Down Expand Up @@ -579,9 +617,7 @@ Pxlqst.World = Class.extend({
world.room.y = -y;
world.room.show();

world.room.move(0, 0, function() {
oldRoom.hide();
});
oldRoom.sleep();

// record old position:
var you_x = world.you.x,
Expand All @@ -596,27 +632,43 @@ Pxlqst.World = Class.extend({
// remove You from old room:
world.you.tile().remove(world.you);

// add to new room, in new location:
console.log(world.room.tile(you_x, you_y), you_x, you_y);
// add You to new room, in new location:
world.room.tile(you_x, you_y).add(world.you);

// sleep everything from old room!
// adjust destination
world.you.destination.x = world.you.x;
world.you.destination.y = world.you.y;

// perform the move:
world.room.move(0, 0, function() {

oldRoom.hide();

// re-assign you.room:
world.you.room = world.room;

world.room.wake();

// adjust destination to new room
world.you.destination.x = you_x;
world.you.destination.y = you_y;

});

return world.room;

}


world.room = world.addRoom();

// add a "choose a profession" intro here
world.you = world.room.tile(8, 8).add(new Pxlqst.You(8, 8, 'thief', world.room));

world.resize();

$(window).on('resize', world.resize);

console.log('World created.');

return world;

}
Expand All @@ -634,14 +686,37 @@ Pxlqst.Actor = Pxlqst.Thing.extend({

// everyone gets 10 health to start with. EVERYONE
actor.health = 10;
actor.interval = 500;


// keeps x, y in bounds
actor.confineToRoom = function(_x, _y) {

if (_x < 0) _x = 0;
if (_y < 0) _y = 0;
if (_x > room.tilesWide) _x = room.tilesWide;
if (_y > room.tilesWide) _y = room.tilesWide;
if (_x > actor.room.tilesWide) _x = actor.room.tilesWide;
if (_y > actor.room.tilesWide) _y = actor.room.tilesWide;

}


actor.continuously = function(activity) {

actor.activity = activity;

}


actor.sleep = function() {

if (actor.timer) clearInterval(actor.timer);

}


actor.wake = function() {

actor.timer = setInterval(actor.activity, actor.interval);

}

Expand All @@ -654,7 +729,7 @@ Pxlqst.Actor = Pxlqst.Thing.extend({

actor.health -= strength;

console.log('I was hit and lost ', strength, ', leaving me at ', actor.health);
console.log('I was hit and lost ', strength, ', leaving me at ', actor.health);

setTimeout(function() {

Expand Down Expand Up @@ -683,9 +758,9 @@ Pxlqst.Enemy = Pxlqst.Actor.extend({
this.tryHit = function(newx, newy) {

// take hit
if (room.tile(newx, newy).has(Pxlqst.You)) {
if (enemy.room.tile(newx, newy).has(Pxlqst.You)) {

room.tile(newx, newy).has(Pxlqst.You).hit();
enemy.room.tile(newx, newy).has(Pxlqst.You).hit();

return true;

Expand All @@ -711,7 +786,7 @@ Pxlqst.Item = Pxlqst.Thing.extend({
item.take = function(_x, _y) {

// user.inventory.add(item);
console.log('you took the item!');
console.log('You took the item!');

}

Expand Down Expand Up @@ -761,10 +836,10 @@ Pxlqst.Rat = Pxlqst.Enemy.extend({
if (!rat.tile().nextTo(Pxlqst.Wall)) {

// run stupidly to nearest exterior room wall
if (rat.x < room.tilesWide / 2) newx -= 1;
else if (rat.x > room.tilesWide / 2) newx += 1;
else if (rat.y < room.tilesWide / 2) newy -= 1;
else if (rat.y > room.tilesWide / 2) newy += 1;
if (rat.x < rat.room.tilesWide / 2) newx -= 1;
else if (rat.x > rat.room.tilesWide / 2) newx += 1;
else if (rat.y < rat.room.tilesWide / 2) newy -= 1;
else if (rat.y > rat.room.tilesWide / 2) newy += 1;

// rats are tentative!
} else if (Math.random() > 0.2 || rat.running) {
Expand All @@ -784,7 +859,7 @@ Pxlqst.Rat = Pxlqst.Enemy.extend({
rat.confineToRoom(newx, newy);

// don't go through walls, or obstacles (stone for now)
if (!room.tile(newx, newy).has(Pxlqst.Wall) && !room.tile(newx, newy).has(Pxlqst.Stone)) {
if (!rat.room.tile(newx, newy).has(Pxlqst.Wall) && !rat.room.tile(newx, newy).has(Pxlqst.Stone)) {

// try to hit You, but if not, go to newx, newy
if (!rat.tryHit(newx, newy)) {
Expand All @@ -796,7 +871,7 @@ Pxlqst.Rat = Pxlqst.Enemy.extend({
}


rat.interval = setInterval(rat.wander, 500);
rat.continuously(rat.wander);

return rat;

Expand Down Expand Up @@ -851,7 +926,7 @@ Pxlqst.You = Pxlqst.Actor.extend({
else newy = you.y - 1;
}

var tile = room.tile(newx, newy);
var tile = you.room.tile(newx, newy);

// don't go through walls (do this in Actor ? but ghosts!)
if (!tile.has(Pxlqst.Wall)) {
Expand All @@ -878,7 +953,8 @@ Pxlqst.You = Pxlqst.Actor.extend({
} else {

// you get it!
console.log('took', item.cssClass);
console.log('Took ', item.cssClass);

you.take(item);

}
Expand All @@ -905,7 +981,7 @@ Pxlqst.You = Pxlqst.Actor.extend({

if (you.x == you.destination.x && you.y == you.destination.y) {

console.log('you arrive at ', you.x, you.y);
console.log('You arrive at ', you.x, you.y);

callback = you.destination.callback; // save the callback so we can call it
you.destination = undefined; // but clear the dest before calling the callback
Expand Down Expand Up @@ -993,6 +1069,8 @@ Pxlqst.Zombie = Pxlqst.Enemy.extend({

zombie.cssClass = 'zombie';

zombie.interval = 1500;


zombie.wander = function() {

Expand All @@ -1004,7 +1082,7 @@ Pxlqst.Zombie = Pxlqst.Enemy.extend({

zombie.confineToRoom(newx, newy);

if (!room.tile(newx, newy).has(Pxlqst.Wall) && !room.tile(newx, newy).has(Pxlqst.Stone)) {
if (!zombie.room.tile(newx, newy).has(Pxlqst.Wall) && !zombie.room.tile(newx, newy).has(Pxlqst.Stone)) {

// try to hit You, but if not, go to newx, newy
if (!zombie.tryHit(newx, newy)) {
Expand All @@ -1015,8 +1093,8 @@ Pxlqst.Zombie = Pxlqst.Enemy.extend({

}

zombie.continuously(zombie.wander);

zombie.interval = setInterval(zombie.wander, 2000);

return zombie;

Expand Down

0 comments on commit 9d091c3

Please sign in to comment.