This commit is contained in:
sli97
2022-11-27 23:23:47 +08:00
commit 1a27f478d0
219 changed files with 15759 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
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
},
})
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "eefb7dfd-3b96-46cc-a83a-c5bafe656d8a",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,58 @@
import { _decorator, Animation, AnimationClip } from 'cc'
import State from '../../Base/State'
import StateMachine, { getInitParamsNumber, getInitParamsTrigger } from '../../Base/StateMachine'
import { EntityStateEnum, EntityTypeEnum, ParamsNameEnum } from '../../Enum'
import { WeaponManager } from './WeaponManager'
const { ccclass, property } = _decorator
@ccclass('WeaponStateMachine')
export class WeaponStateMachine extends StateMachine {
type: EntityTypeEnum
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, () => {
const whiteList = [EntityStateEnum.Attack]
const name = this.animationComponent.defaultClip.name
if (whiteList.some(v => name.includes(v))) {
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
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "1eed66f0-cb8f-4cc9-9f91-aa21b5c2883c",
"files": [],
"subMetas": {},
"userData": {}
}