terra/app/display.js

25 lines
709 B
JavaScript
Raw Normal View History

2014-08-17 03:23:48 +00:00
var _ = require('./util.js');
2014-08-22 23:20:41 +00:00
module.exports = function (canvas, grid, cellSize) {
2014-08-17 03:23:48 +00:00
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
_.each(grid, function (column, x) {
_.each(column, function (creature, y) {
if (creature) {
var color = creature.colorFn ?
creature.colorFn() :
creature.color + ',' + creature.energy / creature.maxEnergy;
ctx.fillStyle = 'rgba(' + color + ')';
if (creature.character) {
ctx.fillText(creature.character, x * cellSize, y * cellSize + cellSize);
} else {
ctx.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
}
});
});
};