grechka-game/src3d/physics/water-detect.js

46 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { TILE } from '../core/constants.js';
import { state } from '../core/state.js';
import { getBlock } from '../world/world-storage.js';
import { playSound } from '../audio/sound-engine.js';
function isWaterAt(px, py) {
const gx = Math.floor(px / TILE);
const gy = Math.floor(py / TILE);
const b = getBlock(gx, gy);
return !!(b && (b.t === 'water' || b.t === 'swamp_water'));
}
function isSwampWaterAt(px, py) {
const gx = Math.floor(px / TILE);
const gy = Math.floor(py / TILE);
const b = getBlock(gx, gy);
return !!(b && b.t === 'swamp_water');
}
function updateWaterFlag(e) {
const cx = e.x + e.w / 2;
const wasInWater = e.inWater;
// В воде, если в воде хотя бы центр/ноги (чтобы корректно работать у поверхности)
const mid = isWaterAt(cx, e.y + e.h / 2);
const feet = isWaterAt(cx, e.y + e.h - 2);
e.inWater = mid || feet;
// Болотная вода
e.inSwampWater = isSwampWaterAt(cx, e.y + e.h / 2);
// Голова под водой — для кислорода/урона
e.headInWater = isWaterAt(cx, e.y + 4);
// Звук при падении в воду
if (e === state.player && !wasInWater && e.inWater && e.vy > 100) {
playSound('splash');
}
// Болотная вода — отравление
if (e === state.player && e.inSwampWater) {
state.player.hp -= 3 * (typeof dt !== 'undefined' ? dt : 0.016);
}
}
export { isWaterAt, isSwampWaterAt, updateWaterFlag };