Slash-The-Hordes/assets/Scripts/Game/Unit/Enemy/Enemy.ts

111 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-12-19 13:24:29 +00:00
import { BoxCollider2D, Component, Material, randomRange, Sprite, Vec3, _decorator } from "cc";
2022-11-28 11:19:04 +00:00
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { Signal } from "../../../Services/EventSystem/Signal";
2022-12-19 13:24:29 +00:00
import { delay } from "../../../Services/Utils/AsyncUtils";
2022-12-12 11:46:17 +00:00
import { EnemySettings } from "../../Data/GameSettings";
2022-11-28 11:19:04 +00:00
import { UnitHealth } from "../UnitHealth";
2022-12-05 14:13:42 +00:00
import { EnemyMovementType } from "./EnemyMovementType";
2022-11-28 11:19:04 +00:00
2022-11-08 18:45:57 +00:00
const { ccclass, property } = _decorator;
@ccclass("Enemy")
2022-12-05 11:19:46 +00:00
export class Enemy extends Component {
2022-12-19 13:24:29 +00:00
@property(BoxCollider2D) private collider: BoxCollider2D;
@property(Sprite) private sprite: Sprite;
@property(Material) private whiteMaterial: Material;
2022-11-08 18:45:57 +00:00
2022-12-12 11:46:17 +00:00
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
2022-12-13 17:56:00 +00:00
private lifetimeEndedEvent: Signal<Enemy> = new Signal<Enemy>();
2022-12-12 11:46:17 +00:00
2022-12-19 13:24:29 +00:00
private defaultMaterial: Material;
2022-12-05 14:13:42 +00:00
private movementType: EnemyMovementType;
2022-12-13 17:56:00 +00:00
private health: UnitHealth;
2022-12-12 11:46:17 +00:00
private damage: number;
2022-12-07 09:47:46 +00:00
private speedX: number;
private speedY: number;
2022-12-13 17:56:00 +00:00
private lifetimeLeft: number;
private xpReward: number;
private goldReward: number;
2022-11-08 18:45:57 +00:00
2022-12-12 11:46:17 +00:00
public setup(position: Vec3, settings: EnemySettings): void {
2022-12-19 13:24:29 +00:00
this.defaultMaterial = this.sprite.material;
2022-12-12 11:46:17 +00:00
this.movementType = <EnemyMovementType>settings.moveType;
this.health = new UnitHealth(settings.health);
this.damage = settings.damage;
this.speedX = randomRange(settings.speed / 2, settings.speed);
this.speedY = randomRange(settings.speed / 2, settings.speed);
2022-12-13 17:56:00 +00:00
this.lifetimeLeft = settings.lifetime;
this.xpReward = settings.xpReward;
this.goldReward = settings.goldReward;
2022-12-12 11:46:17 +00:00
2022-11-16 11:26:20 +00:00
this.node.setWorldPosition(position);
this.node.active = true;
2022-12-19 13:24:29 +00:00
this.health.HealthPointsChangeEvent.on(this.animateHurt, this);
2022-11-08 18:45:57 +00:00
}
2022-12-05 14:13:42 +00:00
public get MovementType(): EnemyMovementType {
return this.movementType;
}
2022-11-08 18:45:57 +00:00
public get Collider(): BoxCollider2D {
return this.collider;
}
public get Damage(): number {
2022-12-12 11:46:17 +00:00
return this.damage;
2022-11-08 18:45:57 +00:00
}
public get Health(): UnitHealth {
return this.health;
}
public get DeathEvent(): ISignal<Enemy> {
return this.deathEvent;
}
2022-12-19 13:24:29 +00:00
public get XPReward(): number {
return this.xpReward;
}
public get GoldReward(): number {
return this.goldReward;
}
2022-12-13 17:56:00 +00:00
public get LifetimeEndedEvent(): ISignal<Enemy> {
return this.lifetimeEndedEvent;
}
2022-11-08 18:45:57 +00:00
public dealDamage(points: number): void {
this.health.damage(points);
if (!this.health.IsAlive) {
this.deathEvent.trigger(this);
}
}
2022-11-14 15:35:47 +00:00
2022-12-13 17:56:00 +00:00
public gameTick(move: Vec3, deltaTime: number): void {
2022-11-14 15:35:47 +00:00
const newPosition: Vec3 = this.node.worldPosition;
2022-12-07 09:47:46 +00:00
newPosition.x += move.x * this.speedX * deltaTime;
newPosition.y += move.y * this.speedY * deltaTime;
2022-11-14 15:35:47 +00:00
this.node.setWorldPosition(newPosition);
2022-12-13 17:56:00 +00:00
if (0 < this.lifetimeLeft) {
this.lifetimeLeft -= deltaTime;
if (this.lifetimeLeft <= 0) {
this.lifetimeEndedEvent.trigger(this);
}
}
2022-11-14 15:35:47 +00:00
}
2022-12-19 13:24:29 +00:00
private async animateHurt(): Promise<void> {
this.sprite.material = this.whiteMaterial;
await delay(100);
this.sprite.material = this.defaultMaterial;
}
2022-11-08 18:45:57 +00:00
}