add oxygen meter

This commit is contained in:
StrangeD0s
2023-02-18 14:14:23 +01:00
parent fcf9c5d2b7
commit 047877bf0d
11 changed files with 75 additions and 7 deletions
+38 -2
View File
@@ -185,6 +185,7 @@ function addAmmo(p) {
}
}
// * Check if Player is Swimming or Diving
function checkLiquids() {
var n, max, l
for (n = 0, max = liquids.length; n < max; n++) {
@@ -197,6 +198,38 @@ function checkLiquids() {
} else {
player.swimming = false
}
if (
overlap(
player.x,
player.y - player.height,
TILE,
TILE,
l.x,
l.y,
l.width,
l.height
)
) {
player.diving = true
reduceOxygen(4)
} else {
player.diving = false
replenishOxygen(16)
}
}
}
// * Reduce & Replenish Oxygen Functions
function reduceOxygen(rate) {
if (player.diving && globalObject.oxygen >= 0) {
globalObject.oxygen -= rate / 100
} else if (player.vul) {
reduceHitpoints(player, 1, 2000)
}
}
function replenishOxygen(rate) {
if (globalObject.oxygen <= globalObject.maxOxygen) {
globalObject.oxygen += rate / 100
}
}
@@ -252,7 +285,9 @@ function killMonster(monster) {
}
// * Take Damage Function
function reduceHitpoints(entity, damage) {
function reduceHitpoints(entity, damage, timeout) {
const milliceconds = timeout || 1500
if (
entity.player ? globalObject.hitpoints > 1 : entity.currentHitpoints > 1
) {
@@ -265,7 +300,7 @@ function reduceHitpoints(entity, damage) {
setTimeout(() => {
entity.vul = true
entity.hurt = false
}, 1500)
}, milliceconds)
} else killEntity(entity)
}
@@ -285,6 +320,7 @@ function killPlayer(player) {
player.y = player.start.y
player.dx = player.dy = 0
globalObject.hitpoints = player.maxHitpoints
globalObject.oxygen = globalObject.maxOxygen
globalObject.lifes--
console.log('log globalObject: ', globalObject.lifes)
}