super-hero/assets/cc-game/scripts/game_ui/HPBar.ts

100 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-04-19 03:49:41 -07:00
import { Color } from 'cc';
import { Sprite } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { UmLog } from '../../../cc-common/cc-util/UmLog';
import { Label } from 'cc';
2024-04-23 19:17:24 -07:00
import { Enum } from 'cc';
import { CCFloat } from 'cc';
import { size } from 'cc';
import { Size } from 'cc';
2024-05-08 02:31:19 -07:00
import { GameGlobalData } from '../global/GameGlobalData';
2024-04-19 03:49:41 -07:00
const { ccclass, property } = _decorator;
2024-04-23 19:17:24 -07:00
export enum PROGRESS_TYPE {
FILL = 0,
SLICED_HOR = 1,
SLICED_VER = 2
}
2024-04-19 03:49:41 -07:00
@ccclass('HPBar')
export class HPBar extends Component {
2024-04-23 19:17:24 -07:00
@property({ type: Enum(PROGRESS_TYPE) }) public progressType: PROGRESS_TYPE = PROGRESS_TYPE.FILL;
2024-04-19 03:49:41 -07:00
@property(Sprite) barProgress: Sprite = null!;
@property(Label) valueTxt: Label = null!;
2024-04-23 19:17:24 -07:00
@property(CCFloat) switchColorValue = 0.4;
2024-04-19 03:49:41 -07:00
@property(Color) public normalColor: Color = new Color(255, 255, 255, 255);
@property(Color) public warningColor: Color = new Color(255, 255, 255, 255);
maxHP = 1000;
currentHP = this.maxHP;
2024-04-23 19:17:24 -07:00
progressSize: Size;
protected onLoad(): void {
this.progressSize = new Size(this.barProgress?.node?.getContentSize());
}
2024-04-19 03:49:41 -07:00
public setMaxHPBar(maxHP: number, isResetHP = true) {
this.maxHP = maxHP;
if (isResetHP)
this.currentHP = maxHP;
this.updateProgress();
}
public decreaseHP(value: number) {
2024-05-08 02:31:19 -07:00
if (!GameGlobalData.Instance.isStatePlay()) return;
2024-04-19 03:49:41 -07:00
this.currentHP -= value;
this.currentHP = Math.max(0, this.currentHP);
// UmLog.log("decreaseHP => ",value, " | ", this.maxHP, " vs ", this.currentHP);
this.updateProgress();
}
public increaseHP(value: number) {
2024-05-08 02:31:19 -07:00
if (!GameGlobalData.Instance.isStatePlay()) return;
2024-04-19 03:49:41 -07:00
this.currentHP = Math.max(0, this.currentHP);
this.currentHP += value;
this.currentHP = Math.min(this.maxHP, this.currentHP);
2024-05-08 02:31:19 -07:00
// UmLog.log("increaseHP => ", this.maxHP, " vs ", this.currentHP);
2024-04-19 03:49:41 -07:00
this.updateProgress();
}
2024-05-08 02:31:19 -07:00
setHPValue(current, max)
2024-04-23 19:17:24 -07:00
{
this.maxHP = max;
this.currentHP = Math.max(0, current);
this.currentHP = Math.min(this.maxHP, this.currentHP);
this.updateProgress();
}
2024-04-19 03:49:41 -07:00
updateProgress() {
var progress = this.currentHP / this.maxHP;
progress = Math.min(1, progress);
2024-04-23 19:17:24 -07:00
this.barProgress.setColor(progress > this.switchColorValue ? this.normalColor : this.warningColor);
switch (this.progressType)
{
case PROGRESS_TYPE.FILL:
this.barProgress.fillRange = progress;
break;
case PROGRESS_TYPE.SLICED_HOR:
this.barProgress?.node?.setContentSize(new Size(this.progressSize.x * progress, this.progressSize.y));
break;
case PROGRESS_TYPE.SLICED_VER:
this.barProgress?.node?.setContentSize(new Size(this.progressSize.x, this.progressSize.y * progress));
break;
}
2024-04-19 03:49:41 -07:00
if (this.valueTxt)
this.valueTxt.string = `${this.currentHP.roundDigits(0).toString()}/${this.maxHP.roundDigits(0).toString()}`;
}
}