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,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),
},
})
});
}
}