mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2025-10-09 00:26:04 +00:00
Folder structure
This commit is contained in:
12
assets/Scripts/Game/Unit/Enemy.meta
Normal file
12
assets/Scripts/Game/Unit/Enemy.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "baa05588-9899-4403-91b3-403907780c7a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
57
assets/Scripts/Game/Unit/Enemy/Enemy.ts
Normal file
57
assets/Scripts/Game/Unit/Enemy/Enemy.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { BoxCollider2D, Component, randomRange, Vec3, _decorator } from "cc";
|
||||
import { ISignal } from "../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../Services/EventSystem/Signal";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Enemy")
|
||||
export class Enemy extends Component implements IDamageDealing {
|
||||
@property(BoxCollider2D) public collider: BoxCollider2D;
|
||||
|
||||
private health: UnitHealth = new UnitHealth(1);
|
||||
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
|
||||
private speed: number;
|
||||
|
||||
public setup(position: Vec3): void {
|
||||
this.health = new UnitHealth(1);
|
||||
this.speed = randomRange(0.5, 1);
|
||||
this.node.setWorldPosition(position);
|
||||
this.node.active = true;
|
||||
}
|
||||
|
||||
public get Collider(): BoxCollider2D {
|
||||
return this.collider;
|
||||
}
|
||||
|
||||
public get Damage(): number {
|
||||
return 3;
|
||||
}
|
||||
|
||||
public get Health(): UnitHealth {
|
||||
return this.health;
|
||||
}
|
||||
|
||||
public get DeathEvent(): ISignal<Enemy> {
|
||||
return this.deathEvent;
|
||||
}
|
||||
|
||||
public dealDamage(points: number): void {
|
||||
this.health.damage(points);
|
||||
if (!this.health.IsAlive) {
|
||||
this.deathEvent.trigger(this);
|
||||
}
|
||||
}
|
||||
|
||||
public moveBy(move: Vec3): void {
|
||||
const newPosition: Vec3 = this.node.worldPosition;
|
||||
newPosition.x += move.x * this.speed;
|
||||
newPosition.y += move.y * this.speed;
|
||||
|
||||
this.node.setWorldPosition(newPosition);
|
||||
}
|
||||
}
|
||||
|
||||
export interface IDamageDealing {
|
||||
Damage: number;
|
||||
}
|
9
assets/Scripts/Game/Unit/Enemy/Enemy.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Enemy/Enemy.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7f8b634a-1ec3-4d5b-99cb-6377f07a3870",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
41
assets/Scripts/Game/Unit/Enemy/EnemyManager.ts
Normal file
41
assets/Scripts/Game/Unit/Enemy/EnemyManager.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Component, Node, _decorator } from "cc";
|
||||
import { XPSpawner } from "../../XP/XPSpawner";
|
||||
import { Enemy } from "./Enemy";
|
||||
import { EnemyMover } from "./EnemyMover";
|
||||
import { EnemySpawner } from "./EnemySpawner";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("EnemyManager")
|
||||
export class EnemyManager extends Component {
|
||||
@property(EnemySpawner) private enemySpawner: EnemySpawner;
|
||||
@property(XPSpawner) private xpSpawner: XPSpawner;
|
||||
|
||||
private enemyMover: EnemyMover;
|
||||
|
||||
public init(targetNode: Node): void {
|
||||
this.enemyMover = new EnemyMover(targetNode);
|
||||
|
||||
this.enemySpawner.init(targetNode);
|
||||
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
|
||||
|
||||
this.xpSpawner.init();
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.enemySpawner.gameTick(deltaTime);
|
||||
this.enemyMover.gameTick(deltaTime);
|
||||
}
|
||||
|
||||
private onEnemyAdded(enemy: Enemy): void {
|
||||
enemy.DeathEvent.on(this.onEnemyDied, this);
|
||||
|
||||
this.enemyMover.addEnemy(enemy);
|
||||
}
|
||||
|
||||
private onEnemyDied(enemy: Enemy): void {
|
||||
enemy.DeathEvent.off(this.onEnemyDied);
|
||||
|
||||
this.xpSpawner.spawnXp(enemy.node.worldPosition, 1);
|
||||
this.enemyMover.removeEnemy(enemy);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Enemy/EnemyManager.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Enemy/EnemyManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "839f4499-bc73-4115-a883-4a9322ea9389",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
28
assets/Scripts/Game/Unit/Enemy/EnemyMover.ts
Normal file
28
assets/Scripts/Game/Unit/Enemy/EnemyMover.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Node, Vec3 } from "cc";
|
||||
import { Enemy } from "./Enemy";
|
||||
|
||||
export class EnemyMover {
|
||||
private targetNode: Node;
|
||||
private enemies: Enemy[] = [];
|
||||
public constructor(targetNode: Node) {
|
||||
this.targetNode = targetNode;
|
||||
}
|
||||
public addEnemy(enemy: Enemy): void {
|
||||
this.enemies.push(enemy);
|
||||
}
|
||||
|
||||
public removeEnemy(enemy: Enemy): void {
|
||||
const index: number = this.enemies.indexOf(enemy);
|
||||
if (index != -1) {
|
||||
this.enemies.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.enemies.forEach((enemy) => {
|
||||
let direction: Vec3 = new Vec3();
|
||||
direction = Vec3.subtract(direction, this.targetNode.worldPosition, enemy.node.worldPosition);
|
||||
enemy.moveBy(direction.multiplyScalar(deltaTime).normalize());
|
||||
});
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Enemy/EnemyMover.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Enemy/EnemyMover.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "179f7bfa-44fc-4f41-9432-4aec4f3991a6",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
54
assets/Scripts/Game/Unit/Enemy/EnemySpawner.ts
Normal file
54
assets/Scripts/Game/Unit/Enemy/EnemySpawner.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Component, Prefab, randomRange, Vec3, _decorator, Node } from "cc";
|
||||
import { ISignal } from "../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../Services/EventSystem/Signal";
|
||||
import { GameTimer } from "../../../Services/GameTimer";
|
||||
import { ObjectPool } from "../../../Services/ObjectPool";
|
||||
|
||||
import { Enemy } from "./Enemy";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("EnemySpawner")
|
||||
export class EnemySpawner extends Component {
|
||||
@property(Prefab) private enemies: Prefab[] = [];
|
||||
|
||||
public enemyAddedEvent: Signal<Enemy> = new Signal<Enemy>();
|
||||
|
||||
private enemyPool: ObjectPool<Enemy>;
|
||||
private spawnTimer: GameTimer;
|
||||
|
||||
private targetNode: Node;
|
||||
|
||||
public init(targetNode: Node): void {
|
||||
this.targetNode = targetNode;
|
||||
this.enemyPool = new ObjectPool(this.enemies[0], this.node, 5, "Enemy");
|
||||
this.spawnTimer = new GameTimer(1);
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.spawnTimer.gameTick(deltaTime);
|
||||
if (this.spawnTimer.tryFinishPeriod()) {
|
||||
this.spawnNewEnemy();
|
||||
}
|
||||
}
|
||||
|
||||
public get EnemyAddedEvent(): ISignal<Enemy> {
|
||||
return this.enemyAddedEvent;
|
||||
}
|
||||
|
||||
private spawnNewEnemy(): void {
|
||||
const enemy = this.enemyPool.borrow();
|
||||
const spawnPosition = new Vec3();
|
||||
spawnPosition.x = this.targetNode.worldPosition.x + randomRange(-300, 300);
|
||||
spawnPosition.y = this.targetNode.worldPosition.y + randomRange(-800, 800);
|
||||
enemy.setup(spawnPosition);
|
||||
|
||||
enemy.DeathEvent.on(this.returnEnemyToPool, this);
|
||||
|
||||
this.enemyAddedEvent.trigger(enemy);
|
||||
}
|
||||
|
||||
private returnEnemyToPool(enemy: Enemy): void {
|
||||
enemy.DeathEvent.off(this.returnEnemyToPool);
|
||||
this.enemyPool.return(enemy);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Enemy/EnemySpawner.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Enemy/EnemySpawner.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7914ba19-6cca-4ee2-8231-baebd9eb559b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Unit/Player.meta
Normal file
12
assets/Scripts/Game/Unit/Player.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "3c590530-b416-4184-9b87-f87755610708",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
62
assets/Scripts/Game/Unit/Player/Player.ts
Normal file
62
assets/Scripts/Game/Unit/Player/Player.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { BoxCollider2D, Collider2D, Component, Vec2, Vec3, _decorator } from "cc";
|
||||
import { IInput } from "../../Input/IInput";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
import { UnitLevel } from "../UnitLevel";
|
||||
import { PlayerUI } from "./PlayerUI/PlayerUI";
|
||||
import { Weapon } from "./Weapon/Weapon";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Player")
|
||||
export class Player extends Component {
|
||||
@property private speed = 0;
|
||||
@property(BoxCollider2D) private collider: BoxCollider2D;
|
||||
@property(PlayerUI) private playerUI: PlayerUI;
|
||||
|
||||
private input: IInput;
|
||||
private weapon: Weapon;
|
||||
private health: UnitHealth;
|
||||
private level: UnitLevel;
|
||||
|
||||
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());
|
||||
|
||||
this.playerUI.init(this.health);
|
||||
}
|
||||
|
||||
public get Health(): UnitHealth {
|
||||
return this.health;
|
||||
}
|
||||
|
||||
public get Level(): UnitLevel {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
public get Weapon(): Weapon {
|
||||
return this.weapon;
|
||||
}
|
||||
|
||||
public get Collider(): Collider2D {
|
||||
return this.collider;
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
const movement: Vec2 = this.input.getAxis();
|
||||
movement.x *= deltaTime * this.speed;
|
||||
movement.y *= deltaTime * this.speed;
|
||||
|
||||
const newPosition: Vec3 = this.node.worldPosition;
|
||||
newPosition.x += movement.x;
|
||||
newPosition.y += movement.y;
|
||||
|
||||
this.node.setWorldPosition(newPosition);
|
||||
|
||||
this.weapon.gameTick(deltaTime);
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Player/Player.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Player/Player.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "b570abb8-1bcd-4ef0-85e8-4d783531001d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Unit/Player/PlayerUI.meta
Normal file
12
assets/Scripts/Game/Unit/Player/PlayerUI.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "7eadb488-192c-4370-9e59-20f27929fba1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
19
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerHealthUI.ts
Normal file
19
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerHealthUI.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Component, ProgressBar, _decorator } from "cc";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("PlayerHealthUI")
|
||||
export class PlayerHealthUI extends Component {
|
||||
@property(ProgressBar) public healthBar: ProgressBar;
|
||||
private health: UnitHealth;
|
||||
|
||||
public init(health: UnitHealth): void {
|
||||
this.healthBar.progress = 1;
|
||||
this.health = health;
|
||||
this.health.HealthPointsChangeEvent.on(this.updateHealthBar, this);
|
||||
}
|
||||
|
||||
private updateHealthBar(): void {
|
||||
this.healthBar.progress = this.health.HealthPoints / this.health.MaxHealthPoints;
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "def45aaf-cc88-4ec6-97ff-4d5b0beb015b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
13
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerUI.ts
Normal file
13
assets/Scripts/Game/Unit/Player/PlayerUI/PlayerUI.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Component, _decorator } from "cc";
|
||||
import { UnitHealth } from "../UnitHealth";
|
||||
import { PlayerHealthUI } from "./PlayerHealthUI";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("PlayerUI")
|
||||
export class PlayerUI extends Component {
|
||||
@property(PlayerHealthUI) private healthUI: PlayerHealthUI;
|
||||
|
||||
public init(playerHealth: UnitHealth): void {
|
||||
this.healthUI.init(playerHealth);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "6247f0de-45c1-4e74-89aa-388c217ce3a3",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
assets/Scripts/Game/Unit/Player/Weapon.meta
Normal file
12
assets/Scripts/Game/Unit/Player/Weapon.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "b232195c-46e2-4508-9f71-5efb42ab490f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
43
assets/Scripts/Game/Unit/Player/Weapon/UpgradableCollider.ts
Normal file
43
assets/Scripts/Game/Unit/Player/Weapon/UpgradableCollider.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { BoxCollider2D, Collider2D, Component, Contact2DType, _decorator } from "cc";
|
||||
import { ISignal } from "../../../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../../../Services/EventSystem/Signal";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("UpgradableCollider")
|
||||
export class UpgradableCollider extends Component {
|
||||
@property(BoxCollider2D) private colliders: BoxCollider2D[] = [];
|
||||
private contactBeginEvent: Signal<Collider2D> = new Signal<Collider2D>();
|
||||
private currentUpgradeLevel = 0;
|
||||
|
||||
public init(): void {
|
||||
this.setUpgradeLevel();
|
||||
|
||||
for (const collider of this.colliders) {
|
||||
collider.on(Contact2DType.BEGIN_CONTACT, this.onColliderContactBegin, this);
|
||||
}
|
||||
}
|
||||
|
||||
public get ContactBeginEvent(): ISignal<Collider2D> {
|
||||
return this.contactBeginEvent;
|
||||
}
|
||||
|
||||
public upgrade(): void {
|
||||
if (this.currentUpgradeLevel == this.colliders.length - 1) throw new Error("Already at max upgrade! " + this.currentUpgradeLevel);
|
||||
|
||||
this.currentUpgradeLevel++;
|
||||
this.setUpgradeLevel();
|
||||
}
|
||||
|
||||
private setUpgradeLevel(): void {
|
||||
for (const collider of this.colliders) {
|
||||
collider.node.active = false;
|
||||
}
|
||||
|
||||
this.colliders[this.currentUpgradeLevel].node.active = true;
|
||||
}
|
||||
|
||||
private onColliderContactBegin(thisCollider: Collider2D, otherCollider: Collider2D): void {
|
||||
this.contactBeginEvent.trigger(otherCollider);
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bba49b00-3fe5-4042-85f3-7d1f017d75d3",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
59
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts
Normal file
59
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { Animation, AnimationState, Component, _decorator } from "cc";
|
||||
import { GameTimer } from "../../../../Services/GameTimer";
|
||||
import { WeaponSettings } from "../../../Data/GameSettings";
|
||||
|
||||
import { UpgradableCollider } from "./UpgradableCollider";
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass("Weapon")
|
||||
export class Weapon extends Component {
|
||||
@property(Animation) private weaponAnimation: Animation;
|
||||
@property(UpgradableCollider) private upgradableCollider: UpgradableCollider;
|
||||
|
||||
private strikeTimer: GameTimer;
|
||||
private strikeState: AnimationState;
|
||||
private damage: number;
|
||||
|
||||
public init(settings: WeaponSettings): void {
|
||||
this.strikeTimer = new GameTimer(settings.strikeDelay);
|
||||
this.damage = settings.damage;
|
||||
this.node.active = false;
|
||||
|
||||
this.weaponAnimation.on(Animation.EventType.FINISHED, this.endStrike, this);
|
||||
this.strikeState = this.weaponAnimation.getState(this.weaponAnimation.clips[0].name);
|
||||
this.strikeState.speed = 1;
|
||||
|
||||
this.upgradableCollider.init();
|
||||
}
|
||||
|
||||
public gameTick(deltaTime: number): void {
|
||||
this.strikeTimer.gameTick(deltaTime);
|
||||
if (this.strikeTimer.tryFinishPeriod()) {
|
||||
this.strike();
|
||||
}
|
||||
}
|
||||
|
||||
public get Collider(): UpgradableCollider {
|
||||
return this.upgradableCollider;
|
||||
}
|
||||
|
||||
public get Damage(): number {
|
||||
return this.damage;
|
||||
}
|
||||
|
||||
public upgradeWeaponDamage(): void {
|
||||
this.damage++;
|
||||
}
|
||||
public upgradeWeaponLength(): void {
|
||||
this.upgradableCollider.upgrade();
|
||||
}
|
||||
|
||||
private strike(): void {
|
||||
this.node.active = true;
|
||||
this.weaponAnimation.play(this.strikeState.name);
|
||||
}
|
||||
|
||||
private endStrike(): void {
|
||||
this.node.active = false;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts.meta
Normal file
9
assets/Scripts/Game/Unit/Player/Weapon/Weapon.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2391b8b7-f9fa-42a8-b046-55ff57b07d02",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
42
assets/Scripts/Game/Unit/UnitHealth.ts
Normal file
42
assets/Scripts/Game/Unit/UnitHealth.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { ISignal } from "../../Services/EventSystem/ISignal";
|
||||
import { Signal } from "../../Services/EventSystem/Signal";
|
||||
|
||||
export class UnitHealth {
|
||||
private healthPoints: number;
|
||||
private maxHealthPoints: number;
|
||||
private healthPointsChangeEvent: Signal<number> = new Signal<number>();
|
||||
|
||||
public constructor(maxHealth: number) {
|
||||
this.maxHealthPoints = maxHealth;
|
||||
this.healthPoints = maxHealth;
|
||||
}
|
||||
|
||||
public get IsAlive(): boolean {
|
||||
return 0 < this.healthPoints;
|
||||
}
|
||||
|
||||
public get HealthPoints(): number {
|
||||
return this.healthPoints;
|
||||
}
|
||||
public get MaxHealthPoints(): number {
|
||||
return this.maxHealthPoints;
|
||||
}
|
||||
|
||||
public get HealthPointsChangeEvent(): ISignal<number> {
|
||||
return this.healthPointsChangeEvent;
|
||||
}
|
||||
|
||||
public heal(points: number): void {
|
||||
this.healthPoints = Math.min(this.maxHealthPoints, this.healthPoints + points);
|
||||
this.healthPointsChangeEvent.trigger(this.healthPoints);
|
||||
}
|
||||
|
||||
public damage(points: number): void {
|
||||
this.healthPoints -= points;
|
||||
this.healthPointsChangeEvent.trigger(this.healthPoints);
|
||||
}
|
||||
|
||||
public setMaxHealth(maxHealth: number): void {
|
||||
this.maxHealthPoints = maxHealth;
|
||||
}
|
||||
}
|
9
assets/Scripts/Game/Unit/UnitHealth.ts.meta
Normal file
9
assets/Scripts/Game/Unit/UnitHealth.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "14972f1d-8009-4d39-8a6f-553ae1b3a011",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
46
assets/Scripts/Game/Unit/UnitLevel.ts
Normal file
46
assets/Scripts/Game/Unit/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/Unit/UnitLevel.ts.meta
Normal file
9
assets/Scripts/Game/Unit/UnitLevel.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "61e76d1e-15a8-42cc-990d-b9c7848ddab3",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user