obstacles and player position drawn from data/levels.json file
parent
98b46f58fc
commit
2dfdbab898
|
|
@ -0,0 +1,171 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": "Level 1",
|
||||||
|
"robots":{
|
||||||
|
"player": {
|
||||||
|
"position": {
|
||||||
|
"x": 200,
|
||||||
|
"y": 200
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"obstacles": [
|
||||||
|
{
|
||||||
|
"vertices": [
|
||||||
|
{
|
||||||
|
"x": -100,
|
||||||
|
"y": -100
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 100,
|
||||||
|
"y": -100
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 100,
|
||||||
|
"y": 100
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": -100,
|
||||||
|
"y": 100
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 400,
|
||||||
|
"y": 300
|
||||||
|
},
|
||||||
|
"strokeColor": "#999999",
|
||||||
|
"fillColor": "#CCCCCC"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"vertices": [
|
||||||
|
{
|
||||||
|
"x": 300,
|
||||||
|
"y": 380
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 420,
|
||||||
|
"y": 380
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 350,
|
||||||
|
"y": 550
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 280,
|
||||||
|
"y": 420
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 400,
|
||||||
|
"y": -50
|
||||||
|
},
|
||||||
|
"strokeColor": "#999999",
|
||||||
|
"fillColor": "#CCCCCC"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"vertices": [
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": -10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 1010,
|
||||||
|
"y": -10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 1010,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": 10
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 500,
|
||||||
|
"y": 0
|
||||||
|
},
|
||||||
|
"strokeColor": "#999999",
|
||||||
|
"fillColor": "#CCCCCC"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"vertices": [
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": -10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 1010,
|
||||||
|
"y": -10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 1010,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": 10
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 500,
|
||||||
|
"y": 620
|
||||||
|
},
|
||||||
|
"strokeColor": "#999999",
|
||||||
|
"fillColor": "#CCCCCC"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"vertices": [
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 10,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 10,
|
||||||
|
"y": 610
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": 610
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 310
|
||||||
|
},
|
||||||
|
"strokeColor": "#999999",
|
||||||
|
"fillColor": "#CCCCCC"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"vertices": [
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 10,
|
||||||
|
"y": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": 10,
|
||||||
|
"y": 610
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"x": -10,
|
||||||
|
"y": 610
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"position": {
|
||||||
|
"x": 1000,
|
||||||
|
"y": 310
|
||||||
|
},
|
||||||
|
"strokeColor": "#999999",
|
||||||
|
"fillColor": "#CCCCCC"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
18
game.js
18
game.js
|
|
@ -6,6 +6,7 @@ const gameCanvas = document.getElementById("gameCanvas");
|
||||||
const ctx = gameCanvas.getContext("2d");
|
const ctx = gameCanvas.getContext("2d");
|
||||||
|
|
||||||
const gameWorld = new GameWorld();
|
const gameWorld = new GameWorld();
|
||||||
|
|
||||||
let monacoEditor;
|
let monacoEditor;
|
||||||
|
|
||||||
let pyodideWorker = startPyodideWorker();
|
let pyodideWorker = startPyodideWorker();
|
||||||
|
|
@ -95,8 +96,8 @@ function resetGame() {
|
||||||
|
|
||||||
// Reset the robots to their initial state
|
// Reset the robots to their initial state
|
||||||
robots = createInitialRobots();
|
robots = createInitialRobots();
|
||||||
gameWorld.reset();
|
gameWorld.reset(robots["player"]);
|
||||||
gameWorld.addRobot(robots["player"]);
|
//gameWorld.addRobot(robots["player"]);
|
||||||
|
|
||||||
// Clear the console
|
// Clear the console
|
||||||
consoleElement.innerHTML = "";
|
consoleElement.innerHTML = "";
|
||||||
|
|
@ -139,10 +140,6 @@ function gameLoop(timestamp) {
|
||||||
requestAnimationFrame(gameLoop);
|
requestAnimationFrame(gameLoop);
|
||||||
}
|
}
|
||||||
|
|
||||||
resetGame(); // Initialize the game and robots
|
|
||||||
// Start game loop
|
|
||||||
gameLoop();
|
|
||||||
|
|
||||||
// ✅ Update "distance" and "speed" every 2 seconds with random values
|
// ✅ Update "distance" and "speed" every 2 seconds with random values
|
||||||
function updateSensorData() {
|
function updateSensorData() {
|
||||||
const distance = Math.random() * 100; // Random distance (0-100)
|
const distance = Math.random() * 100; // Random distance (0-100)
|
||||||
|
|
@ -270,8 +267,17 @@ function setupCanvas() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call this function when the page loads
|
// Call this function when the page loads
|
||||||
|
|
||||||
|
fetch('/data/levels.json')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
gameWorld.levelData = data;
|
||||||
setupCanvas();
|
setupCanvas();
|
||||||
|
|
||||||
|
resetGame(); // Initialize the game and robots
|
||||||
|
// Start game loop
|
||||||
|
gameLoop();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
47
gameworld.js
47
gameworld.js
|
|
@ -10,7 +10,7 @@ export class GameWorld {
|
||||||
|
|
||||||
let ground = Matter.Bodies.rectangle(400, 600, 800, 50, { isStatic: true });
|
let ground = Matter.Bodies.rectangle(400, 600, 800, 50, { isStatic: true });
|
||||||
Matter.World.add(this.world, ground);
|
Matter.World.add(this.world, ground);
|
||||||
|
this.levelData = null;
|
||||||
|
|
||||||
this.obstacles = [];
|
this.obstacles = [];
|
||||||
this.robots = [];
|
this.robots = [];
|
||||||
|
|
@ -22,25 +22,42 @@ export class GameWorld {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reset() {
|
reset(player = null) {
|
||||||
this.robots = []
|
this.robots = []
|
||||||
this.obstacles = []
|
this.obstacles = []
|
||||||
Matter.World.clear(this.world); // Clear the world without resetting the engine
|
Matter.World.clear(this.world); // Clear the world without resetting the engine
|
||||||
console.log(this.obstacles.length);
|
|
||||||
this.addObstacle([
|
let level = this.levelData[0];
|
||||||
{ x: -100, y: -100 }, // Vertex 1
|
|
||||||
{ x: 100, y: -100 }, // Vertex 2
|
if (player) {
|
||||||
{ x: 100, y: 100 }, // Vertex 3
|
console.log("Resetting player position to:", level.robots["player"]["position"]);
|
||||||
{ x: -100, y: 100 } // Vertex 4
|
console.log(this.robots);
|
||||||
], { x: 400, y: 300 });
|
player.x = level.robots["player"]["position"]["x"];
|
||||||
|
player.y = level.robots["player"]["position"]["y"];
|
||||||
|
|
||||||
|
this.addRobot(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
this.addObstacle([
|
for (let i = 0; i < level.obstacles.length; i++) {
|
||||||
{ x: 300, y: 380 }, // Vertex 1
|
let obstacle = level.obstacles[i];
|
||||||
{ x: 420, y: 380 }, // Vertex 2
|
this.addObstacle(obstacle.vertices, obstacle.position);
|
||||||
{ x: 350, y: 550 }, // Vertex 3
|
}
|
||||||
{ x: 280, y: 420 } // Vertex 4
|
|
||||||
], { x: 400, y: -50 });
|
// this.addObstacle([
|
||||||
|
// { x: -100, y: -100 }, // Vertex 1
|
||||||
|
// { x: 100, y: -100 }, // Vertex 2
|
||||||
|
// { x: 100, y: 100 }, // Vertex 3
|
||||||
|
// { x: -100, y: 100 } // Vertex 4
|
||||||
|
// ], { x: 400, y: 300 });
|
||||||
|
|
||||||
|
|
||||||
|
// this.addObstacle([
|
||||||
|
// { x: 300, y: 380 }, // Vertex 1
|
||||||
|
// { x: 420, y: 380 }, // Vertex 2
|
||||||
|
// { x: 350, y: 550 }, // Vertex 3
|
||||||
|
// { x: 280, y: 420 } // Vertex 4
|
||||||
|
// ], { x: 400, y: -50 });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue