Open CyberLord09 opened 6 months ago
timerElement
is an HTML <div>
element created in the constructor of the Lava
class:
this.timerElement = document.createElement('div');
this.timerElement.style.position = 'absolute';
this.timerElement.style.fontFamily = 'Stencil Std, fantasy';
this.timerElement.style.fontSize = '24px';
this.timerElement.style.color = 'white';
this.timerElement.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
this.timerElement.style.padding = '10px 20px';
this.timerElement.style.borderRadius = '10px';
this.timerElement.style.boxShadow = '0 0 10px rgba(0, 0, 0, 0.5)';
this.timerElement.style.top = '70%';
this.timerElement.style.left = '50%';
this.timerElement.style.transform = 'translate(-50%, -50%)';
this.timerElement.style.display = 'none'; // Initially hidden
document.body.appendChild(this.timerElement);
top
and left
properties and transform: translate(-50%, -50%)
to center it.timerElement
is hidden (display: none
). It is then shown when the level is loaded.startTimer
method is responsible for counting down and updating the timerElement
every second:
startTimer() {
setInterval(() => {
this.timeUntilRise -= 1000; // Decrease the time remaining by 1 second
if (this.timeUntilRise <= 0) {
this.timeUntilRise = 0;
this.initialDelayElapsed = true; // Mark the initial delay as elapsed
this.timerElement.style.display = 'none'; // Hide the timer
} else if (this.timeUntilRise <= this.initialDelay) {
this.timerElement.style.display = 'block'; // Show the timer
}
this.timerElement.innerText = `TIME UNTIL LAVA RISES: ${this.timeUntilRise / 1000}s`;
}, 1000); // Execute this block every second (1000 milliseconds)
}
timeUntilRise
increments by 1000 milliseconds (1 second) each time the function runs.timeUntilRise
reaches 0, it sets initialDelayElapsed
to true
, indicating that the initial delay is over, and hides the timerElement
.timeUntilRise
is less than or equal to the initialDelay
but greater than 0, the timerElement
is displayed, showing the countdown.Update Method:
The update
method is called to raise the lava once the initial delay has elapsed:
update() {
if (this.initialDelayElapsed) {
// Calculate time passed since the last update
const currentTime = Date.now();
const deltaTime = currentTime - this.lastUpdateTime;
// Update the lava's position based on rising speed and delta time
this.islandY -= (this.risingSpeed * deltaTime) / 1000;
// Update last update time
this.lastUpdateTime = currentTime;
// Call collision checks
this.collisionChecks();
this.size();
}
}
initialDelayElapsed
is true
.deltaTime
) since the last update by comparing the current time (currentTime
) with the last recorded update time (this.lastUpdateTime
).islandY
position is decreased based on the risingSpeed
and deltaTime
. This basically moves the lava upwards at a rate defined by the risingSpeed
property.lastUpdateTime
is updated to the current time for the next cycle.As when the lava rises, if the EndGameObject is on the floor, it wouldn't be reachable. For this, we created a flying platform that has the Flag from our level.
The code is as follows:
import GameObject from './GameObject.js';
import GameEnv from './GameEnv.js';
import GameControl from './GameControl.js';
import BlockPlatform from './BlockPlatform.js';
export class FlyingIsland extends BlockPlatform {
constructor(canvas, image, data, xPercentage, yPercentage) {
super(canvas, image, data, xPercentage, yPercentage);
}
update() {
super.update();
}
draw() {
super.draw();
}
size() {
// Formula for Height should be on constant ratio, using a proportion of 832
const scaledWidth = this.canvas.width*0.75;
const scaledHeight = this.canvas.height*0.75;
const platformX = this.platformX;
const platformY = (GameEnv.bottom - scaledHeight) * this.platformY;
// set variables used in Display and Collision algorithms
this.bottom = platformY;
this.collisionHeight = scaledHeight;
this.collisionWidth = scaledWidth;
//this.canvas.width = this.width;
//this.canvas.height = this.height;
this.canvas.style.width = `${scaledWidth}px`;
this.canvas.style.height = `${scaledHeight}px`;
this.canvas.style.position = 'absolute';
this.canvas.style.left = `${platformX}px`;
this.canvas.style.top = `${platformY}px`;
}
}
export default FlyingIsland;
To break it down:
In GameSetup.js:
island: { src: "/images/platformer/platforms/island.png" },
xPercentage: 0.82, yPercentage: 0.55
With this, complications arised with the collisions between the player and the flag as the flag was raised above the ground. This meant that the previous conditions for collisions (player hits top and falls lower) did not work anymore. Due to this issue, we had to change the collision code to support collision at any place on the flag.
Pre-eruption Actions
Before the lava erupts, there are a couple of things that the game uses to inform the player that lava is rising. The first is a large warning symbol.
By creating a warning symbol element, which plays a gif, it is clear to the player that something is happening. To ensure that the players knows what the warning symbol is actually for, we have created text under the symbol that the player can see.
Player interaction with lava
In order for the player to interact with the lava, we added a case in playerGreece.js for lava collisions.
We also wanted to add a twist, that the player doesn't just die the first time he hits the lava. For this reason, we created an HP bar, and also caused the player to jump on a collision with lava.
The HP bar is drawn in the drawHPbox function, and the parameters are placed in the constructor. For the sake of brevity, that code will not be shown here, but can be accessed in playerGreece.js
As for the collisions, if the player collides with the lava, there are two things that can happen. Note that the player has 3 health ticks before it dies. On the first and second tick (each doing 33 damage out of 99 total) the player is sent up in the air as a jump, and also loses 33 HP. On the third tick, the player loses all HP (clearing the HP bar), and then dies like normal.