2022-12-08 21:14:02 +08:00
|
|
|
import { _decorator, Node, Vec2, UITransform } from "cc";
|
|
|
|
import { EntityManager } from "../../Base/EntityManager";
|
|
|
|
import { EntityTypeEnum, InputTypeEnum, toFixed } from "../../Common";
|
|
|
|
import { EntityStateEnum, EventEnum } from "../../Enum";
|
|
|
|
import DataManager from "../../Global/DataManager";
|
|
|
|
import EventManager from "../../Global/EventManager";
|
|
|
|
import { WeaponStateMachine } from "./WeaponStateMachine";
|
|
|
|
const { ccclass } = _decorator;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
@ccclass("WeaponManager")
|
2022-11-27 23:23:47 +08:00
|
|
|
export class WeaponManager extends EntityManager {
|
2022-12-08 21:14:02 +08:00
|
|
|
owner: number;
|
|
|
|
type: EntityTypeEnum;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
private body: Node;
|
|
|
|
private anchor: Node;
|
|
|
|
private point: Node;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
init({ id, weaponType }: { id: number; weaponType: EntityTypeEnum }) {
|
|
|
|
this.owner = id;
|
|
|
|
this.type = weaponType;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
this.node.setSiblingIndex(0);
|
|
|
|
this.body = this.node.getChildByName("Body");
|
|
|
|
this.anchor = this.body.getChildByName("Anchor");
|
|
|
|
this.point = this.anchor.getChildByName("Point");
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
this.fsm = this.body.addComponent(WeaponStateMachine);
|
|
|
|
this.fsm.init(weaponType);
|
|
|
|
this.state = EntityStateEnum.Idle;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
EventManager.Instance.on(EventEnum.WeaponShoot, this.handleWeaponShoot, this);
|
|
|
|
EventManager.Instance.on(EventEnum.BulletBorn, this.handleBulletBorn, this);
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy() {
|
2022-12-08 21:14:02 +08:00
|
|
|
EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this);
|
|
|
|
EventManager.Instance.off(EventEnum.BulletBorn, this.handleBulletBorn, this);
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 22:26:41 +08:00
|
|
|
handleBulletBorn(owner: number) {
|
|
|
|
if (this.owner !== owner) {
|
2022-12-08 21:14:02 +08:00
|
|
|
return;
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
this.state = EntityStateEnum.Attack;
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
|
2022-12-01 22:26:41 +08:00
|
|
|
handleWeaponShoot() {
|
2022-12-08 21:14:02 +08:00
|
|
|
if (DataManager.Instance.myPlayerId !== this.owner) {
|
|
|
|
return;
|
2022-12-01 22:26:41 +08:00
|
|
|
}
|
2022-12-08 21:14:02 +08:00
|
|
|
const pointWorldPos = this.point.getWorldPosition();
|
2022-12-01 22:26:41 +08:00
|
|
|
const pointStagePos = DataManager.Instance.stage.getComponent(UITransform).convertToNodeSpaceAR(pointWorldPos);
|
2022-12-08 21:14:02 +08:00
|
|
|
const anchorWorldPos = this.anchor.getWorldPosition();
|
|
|
|
const directionVec2 = new Vec2(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y).normalize();
|
2022-11-27 23:23:47 +08:00
|
|
|
|
2022-12-08 21:14:02 +08:00
|
|
|
EventManager.Instance.emit(EventEnum.ClientSync, {
|
2022-12-04 22:10:30 +08:00
|
|
|
type: InputTypeEnum.WeaponShoot,
|
|
|
|
owner: this.owner,
|
|
|
|
position: {
|
|
|
|
x: toFixed(pointStagePos.x),
|
|
|
|
y: toFixed(pointStagePos.y),
|
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
x: toFixed(directionVec2.x),
|
|
|
|
y: toFixed(directionVec2.y),
|
|
|
|
},
|
2022-12-08 21:14:02 +08:00
|
|
|
});
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
}
|