onlinecodesimulator/gameworld.js

114 lines
3.7 KiB
JavaScript

export class GameWorld {
constructor() {
let polygon1 = [
{ x: 80, y: 100 }, // Vertex 1
{ x: 200, y: 100 }, // Vertex 2
{ x: 200, y: 200 }, // Vertex 3
{ x: 100, y: 200 } // Vertex 4
];
let polygon2 = [
{ x: 300, y: 400 }, // Vertex 1
{ x: 320, y: 450 }, // Vertex 2
{ x: 250, y: 550 }, // Vertex 3
{ x: 280, y: 420 } // Vertex 4
];
this.obstacles = [
polygon1, // Example obstacle
polygon2 // 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 true;
}
// 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.strokeStyle = "gray"; // Obstacle outline color
ctx.lineWidth = 2; // Optional: to make the outline thicker
this.obstacles.forEach(obstacle => {
ctx.beginPath();
ctx.moveTo(obstacle[0].x, obstacle[0].y); // Start at the first vertex
// Loop through the rest of the vertices and draw lines
for (let i = 1; i < obstacle.length; i++) {
ctx.lineTo(obstacle[i].x, obstacle[i].y);
}
// Close the path to form a closed polygon
ctx.closePath();
ctx.stroke(); // Draw the outline
});
}
rayCast(startX, startY, endX, endY) {
let closestIntersection = null;
// Loop through all obstacles
for (let i = 0; i < this.obstacles.length; i++) {
let obstacle = this.obstacles[i];
return this.lineIntersectsPolygon(startX, startY, endX, endY, obstacle);
}
return closestIntersection;
}
lineIntersection(line1Start, line1End, line2Start, line2End) {
let denom = (line1Start.x - line1End.x) * (line2Start.y - line2End.y) -
(line1Start.y - line1End.y) * (line2Start.x - line2End.x);
if (denom === 0) return null; // Lines are parallel
let t = ((line1Start.x - line2Start.x) * (line2Start.y - line2End.y) -
(line1Start.y - line2Start.y) * (line2Start.x - line2End.x)) / denom;
let u = ((line1Start.x - line2Start.x) * (line1Start.y - line1End.y) -
(line1Start.y - line2Start.y) * (line1Start.x - line1End.x)) / denom;
if (t >= 0 && t <= 1 && u >= 0 && u <= 1) {
let ix = line1Start.x + t * (line1End.x - line1Start.x);
let iy = line1Start.y + t * (line1End.y - line1Start.y);
return { x: ix, y: iy };
}
return null; // No intersection
}
lineIntersectsPolygon(startX, startY, endX, endY, polygon) {
let lineStart = { x: startX, y: startY };
let lineEnd = { x: endX, y: endY };
// Loop through all edges of the polygon
for (let i = 0; i < polygon.length; i++) {
let currentVertex = polygon[i];
let nextVertex = polygon[(i + 1) % polygon.length]; // Loop back to the first vertex
let intersection = this.lineIntersection(lineStart, lineEnd, currentVertex, nextVertex);
if (intersection) {
return intersection; // Return the first intersection found
}
}
return null; // No intersection with any edge of the polygon
}
}