Move the C&S thing into the attic
This commit is contained in:
81
oldstuff/intro-to-crafty/src/components.js
Normal file
81
oldstuff/intro-to-crafty/src/components.js
Normal file
@@ -0,0 +1,81 @@
|
||||
Crafty.c('Grid', {
|
||||
init: function() {
|
||||
this.attr({
|
||||
w: Game.map_grid.tile.width,
|
||||
h: Game.map_grid.tile.height
|
||||
})
|
||||
},
|
||||
|
||||
at: function(x, y) {
|
||||
if (x === undefined && y === undefined) {
|
||||
return {
|
||||
x: this.x / Game.map_grid.tile.width,
|
||||
y: this.y / Game.map_grid.tile.height
|
||||
}
|
||||
} else {
|
||||
this.attr({
|
||||
x: x * Game.map_grid.tile.width,
|
||||
y: y * Game.map_grid.tile.height
|
||||
});
|
||||
return this;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Crafty.c('Actor', {
|
||||
init: function() {
|
||||
this.requires('2D, Canvas, Grid');
|
||||
}
|
||||
});
|
||||
|
||||
Crafty.c('Tree', {
|
||||
init: function() {
|
||||
this.requires('Actor, Color, Solid')
|
||||
.color('rgb(20, 125, 40)');
|
||||
}
|
||||
});
|
||||
|
||||
Crafty.c('Bush', {
|
||||
init: function() {
|
||||
this.requires('Actor, Color, Solid')
|
||||
.color('rgb(20, 185, 40)');
|
||||
}
|
||||
});
|
||||
|
||||
Crafty.c('PlayerCharacter', {
|
||||
init: function() {
|
||||
this.requires('Actor, Fourway, Color, Collision')
|
||||
.fourway(4)
|
||||
.color('rgb(20, 75, 40)')
|
||||
.stopOnSolids()
|
||||
.onHit('Village', this.visitVillage);
|
||||
},
|
||||
|
||||
stopOnSolids: function() {
|
||||
this.onHit('Solid', this.stopMovement);
|
||||
return this;
|
||||
},
|
||||
|
||||
stopMovement: function() {
|
||||
this._speed = 0;
|
||||
if (this._movement) {
|
||||
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();
|
||||
}
|
||||
});
|
46
oldstuff/intro-to-crafty/src/game.js
Normal file
46
oldstuff/intro-to-crafty/src/game.js
Normal file
@@ -0,0 +1,46 @@
|
||||
Game = {
|
||||
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() {
|
||||
Crafty.init(Game.width(), Game.height());
|
||||
Crafty.background('rgb(249, 223, 125)');
|
||||
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user