2022-12-01 22:26:41 +08:00
|
|
|
import { _decorator, instantiate, ProgressBar, Label } from 'cc';
|
2022-11-27 23:23:47 +08:00
|
|
|
import { EntityManager } from '../../Base/EntityManager';
|
2022-12-01 22:26:41 +08:00
|
|
|
import { EntityTypeEnum, EntityStateEnum, InputType } from '../../Enum';
|
2022-11-27 23:23:47 +08:00
|
|
|
import DataManager, { IPlayer, IVec2 } from '../../Global/DataManager';
|
|
|
|
import { rad2Angle } from '../../Utils';
|
|
|
|
import { WeaponManager } from '../Weapon/WeaponManager';
|
|
|
|
import { PlayerStateMachine } from './PlayerStateMachine';
|
2022-12-01 22:26:41 +08:00
|
|
|
const { ccclass } = _decorator;
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
@ccclass('PlayerManager')
|
|
|
|
export class PlayerManager extends EntityManager {
|
|
|
|
//静态数据
|
2022-12-01 22:26:41 +08:00
|
|
|
id: number
|
|
|
|
nickname: string
|
|
|
|
type: EntityTypeEnum
|
|
|
|
weaponType: EntityTypeEnum
|
|
|
|
bulletType: EntityTypeEnum
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
//动态数据
|
|
|
|
hp: number
|
|
|
|
position: IVec2
|
|
|
|
direction: IVec2
|
|
|
|
|
|
|
|
private hpBar: ProgressBar
|
2022-12-01 22:26:41 +08:00
|
|
|
private label: Label
|
2022-11-27 23:23:47 +08:00
|
|
|
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)
|
2022-12-01 22:26:41 +08:00
|
|
|
this.label = this.node.getComponentInChildren(Label)
|
|
|
|
this.label.string = nickname
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
this.fsm = this.addComponent(PlayerStateMachine)
|
|
|
|
this.fsm.init(type)
|
2022-12-01 22:26:41 +08:00
|
|
|
this.state = EntityStateEnum.Idle
|
2022-11-27 23:23:47 +08:00
|
|
|
|
|
|
|
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)
|
2022-12-01 22:26:41 +08:00
|
|
|
this.label.node.setScale(x > 0 ? 1 : -1, 1)
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|