53 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-12-15 11:14:35 +01:00
import { Collider2D, Component, Contact2DType, Vec3, _decorator } from "cc";
2022-11-30 08:21:22 +01:00
import { ISignal } from "../../Services/EventSystem/ISignal";
import { Signal } from "../../Services/EventSystem/Signal";
import { ProjectileCollision } from "./ProjectileCollision";
const { ccclass, property } = _decorator;
@ccclass("Projectile")
export class Projectile extends Component {
2022-12-15 11:14:35 +01:00
@property(Collider2D) private collider: Collider2D;
2022-12-13 11:58:40 +01:00
private contactBeginEvent = new Signal<ProjectileCollision>();
private piercesDepletedEvent = new Signal<Projectile>();
2022-11-30 08:21:22 +01:00
2022-12-13 11:58:40 +01:00
private isContactListenerSet = false;
2022-11-30 08:21:22 +01:00
2022-12-13 11:58:40 +01:00
private piercesLeft = 0;
private damage = 0;
2022-12-30 15:19:32 +01:00
public setup(damage: number, pierces: number, angle: number): void {
2022-12-13 11:58:40 +01:00
this.piercesLeft = pierces;
this.damage = damage;
if (!this.isContactListenerSet) {
this.isContactListenerSet = true;
this.collider.on(Contact2DType.BEGIN_CONTACT, this.onColliderContactBegin, this);
}
2022-12-15 11:14:35 +01:00
this.node.setRotationFromEuler(new Vec3(0, 0, angle));
2022-12-13 11:58:40 +01:00
}
public pierce(): void {
this.piercesLeft--;
if (this.piercesLeft <= 0) {
this.piercesDepletedEvent.trigger(this);
}
}
public get Damage(): number {
return this.damage;
2022-11-30 08:21:22 +01:00
}
public get ContactBeginEvent(): ISignal<ProjectileCollision> {
return this.contactBeginEvent;
}
2022-12-13 11:58:40 +01:00
public get PiercesDepletedEvent(): ISignal<Projectile> {
return this.piercesDepletedEvent;
}
2022-11-30 08:21:22 +01:00
private onColliderContactBegin(thisCollider: Collider2D, otherCollider: Collider2D): void {
this.contactBeginEvent.trigger({ otherCollider, projectile: this });
}
}