新增mvvm示例

This commit is contained in:
YHH
2025-07-08 20:23:19 +08:00
parent 731edf5872
commit 2925ee380d
20 changed files with 4164 additions and 1673 deletions

View File

@@ -0,0 +1,45 @@
import { ViewModel, observable, computed, command } from '@esengine/mvvm-ui-framework';
/**
* 游戏状态视图模型
*/
export class GameStateViewModel extends ViewModel {
public get name(): string {
return 'GameStateViewModel';
}
@observable
currentLevel: number = 1;
@observable
health: number = 100;
@observable
mana: number = 50;
@observable
experience: number = 0;
@computed(['health'])
get healthPercent(): number {
return (this.health / 100) * 100;
}
@computed(['experience', 'currentLevel'])
get experienceToNextLevel(): number {
return (this.currentLevel * 100) - this.experience;
}
@command()
public levelUp(): void {
this.currentLevel += 1;
this.health = 100;
this.mana += 10;
}
@command()
public takeDamage(damage: number): void {
this.health = Math.max(0, this.health - damage);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "8fa14e6f-46cd-4cb4-9ac1-0a1919e260a5",
"files": [],
"subMetas": {},
"userData": {}
}