HumanBreak/src/plugin/utils.ts

54 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-11-16 23:01:23 +08:00
import { isNil } from 'lodash';
2022-11-19 18:15:42 +08:00
import { EVENT_KEY_CODE_MAP } from './keyCodes';
2022-11-16 23:01:23 +08:00
export default function init() {
return { has, getDamageColor };
}
/**
* undefined或null
* @param value
*/
export function has<T>(value: T): value is NonNullable<T> {
return !isNil(value);
}
/**
*
* @param damage
*/
export function getDamageColor(damage: number): string {
if (typeof damage !== 'number') return '#f00';
if (damage === 0) return '#2f2';
if (damage < 0) return '#7f7';
if (damage < core.status.hero.hp / 3) return '#fff';
if (damage < (core.status.hero.hp * 2) / 3) return '#ff4';
if (damage < core.status.hero.hp) return '#f22';
return '#f00';
}
2022-11-19 11:30:14 +08:00
/**
*
* @param canvas
* @param w
* @param h
*/
export function setCanvasSize(
canvas: HTMLCanvasElement,
w: number,
h: number
): void {
canvas.width = w;
canvas.height = h;
canvas.style.width = `${w}px`;
canvas.style.height = `${h}px`;
}
2022-11-19 18:15:42 +08:00
/**
* keycode对应的键
* @param key
*/
export function keycode(key: number) {
return EVENT_KEY_CODE_MAP[key];
}