box-o-sand/oldstuff/intro-to-crafty/src/game.js

47 lines
1016 B
JavaScript
Raw Normal View History

Game = {
2015-05-15 02:26:45 +00:00
map_grid: {
width: 24,
height: 16,
tile: {
width: 16,
height: 16
}
},
width: function() {
return this.map_grid.width * this.map_grid.tile.width;
},
height: function() {
return this.map_grid.height * this.map_grid.tile.height;
},
start: function() {
2015-05-15 02:26:45 +00:00
Crafty.init(Game.width(), Game.height());
Crafty.background('rgb(249, 223, 125)');
2015-05-16 02:18:09 +00:00
Crafty.e('PlayerCharacter').at(5, 5);
2015-05-16 02:27:06 +00:00
var max_villages = 5;
2015-05-15 02:26:45 +00:00
for (var x = 0; x < Game.map_grid.width; x++) {
for (var y = 0; y < Game.map_grid.height; y++) {
2015-05-16 02:27:06 +00:00
var at_edge = x == 0 ||
x == Game.map_grid.width - 1 ||
y == 0 ||
y == Game.map_grid.height - 1;
2015-05-15 02:26:45 +00:00
if (at_edge) {
2015-05-16 02:12:32 +00:00
Crafty.e('Tree').at(x, y);
2015-05-15 02:26:45 +00:00
} else if (Math.random() < 0.06) {
2015-05-16 02:12:32 +00:00
Crafty.e('Bush').at(x, y);
2015-05-15 02:26:45 +00:00
}
2015-05-16 02:27:06 +00:00
if (Math.random() < 0.02 && Crafty('Village').length < max_villages) {
Crafty.e('Village').at(x, y);
}
2015-05-15 02:26:45 +00:00
}
}
}
}