update
This commit is contained in:
74
apps/client/assets/Scripts/Entity/Weapon/WeaponManager.ts
Normal file
74
apps/client/assets/Scripts/Entity/Weapon/WeaponManager.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { _decorator, Node, 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 } = _decorator
|
||||
|
||||
@ccclass('WeaponManager')
|
||||
export class WeaponManager extends EntityManager {
|
||||
owner: number
|
||||
type: EntityTypeEnum
|
||||
|
||||
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)
|
||||
EventManager.Instance.on(EventEnum.BulletBorn, this.handleBulletBorn, this)
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this)
|
||||
EventManager.Instance.off(EventEnum.BulletBorn, this.handleBulletBorn, this)
|
||||
}
|
||||
|
||||
handleBulletBorn(owner: number) {
|
||||
if (this.owner !== owner) {
|
||||
return
|
||||
}
|
||||
|
||||
this.state = EntityStateEnum.Attack
|
||||
}
|
||||
|
||||
handleWeaponShoot() {
|
||||
if (!this.isSelf) {
|
||||
return
|
||||
}
|
||||
const pointWorldPos = this.point.getWorldPosition()
|
||||
const pointStagePos = DataManager.Instance.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
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "eefb7dfd-3b96-46cc-a83a-c5bafe656d8a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
import { _decorator, Animation, AnimationClip } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityStateEnum, EntityTypeEnum, ParamsNameEnum } from '../../Enum'
|
||||
import { WeaponManager } from './WeaponManager'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('WeaponStateMachine')
|
||||
export class WeaponStateMachine extends StateMachine {
|
||||
init(type: EntityTypeEnum) {
|
||||
this.type = type
|
||||
this.animationComponent = this.node.addComponent(Animation)
|
||||
|
||||
this.initParams()
|
||||
this.initStateMachines()
|
||||
this.initAnimationEvent()
|
||||
}
|
||||
|
||||
initParams() {
|
||||
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger())
|
||||
this.params.set(ParamsNameEnum.Attack, getInitParamsTrigger())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
|
||||
this.stateMachines.set(ParamsNameEnum.Attack, new State(this, `${this.type}${EntityStateEnum.Attack}`, AnimationClip.WrapMode.Normal, true))
|
||||
}
|
||||
|
||||
initAnimationEvent() {
|
||||
this.animationComponent.on(Animation.EventType.FINISHED, () => {
|
||||
if (this.animationComponent.defaultClip.name.includes(EntityStateEnum.Attack)) {
|
||||
this.node.parent.getComponent(WeaponManager).state = EntityStateEnum.Idle
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
run() {
|
||||
switch (this.currentState) {
|
||||
case this.stateMachines.get(ParamsNameEnum.Idle):
|
||||
case this.stateMachines.get(ParamsNameEnum.Attack):
|
||||
if (this.params.get(ParamsNameEnum.Attack).value) {
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Attack)
|
||||
} if (this.params.get(ParamsNameEnum.Idle).value) {
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
|
||||
} else {
|
||||
this.currentState = this.currentState
|
||||
}
|
||||
break
|
||||
default:
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1eed66f0-cb8f-4cc9-9f91-aa21b5c2883c",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user