74 lines
2.3 KiB
TypeScript
74 lines
2.3 KiB
TypeScript
|
import { _decorator, Animation, AnimationClip, Node, Sprite, Vec3, Vec2, UITransform } from 'cc'
|
||
|
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'
|
||
|
const { ccclass, property } = _decorator
|
||
|
|
||
|
@ccclass('WeaponManager')
|
||
|
export class WeaponManager extends EntityManager {
|
||
|
owner = 1
|
||
|
type = EntityTypeEnum.Weapon1
|
||
|
|
||
|
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)
|
||
|
this.isSelf && EventManager.Instance.on(EventEnum.ShootBtnClick, this.handleShootBtnClick, this)
|
||
|
}
|
||
|
|
||
|
onDestroy() {
|
||
|
this.isSelf && EventManager.Instance.off(EventEnum.ShootBtnClick, this.handleShootBtnClick, this)
|
||
|
EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this)
|
||
|
}
|
||
|
|
||
|
handleWeaponShoot(id: number) {
|
||
|
if (this.owner !== id) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
this.state = EntityStateEnum.Attack
|
||
|
}
|
||
|
|
||
|
handleShootBtnClick() {
|
||
|
const pointWorldPos = this.point.getWorldPosition()
|
||
|
const pointStagePos = DataManager.Instance.gm.stage.getComponent(UITransform).convertToNodeSpaceAR(pointWorldPos);
|
||
|
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
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
}
|