This commit is contained in:
sli97
2022-12-08 21:14:02 +08:00
parent 29104d4bed
commit 81a9a5a2f8
51 changed files with 1694 additions and 2815 deletions

View File

@@ -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)
}
}

View File

@@ -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;
}
}
}

View File

@@ -1,79 +1,83 @@
import { Tween, tween, Vec3, _decorator } from 'cc'
import { EntityManager } from '../../Base/EntityManager'
import { EntityTypeEnum, IBullet, IVec2 } from '../../Common'
import { EntityStateEnum, EventEnum } from '../../Enum'
import DataManager 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
import { Tween, tween, Vec3, _decorator } from "cc";
import { EntityManager } from "../../Base/EntityManager";
import { EntityTypeEnum, IBullet, IVec2 } from "../../Common";
import { EntityStateEnum, EventEnum } from "../../Enum";
import DataManager 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')
@ccclass("BulletManager")
export class BulletManager extends EntityManager implements IBullet {
//静态数据
id: number
owner: number
type: EntityTypeEnum
id: number;
owner: number;
type: EntityTypeEnum;
//动态数据
position: IVec2
direction: IVec2
position: IVec2;
direction: IVec2;
private angle: number
private tw: Tween<any>
private targetPos: Vec3
private angle: number;
private tw: Tween<any>;
private targetPos: Vec3;
init({ id, owner, type }: IBullet) {
this.id = id
this.owner = owner
this.type = type
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
this.targetPos = undefined
this.fsm = this.addComponent(BulletStateMachine);
this.fsm.init(type);
this.state = EntityStateEnum.Idle;
EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this)
this.node.active = false;
this.targetPos = undefined;
this.angle = undefined;
EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this);
}
handleExplosion(id: number, { x, y }: IVec2) {
if (this.id !== id) {
return
return;
}
const explosion = ObjectPoolManager.Instance.get(EntityTypeEnum.Explosion)
const explosionManager = explosion.getComponent(ExplosionManager) || explosion.addComponent(ExplosionManager)
const explosion = ObjectPoolManager.Instance.get(EntityTypeEnum.Explosion);
const explosionManager = explosion.getComponent(ExplosionManager) || explosion.addComponent(ExplosionManager);
explosionManager.init(EntityTypeEnum.Explosion, {
x, y,
})
x,
y,
});
EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosion, this)
ObjectPoolManager.Instance.ret(this.node)
DataManager.Instance.bulletMap.delete(this.id)
this.angle = undefined
EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosion, this);
ObjectPoolManager.Instance.ret(this.node);
DataManager.Instance.bulletMap.delete(this.id);
}
render(data: IBullet) {
this.renderPosition(data)
this.renderDirection(data)
this.renderPosition(data);
this.renderDirection(data);
}
renderPosition(data: IBullet) {
const newPos = new Vec3(data.position.x, data.position.y)
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)
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.tw = tween(this.node).to(0.1, {
position: this.targetPos
}).start()
this.tw?.stop();
this.node.setPosition(this.targetPos);
this.targetPos.set(newPos);
this.tw = tween(this.node)
.to(0.1, {
position: this.targetPos,
})
.start();
}
// this.node.setPosition(data.position.x, data.position.y)
@@ -81,12 +85,12 @@ export class BulletManager extends EntityManager implements IBullet {
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
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)
this.node.setRotationFromEuler(0, 0, this.angle);
// let angle: number, sign: number
// if (x !== 0) {

View File

@@ -1,45 +1,43 @@
import { _decorator, Animation } 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 } 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('BulletStateMachine')
@ccclass("BulletStateMachine")
export class BulletStateMachine extends StateMachine {
init(type: EntityTypeEnum) {
this.type = type
this.animationComponent = this.node.addComponent(Animation)
this.type = type;
this.animationComponent = this.node.addComponent(Animation);
this.initParams()
this.initStateMachines()
this.initAnimationEvent()
this.initParams();
this.initStateMachines();
this.initAnimationEvent();
}
initParams() {
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger())
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger());
}
initStateMachines() {
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`));
}
initAnimationEvent() {
}
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)
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;
}
}
}

View File

@@ -1,17 +1,16 @@
import { _decorator } from 'cc'
import { EntityManager } from '../../Base/EntityManager'
import { EntityTypeEnum, IVec2 } from '../../Common'
import { EntityStateEnum } from '../../Enum'
import { ExplosionStateMachine } from './ExplosionStateMachine'
const { ccclass, property } = _decorator
import { _decorator } from "cc";
import { EntityManager } from "../../Base/EntityManager";
import { EntityTypeEnum, IVec2 } from "../../Common";
import { EntityStateEnum } from "../../Enum";
import { ExplosionStateMachine } from "./ExplosionStateMachine";
const { ccclass, property } = _decorator;
@ccclass('ExplosionManager')
@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
this.node.setPosition(x, y);
this.fsm = this.addComponent(ExplosionStateMachine);
this.fsm.init(type);
this.state = EntityStateEnum.Idle;
}
}

View File

@@ -1,52 +1,52 @@
import { _decorator, Animation } from 'cc'
import State from '../../Base/State'
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
import { EntityTypeEnum } from '../../Common'
import { EntityStateEnum, ParamsNameEnum } from '../../Enum'
import ObjectPoolManager from '../../Global/ObjectPoolManager'
const { ccclass, property } = _decorator
import { _decorator, Animation } from "cc";
import State from "../../Base/State";
import StateMachine, { getInitParamsTrigger } from "../../Base/StateMachine";
import { EntityTypeEnum } from "../../Common";
import { EntityStateEnum, ParamsNameEnum } from "../../Enum";
import ObjectPoolManager from "../../Global/ObjectPoolManager";
const { ccclass, property } = _decorator;
@ccclass('ExplosionStateMachine')
@ccclass("ExplosionStateMachine")
export class ExplosionStateMachine extends StateMachine {
init(type: EntityTypeEnum) {
this.type = type
this.animationComponent = this.node.addComponent(Animation)
this.type = type;
this.animationComponent = this.node.addComponent(Animation);
this.initParams()
this.initStateMachines()
this.initAnimationEvent()
this.initParams();
this.initStateMachines();
this.initAnimationEvent();
}
initParams() {
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger())
this.params.set(ParamsNameEnum.Idle, getInitParamsTrigger());
}
initStateMachines() {
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
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)
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)
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;
}
}
}

View File

@@ -1,66 +1,61 @@
import { _decorator, Node, Vec2, UITransform } from 'cc'
import { EntityManager } from '../../Base/EntityManager'
import { ApiMsgEnum, EntityTypeEnum, InputTypeEnum, toFixed } from '../../Common'
import { EntityStateEnum, EventEnum } from '../../Enum'
import DataManager from '../../Global/DataManager'
import EventManager from '../../Global/EventManager'
import NetworkManager from '../../Global/NetworkManager'
import { WeaponStateMachine } from './WeaponStateMachine'
const { ccclass } = _decorator
import { _decorator, Node, Vec2, UITransform } from "cc";
import { EntityManager } from "../../Base/EntityManager";
import { EntityTypeEnum, InputTypeEnum, toFixed } from "../../Common";
import { EntityStateEnum, EventEnum } from "../../Enum";
import DataManager from "../../Global/DataManager";
import EventManager from "../../Global/EventManager";
import { WeaponStateMachine } from "./WeaponStateMachine";
const { ccclass } = _decorator;
@ccclass('WeaponManager')
@ccclass("WeaponManager")
export class WeaponManager extends EntityManager {
owner: number
type: EntityTypeEnum
owner: number;
type: EntityTypeEnum;
private body: Node
private anchor: Node
private point: Node
private body: Node;
private anchor: Node;
private point: Node;
get isSelf() {
return DataManager.Instance.myPlayerId === this.owner
}
init({ id, weaponType }: { id: number; weaponType: EntityTypeEnum }) {
this.owner = id;
this.type = weaponType;
init({ id, weaponType }: { id: number, weaponType: EntityTypeEnum }) {
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.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;
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)
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)
EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this);
EventManager.Instance.off(EventEnum.BulletBorn, this.handleBulletBorn, this);
}
handleBulletBorn(owner: number) {
if (this.owner !== owner) {
return
return;
}
this.state = EntityStateEnum.Attack
this.state = EntityStateEnum.Attack;
}
handleWeaponShoot() {
if (!this.isSelf) {
return
if (DataManager.Instance.myPlayerId !== this.owner) {
return;
}
const pointWorldPos = this.point.getWorldPosition()
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()
const anchorWorldPos = this.anchor.getWorldPosition();
const directionVec2 = new Vec2(pointWorldPos.x - anchorWorldPos.x, pointWorldPos.y - anchorWorldPos.y).normalize();
NetworkManager.Instance.sendMsg(ApiMsgEnum.MsgClientSync, {
EventManager.Instance.emit(EventEnum.ClientSync, {
type: InputTypeEnum.WeaponShoot,
owner: this.owner,
position: {
@@ -71,6 +66,6 @@ export class WeaponManager extends EntityManager {
x: toFixed(directionVec2.x),
y: toFixed(directionVec2.y),
},
})
});
}
}