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

61 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-11-16 11:26:20 +00:00
import { BoxCollider2D, Component, randomRange, Vec3, _decorator } from "cc";
2022-11-28 11:19:04 +00:00
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { Signal } from "../../../Services/EventSystem/Signal";
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-11-08 18:45:57 +00:00
@property(BoxCollider2D) public collider: BoxCollider2D;
2022-12-05 14:13:42 +00:00
private movementType: EnemyMovementType;
2022-11-08 18:45:57 +00:00
private health: UnitHealth = new UnitHealth(1);
private deathEvent: Signal<Enemy> = new Signal<Enemy>();
2022-11-16 11:26:20 +00:00
private speed: number;
2022-11-08 18:45:57 +00:00
2022-12-05 14:13:42 +00:00
public setup(position: Vec3, movementType: EnemyMovementType): void {
this.movementType = movementType;
2022-11-08 18:45:57 +00:00
this.health = new UnitHealth(1);
2022-12-05 11:19:46 +00:00
this.speed = randomRange(40, 90);
2022-11-16 11:26:20 +00:00
this.node.setWorldPosition(position);
this.node.active = true;
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 {
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);
}
}
2022-11-14 15:35:47 +00:00
2022-12-05 14:13:42 +00:00
public moveBy(move: Vec3, deltaTime: number): void {
2022-11-14 15:35:47 +00:00
const newPosition: Vec3 = this.node.worldPosition;
2022-12-05 14:13:42 +00:00
newPosition.x += move.x * this.speed * deltaTime;
newPosition.y += move.y * this.speed * deltaTime;
2022-11-14 15:35:47 +00:00
this.node.setWorldPosition(newPosition);
}
2022-11-08 18:45:57 +00:00
}