update
This commit is contained in:
12
apps/client/assets/Scripts/Entity/Bullet.meta
Normal file
12
apps/client/assets/Scripts/Entity/Bullet.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "9265fd0c-f1ae-471b-afa0-7f17e87d408f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
84
apps/client/assets/Scripts/Entity/Bullet/BulletManager.ts
Normal file
84
apps/client/assets/Scripts/Entity/Bullet/BulletManager.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { _decorator } from 'cc'
|
||||
import { EntityManager } from '../../Base/EntityManager'
|
||||
import { EntityTypeEnum, EntityStateEnum, EventEnum } from '../../Enum'
|
||||
import DataManager, { IBullet, IVec2 } from '../../Global/DataManager'
|
||||
import EventManager from '../../Global/EventManager'
|
||||
import ObjectPoolManager from '../../Global/ObjectPoolManager'
|
||||
import { rad2Angle } from '../../Utils'
|
||||
import { ExplosionManager } from '../Explosion/ExplosionManager'
|
||||
import { BulletStateMachine } from './BulletStateMachine'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('BulletManager')
|
||||
export class BulletManager extends EntityManager {
|
||||
//静态数据
|
||||
id: number
|
||||
owner: number
|
||||
type: EntityTypeEnum
|
||||
|
||||
//动态数据
|
||||
position: IVec2
|
||||
direction: IVec2
|
||||
|
||||
private angle: number
|
||||
|
||||
init({ id, owner, type }: IBullet) {
|
||||
this.id = id
|
||||
this.owner = owner
|
||||
this.type = type
|
||||
|
||||
this.fsm = this.addComponent(BulletStateMachine)
|
||||
this.fsm.init(type)
|
||||
this.state = EntityStateEnum.Idle
|
||||
this.node.active = false
|
||||
|
||||
EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this)
|
||||
}
|
||||
|
||||
handleExplosion(id: number, { x, y }: IVec2) {
|
||||
if (this.id !== id) {
|
||||
return
|
||||
}
|
||||
|
||||
const explosion = ObjectPoolManager.Instance.get(EntityTypeEnum.Explosion)
|
||||
const explosionManager = explosion.getComponent(ExplosionManager) || explosion.addComponent(ExplosionManager)
|
||||
explosionManager.init(EntityTypeEnum.Explosion, {
|
||||
x, y,
|
||||
})
|
||||
|
||||
EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosion, this)
|
||||
ObjectPoolManager.Instance.ret(this.node)
|
||||
DataManager.Instance.bulletMap.delete(this.id)
|
||||
this.angle = undefined
|
||||
}
|
||||
|
||||
render(data: IBullet) {
|
||||
this.node.active = true
|
||||
this.renderPosition(data)
|
||||
this.renderDirection(data)
|
||||
}
|
||||
|
||||
renderPosition(data: IBullet) {
|
||||
this.node.setPosition(data.position.x, data.position.y)
|
||||
}
|
||||
|
||||
renderDirection(data: IBullet) {
|
||||
if (this.angle === undefined) {
|
||||
const { x, y } = data.direction
|
||||
const side = Math.sqrt(x * x + y * y)
|
||||
this.angle = x > 0 ? rad2Angle(Math.asin(y / side)) : rad2Angle(Math.asin(- y / side)) + 180
|
||||
}
|
||||
|
||||
this.node.setRotationFromEuler(0, 0, this.angle)
|
||||
|
||||
// let angle: number, sign: number
|
||||
// if (x !== 0) {
|
||||
// angle = x > 0 ? rad2Angle(Math.atan(y / x)) : rad2Angle(Math.atan(-y / x)) + 180
|
||||
// sign = x > 0 ? 1 : -1
|
||||
// } else {
|
||||
// angle = rad2Angle(Math.PI / 2)
|
||||
// sign = y > 0 ? 1 : -1
|
||||
// }
|
||||
// this.node.setRotationFromEuler(0, 0, sign * angle)
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3cb4bec9-dd63-4ffe-9a28-8f8347cd327b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
import { _decorator, Animation } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityTypeEnum, EntityStateEnum, ParamsNameEnum } from '../../Enum'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('BulletStateMachine')
|
||||
export class BulletStateMachine 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())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
|
||||
}
|
||||
|
||||
initAnimationEvent() {
|
||||
|
||||
}
|
||||
|
||||
run() {
|
||||
switch (this.currentState) {
|
||||
case this.stateMachines.get(ParamsNameEnum.Idle):
|
||||
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": "531b884c-93b6-4d0e-a0fc-e8861eebf6e7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Explosion.meta
Normal file
12
apps/client/assets/Scripts/Entity/Explosion.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "3302ce3a-fac5-42c6-8060-e3e8682cfcf1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
import { _decorator } from 'cc'
|
||||
import { EntityManager } from '../../Base/EntityManager'
|
||||
import { EntityTypeEnum, EntityStateEnum } from '../../Enum'
|
||||
import { IVec2 } from '../../Global/DataManager'
|
||||
import { ExplosionStateMachine } from './ExplosionStateMachine'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('ExplosionManager')
|
||||
export class ExplosionManager extends EntityManager {
|
||||
|
||||
init(type: EntityTypeEnum, { x, y }: IVec2) {
|
||||
this.node.setPosition(x, y)
|
||||
this.fsm = this.addComponent(ExplosionStateMachine)
|
||||
this.fsm.init(type)
|
||||
this.state = EntityStateEnum.Idle
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "34e9e358-082f-4674-9e38-a3c27f8b013b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
import { _decorator, Animation } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityTypeEnum, EntityStateEnum, ParamsNameEnum } from '../../Enum'
|
||||
import ObjectPoolManager from '../../Global/ObjectPoolManager'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('ExplosionStateMachine')
|
||||
export class ExplosionStateMachine 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())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
|
||||
}
|
||||
|
||||
initAnimationEvent() {
|
||||
this.animationComponent.on(Animation.EventType.FINISHED, () => {
|
||||
const whiteList = [EntityStateEnum.Idle]
|
||||
const name = this.animationComponent.defaultClip.name
|
||||
if (whiteList.some(v => name.includes(v))) {
|
||||
ObjectPoolManager.Instance.ret(this.node)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
run() {
|
||||
switch (this.currentState) {
|
||||
case this.stateMachines.get(ParamsNameEnum.Idle):
|
||||
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": "35a9f553-26d5-49c8-bcc0-d1011c07e124",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Player.meta
Normal file
12
apps/client/assets/Scripts/Entity/Player.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "5802fd2f-7823-4294-91c8-d71adca41319",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
113
apps/client/assets/Scripts/Entity/Player/PlayerManager.ts
Normal file
113
apps/client/assets/Scripts/Entity/Player/PlayerManager.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { _decorator, instantiate, ProgressBar, Label } from 'cc';
|
||||
import { EntityManager } from '../../Base/EntityManager';
|
||||
import { EntityTypeEnum, EntityStateEnum, InputType } from '../../Enum';
|
||||
import DataManager, { IPlayer, IVec2 } from '../../Global/DataManager';
|
||||
import { rad2Angle } from '../../Utils';
|
||||
import { WeaponManager } from '../Weapon/WeaponManager';
|
||||
import { PlayerStateMachine } from './PlayerStateMachine';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('PlayerManager')
|
||||
export class PlayerManager extends EntityManager {
|
||||
//静态数据
|
||||
id: number
|
||||
nickname: string
|
||||
type: EntityTypeEnum
|
||||
weaponType: EntityTypeEnum
|
||||
bulletType: EntityTypeEnum
|
||||
|
||||
//动态数据
|
||||
hp: number
|
||||
position: IVec2
|
||||
direction: IVec2
|
||||
|
||||
private hpBar: ProgressBar
|
||||
private label: Label
|
||||
private weapon: WeaponManager
|
||||
|
||||
get isSelf() {
|
||||
return DataManager.Instance.myPlayerId === this.id
|
||||
}
|
||||
|
||||
init(data: IPlayer) {
|
||||
const { id, nickname, type, weaponType, bulletType } = data
|
||||
this.id = id
|
||||
this.nickname = nickname
|
||||
this.type = type
|
||||
this.weaponType = weaponType
|
||||
this.bulletType = bulletType
|
||||
|
||||
this.hpBar = this.node.getComponentInChildren(ProgressBar)
|
||||
this.label = this.node.getComponentInChildren(Label)
|
||||
this.label.string = nickname
|
||||
|
||||
this.fsm = this.addComponent(PlayerStateMachine)
|
||||
this.fsm.init(type)
|
||||
this.state = EntityStateEnum.Idle
|
||||
|
||||
const weaponPrefab = DataManager.Instance.prefabMap.get(this.weaponType)
|
||||
const weapon = instantiate(weaponPrefab)
|
||||
weapon.setParent(this.node)
|
||||
this.weapon = weapon.addComponent(WeaponManager)
|
||||
this.weapon.init(data)
|
||||
}
|
||||
|
||||
tick(dt: number) {
|
||||
if (!this.isSelf) {
|
||||
return
|
||||
}
|
||||
|
||||
const { x, y } = DataManager.Instance.jm.input
|
||||
DataManager.Instance.applyInput({
|
||||
type: InputType.PlayerMove,
|
||||
id: this.id,
|
||||
direction: {
|
||||
x,
|
||||
y
|
||||
},
|
||||
dt
|
||||
})
|
||||
}
|
||||
|
||||
render(data: IPlayer) {
|
||||
this.renderHP(data)
|
||||
this.renderPosition(data)
|
||||
this.renderDirection(data)
|
||||
}
|
||||
|
||||
renderHP(data: IPlayer) {
|
||||
this.hpBar.progress = data.hp / this.hpBar.totalLength
|
||||
}
|
||||
|
||||
renderPosition(data: IPlayer) {
|
||||
this.node.setPosition(data.position.x, data.position.y)
|
||||
}
|
||||
|
||||
renderDirection(data: IPlayer) {
|
||||
if (data.direction.x === 0 && data.direction.y === 0) {
|
||||
this.state = EntityStateEnum.Idle
|
||||
return
|
||||
}
|
||||
this.state = EntityStateEnum.Run
|
||||
const { x, y } = data.direction
|
||||
if (x !== 0) {
|
||||
this.node.setScale(x > 0 ? 1 : -1, 1)
|
||||
this.label.node.setScale(x > 0 ? 1 : -1, 1)
|
||||
}
|
||||
const side = Math.sqrt(x * x + y * y)
|
||||
const angle = rad2Angle(Math.asin(y / side))
|
||||
this.weapon.node.setRotationFromEuler(0, 0, angle)
|
||||
|
||||
// const { x, y } = joystick.input
|
||||
// let angle: number, sign: number
|
||||
// if (x !== 0) {
|
||||
// angle = rad2Angle(Math.atan(y / x))
|
||||
// sign = joystick.input.x > 0 ? 1 : -1
|
||||
// } else {
|
||||
// angle = rad2Angle(Math.PI / 2)
|
||||
// sign = joystick.input.y > 0 ? 1 : -1
|
||||
// }
|
||||
// this.node.setRotationFromEuler(0, 0, sign * angle)
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4cc81cda-b869-4eb8-855e-126f16aa7a12",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
import { _decorator, Animation, AnimationClip } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityTypeEnum, EntityStateEnum, ParamsNameEnum } from '../../Enum'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('PlayerStateMachine')
|
||||
export class PlayerStateMachine 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.Run, getInitParamsTrigger())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`, AnimationClip.WrapMode.Loop))
|
||||
this.stateMachines.set(ParamsNameEnum.Run, new State(this, `${this.type}${EntityStateEnum.Run}`, AnimationClip.WrapMode.Loop))
|
||||
}
|
||||
|
||||
initAnimationEvent() {
|
||||
}
|
||||
|
||||
run() {
|
||||
switch (this.currentState) {
|
||||
case this.stateMachines.get(ParamsNameEnum.Idle):
|
||||
case this.stateMachines.get(ParamsNameEnum.Run):
|
||||
if (this.params.get(ParamsNameEnum.Run).value) {
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Run)
|
||||
} else 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": "3bdb087b-0d43-4b85-81e4-e480cd43405d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Weapon.meta
Normal file
12
apps/client/assets/Scripts/Entity/Weapon.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "b8a46a8b-5505-436b-8870-2965599d2168",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
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