Skip to content

Commit

Permalink
fix: welcome page can also attack and keep alive
Browse files Browse the repository at this point in the history
  • Loading branch information
chiderlin committed Feb 25, 2025
1 parent 7a3f173 commit 4687f5c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
6 changes: 5 additions & 1 deletion docs/src/classes/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Entity {
* @param {typeof Theme.palette.player[keyof typeof Theme.palette.player]} [params.color] - Optional. Only applicable for players.
* @param {keyof typeof Constants.EntitySize} [params.size] - Optional. The size of the entity.
* @param {{ x: number, y: number }} [params.position] - Optional. If not provided, will be randomly placed.
* @param {boolean} params.canDie - Setting if the entity would die after hitting. default is true
*/
constructor(params) {
this.idx = params.idx;
Expand All @@ -27,6 +28,7 @@ class Entity {
this.status = Constants.EntityStatus.ALIVE;
this.speed = Settings.entity.speed;
this.isWalking = false;
this.canDie = params?.canDie ?? true; // default player will die after hitting

this.frameIdx = 0;
this.frameCtn = 0;
Expand Down Expand Up @@ -137,7 +139,9 @@ class Entity {
...hitEntities[Constants.EntityType.PLAYER],
...hitEntities[Constants.EntityType.ROBOT],
]) {
entity.status = Constants.EntityStatus.DIED;
if (this.canDie) {
entity.status = Constants.EntityStatus.DIED;
}
onHitEntity(entity);
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/src/classes/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Player extends Entity {
color: params?.color,
size: params?.size,
position: params?.position,
canDie: params?.canDie,
});

this.controls = params.controls;
Expand Down
22 changes: 0 additions & 22 deletions docs/src/pages/BasePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class BasePage {
this.background = params?.background || null;
this.bgm = params?.bgm || null;
this.players = [];
this.playerAvatars = [];
this.statusTextImages = [];
this.shapeType = params?.shapeType || Constants.EntityType.ROBOT;
}

Expand All @@ -25,26 +23,6 @@ class BasePage {
*/
setup() {
if (Store.getIsAllowSound()) this.initBgm();

for (let pIdx = 0; pIdx < Settings.players.length; pIdx++) {
const createPlayer = {
...this.playerParams,
idx: pIdx,
controls: Settings.players[pIdx].controls,
shapeType: this.shapeType,
size: Constants.EntitySize.M,
};

if (this.shapeType == Constants.EntityType.PLAYER) {
createPlayer.color = Object.values(Theme.palette.player)[pIdx];
}

const newPlayer = new Player(createPlayer);
this.players.push(newPlayer);

this.playerAvatars.push(Resources.images.playerlist[pIdx]);
this.statusTextImages.push(Resources.images.playerlist[pIdx]);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions docs/src/pages/Welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ class Welcome extends BasePage {
textParams: { label: 'Start Game' },
});

// initialize players
for (let pIdx = 0; pIdx < Settings.players.length; pIdx++) {
const createPlayer = {
...this.playerParams,
idx: pIdx,
controls: Settings.players[pIdx].controls,
shapeType: this.shapeType,
size: Constants.EntitySize.M,
canDie: false,
};

if (this.shapeType == Constants.EntityType.PLAYER) {
createPlayer.color = Object.values(Theme.palette.player)[pIdx];
}

const newPlayer = new Player(createPlayer);
this.players.push(newPlayer);
}

//delay the intro
this.welcomeIntro = new WelcomeIntro();
setTimeout(() => {
Expand Down Expand Up @@ -132,6 +151,12 @@ class Welcome extends BasePage {
this.pauseGame = false;
this.players.forEach((player) => player.setPauseState(false));
}
this.players.forEach((player) => {
console.log('player:', player);
player.keyPressed(event, [...this.players], (diedEntity) => {
console.log('diedEntity', diedEntity);
});
});
}

loadCheckImg(color, moveDown = 0) {
Expand Down
19 changes: 19 additions & 0 deletions docs/src/pages/maps/BaseMapGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ class BaseMapGame extends BasePage {
textAlign: [CENTER, CENTER],
});

// initialize players
for (let pIdx = 0; pIdx < Settings.players.length; pIdx++) {
const createPlayer = {
...this.playerParams,
idx: pIdx,
controls: Settings.players[pIdx].controls,
shapeType: this.shapeType,
size: Constants.EntitySize.M,
canDie: true,
};

if (this.shapeType == Constants.EntityType.PLAYER) {
createPlayer.color = Object.values(Theme.palette.player)[pIdx];
}

const newPlayer = new Player(createPlayer);
this.players.push(newPlayer);
}

// initialize robots
for (var rIdx = 0; rIdx < this.robotNumber; rIdx++) {
this.robots.push(
Expand Down

0 comments on commit 4687f5c

Please sign in to comment.