update
This commit is contained in:
@@ -1,147 +1,141 @@
|
||||
import { _decorator, instantiate, ProgressBar, Label, Vec3, Tween, tween, director } from 'cc';
|
||||
import { EntityManager } from '../../Base/EntityManager';
|
||||
import { ApiMsgEnum, EntityTypeEnum, IActor, InputTypeEnum, IVec2, toFixed } from '../../Common';
|
||||
import { EntityStateEnum, EventEnum, SceneEnum } from '../../Enum';
|
||||
import DataManager from '../../Global/DataManager';
|
||||
import EventManager from '../../Global/EventManager';
|
||||
import NetworkManager from '../../Global/NetworkManager';
|
||||
import { rad2Angle } from '../../Utils';
|
||||
import { WeaponManager } from '../Weapon/WeaponManager';
|
||||
import { PlayerStateMachine } from './ActorStateMachine';
|
||||
import { _decorator, instantiate, ProgressBar, Label, Vec3, Tween, tween } from "cc";
|
||||
import { EntityManager } from "../../Base/EntityManager";
|
||||
import { EntityTypeEnum, IActor, InputTypeEnum, IVec2, toFixed } from "../../Common";
|
||||
import { EntityStateEnum, EventEnum, SceneEnum } from "../../Enum";
|
||||
import DataManager from "../../Global/DataManager";
|
||||
import EventManager from "../../Global/EventManager";
|
||||
import { rad2Angle } from "../../Utils";
|
||||
import { WeaponManager } from "../Weapon/WeaponManager";
|
||||
import { ActorStateMachine } from "./ActorStateMachine";
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('ActorManager')
|
||||
@ccclass("ActorManager")
|
||||
export class ActorManager extends EntityManager implements IActor {
|
||||
//静态数据
|
||||
id: number
|
||||
nickname: string
|
||||
type: EntityTypeEnum
|
||||
weaponType: EntityTypeEnum
|
||||
bulletType: EntityTypeEnum
|
||||
//静态数据
|
||||
id: number;
|
||||
nickname: string;
|
||||
type: EntityTypeEnum;
|
||||
weaponType: EntityTypeEnum;
|
||||
bulletType: EntityTypeEnum;
|
||||
|
||||
//动态数据
|
||||
hp: number
|
||||
position: IVec2
|
||||
direction: IVec2
|
||||
//动态数据
|
||||
hp: number;
|
||||
position: IVec2;
|
||||
direction: IVec2;
|
||||
|
||||
private hpBar: ProgressBar
|
||||
private label: Label
|
||||
private weapon: WeaponManager
|
||||
private hpBar: ProgressBar;
|
||||
private label: Label;
|
||||
private weapon: WeaponManager;
|
||||
|
||||
private tw: Tween<any>
|
||||
private targetPos: Vec3
|
||||
private tw: Tween<unknown>;
|
||||
private targetPos: Vec3;
|
||||
private isDead = false;
|
||||
|
||||
private isDead = false
|
||||
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;
|
||||
|
||||
get isSelf() {
|
||||
return DataManager.Instance.myPlayerId === this.id
|
||||
this.hpBar = this.node.getComponentInChildren(ProgressBar);
|
||||
this.label = this.node.getComponentInChildren(Label);
|
||||
this.label.string = nickname;
|
||||
|
||||
this.fsm = this.addComponent(ActorStateMachine);
|
||||
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);
|
||||
|
||||
this.targetPos = undefined;
|
||||
this.node.active = false;
|
||||
}
|
||||
|
||||
async tick(dt: number) {
|
||||
if (DataManager.Instance.myPlayerId !== this.id || this.isDead) {
|
||||
return;
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
this.targetPos = undefined
|
||||
this.node.active = false
|
||||
if (this.hp <= 0) {
|
||||
EventManager.Instance.emit(EventEnum.GameEnd);
|
||||
this.isDead = true;
|
||||
return;
|
||||
}
|
||||
|
||||
async tick(dt: number) {
|
||||
if (!this.isSelf) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.hp <= 0 && !this.isDead) {
|
||||
EventManager.Instance.emit(EventEnum.GameEnd)
|
||||
this.isDead = true
|
||||
return
|
||||
}
|
||||
|
||||
if (DataManager.Instance.jm.input.length()) {
|
||||
const { x, y } = DataManager.Instance.jm.input
|
||||
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
|
||||
type: InputTypeEnum.ActorMove,
|
||||
id: this.id,
|
||||
direction: {
|
||||
x: toFixed(x),
|
||||
y: toFixed(y),
|
||||
},
|
||||
dt: toFixed(dt)
|
||||
})
|
||||
}
|
||||
|
||||
if (DataManager.Instance.jm.input.length()) {
|
||||
const { x, y } = DataManager.Instance.jm.input;
|
||||
EventManager.Instance.emit(EventEnum.ClientSync, {
|
||||
type: InputTypeEnum.ActorMove,
|
||||
id: this.id,
|
||||
direction: {
|
||||
x: toFixed(x),
|
||||
y: toFixed(y),
|
||||
},
|
||||
dt: toFixed(dt),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
render(data: IActor) {
|
||||
this.renderHP(data)
|
||||
this.renderPosition(data)
|
||||
this.renderDirection(data)
|
||||
render(data: IActor) {
|
||||
this.renderHP(data);
|
||||
this.renderPosition(data);
|
||||
this.renderDirection(data);
|
||||
}
|
||||
|
||||
renderHP(data: IActor) {
|
||||
this.hp = data.hp;
|
||||
this.hpBar.progress = data.hp / this.hpBar.totalLength;
|
||||
}
|
||||
|
||||
renderPosition(data: IActor) {
|
||||
const newPos = new Vec3(data.position.x, data.position.y);
|
||||
if (!this.targetPos) {
|
||||
this.node.active = true;
|
||||
this.node.setPosition(newPos);
|
||||
this.targetPos = new Vec3(newPos);
|
||||
} else if (!this.targetPos.equals(newPos)) {
|
||||
this.tw?.stop();
|
||||
this.node.setPosition(this.targetPos);
|
||||
this.targetPos.set(newPos);
|
||||
this.state = EntityStateEnum.Run;
|
||||
this.tw = tween(this.node)
|
||||
.to(0.1, {
|
||||
position: this.targetPos,
|
||||
})
|
||||
.call(() => {
|
||||
this.state = EntityStateEnum.Idle;
|
||||
})
|
||||
.start();
|
||||
}
|
||||
// this.node.setPosition(data.position.x, data.position.y)
|
||||
}
|
||||
|
||||
renderHP(data: IActor) {
|
||||
this.hp = data.hp
|
||||
this.hpBar.progress = data.hp / this.hpBar.totalLength
|
||||
renderDirection(data: IActor) {
|
||||
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);
|
||||
this.hpBar.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);
|
||||
|
||||
renderPosition(data: IActor) {
|
||||
const newPos = new Vec3(data.position.x, data.position.y)
|
||||
if (!this.targetPos) {
|
||||
this.node.active = true
|
||||
this.node.setPosition(newPos)
|
||||
this.targetPos = new Vec3(newPos)
|
||||
} else if (!this.targetPos.equals(newPos)) {
|
||||
this.tw?.stop()
|
||||
this.node.setPosition(this.targetPos)
|
||||
this.targetPos.set(newPos)
|
||||
this.state = EntityStateEnum.Run
|
||||
this.tw = tween(this.node).to(0.1, {
|
||||
position: this.targetPos
|
||||
}).call(() => {
|
||||
this.state = EntityStateEnum.Idle
|
||||
}).start()
|
||||
}
|
||||
|
||||
// this.node.setPosition(data.position.x, data.position.y)
|
||||
}
|
||||
|
||||
renderDirection(data: IActor) {
|
||||
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)
|
||||
this.hpBar.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)
|
||||
}
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,48 +1,47 @@
|
||||
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
|
||||
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 {
|
||||
@ccclass("ActorStateMachine")
|
||||
export class ActorStateMachine extends StateMachine {
|
||||
init(type: EntityTypeEnum) {
|
||||
this.type = type
|
||||
this.animationComponent = this.node.addComponent(Animation)
|
||||
this.initParams()
|
||||
this.initStateMachines()
|
||||
this.initAnimationEvent()
|
||||
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())
|
||||
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))
|
||||
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() {
|
||||
}
|
||||
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)
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Run);
|
||||
} else if (this.params.get(ParamsNameEnum.Idle).value) {
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
|
||||
} else {
|
||||
this.currentState = this.currentState
|
||||
this.currentState = this.currentState;
|
||||
}
|
||||
break
|
||||
break;
|
||||
default:
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
|
||||
break
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user