mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-27 01:16:07 +00:00
Game ui
This commit is contained in:
@@ -3,6 +3,7 @@ import { IInput } from "../Input/IInput";
|
||||
import { Weapon } from "../Weapon";
|
||||
import { PlayerUI } from "./PlayerUI/PlayerUI";
|
||||
import { UnitHealth } from "./UnitHealth";
|
||||
import { UnitLevel } from "./UnitLevel";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Player")
|
||||
@@ -14,13 +15,15 @@ export class Player extends Component {
|
||||
private input: IInput;
|
||||
private weapon: Weapon;
|
||||
private health: UnitHealth;
|
||||
private level: UnitLevel;
|
||||
|
||||
private xp: number;
|
||||
|
||||
public init(input: IInput, weapon: Weapon, maxHp: number): void {
|
||||
public init(input: IInput, weapon: Weapon, maxHp: number, requiredLevelXps: number[]): void {
|
||||
this.input = input;
|
||||
this.weapon = weapon;
|
||||
this.health = new UnitHealth(maxHp);
|
||||
this.level = new UnitLevel(requiredLevelXps);
|
||||
|
||||
this.weapon.node.parent = this.node;
|
||||
this.weapon.node.setPosition(new Vec3());
|
||||
@@ -32,12 +35,12 @@ export class Player extends Component {
|
||||
return this.health;
|
||||
}
|
||||
|
||||
public get Collider(): Collider2D {
|
||||
return this.collider;
|
||||
public get Level(): UnitLevel {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
public addXp(points: number): void {
|
||||
this.xp += points;
|
||||
public get Collider(): Collider2D {
|
||||
return this.collider;
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
|
||||
46
assets/Scripts/Game/Player/UnitLevel.ts
Normal file
46
assets/Scripts/Game/Player/UnitLevel.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { ISignal } from "../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../Services/EventSystem/Signal";
|
||||
|
||||
export class UnitLevel {
|
||||
private xp = 0;
|
||||
private requiredXPs: number[];
|
||||
private currentLevel = 0;
|
||||
private levelUpEvent: Signal<number> = new Signal<number>();
|
||||
private xpAddedEvent: Signal<number> = new Signal<number>();
|
||||
|
||||
public constructor(requiredXPs: number[]) {
|
||||
this.requiredXPs = requiredXPs;
|
||||
}
|
||||
|
||||
public addXp(points: number): void {
|
||||
this.xp += points;
|
||||
this.xpAddedEvent.trigger(this.xp);
|
||||
this.tryLevelUp();
|
||||
}
|
||||
|
||||
public get XP(): number {
|
||||
return this.xp;
|
||||
}
|
||||
|
||||
public get RequiredXP(): number {
|
||||
return this.requiredXPs[this.currentLevel];
|
||||
}
|
||||
|
||||
public get LevelUpEvent(): ISignal<number> {
|
||||
return this.levelUpEvent;
|
||||
}
|
||||
|
||||
public get XpAddedEvent(): ISignal<number> {
|
||||
return this.xpAddedEvent;
|
||||
}
|
||||
|
||||
private tryLevelUp(): void {
|
||||
if (this.requiredXPs.length <= this.currentLevel) return;
|
||||
if (this.xp < this.requiredXPs[this.currentLevel]) return;
|
||||
|
||||
this.xp -= this.requiredXPs[this.currentLevel];
|
||||
this.currentLevel++;
|
||||
|
||||
this.levelUpEvent.trigger(this.currentLevel);
|
||||
}
|
||||
}
|
||||
9
assets/Scripts/Game/Player/UnitLevel.ts.meta
Normal file
9
assets/Scripts/Game/Player/UnitLevel.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "5eeabc4f-21b1-4983-b935-e44c12051766",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user