added keys and doors (closes #5) with temp assets

This commit is contained in:
Nathan DECHER
2020-04-14 15:43:00 +02:00
parent a27b8fcd9e
commit c90bfd5103
7 changed files with 44 additions and 4 deletions
+3 -1
View File
@@ -12,6 +12,8 @@ const assetSpecs=[
{ name: 'portalB', filename: 'portal-b-anim.png', type: 'image' },
{ name: 'portalC', filename: 'portal-c-anim.png', type: 'image' },
{ name: 'portalD', filename: 'portal-d-anim.png', type: 'image' },
{ name: 'key', filename: 'key32.png', type: 'image' },
{ name: 'door', filename: 'door32.png', type: 'image' },
{ name: 'snake', filename: 'snake.json', type: 'json' },
{ name: 'levelList', filename: 'levelList.json', type: 'json' },
{ name: 'config', filename: 'config.json', type: 'json' },
@@ -19,7 +21,7 @@ const assetSpecs=[
];
const tasks=[
{ from: 'hole', type: 'tileset', steps: 3, tiles: ['base', 'ul', 'dr', 'dl', 'ur', 'l', 'r', 'd', 'u'] },
{ from: 'hole', type: 'tileset', steps: 1, tiles: ['base', 'ul', 'dr', 'dl', 'ur', 'l', 'r', 'd', 'u'] },
{ from: 'fire', type: 'animation', steps: 3 },
{ from: 'portalA', type: 'animation', steps: 3 },
{ from: 'portalB', type: 'animation', steps: 3 },
+26 -1
View File
@@ -1,4 +1,4 @@
const [EMPTY, FOOD, SUPER_FOOD, DECAY_FOOD, WALL, FIRE, FLAMMABLE, FLAMMABLE_S, HOLE, HOLE_S, PORTAL_A, PORTAL_A_S, PORTAL_B, PORTAL_B_S, PORTAL_C, PORTAL_C_S, PORTAL_D, PORTAL_D_S, SNAKE]=Array(255).keys();
const [EMPTY, FOOD, SUPER_FOOD, DECAY_FOOD, WALL, FIRE, FLAMMABLE, FLAMMABLE_S, HOLE, HOLE_S, PORTAL_A, PORTAL_A_S, PORTAL_B, PORTAL_B_S, PORTAL_C, PORTAL_C_S, PORTAL_D, PORTAL_D_S, KEY, DOOR, SNAKE]=Array(255).keys();
class SnekGame {
constructor(settings, canvas, rules) {
@@ -30,6 +30,8 @@ class SnekGame {
case 'B': return PORTAL_B;
case 'C': return PORTAL_C;
case 'D': return PORTAL_D;
case 'k': return KEY;
case 'K': return DOOR;
}
})();
}
@@ -101,6 +103,12 @@ class SnekGame {
} else {
this.portals={};
}
// add the keys
if(settings.keys) settings.keys.forEach(([x, y]) => this.world[x][y]=KEY);
// add the doors
if(settings.doors) settings.doors.forEach(([x, y]) => this.world[x][y]=DOOR);
}
// add the snake to the world
@@ -225,6 +233,8 @@ class SnekGame {
const portalB=assets.get('portalB');
const portalC=assets.get('portalC');
const portalD=assets.get('portalD');
const key=assets.get('key');
const door=assets.get('door');
const putTile=(x, y, tile) => this.ctx.drawImage(
tile,
offsetX+cellSize*x,
@@ -299,6 +309,13 @@ class SnekGame {
case PORTAL_D_S:
putTileAnim(x, y, portalD);
break;
case KEY:
putTile(x, y, key);
break;
case DOOR:
putTile(x, y, door);
break;
}
}
}
@@ -512,6 +529,9 @@ class SnekGame {
// you hit, you die
case WALL: return this.die("thought walls were edible", "hit a wall");
case FIRE: return this.die("burned to a crisp", "hit fire");
case DOOR: return this.die("forgot to OPEN the door", "hit a door");
// congratilations, you played yourself!
case SNAKE:
case HOLE_S:
case FLAMMABLE_S:
@@ -546,6 +566,11 @@ class SnekGame {
);
break;
// you eat, you destroy all doors
case KEY:
this.getTilesOfType(DOOR).forEach(([x, y]) => this.world[x][y]=EMPTY);
break;
// you eat, you grow
case FOOD:
// re-grow the snake partially (can't hit the tail, but it's there for all other intents and purposes