onlinecodesimulator/gameworld.js

34 lines
1.1 KiB
JavaScript

export class GameWorld {
constructor() {
this.obstacles = [
{ x: 300, y: 300, width: 50, height: 50 }, // Example obstacle
{ x: 500, y: 400, width: 70, height: 70 } // Another obstacle
];
}
// Check if a point (x, y) intersects with any obstacles
isObstacle(x, y) {
return this.obstacles.some(
obj => x > obj.x && x < obj.x + obj.width && y > obj.y && y < obj.y + obj.height
);
}
// Return the floor color based on (x, y) coordinates
getFloorColor(x, y) {
return (x + y) % 50 < 25 ? "black" : "white"; // Example pattern
}
// Draw the game world (e.g., obstacles, background)
draw(ctx) {
// Draw obstacles
ctx.fillStyle = "gray"; // Obstacle color
this.obstacles.forEach(obstacle => {
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
});
// Optionally, draw the grid or other visual elements (e.g., floor color patterns)
ctx.fillStyle = "lightgray"; // Example background
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); // Draw background
}
}