2022-12-01 22:26:41 +08:00
|
|
|
import { _decorator, Node, Vec2, UITransform } from 'cc'
|
2022-11-27 23:23:47 +08:00
|
|
|
import { EntityManager } from '../../Base/EntityManager'
|
|
|
|
import { EntityTypeEnum, EntityStateEnum, EventEnum, InputType } from '../../Enum'
|
|
|
|
import DataManager, { IPlayer } from '../../Global/DataManager'
|
|
|
|
import EventManager from '../../Global/EventManager'
|
|
|
|
import { WeaponStateMachine } from './WeaponStateMachine'
|
2022-12-01 22:26:41 +08:00
|
|
|
const { ccclass } = _decorator
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
@ccclass('WeaponManager')
|
|
|
|
export class WeaponManager extends EntityManager {
|
2022-12-01 22:26:41 +08:00
|
|
|
owner: number
|
|
|
|
type: EntityTypeEnum
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
private body: Node
|
|
|
|
private anchor: Node
|
|
|
|
private point: Node
|
|
|
|
|
|
|
|
get isSelf() {
|
|
|
|
return DataManager.Instance.myPlayerId === this.owner
|
|
|
|
}
|
|
|
|
|
|
|
|
init({ id, weaponType }: IPlayer) {
|
|
|
|
this.owner = id
|
|
|
|
this.type = weaponType
|
|
|
|
|
|
|
|
this.node.setSiblingIndex(0)
|
|
|
|
this.body = this.node.getChildByName("Body")
|
|
|
|
this.anchor = this.body.getChildByName("Anchor")
|
|
|
|
this.point = this.anchor.getChildByName("Point")
|
|
|
|
|
|
|
|
this.fsm = this.body.addComponent(WeaponStateMachine)
|
|
|
|
this.fsm.init(weaponType)
|
|
|
|
this.state = EntityStateEnum.Idle
|
|
|
|
|
|
|
|
EventManager.Instance.on(EventEnum.WeaponShoot, this.handleWeaponShoot, this)
|
2022-12-01 22:26:41 +08:00
|
|
|
EventManager.Instance.on(EventEnum.BulletBorn, this.handleBulletBorn, this)
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
onDestroy() {
|
|
|
|
EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this)
|
2022-12-01 22:26:41 +08:00
|
|
|
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-11-27 23:23:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state = EntityStateEnum.Attack
|
|
|
|
}
|
|
|
|
|
2022-12-01 22:26:41 +08:00
|
|
|
handleWeaponShoot() {
|
|
|
|
if (!this.isSelf) {
|
|
|
|
return
|
|
|
|
}
|
2022-11-27 23:23:47 +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-11-27 23:23:47 +08:00
|
|
|
const anchorWorldPos = this.anchor.getWorldPosition()
|
|
|
|
const directionVec2 = new Vec2(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y).normalize()
|
|
|
|
|
|
|
|
DataManager.Instance.applyInput({
|
|
|
|
type: InputType.WeaponShoot,
|
|
|
|
owner: this.owner,
|
|
|
|
position: {
|
|
|
|
x: pointStagePos.x,
|
|
|
|
y: pointStagePos.y
|
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
x: directionVec2.x,
|
|
|
|
y: directionVec2.y
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|