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

82 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-05-16 02:04:21 +00:00
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;
}
}
});
2015-05-16 02:12:32 +00:00
Crafty.c('Actor', {
init: function() {
this.requires('2D, Canvas, Grid');
}
});
2015-05-16 02:04:21 +00:00
Crafty.c('Tree', {
init: function() {
2015-05-16 02:20:39 +00:00
this.requires('Actor, Color, Solid')
2015-05-16 02:12:32 +00:00
.color('rgb(20, 125, 40)');
2015-05-16 02:04:21 +00:00
}
});
Crafty.c('Bush', {
init: function() {
2015-05-16 02:20:39 +00:00
this.requires('Actor, Color, Solid')
2015-05-16 02:12:32 +00:00
.color('rgb(20, 185, 40)');
2015-05-16 02:04:21 +00:00
}
});
2015-05-16 02:18:09 +00:00
Crafty.c('PlayerCharacter', {
init: function() {
2015-05-16 02:20:39 +00:00
this.requires('Actor, Fourway, Color, Collision')
2015-05-16 02:18:09 +00:00
.fourway(4)
2015-05-16 02:20:39 +00:00
.color('rgb(20, 75, 40)')
2015-05-16 02:27:06 +00:00
.stopOnSolids()
.onHit('Village', this.visitVillage);
2015-05-16 02:20:39 +00:00
},
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;
}
2015-05-16 02:27:06 +00:00
},
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();
2015-05-16 02:18:09 +00:00
}
});