From a6ec1d560ef7b129ac8b238ce79a6db6f1c6f587 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 15 May 2015 22:27:06 -0400 Subject: [PATCH] Adding villages and village collection --- intro-to-crafty/src/components.js | 18 +++++++++++++++++- intro-to-crafty/src/game.js | 11 ++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/intro-to-crafty/src/components.js b/intro-to-crafty/src/components.js index a798b86..24c16ae 100644 --- a/intro-to-crafty/src/components.js +++ b/intro-to-crafty/src/components.js @@ -47,7 +47,8 @@ Crafty.c('PlayerCharacter', { this.requires('Actor, Fourway, Color, Collision') .fourway(4) .color('rgb(20, 75, 40)') - .stopOnSolids(); + .stopOnSolids() + .onHit('Village', this.visitVillage); }, stopOnSolids: function() { @@ -61,5 +62,20 @@ Crafty.c('PlayerCharacter', { this.x -= this._movement.x; this.y -= this._movement.y; } + }, + + visitVillage: function(data) { + data[0].obj.collect(); + } +}); + +Crafty.c('Village', { + init: function() { + this.requires('Actor, Color') + .color('rgb(170, 125, 40)'); + }, + + collect: function() { + this.destroy(); } }); diff --git a/intro-to-crafty/src/game.js b/intro-to-crafty/src/game.js index 3011373..75fa8f2 100644 --- a/intro-to-crafty/src/game.js +++ b/intro-to-crafty/src/game.js @@ -22,15 +22,24 @@ Game = { Crafty.e('PlayerCharacter').at(5, 5); + var max_villages = 5; + for (var x = 0; x < Game.map_grid.width; x++) { for (var y = 0; y < Game.map_grid.height; y++) { - var at_edge = x == 0 || x == Game.map_grid.width - 1 || y == 0 || y == Game.map_grid.height - 1; + var at_edge = x == 0 || + x == Game.map_grid.width - 1 || + y == 0 || + y == Game.map_grid.height - 1; if (at_edge) { Crafty.e('Tree').at(x, y); } else if (Math.random() < 0.06) { Crafty.e('Bush').at(x, y); } + + if (Math.random() < 0.02 && Crafty('Village').length < max_villages) { + Crafty.e('Village').at(x, y); + } } } }