This commit is contained in:
sli97
2022-12-03 20:06:57 +08:00
parent bdead4a6d1
commit f3dc3ef7ba
110 changed files with 1347 additions and 1028 deletions

View File

@@ -0,0 +1,115 @@
import { _decorator, instantiate, ProgressBar, Label } from 'cc';
import { EntityManager } from '../../Base/EntityManager';
import { ApiMsgEnum, EntityTypeEnum, IActor, InputTypeEnum, IVec2 } from '../../Common';
import { EntityStateEnum } from '../../Enum';
import DataManager from '../../Global/DataManager';
import NetworkManager from '../../Global/NetworkManager';
import { rad2Angle } from '../../Utils';
import { WeaponManager } from '../Weapon/WeaponManager';
import { PlayerStateMachine } from './ActorStateMachine';
const { ccclass } = _decorator;
@ccclass('ActorManager')
export class ActorManager extends EntityManager implements IActor {
//静态数据
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: IActor) {
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
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
type: InputTypeEnum.ActorMove,
id: this.id,
direction: {
x,
y
},
dt
})
}
render(data: IActor) {
this.renderHP(data)
this.renderPosition(data)
this.renderDirection(data)
}
renderHP(data: IActor) {
this.hpBar.progress = data.hp / this.hpBar.totalLength
}
renderPosition(data: IActor) {
this.node.setPosition(data.position.x, data.position.y)
}
renderDirection(data: IActor) {
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)
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "aaa694fa-4476-44b2-9213-4b4978a57d66",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,48 @@
import { _decorator, Animation, AnimationClip } from 'cc'
import State from '../../Base/State'
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
import { EntityTypeEnum } from '../../Common'
import { 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
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "67239e69-9cbb-42c8-ae8f-27f8bae24103",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "4cc81cda-b869-4eb8-855e-126f16aa7a12",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "3bdb087b-0d43-4b85-81e4-e480cd43405d",
"files": [],
"subMetas": {},
"userData": {}
}