Move the C&S thing into the attic

This commit is contained in:
Dan Buch
2015-07-25 14:41:14 -04:00
parent fc144fc403
commit af31512251
11 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
INTRO_ASSETS_ZIP := tmp/crafty_bng_tut_assets.zip
INTRO_ASSETS_EXTRACTED_README := tmp/crafty_bng_tut_assets/README
.PHONY: all
all: lib/crafty.js assets/README
.PHONY: serve
serve:
python3 -m http.server
lib/crafty.js:
curl -sSL -o $@ http://craftyjs.com/release/0.5.3/crafty.js
assets/README: $(INTRO_ASSETS_EXTRACTED_README)
rsync -av $(dir $(INTRO_ASSETS_EXTRACTED_README)) assets/
$(INTRO_ASSETS_EXTRACTED_README): $(INTRO_ASSETS_ZIP)
cd tmp && \
unzip -o $(notdir $(INTRO_ASSETS_ZIP)) && \
find . -type f | xargs touch
$(INTRO_ASSETS_ZIP):
curl -sSL -o $@ http://buildnewgames.com/assets/article//introduction-to-crafty/crafty_bng_tut_assets.zip

View File

@@ -0,0 +1,3 @@
# Intro To Crafty
As presented at http://buildnewgames.com/introduction-to-crafty/.

View File

View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<script src="lib/crafty.js"></script>
<script src="src/game.js"></script>
<script src="src/components.js"></script>
<script>
window.addEventListener('load', Game.start);
</script>
</head>
<body>
</body>
</html>

View 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();
}
});

View 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);
}
}
}
}
}

View File