Skip to content

Commit

Permalink
Add load room feature but remaining problems:
Browse files Browse the repository at this point in the history
1. successful menu
2. obstacles and enemies need to be different each level
  • Loading branch information
shandy-del committed Mar 1, 2025
1 parent 37834e3 commit 32f90fb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 31 deletions.
2 changes: 2 additions & 0 deletions docs/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
<script src="../src/entities/Bullet.js"></script>
<script src="../src/entities/Obstacle.js"></script>
<script src="../src/entities/enemies/Enemy.js"></script>
<!--
<script src="../src/entities/enemies/Chaser.js"></script>
<script src="../src/entities/enemies/Shooter.js"></script>
-->
<script src="../src/entities/Door.js"></script>
<script src="../src/entities/Room.js"></script>

Expand Down
33 changes: 21 additions & 12 deletions docs/src/core/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const rooms = [
id: 2,
background: 'assets/background/room_level1.jpg',
savePoint: { x: 300, y: 200, w: 30, h: 30 }

}
];
let currentRoomIndex = 0;
Expand Down Expand Up @@ -46,7 +46,7 @@ function saveGameData() {

function loadGameData() {
const savedRoomIndex = localStorage.getItem('currentRoomIndex');
if(savedRoomIndex) {
if (savedRoomIndex) {
currentRoomIndex = parseInt(savedRoomIndex);
}

Expand All @@ -64,7 +64,7 @@ function loadGameData() {
player.position.y = savedPosition.position.y;
player.hp = JSON.parse(playerHp);
console.log("Game Loaded!");

menuDisplayed = false;
toggleButtons();
}
Expand Down Expand Up @@ -121,10 +121,12 @@ function resetGame() {
menuDisplayed = false;
isGamePaused = false;
gameOver = false;


currentRoomIndex = 0;

player = new Player(playerX, playerY);
room = new Room();
room.setup(rooms[currentRoomIndex]);
//room = new Room();
room.setup(rooms[currentRoomIndex]); // reset to the initial room
inputHandler = new InputHandler(room);
console.log("Game is reset!")
}
Expand All @@ -136,19 +138,26 @@ function isGameOver() {
function loadRoom() {
currentRoomIndex++;

if(currentRoomIndex >= rooms.length) {
//showEnding(); // TODO: show--successfully pass all levels
if (currentRoomIndex >= rooms.length) {
// TODO: showEnding(); // TODO: show--successfully pass all levels
print("Successfully Passed All Levels!");
return;
}


// Keep play hp (need or not)
// const prevHp = player.hp;
resetGame();
// player.hp = prevHp;
// TODO: 在player类中设置resetStatus函数,在除了宝箱房外的房间内调用
// Reset status of player (keep HP)
// player.resetStatus()
const prevHp = player.hp;
player = new Player(playerX, playerY);
player.hp = prevHp;

// Load level
// Load room
room.setup(rooms[currentRoomIndex]);




}

21 changes: 12 additions & 9 deletions docs/src/core/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ function preload() {
startMenuImg = loadImage('assets/background/menu_start.png');
closedDoorImg = loadImage('assets/door/door_close.png');
openDoorImg = loadImage('assets/door/door_open.png');
officeRoomImg = loadImage('assets/background/room_tutorial.png');
level1RoomImg = loadImage('assets/background/room_level1.jpg');

// officeRoomImg = loadImage('assets/background/room_tutorial.png');
// level1RoomImg = loadImage('assets/background/room_level1.jpg');
// Explicitly preload all room backgrounds
rooms.forEach((room, i) => {
room.backgroundImg = loadImage(room.background);
rooms[i] = room; // Ensure the reference is updated
});

}

function setup() {
Expand All @@ -27,7 +34,8 @@ function setup() {
inputHandler = new InputHandler(room);

setupMenu();
setupPauseMenu();
setupPauseMenu();

}

function draw() {
Expand All @@ -41,19 +49,14 @@ function draw() {
drawGameOver();
}
else {
//displayTutorial();

room.update();
inputHandler.update();
player.display();
drawUiHub();

checkSavePoint();

// TODO: Check if needed to load next room
if(player.collides(room.door) && room.door.isOpen) {
loadRoom();
}

}
}

Expand Down
6 changes: 6 additions & 0 deletions docs/src/entities/Door.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Door {
this.currentDoorImg = closedDoorImg;

this.isOpen = false;
this.updateImage();
}

display() {
Expand All @@ -17,4 +18,9 @@ class Door {

this.isOpen = true;
}

updateImage() {
image(this.currentDoorImg, this.position.x, this.position.y);
}

}
10 changes: 9 additions & 1 deletion docs/src/entities/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Player {
updateHp(newHp) {
this.hp = max(0, newHp);

if(this.hp === 0) {
if (this.hp === 0) {
deathSound.currentTime = 0;
deathSound.play();
}
Expand All @@ -82,4 +82,12 @@ class Player {
shootSound.currentTime = 0;
shootSound.play();
}

// resetStatus() {
// this.position.set(playerX, playerY); // 重置到初始位置
// this.speed = defaultSpeed; // 重置速度
// this.atk = defaultAtk; // 重置攻击力
// this.bullets = []; // 清空子弹

// }
}
29 changes: 21 additions & 8 deletions docs/src/entities/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,40 @@ class Room {

this.currentRoomData = null;
this.backgroundImg = null;
this.size = null;
}

setup(roomData) {
// Clear old objects, (need or not)
this.enemies = [];
this.obstacles = [];
// // Clear old objects, (need or not)
// this.enemies = [];
// this.obstacles = [];

// Load room configuration
this.backgroundImg = roomData.backgroundImg;
this.size = {
width: widthInPixel,
height: heightInPixel
};

// Load level configuration
this.door = new Door();
this.generateObstacles();
this.generateEnemies();

this.currentRoomData = roomData; // Store level data
this.currentRoomData = roomData; // Store room data
this.savePoint = new SavePoint(roomData.savePoint.x, roomData.savePoint.y);
this.backgroundImg = roomData.backgroundImg;
startTime = millis();

if(roomData.id == 1) {
startTime = millis();
}


}

update() {
// Use corresponding backgroundImg for current level
image(this.backgroundImg, 0, 0, widthInPixel, heightInPixel);
image(this.backgroundImg, 0, 0, this.size.width, this.size.height);
//image(officeRoomImg, 0, 0, widthInPixel, heightInPixel);

this.savePoint.display();
this.updateObstacles();
this.updateEnemies();
Expand All @@ -42,6 +53,7 @@ class Room {
generateObstacles() {
this.obstacles = [];
const maxEntitySize = heightInPixel / 8;
// TODO: obstacleCount不同关卡设为不一样的数值。根据难度或者其他条件
for (let i = 0; i < obstacleCount; i++) {
let x = random(leftBoundary, rightBoundary - maxEntitySize);
let y = random(topBoundary, bottomBoundary - maxEntitySize);
Expand All @@ -52,6 +64,7 @@ class Room {
generateEnemies() {
this.enemies = [];
const maxEntitySize = heightInPixel / 8;
// TODO: enemyCount不同关卡设为不一样的数值。根据难度或者其他条件
for (let i = 0; i < enemyCount; i++) {
let x = random(leftBoundary, rightBoundary - maxEntitySize);
let y = random(topBoundary, bottomBoundary - maxEntitySize);
Expand Down
2 changes: 1 addition & 1 deletion docs/src/ui/hud.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function drawCurrentLevel() {
textFont(uiFont, uiTextSize);
textAlign(LEFT, BOTTOM);
// text(`Level:${currentLevel}-${currentStage}`, hPadding, heightInPixel - vPadding);
text(`Level:${currentRoomIndex + 1} = 1`, hPadding, heightInPixel - vPadding);
text(`Level:${currentRoomIndex + 1} `, hPadding, heightInPixel - vPadding);

}

Expand Down

0 comments on commit 32f90fb

Please sign in to comment.