feat: Add terrain rendering with water and grass generation

This commit is contained in:
Kacper Kostka (aider)
2025-04-02 12:12:06 +02:00
parent 1ce5abc901
commit cde5301c2b
2 changed files with 43 additions and 6 deletions

View File

@@ -3,6 +3,10 @@
**********************************************************************/
function drawWorld() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw terrain first (water and grass)
drawTerrain();
drawGrid();
// resources
@@ -29,6 +33,38 @@ function drawWorld() {
});
}
function drawTerrain() {
ctx.save();
ctx.translate(canvas.width/2 + offsetX, canvas.height/2 + offsetY);
ctx.scale(scale, scale);
// Calculate visible area based on canvas size and scale
const visibleWidth = canvas.width / scale;
const visibleHeight = canvas.height / scale;
const startX = Math.floor((-offsetX / scale) - (visibleWidth / 2));
const startY = Math.floor((-offsetY / scale) - (visibleHeight / 2));
const endX = Math.ceil(startX + visibleWidth);
const endY = Math.ceil(startY + visibleHeight);
// Draw terrain cells
const cellSize = 10; // Size of each terrain cell
for (let x = startX; x <= endX; x += cellSize) {
for (let y = startY; y <= endY; y += cellSize) {
// Check if this position is water
if (isWater(x, y)) {
ctx.fillStyle = "rgba(0, 100, 255, 0.5)"; // Blue for water
ctx.fillRect(x, y, cellSize, cellSize);
} else {
ctx.fillStyle = "rgba(100, 200, 100, 0.3)"; // Green for grass
ctx.fillRect(x, y, cellSize, cellSize);
}
}
}
ctx.restore();
}
function drawGrid() {
ctx.save();
ctx.translate(canvas.width/2 + offsetX, canvas.height/2 + offsetY);