update
This commit is contained in:
12
apps/client/assets/Scripts/Base.meta
Normal file
12
apps/client/assets/Scripts/Base.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "b9842cc1-d805-42af-b6b9-b15d5dd32f65",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
23
apps/client/assets/Scripts/Base/EntityManager.ts
Normal file
23
apps/client/assets/Scripts/Base/EntityManager.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { _decorator, Component } from 'cc';
|
||||
import { EntityStateEnum } from '../Enum';
|
||||
import StateMachine from './StateMachine';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('EntityManager')
|
||||
export abstract class EntityManager extends Component {
|
||||
fsm: StateMachine
|
||||
private _state: EntityStateEnum
|
||||
|
||||
|
||||
get state() {
|
||||
return this._state
|
||||
}
|
||||
|
||||
set state(newState) {
|
||||
this._state = newState
|
||||
this.fsm.setParams(newState, true)
|
||||
}
|
||||
|
||||
abstract init(...args: any[]): void
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Base/EntityManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Base/EntityManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2e4eed53-d1b9-42ef-97aa-8d0a8da927f7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
13
apps/client/assets/Scripts/Base/Singleton.ts
Normal file
13
apps/client/assets/Scripts/Base/Singleton.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export default class Singleton {
|
||||
private static _instance: any = null
|
||||
|
||||
static GetInstance<T>(): T {
|
||||
if (this._instance === null) {
|
||||
this._instance = new this()
|
||||
}
|
||||
return this._instance
|
||||
}
|
||||
|
||||
protected constructor() {
|
||||
}
|
||||
}
|
9
apps/client/assets/Scripts/Base/Singleton.ts.meta
Normal file
9
apps/client/assets/Scripts/Base/Singleton.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0ee5b84a-8971-410f-a39d-2cf96597dd45",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
48
apps/client/assets/Scripts/Base/State.ts
Normal file
48
apps/client/assets/Scripts/Base/State.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { animation, AnimationClip, Sprite, SpriteFrame } from 'cc'
|
||||
import DataManager from '../Global/DataManager'
|
||||
import { ResourceManager } from '../Global/ResourceManager'
|
||||
import { sortSpriteFrame } from '../Utils'
|
||||
import StateMachine from './StateMachine'
|
||||
|
||||
/***
|
||||
* unit:milisecond
|
||||
*/
|
||||
export const ANIMATION_SPEED = 1 / 10
|
||||
|
||||
/***
|
||||
* 状态(每组动画的承载容器,持有SpriteAnimation组件执行播放)
|
||||
*/
|
||||
export default class State {
|
||||
private animationClip: AnimationClip
|
||||
constructor(
|
||||
private fsm: StateMachine,
|
||||
private path: string,
|
||||
private wrapMode: AnimationClip.WrapMode = AnimationClip.WrapMode.Normal,
|
||||
private force: boolean = false,
|
||||
) {
|
||||
//生成动画轨道属性
|
||||
const track = new animation.ObjectTrack()
|
||||
track.path = new animation.TrackPath().toComponent(Sprite).toProperty('spriteFrame')
|
||||
const spriteFrames = DataManager.Instance.textureMap.get(this.path)
|
||||
const frames: Array<[number, SpriteFrame]> = sortSpriteFrame(spriteFrames).map((item, index) => [
|
||||
index * ANIMATION_SPEED,
|
||||
item,
|
||||
])
|
||||
track.channel.curve.assignSorted(frames)
|
||||
|
||||
//动画添加轨道
|
||||
this.animationClip = new AnimationClip()
|
||||
this.animationClip.name = this.path
|
||||
this.animationClip.duration = frames.length * ANIMATION_SPEED
|
||||
this.animationClip.addTrack(track)
|
||||
this.animationClip.wrapMode = this.wrapMode
|
||||
}
|
||||
|
||||
run() {
|
||||
if (this.fsm.animationComponent.defaultClip?.name === this.animationClip.name && !this.force) {
|
||||
return
|
||||
}
|
||||
this.fsm.animationComponent.defaultClip = this.animationClip
|
||||
this.fsm.animationComponent.play()
|
||||
}
|
||||
}
|
9
apps/client/assets/Scripts/Base/State.ts.meta
Normal file
9
apps/client/assets/Scripts/Base/State.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "226c1921-1459-4764-82e6-b1a11c5a73c2",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
91
apps/client/assets/Scripts/Base/StateMachine.ts
Normal file
91
apps/client/assets/Scripts/Base/StateMachine.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { _decorator, Animation, Component } from 'cc'
|
||||
import { EntityTypeEnum, FsmParamTypeEnum } from '../Enum'
|
||||
const { ccclass } = _decorator
|
||||
import State from './State'
|
||||
import SubStateMachine from './SubStateMachine'
|
||||
|
||||
type ParamsValueType = boolean | number
|
||||
|
||||
export interface IParamsValue {
|
||||
type: FsmParamTypeEnum
|
||||
value: ParamsValueType
|
||||
}
|
||||
|
||||
export const getInitParamsTrigger = () => {
|
||||
return {
|
||||
type: FsmParamTypeEnum.Trigger,
|
||||
value: false,
|
||||
}
|
||||
}
|
||||
|
||||
export const getInitParamsNumber = () => {
|
||||
return {
|
||||
type: FsmParamTypeEnum.Number,
|
||||
value: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* 流动图
|
||||
* 1.entity的state或者direction改变触发setter
|
||||
* 2.setter里触发fsm的setParams方法
|
||||
* 3.setParams执行run方法(run方法由子类重写)
|
||||
* 4.run方法会更改currentState,然后触发currentState的setter
|
||||
* 5-1.如果currentState是子状态机,继续执行他的run方法,run方法又会设置子状态机的currentState,触发子状态run方法
|
||||
* 5-2.如果是子状态,run方法就是播放动画
|
||||
*/
|
||||
|
||||
/***
|
||||
* 有限状态机基类
|
||||
*/
|
||||
@ccclass('StateMachine')
|
||||
export default abstract class StateMachine extends Component {
|
||||
private _currentState: State | SubStateMachine = null
|
||||
params: Map<string, IParamsValue> = new Map()
|
||||
stateMachines: Map<string, SubStateMachine | State> = new Map()
|
||||
animationComponent: Animation
|
||||
type: EntityTypeEnum
|
||||
|
||||
getParams(paramName: string) {
|
||||
if (this.params.has(paramName)) {
|
||||
return this.params.get(paramName).value
|
||||
}
|
||||
}
|
||||
|
||||
setParams(paramName: string, value: ParamsValueType) {
|
||||
if (this.params.has(paramName)) {
|
||||
this.params.get(paramName).value = value
|
||||
this.run()
|
||||
this.resetTrigger()
|
||||
}
|
||||
}
|
||||
|
||||
get currentState() {
|
||||
return this._currentState
|
||||
}
|
||||
|
||||
set currentState(newState) {
|
||||
if (!newState) {
|
||||
return
|
||||
}
|
||||
this._currentState = newState
|
||||
this._currentState.run()
|
||||
}
|
||||
|
||||
/***
|
||||
* 清空所有trigger
|
||||
*/
|
||||
resetTrigger() {
|
||||
for (const [, value] of this.params) {
|
||||
if (value.type === FsmParamTypeEnum.Trigger) {
|
||||
value.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* 由子类重写,方法目标是根据当前状态和参数修改currentState
|
||||
*/
|
||||
abstract init(...args: any[]): void
|
||||
abstract run(): void
|
||||
}
|
9
apps/client/assets/Scripts/Base/StateMachine.ts.meta
Normal file
9
apps/client/assets/Scripts/Base/StateMachine.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "66538d45-0fcf-4ed2-8e47-a0a688ddaf3c",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
30
apps/client/assets/Scripts/Base/SubStateMachine.ts
Normal file
30
apps/client/assets/Scripts/Base/SubStateMachine.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import State from './State'
|
||||
import StateMachine from './StateMachine'
|
||||
|
||||
/***
|
||||
* 子有限状态机基类
|
||||
* 用处:例如有个idle的state,但是有多个方向,为了让主状态机更整洁,可以把同类型的但具体不同的state都封装在子状态机中
|
||||
*/
|
||||
export default abstract class SubStateMachine {
|
||||
private _currentState: State = null
|
||||
stateMachines: Map<string, State> = new Map()
|
||||
|
||||
constructor(public fsm: StateMachine) {}
|
||||
|
||||
get currentState() {
|
||||
return this._currentState
|
||||
}
|
||||
|
||||
set currentState(newState) {
|
||||
if (!newState) {
|
||||
return
|
||||
}
|
||||
this._currentState = newState
|
||||
this._currentState.run()
|
||||
}
|
||||
|
||||
/***
|
||||
* 具体类实现
|
||||
*/
|
||||
abstract run(): void
|
||||
}
|
9
apps/client/assets/Scripts/Base/SubStateMachine.ts.meta
Normal file
9
apps/client/assets/Scripts/Base/SubStateMachine.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "7f9fac71-9820-41b2-8b4d-2dc4185414ef",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity.meta
Normal file
12
apps/client/assets/Scripts/Entity.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "b0c3f1a6-89a2-4921-bf10-22ae7925cf6e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Bullet.meta
Normal file
12
apps/client/assets/Scripts/Entity/Bullet.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "9265fd0c-f1ae-471b-afa0-7f17e87d408f",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
84
apps/client/assets/Scripts/Entity/Bullet/BulletManager.ts
Normal file
84
apps/client/assets/Scripts/Entity/Bullet/BulletManager.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { _decorator } from 'cc'
|
||||
import { EntityManager } from '../../Base/EntityManager'
|
||||
import { EntityTypeEnum, EntityStateEnum, EventEnum } from '../../Enum'
|
||||
import DataManager, { IBullet, IVec2 } 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')
|
||||
export class BulletManager extends EntityManager {
|
||||
//静态数据
|
||||
id: number
|
||||
owner: number
|
||||
type: EntityTypeEnum
|
||||
|
||||
//动态数据
|
||||
position: IVec2
|
||||
direction: IVec2
|
||||
|
||||
private angle: number
|
||||
|
||||
init({ id, owner, type }: IBullet) {
|
||||
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
|
||||
|
||||
EventManager.Instance.on(EventEnum.ExplosionBorn, this.handleExplosion, this)
|
||||
}
|
||||
|
||||
handleExplosion(id: number, { x, y }: IVec2) {
|
||||
if (this.id !== id) {
|
||||
return
|
||||
}
|
||||
|
||||
const explosion = ObjectPoolManager.Instance.get(EntityTypeEnum.Explosion)
|
||||
const explosionManager = explosion.getComponent(ExplosionManager) || explosion.addComponent(ExplosionManager)
|
||||
explosionManager.init(EntityTypeEnum.Explosion, {
|
||||
x, y,
|
||||
})
|
||||
|
||||
EventManager.Instance.off(EventEnum.ExplosionBorn, this.handleExplosion, this)
|
||||
ObjectPoolManager.Instance.ret(this.node)
|
||||
DataManager.Instance.bulletMap.delete(this.id)
|
||||
this.angle = undefined
|
||||
}
|
||||
|
||||
render(data: IBullet) {
|
||||
this.node.active = true
|
||||
this.renderPosition(data)
|
||||
this.renderDirection(data)
|
||||
}
|
||||
|
||||
renderPosition(data: IBullet) {
|
||||
this.node.setPosition(data.position.x, data.position.y)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
this.node.setRotationFromEuler(0, 0, this.angle)
|
||||
|
||||
// let angle: number, sign: number
|
||||
// if (x !== 0) {
|
||||
// angle = x > 0 ? rad2Angle(Math.atan(y / x)) : rad2Angle(Math.atan(-y / x)) + 180
|
||||
// sign = x > 0 ? 1 : -1
|
||||
// } else {
|
||||
// angle = rad2Angle(Math.PI / 2)
|
||||
// sign = y > 0 ? 1 : -1
|
||||
// }
|
||||
// this.node.setRotationFromEuler(0, 0, sign * angle)
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3cb4bec9-dd63-4ffe-9a28-8f8347cd327b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
import { _decorator, Animation } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityTypeEnum, EntityStateEnum, ParamsNameEnum } from '../../Enum'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('BulletStateMachine')
|
||||
export class BulletStateMachine 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())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
|
||||
}
|
||||
|
||||
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)
|
||||
} else {
|
||||
this.currentState = this.currentState
|
||||
}
|
||||
break
|
||||
default:
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Idle)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "531b884c-93b6-4d0e-a0fc-e8861eebf6e7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Explosion.meta
Normal file
12
apps/client/assets/Scripts/Entity/Explosion.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "3302ce3a-fac5-42c6-8060-e3e8682cfcf1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
import { _decorator } from 'cc'
|
||||
import { EntityManager } from '../../Base/EntityManager'
|
||||
import { EntityTypeEnum, EntityStateEnum } from '../../Enum'
|
||||
import { IVec2 } from '../../Global/DataManager'
|
||||
import { ExplosionStateMachine } from './ExplosionStateMachine'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@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
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "34e9e358-082f-4674-9e38-a3c27f8b013b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
import { _decorator, Animation } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityTypeEnum, EntityStateEnum, ParamsNameEnum } from '../../Enum'
|
||||
import ObjectPoolManager from '../../Global/ObjectPoolManager'
|
||||
const { ccclass, property } = _decorator
|
||||
|
||||
@ccclass('ExplosionStateMachine')
|
||||
export class ExplosionStateMachine 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())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
run() {
|
||||
switch (this.currentState) {
|
||||
case this.stateMachines.get(ParamsNameEnum.Idle):
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "35a9f553-26d5-49c8-bcc0-d1011c07e124",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Player.meta
Normal file
12
apps/client/assets/Scripts/Entity/Player.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "5802fd2f-7823-4294-91c8-d71adca41319",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
113
apps/client/assets/Scripts/Entity/Player/PlayerManager.ts
Normal file
113
apps/client/assets/Scripts/Entity/Player/PlayerManager.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { _decorator, instantiate, ProgressBar, Label } from 'cc';
|
||||
import { EntityManager } from '../../Base/EntityManager';
|
||||
import { EntityTypeEnum, EntityStateEnum, InputType } from '../../Enum';
|
||||
import DataManager, { IPlayer, IVec2 } from '../../Global/DataManager';
|
||||
import { rad2Angle } from '../../Utils';
|
||||
import { WeaponManager } from '../Weapon/WeaponManager';
|
||||
import { PlayerStateMachine } from './PlayerStateMachine';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('PlayerManager')
|
||||
export class PlayerManager extends EntityManager {
|
||||
//静态数据
|
||||
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: 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)
|
||||
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
|
||||
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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "4cc81cda-b869-4eb8-855e-126f16aa7a12",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
import { _decorator, Animation, AnimationClip } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityTypeEnum, 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
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3bdb087b-0d43-4b85-81e4-e480cd43405d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Entity/Weapon.meta
Normal file
12
apps/client/assets/Scripts/Entity/Weapon.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "b8a46a8b-5505-436b-8870-2965599d2168",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
74
apps/client/assets/Scripts/Entity/Weapon/WeaponManager.ts
Normal file
74
apps/client/assets/Scripts/Entity/Weapon/WeaponManager.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { _decorator, Node, Vec2, UITransform } from 'cc'
|
||||
import { EntityManager } from '../../Base/EntityManager'
|
||||
import { EntityTypeEnum, EntityStateEnum, EventEnum, InputType } from '../../Enum'
|
||||
import DataManager, { IPlayer } from '../../Global/DataManager'
|
||||
import EventManager from '../../Global/EventManager'
|
||||
import { WeaponStateMachine } from './WeaponStateMachine'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('WeaponManager')
|
||||
export class WeaponManager extends EntityManager {
|
||||
owner: number
|
||||
type: EntityTypeEnum
|
||||
|
||||
private body: Node
|
||||
private anchor: Node
|
||||
private point: Node
|
||||
|
||||
get isSelf() {
|
||||
return DataManager.Instance.myPlayerId === this.owner
|
||||
}
|
||||
|
||||
init({ id, weaponType }: IPlayer) {
|
||||
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.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)
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
EventManager.Instance.off(EventEnum.WeaponShoot, this.handleWeaponShoot, this)
|
||||
EventManager.Instance.off(EventEnum.BulletBorn, this.handleBulletBorn, this)
|
||||
}
|
||||
|
||||
handleBulletBorn(owner: number) {
|
||||
if (this.owner !== owner) {
|
||||
return
|
||||
}
|
||||
|
||||
this.state = EntityStateEnum.Attack
|
||||
}
|
||||
|
||||
handleWeaponShoot() {
|
||||
if (!this.isSelf) {
|
||||
return
|
||||
}
|
||||
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()
|
||||
|
||||
DataManager.Instance.applyInput({
|
||||
type: InputType.WeaponShoot,
|
||||
owner: this.owner,
|
||||
position: {
|
||||
x: pointStagePos.x,
|
||||
y: pointStagePos.y
|
||||
},
|
||||
direction: {
|
||||
x: directionVec2.x,
|
||||
y: directionVec2.y
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "eefb7dfd-3b96-46cc-a83a-c5bafe656d8a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
import { _decorator, Animation, AnimationClip } from 'cc'
|
||||
import State from '../../Base/State'
|
||||
import StateMachine, { getInitParamsTrigger } from '../../Base/StateMachine'
|
||||
import { EntityStateEnum, EntityTypeEnum, ParamsNameEnum } from '../../Enum'
|
||||
import { WeaponManager } from './WeaponManager'
|
||||
const { ccclass } = _decorator
|
||||
|
||||
@ccclass('WeaponStateMachine')
|
||||
export class WeaponStateMachine 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.Attack, getInitParamsTrigger())
|
||||
}
|
||||
|
||||
initStateMachines() {
|
||||
this.stateMachines.set(ParamsNameEnum.Idle, new State(this, `${this.type}${EntityStateEnum.Idle}`))
|
||||
this.stateMachines.set(ParamsNameEnum.Attack, new State(this, `${this.type}${EntityStateEnum.Attack}`, AnimationClip.WrapMode.Normal, true))
|
||||
}
|
||||
|
||||
initAnimationEvent() {
|
||||
this.animationComponent.on(Animation.EventType.FINISHED, () => {
|
||||
if (this.animationComponent.defaultClip.name.includes(EntityStateEnum.Attack)) {
|
||||
this.node.parent.getComponent(WeaponManager).state = EntityStateEnum.Idle
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
run() {
|
||||
switch (this.currentState) {
|
||||
case this.stateMachines.get(ParamsNameEnum.Idle):
|
||||
case this.stateMachines.get(ParamsNameEnum.Attack):
|
||||
if (this.params.get(ParamsNameEnum.Attack).value) {
|
||||
this.currentState = this.stateMachines.get(ParamsNameEnum.Attack)
|
||||
} 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
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1eed66f0-cb8f-4cc9-9f91-aa21b5c2883c",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Enum.meta
Normal file
12
apps/client/assets/Scripts/Enum.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "5b95c11b-0954-45ac-99ba-98df4825c906",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
77
apps/client/assets/Scripts/Enum/index.ts
Normal file
77
apps/client/assets/Scripts/Enum/index.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
export enum FsmParamTypeEnum {
|
||||
Number = 'Number',
|
||||
Trigger = 'Trigger',
|
||||
}
|
||||
|
||||
export enum EntityStateEnum {
|
||||
Idle = 'Idle',
|
||||
Run = 'Run',
|
||||
Attack = 'Attack',
|
||||
}
|
||||
|
||||
export enum ParamsNameEnum {
|
||||
Idle = 'Idle',
|
||||
Run = 'Run',
|
||||
Attack = 'Attack',
|
||||
}
|
||||
|
||||
export enum EventEnum {
|
||||
WeaponShoot = 'WeaponShoot',
|
||||
BulletBorn = 'BulletBorn',
|
||||
ExplosionBorn = 'ExplosionBorn',
|
||||
RoomJoin = 'RoomJoin',
|
||||
GameStart = 'GameStart',
|
||||
}
|
||||
|
||||
export enum EntityTypeEnum {
|
||||
Map1 = 'Map1',
|
||||
Player1 = 'Player1',
|
||||
Player2 = 'Player2',
|
||||
Weapon1 = 'Weapon1',
|
||||
Weapon2 = 'Weapon2',
|
||||
Bullet1 = 'Bullet1',
|
||||
Bullet2 = 'Bullet2',
|
||||
Explosion = 'Explosion',
|
||||
JoyStick = 'JoyStick',
|
||||
Shoot = 'Shoot',
|
||||
}
|
||||
|
||||
export enum PrefabPathEnum {
|
||||
Map1 = 'prefab/Map1',
|
||||
Player1 = 'prefab/Player',
|
||||
Player2 = 'prefab/Player',
|
||||
Weapon1 = 'prefab/Weapon1',
|
||||
Weapon2 = 'prefab/Weapon2',
|
||||
Bullet1 = 'prefab/Bullet',
|
||||
Bullet2 = 'prefab/Bullet',
|
||||
Explosion = 'prefab/Explosion',
|
||||
JoyStick = 'prefab/JoyStick',
|
||||
Shoot = 'prefab/Shoot',
|
||||
}
|
||||
|
||||
export enum TexturePathEnum {
|
||||
Player1Idle = 'texture/player/player1/idle',
|
||||
Player1Run = 'texture/player/player1/run',
|
||||
Player2Idle = 'texture/player/player2/idle',
|
||||
Player2Run = 'texture/player/player2/run',
|
||||
Weapon1Idle = 'texture/weapon/weapon1/idle',
|
||||
Weapon1Attack = 'texture/weapon/weapon1/attack',
|
||||
Weapon2Idle = 'texture/weapon/weapon2/idle',
|
||||
Weapon2Attack = 'texture/weapon/weapon2/attack',
|
||||
Bullet1Idle = 'texture/bullet/bullet1',
|
||||
Bullet2Idle = 'texture/bullet/bullet2',
|
||||
ExplosionIdle = 'texture/explosion',
|
||||
}
|
||||
|
||||
export enum InputType {
|
||||
PlayerMove = 'PlayerMove',
|
||||
WeaponShoot = 'WeaponShoot',
|
||||
TimePast = 'TimePast',
|
||||
}
|
||||
|
||||
export enum SceneEnum {
|
||||
Login = 'Login',
|
||||
Hall = 'Hall',
|
||||
Room = 'Room',
|
||||
Battle = 'Battle',
|
||||
}
|
9
apps/client/assets/Scripts/Enum/index.ts.meta
Normal file
9
apps/client/assets/Scripts/Enum/index.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "393286c2-7b09-44d1-955f-049e7e5b36ea",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Global.meta
Normal file
12
apps/client/assets/Scripts/Global.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6262be8a-effd-4208-89cd-6532b055b4a0",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
173
apps/client/assets/Scripts/Global/DataManager.ts
Normal file
173
apps/client/assets/Scripts/Global/DataManager.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import { Node, Prefab, SpriteFrame } from 'cc'
|
||||
import Singleton from '../Base/Singleton'
|
||||
import { BulletManager } from '../Entity/Bullet/BulletManager'
|
||||
import { PlayerManager } from '../Entity/Player/PlayerManager'
|
||||
import { EntityTypeEnum, EventEnum, InputType } from '../Enum'
|
||||
import { JoyStickManager } from '../UI/JoyStickManager'
|
||||
import EventManager from './EventManager'
|
||||
import { IData } from './NetworkManager'
|
||||
|
||||
export type IPlayer = Pick<PlayerManager, 'id' | 'nickname' | 'hp' | 'position' | 'direction' | 'type' | 'weaponType' | 'bulletType'>
|
||||
export type IBullet = Pick<BulletManager, 'id' | 'owner' | 'position' | 'direction' | 'type'>
|
||||
|
||||
const PLAYER_SPEED = 100
|
||||
const BULLET_SPEED = 600
|
||||
|
||||
const WEAPON_DAMAGE = 5
|
||||
|
||||
const PLAYER_RADIUS = 50
|
||||
const BULLET_RADIUS = 10
|
||||
|
||||
export interface IVec2 {
|
||||
x: number;
|
||||
y: number
|
||||
}
|
||||
|
||||
interface IState {
|
||||
players: IPlayer[],
|
||||
bullets: IBullet[],
|
||||
nextBulletId: number
|
||||
}
|
||||
|
||||
interface IPlayerMove {
|
||||
type: InputType.PlayerMove
|
||||
id: number;
|
||||
direction: IVec2;
|
||||
dt: number;
|
||||
}
|
||||
|
||||
interface IWeaponShoot {
|
||||
type: InputType.WeaponShoot
|
||||
owner: number;
|
||||
position: IVec2;
|
||||
direction: IVec2;
|
||||
}
|
||||
|
||||
interface ITimePast {
|
||||
type: InputType.TimePast;
|
||||
dt: number
|
||||
}
|
||||
|
||||
export default class DataManager extends Singleton {
|
||||
static get Instance() {
|
||||
return super.GetInstance<DataManager>()
|
||||
}
|
||||
|
||||
stage: Node
|
||||
jm: JoyStickManager
|
||||
|
||||
prefabMap: Map<string, Prefab> = new Map()
|
||||
textureMap: Map<string, SpriteFrame[]> = new Map()
|
||||
|
||||
playerMap: Map<number, PlayerManager> = new Map()
|
||||
bulletMap: Map<number, BulletManager> = new Map()
|
||||
|
||||
myPlayerId = 1
|
||||
roomInfo: IData
|
||||
mapSize = {
|
||||
x: 960,
|
||||
y: 640,
|
||||
}
|
||||
|
||||
state: IState = {
|
||||
players: [{
|
||||
id: 2,
|
||||
nickname: "哈哈1",
|
||||
position: {
|
||||
x: -200,
|
||||
y: -200
|
||||
},
|
||||
direction: {
|
||||
x: 1,
|
||||
y: 0
|
||||
},
|
||||
hp: 100,
|
||||
type: EntityTypeEnum.Player1,
|
||||
weaponType: EntityTypeEnum.Weapon1,
|
||||
bulletType: EntityTypeEnum.Bullet1,
|
||||
}, {
|
||||
id: 1,
|
||||
nickname: "哈哈2",
|
||||
position: {
|
||||
x: 200,
|
||||
y: 200
|
||||
},
|
||||
direction: {
|
||||
x: 0,
|
||||
y: -1
|
||||
},
|
||||
hp: 100,
|
||||
type: EntityTypeEnum.Player2,
|
||||
weaponType: EntityTypeEnum.Weapon2,
|
||||
bulletType: EntityTypeEnum.Bullet2,
|
||||
}],
|
||||
bullets: [],
|
||||
nextBulletId: 1
|
||||
}
|
||||
|
||||
applyInput(input: IPlayerMove | IWeaponShoot | ITimePast) {
|
||||
switch (input.type) {
|
||||
case InputType.PlayerMove: {
|
||||
const { direction: { x, y }, dt, id } = input
|
||||
const player = this.state.players.find(e => e.id === id)
|
||||
if (!player) {
|
||||
return
|
||||
}
|
||||
|
||||
player.position.x += x * PLAYER_SPEED * dt
|
||||
player.position.y += y * PLAYER_SPEED * dt
|
||||
player.direction = { x, y }
|
||||
break
|
||||
}
|
||||
|
||||
case InputType.WeaponShoot: {
|
||||
const { owner, position, direction } = input
|
||||
const bullet: IBullet = {
|
||||
id: this.state.nextBulletId++,
|
||||
owner,
|
||||
position,
|
||||
direction,
|
||||
type: this.playerMap.get(owner).bulletType
|
||||
}
|
||||
this.state.bullets.push(bullet)
|
||||
|
||||
EventManager.Instance.emit(EventEnum.BulletBorn, owner)
|
||||
break
|
||||
}
|
||||
case InputType.TimePast: {
|
||||
const { dt } = input
|
||||
const { bullets, players } = this.state
|
||||
|
||||
for (let i = bullets.length - 1; i >= 0; i--) {
|
||||
const bullet = bullets[i];
|
||||
for (let j = players.length - 1; j >= 0; j--) {
|
||||
const player = players[j];
|
||||
if (((player.position.x - bullet.position.x) ** 2 + (player.position.y - bullet.position.y) ** 2) < (PLAYER_RADIUS + BULLET_RADIUS) ** 2) {
|
||||
EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
|
||||
x: (player.position.x + bullet.position.x) / 2,
|
||||
y: (player.position.y + bullet.position.y) / 2,
|
||||
})
|
||||
|
||||
player.hp -= WEAPON_DAMAGE
|
||||
bullets.splice(i, 1)
|
||||
break
|
||||
}
|
||||
}
|
||||
if (Math.abs(bullet.position.x) > this.mapSize.x / 2 || Math.abs(bullet.position.y) > this.mapSize.y / 2) {
|
||||
EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
|
||||
x: bullet.position.x,
|
||||
y: bullet.position.y,
|
||||
})
|
||||
bullets.splice(i, 1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for (const bullet of this.state.bullets) {
|
||||
bullet.position.x += bullet.direction.x * BULLET_SPEED * dt
|
||||
bullet.position.y += bullet.direction.y * BULLET_SPEED * dt
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
9
apps/client/assets/Scripts/Global/DataManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Global/DataManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "1682ed16-4e4f-46b3-8aca-92baecd30b9d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
42
apps/client/assets/Scripts/Global/EventManager.ts
Normal file
42
apps/client/assets/Scripts/Global/EventManager.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import Singleton from '../Base/Singleton'
|
||||
import { EventEnum } from '../Enum';
|
||||
|
||||
interface IItem {
|
||||
func: Function;
|
||||
ctx: unknown;
|
||||
}
|
||||
|
||||
export default class EventManager extends Singleton {
|
||||
static get Instance() {
|
||||
return super.GetInstance<EventManager>();
|
||||
}
|
||||
|
||||
private map: Map<EventEnum, Array<IItem>> = new Map();
|
||||
|
||||
on(event: EventEnum, func: Function, ctx?: unknown) {
|
||||
if (this.map.has(event)) {
|
||||
this.map.get(event).push({ func, ctx });
|
||||
} else {
|
||||
this.map.set(event, [{ func, ctx }]);
|
||||
}
|
||||
}
|
||||
|
||||
off(event: EventEnum, func: Function, ctx?: unknown) {
|
||||
if (this.map.has(event)) {
|
||||
const index = this.map.get(event).findIndex(i => func === i.func && i.ctx === ctx);
|
||||
index > -1 && this.map.get(event).splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
emit(event: EventEnum, ...params: unknown[]) {
|
||||
if (this.map.has(event)) {
|
||||
this.map.get(event).forEach(({ func, ctx }) => {
|
||||
ctx ? func.apply(ctx, params) : func(...params);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.map.clear();
|
||||
}
|
||||
}
|
9
apps/client/assets/Scripts/Global/EventManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Global/EventManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2aa2e7de-b014-4ee7-a537-4bc99dbf9164",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
117
apps/client/assets/Scripts/Global/NetworkManager.ts
Normal file
117
apps/client/assets/Scripts/Global/NetworkManager.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import Singleton from '../Base/Singleton'
|
||||
|
||||
const TIMEOUT = 5000
|
||||
|
||||
export type IData = Record<string, any>
|
||||
|
||||
export interface ICallApiRet {
|
||||
success: boolean;
|
||||
error?: Error;
|
||||
res?: IData
|
||||
}
|
||||
|
||||
export enum ApiMsgEnum {
|
||||
ApiPlayerList = 'ApiPlayerList',
|
||||
ApiPlayerJoin = 'ApiPlayerJoin',
|
||||
ApiRoomList = 'ApiRoomList',
|
||||
ApiRoomCreate = 'ApiRoomCreate',
|
||||
ApiRoomJoin = 'ApiRoomJoin',
|
||||
ApiRoomLeave = 'ApiRoomLeave',
|
||||
MsgPlayerList = 'MsgPlayerList',
|
||||
MsgRoomList = 'MsgRoomList',
|
||||
MsgRoom = 'MsgRoom',
|
||||
}
|
||||
|
||||
export default class NetworkManager extends Singleton {
|
||||
static get Instance() {
|
||||
return super.GetInstance<NetworkManager>()
|
||||
}
|
||||
|
||||
ws: WebSocket
|
||||
port = 8888
|
||||
cbs: Map<string, Function[]> = new Map()
|
||||
isConnected = false
|
||||
|
||||
connect() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.ws = new WebSocket(`ws://localhost:${this.port}`)
|
||||
this.ws.onopen = () => {
|
||||
console.log("ws onopen")
|
||||
this.isConnected = true
|
||||
resolve(true)
|
||||
}
|
||||
|
||||
this.ws.onerror = () => {
|
||||
this.isConnected = false
|
||||
reject("ws onerror")
|
||||
}
|
||||
|
||||
this.ws.onclose = () => {
|
||||
this.isConnected = false
|
||||
reject("ws onclose")
|
||||
}
|
||||
|
||||
this.ws.onmessage = (e) => {
|
||||
try {
|
||||
const json = JSON.parse(e.data)
|
||||
const { name, data } = json
|
||||
try {
|
||||
if (this.cbs.has(name) && this.cbs.get(name).length) {
|
||||
console.log(json);
|
||||
this.cbs.get(name).forEach(cb => cb(data))
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("this.cbs.get(name).forEach(cb => cb(restData))", error)
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.log('解析失败,不是合法的JSON格式', error)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
sendMsg(name: string, data: IData) {
|
||||
this.ws.send(JSON.stringify({ name, data }))
|
||||
}
|
||||
|
||||
listenMsg(name: string, cb: Function) {
|
||||
if (this.cbs.has(name)) {
|
||||
this.cbs.get(name).push(cb)
|
||||
} else {
|
||||
this.cbs.set(name, [cb])
|
||||
}
|
||||
}
|
||||
|
||||
unlistenMsg(name: string, cb: Function) {
|
||||
if (this.cbs.has(name)) {
|
||||
const index = this.cbs.get(name).indexOf(cb)
|
||||
index > -1 && this.cbs.get(name).splice(index, 1)
|
||||
}
|
||||
}
|
||||
|
||||
callApi(name: string, data: IData) {
|
||||
return new Promise<ICallApiRet>((resolve) => {
|
||||
try {
|
||||
// 超时处理
|
||||
const timer = setTimeout(() => {
|
||||
resolve({ success: false, error: new Error('timeout') })
|
||||
this.unlistenMsg(name, cb)
|
||||
}, TIMEOUT)
|
||||
|
||||
// 回调处理
|
||||
const cb = (res: ICallApiRet) => {
|
||||
resolve(res)
|
||||
clearTimeout(timer)
|
||||
this.unlistenMsg(name, cb)
|
||||
}
|
||||
this.listenMsg(name, cb)
|
||||
|
||||
this.ws.send(JSON.stringify({ name, data }))
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
resolve({ success: false, error: error as Error })
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
9
apps/client/assets/Scripts/Global/NetworkManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Global/NetworkManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "588dfbf5-0456-472f-8382-746392fb1a06",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
50
apps/client/assets/Scripts/Global/ObjectPoolManager.ts
Normal file
50
apps/client/assets/Scripts/Global/ObjectPoolManager.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import Singleton from '../Base/Singleton'
|
||||
import { instantiate, Node } from 'cc'
|
||||
import { EntityTypeEnum } from '../Enum'
|
||||
import DataManager from './DataManager'
|
||||
|
||||
export default class ObjectPoolManager extends Singleton {
|
||||
static get Instance() {
|
||||
return super.GetInstance<ObjectPoolManager>()
|
||||
}
|
||||
|
||||
private objectPool: Node = null
|
||||
private map: Map<EntityTypeEnum, Node[]> = new Map()
|
||||
|
||||
private getContainerName(objectName: EntityTypeEnum) {
|
||||
return objectName + 'Pool'
|
||||
}
|
||||
|
||||
get(objectName: EntityTypeEnum) {
|
||||
if (this.objectPool === null) {
|
||||
this.objectPool = new Node("ObjectPool")
|
||||
this.objectPool.setParent(DataManager.Instance.stage)
|
||||
}
|
||||
|
||||
if (!this.map.has(objectName)) {
|
||||
this.map.set(objectName, [])
|
||||
const container = new Node(this.getContainerName(objectName))
|
||||
container.setParent(this.objectPool)
|
||||
}
|
||||
|
||||
let node: Node
|
||||
const nodes = this.map.get(objectName)
|
||||
|
||||
if (!nodes.length) {
|
||||
const prefab = DataManager.Instance.prefabMap.get(objectName)
|
||||
node = instantiate(prefab)
|
||||
node.name = objectName
|
||||
node.setParent(this.objectPool.getChildByName(this.getContainerName(objectName)))
|
||||
} else {
|
||||
node = nodes.pop()
|
||||
}
|
||||
node.active = true
|
||||
return node
|
||||
}
|
||||
|
||||
ret(object: Node) {
|
||||
object.active = false
|
||||
const objectName = object.name as EntityTypeEnum
|
||||
this.map.get(objectName).push(object)
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "01a37612-1a4a-45ee-8be8-3e3360996442",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
32
apps/client/assets/Scripts/Global/ResourceManager.ts
Normal file
32
apps/client/assets/Scripts/Global/ResourceManager.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { _decorator, resources, Asset } from 'cc'
|
||||
import Singleton from '../Base/Singleton'
|
||||
|
||||
export class ResourceManager extends Singleton {
|
||||
static get Instance() {
|
||||
return super.GetInstance<ResourceManager>()
|
||||
}
|
||||
|
||||
loadRes<T extends Asset>(path: string, type: new (...args: any[]) => T) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
resources.load(path, type, (err, res) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
loadDir<T extends Asset>(path: string, type: new (...args: any[]) => T) {
|
||||
return new Promise<T[]>((resolve, reject) => {
|
||||
resources.loadDir(path, type, (err, res) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
resolve(res)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "6278727d-3641-46d1-ae43-52b7829402d1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Scene.meta
Normal file
12
apps/client/assets/Scripts/Scene.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "7129d004-4256-413c-a2e9-34ee8607d89d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
151
apps/client/assets/Scripts/Scene/BattleManager.ts
Normal file
151
apps/client/assets/Scripts/Scene/BattleManager.ts
Normal file
@@ -0,0 +1,151 @@
|
||||
import { _decorator, Component, Node, Prefab, instantiate, SpriteFrame } from 'cc';
|
||||
import { PlayerManager } from '../Entity/Player/PlayerManager';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import { JoyStickManager } from '../UI/JoyStickManager';
|
||||
import { ResourceManager } from '../Global/ResourceManager';
|
||||
import { EntityTypeEnum, InputType, PrefabPathEnum, TexturePathEnum } from '../Enum';
|
||||
import NetworkManager from '../Global/NetworkManager';
|
||||
import ObjectPoolManager from '../Global/ObjectPoolManager';
|
||||
import { BulletManager } from '../Entity/Bullet/BulletManager';
|
||||
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('BattleManager')
|
||||
export class BattleManager extends Component {
|
||||
stage: Node
|
||||
ui: Node
|
||||
isInited = false
|
||||
onLoad() {
|
||||
this.stage = DataManager.Instance.stage = this.node.getChildByName("Stage")
|
||||
this.ui = this.node.getChildByName("UI")
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.clearGame()
|
||||
await this.loadRes()
|
||||
this.initScene()
|
||||
await this.connectServer()
|
||||
this.isInited = true
|
||||
}
|
||||
|
||||
clearGame() {
|
||||
this.stage.destroyAllChildren()
|
||||
this.ui.destroyAllChildren()
|
||||
}
|
||||
|
||||
async loadRes() {
|
||||
const list = []
|
||||
for (const type in PrefabPathEnum) {
|
||||
const p = ResourceManager.Instance.loadRes(PrefabPathEnum[type], Prefab).then((prefab) => {
|
||||
DataManager.Instance.prefabMap.set(type, prefab)
|
||||
})
|
||||
list.push(p)
|
||||
}
|
||||
for (const type in TexturePathEnum) {
|
||||
const p = ResourceManager.Instance.loadDir(TexturePathEnum[type], SpriteFrame).then((spriteFrames) => {
|
||||
DataManager.Instance.textureMap.set(type, spriteFrames)
|
||||
})
|
||||
list.push(p)
|
||||
}
|
||||
await Promise.all(list)
|
||||
}
|
||||
|
||||
async initScene() {
|
||||
this.initJoyStick()
|
||||
this.initShoot()
|
||||
this.initMap()
|
||||
}
|
||||
|
||||
async connectServer() {
|
||||
if (!await NetworkManager.Instance.connect().catch(() => false)) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
||||
await this.connectServer()
|
||||
}
|
||||
}
|
||||
|
||||
initJoyStick() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.JoyStick)
|
||||
const joySitck = instantiate(prefab)
|
||||
joySitck.setParent(this.ui)
|
||||
const jm = DataManager.Instance.jm = joySitck.getComponent(JoyStickManager)
|
||||
jm.init()
|
||||
}
|
||||
|
||||
initShoot() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Shoot)
|
||||
const shoot = instantiate(prefab)
|
||||
shoot.setParent(this.ui)
|
||||
}
|
||||
|
||||
initMap() {
|
||||
const prefab = DataManager.Instance.prefabMap.get(EntityTypeEnum.Map1)
|
||||
const map = instantiate(prefab)
|
||||
map.setParent(this.stage)
|
||||
}
|
||||
|
||||
update(dt: number) {
|
||||
if (!this.isInited) {
|
||||
return
|
||||
}
|
||||
this.render()
|
||||
this.tick(dt)
|
||||
}
|
||||
|
||||
tick(dt: number) {
|
||||
this.tickPlayer(dt)
|
||||
this.tickGlobal(dt)
|
||||
}
|
||||
|
||||
tickPlayer(dt: number) {
|
||||
for (const p of DataManager.Instance.state.players) {
|
||||
const playerManager = DataManager.Instance.playerMap.get(p.id)
|
||||
if (!playerManager) {
|
||||
return
|
||||
}
|
||||
playerManager.tick(dt)
|
||||
}
|
||||
}
|
||||
|
||||
tickGlobal(dt: number) {
|
||||
DataManager.Instance.applyInput({
|
||||
type: InputType.TimePast,
|
||||
dt
|
||||
})
|
||||
}
|
||||
|
||||
render() {
|
||||
this.renderPlayer()
|
||||
this.renderBullet()
|
||||
}
|
||||
|
||||
renderPlayer() {
|
||||
for (const p of DataManager.Instance.state.players) {
|
||||
let playerManager = DataManager.Instance.playerMap.get(p.id)
|
||||
if (!playerManager) {
|
||||
const playerPrefab = DataManager.Instance.prefabMap.get(p.type)
|
||||
const player = instantiate(playerPrefab)
|
||||
player.setParent(this.stage)
|
||||
playerManager = player.addComponent(PlayerManager)
|
||||
DataManager.Instance.playerMap.set(p.id, playerManager)
|
||||
playerManager.init(p)
|
||||
} else {
|
||||
playerManager.render(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
renderBullet() {
|
||||
for (const b of DataManager.Instance.state.bullets) {
|
||||
let bulletManager = DataManager.Instance.bulletMap.get(b.id)
|
||||
if (!bulletManager) {
|
||||
const bullet = ObjectPoolManager.Instance.get(b.type)
|
||||
bulletManager = bullet.getComponent(BulletManager) || bullet.addComponent(BulletManager)
|
||||
DataManager.Instance.bulletMap.set(b.id, bulletManager)
|
||||
bulletManager.init(b)
|
||||
} else {
|
||||
bulletManager.render(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/BattleManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/BattleManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "d8c46424-e6ab-4d22-af07-293ce98d8b5b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
122
apps/client/assets/Scripts/Scene/HallManager.ts
Normal file
122
apps/client/assets/Scripts/Scene/HallManager.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import { _decorator, Component, Node, Prefab, director, instantiate } from 'cc';
|
||||
import { EventEnum, SceneEnum } from '../Enum';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import EventManager from '../Global/EventManager';
|
||||
import NetworkManager, { ApiMsgEnum, IData } from '../Global/NetworkManager';
|
||||
import { PlayerItemManager } from '../UI/PlayerItemManager';
|
||||
import { RoomItemManager } from '../UI/RoomItemManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('HallManager')
|
||||
export class HallManager extends Component {
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
playerItem: Prefab = null;
|
||||
|
||||
@property(Node)
|
||||
roomContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
roomItem: Prefab = null;
|
||||
|
||||
onLoad() {
|
||||
director.preloadScene(SceneEnum.Room);
|
||||
EventManager.Instance.on(EventEnum.RoomJoin, this.joinRoom, this)
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
EventManager.Instance.off(EventEnum.RoomJoin, this.joinRoom, this)
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgPlayerList, this.renderPlayer);
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoomList, this.renderRooms);
|
||||
}
|
||||
|
||||
start() {
|
||||
this.getPlayers()
|
||||
this.getRooms()
|
||||
}
|
||||
|
||||
async getPlayers() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerList, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
|
||||
this.renderPlayer(res)
|
||||
}
|
||||
|
||||
renderPlayer = ({ list }: IData) => {
|
||||
for (const item of this.playerContainer.children) {
|
||||
item.active = false
|
||||
}
|
||||
while (this.playerContainer.children.length < list.length) {
|
||||
const playerItem = instantiate(this.playerItem);
|
||||
playerItem.active = false
|
||||
playerItem.setParent(this.playerContainer)
|
||||
}
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const data = list[i];
|
||||
const node = this.playerContainer.children[i]
|
||||
const playerItemManager = node.getComponent(PlayerItemManager)
|
||||
playerItemManager.init(data)
|
||||
}
|
||||
}
|
||||
|
||||
async getRooms() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomList, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
|
||||
this.renderRooms(res)
|
||||
}
|
||||
|
||||
renderRooms = ({ list }: IData) => {
|
||||
for (const item of this.roomContainer.children) {
|
||||
item.active = false
|
||||
}
|
||||
while (this.roomContainer.children.length < list.length) {
|
||||
const roomItem = instantiate(this.roomItem);
|
||||
roomItem.active = false
|
||||
roomItem.setParent(this.roomContainer)
|
||||
}
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const data = list[i];
|
||||
const node = this.roomContainer.children[i]
|
||||
const roomItemManager = node.getComponent(RoomItemManager)
|
||||
roomItemManager.init(data)
|
||||
}
|
||||
}
|
||||
|
||||
async createRoom() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomCreate, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
|
||||
DataManager.Instance.roomInfo = { ...res }
|
||||
|
||||
director.loadScene(SceneEnum.Room);
|
||||
}
|
||||
|
||||
async joinRoom(rid: number) {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomJoin, { rid });
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
|
||||
DataManager.Instance.roomInfo = { ...res }
|
||||
|
||||
director.loadScene(SceneEnum.Room);
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/HallManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/HallManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "a14b4d33-c5f8-455c-a935-dbfd7f3ab62b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
45
apps/client/assets/Scripts/Scene/LoginManager.ts
Normal file
45
apps/client/assets/Scripts/Scene/LoginManager.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { _decorator, Component, Node, EditBox, Button, director } from 'cc';
|
||||
import { SceneEnum } from '../Enum';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import NetworkManager, { ApiMsgEnum } from '../Global/NetworkManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('LoginManager')
|
||||
export class LoginManager extends Component {
|
||||
input: EditBox
|
||||
|
||||
onLoad() {
|
||||
this.input = this.node.getChildByName('Input').getComponent(EditBox)
|
||||
director.preloadScene(SceneEnum.Hall);
|
||||
}
|
||||
|
||||
async start() {
|
||||
await NetworkManager.Instance.connect();
|
||||
console.log("服务连接成功!");
|
||||
|
||||
}
|
||||
|
||||
async handleClick() {
|
||||
if (!NetworkManager.Instance.isConnected) {
|
||||
console.log("未连接!");
|
||||
await NetworkManager.Instance.connect();
|
||||
}
|
||||
const nickname = this.input.string;
|
||||
if (!nickname) {
|
||||
console.log("请输入昵称!")
|
||||
return;
|
||||
}
|
||||
let { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerJoin, {
|
||||
nickname,
|
||||
});
|
||||
|
||||
if (!success) {
|
||||
console.log(error);
|
||||
return;
|
||||
}
|
||||
|
||||
DataManager.Instance.myPlayerId = res.id;
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/LoginManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/LoginManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3be66806-c736-419c-b6cc-0dab1e13cbda",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
64
apps/client/assets/Scripts/Scene/RoomManager.ts
Normal file
64
apps/client/assets/Scripts/Scene/RoomManager.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { _decorator, Component, Node, Prefab, director, instantiate } from 'cc';
|
||||
import { EventEnum, SceneEnum } from '../Enum';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import EventManager from '../Global/EventManager';
|
||||
import NetworkManager, { ApiMsgEnum, IData } from '../Global/NetworkManager';
|
||||
import { PlayerItemManager } from '../UI/PlayerItemManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('RoomManager')
|
||||
export class RoomManager extends Component {
|
||||
@property(Node)
|
||||
playerContainer: Node = null;
|
||||
|
||||
@property(Prefab)
|
||||
playerItem: Prefab = null;
|
||||
|
||||
onLoad() {
|
||||
director.preloadScene(SceneEnum.Battle);
|
||||
NetworkManager.Instance.listenMsg(ApiMsgEnum.MsgRoom, this.renderPlayer);
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
NetworkManager.Instance.unlistenMsg(ApiMsgEnum.MsgRoom, this.renderPlayer);
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.renderPlayer(DataManager.Instance.roomInfo)
|
||||
}
|
||||
|
||||
renderPlayer = ({ players: list }: any) => {
|
||||
for (const item of this.playerContainer.children) {
|
||||
item.active = false
|
||||
}
|
||||
while (this.playerContainer.children.length < list.length) {
|
||||
const playerItem = instantiate(this.playerItem);
|
||||
playerItem.active = false
|
||||
playerItem.setParent(this.playerContainer)
|
||||
}
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const data = list[i];
|
||||
const node = this.playerContainer.children[i]
|
||||
const playerItemManager = node.getComponent(PlayerItemManager)
|
||||
playerItemManager.init(data)
|
||||
}
|
||||
}
|
||||
|
||||
async handleLeave() {
|
||||
const { success, res, error } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiRoomLeave, {});
|
||||
if (!success) {
|
||||
console.log(error)
|
||||
return;
|
||||
}
|
||||
|
||||
DataManager.Instance.roomInfo = null
|
||||
director.loadScene(SceneEnum.Hall);
|
||||
}
|
||||
|
||||
handleGameStart() {
|
||||
console.log("handleGameStart");
|
||||
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/Scene/RoomManager.ts.meta
Normal file
9
apps/client/assets/Scripts/Scene/RoomManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8ee1bd73-5b3b-48ad-a0a0-5a1d6a08da7d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/UI.meta
Normal file
12
apps/client/assets/Scripts/UI.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "03ce230a-7d62-422c-b453-aafdbec11e91",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
57
apps/client/assets/Scripts/UI/JoyStickManager.ts
Normal file
57
apps/client/assets/Scripts/UI/JoyStickManager.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { _decorator, Component, Node, input, Input, EventTouch, Vec2, Vec3, UITransform } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('JoyStickManager')
|
||||
export class JoyStickManager extends Component {
|
||||
input: Vec2 = Vec2.ZERO
|
||||
|
||||
private body: Node
|
||||
private stick: Node
|
||||
private touchStartPos: Vec2
|
||||
private defaultPos: Vec2
|
||||
private radius: number = 0
|
||||
|
||||
init() {
|
||||
this.body = this.node.getChildByName("Body")
|
||||
this.stick = this.body.getChildByName("Stick")
|
||||
const { x, y } = this.body.position
|
||||
this.defaultPos = new Vec2(x, y)
|
||||
this.radius = this.body.getComponent(UITransform).contentSize.x / 2
|
||||
|
||||
input.on(Input.EventType.TOUCH_START, this.onTouchMove, this);
|
||||
input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
||||
input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
|
||||
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
input.off(Input.EventType.TOUCH_START, this.onTouchMove, this);
|
||||
input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
||||
input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
|
||||
}
|
||||
|
||||
onTouchMove(e: EventTouch) {
|
||||
const touchPos = e.touch.getUILocation()
|
||||
if (!this.touchStartPos) {
|
||||
this.touchStartPos = touchPos.clone()
|
||||
this.body.setPosition(this.touchStartPos.x, this.touchStartPos.y);
|
||||
}
|
||||
|
||||
const stickPos = new Vec2(touchPos.x - this.touchStartPos.x, touchPos.y - this.touchStartPos.y)
|
||||
const len = stickPos.length()
|
||||
if (len > this.radius) {
|
||||
stickPos.multiplyScalar(this.radius / len)
|
||||
}
|
||||
|
||||
this.stick.setPosition(stickPos.x, stickPos.y)
|
||||
this.input = stickPos.clone().normalize()
|
||||
}
|
||||
|
||||
onTouchEnd() {
|
||||
this.body.setPosition(this.defaultPos.x, this.defaultPos.y)
|
||||
this.stick.setPosition(0, 0)
|
||||
this.touchStartPos = undefined
|
||||
this.input = Vec2.ZERO
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/UI/JoyStickManager.ts.meta
Normal file
9
apps/client/assets/Scripts/UI/JoyStickManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "bfc9a6e3-7ecf-4422-af5c-670bff4ec928",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
22
apps/client/assets/Scripts/UI/PlayerItemManager.ts
Normal file
22
apps/client/assets/Scripts/UI/PlayerItemManager.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { _decorator, Component, Node, Label } from 'cc';
|
||||
import { EventEnum } from '../Enum';
|
||||
import DataManager from '../Global/DataManager';
|
||||
import EventManager from '../Global/EventManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('PlayerItemManager')
|
||||
export class PlayerItemManager extends Component {
|
||||
init({ id, nickname, rid }: { id: number, nickname: string, rid: number }) {
|
||||
const label = this.getComponent(Label)
|
||||
let str = nickname
|
||||
if (DataManager.Instance.myPlayerId === id) {
|
||||
str += `(我)`
|
||||
}
|
||||
if (rid !== -1) {
|
||||
str += `(房间${rid})`
|
||||
}
|
||||
label.string = str
|
||||
this.node.active = true
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/UI/PlayerItemManager.ts.meta
Normal file
9
apps/client/assets/Scripts/UI/PlayerItemManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0b36580d-5fc7-40c1-9ac0-0fb78a685fd1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
20
apps/client/assets/Scripts/UI/RoomItemManager.ts
Normal file
20
apps/client/assets/Scripts/UI/RoomItemManager.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { _decorator, Component, Node, Label } from 'cc';
|
||||
import { EventEnum } from '../Enum';
|
||||
import EventManager from '../Global/EventManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('RoomItemManager')
|
||||
export class RoomItemManager extends Component {
|
||||
id: number
|
||||
init({ id, players }: { id: number, players: Array<{ id: number, nickname: string }> }) {
|
||||
this.id = id
|
||||
const label = this.getComponent(Label)
|
||||
label.string = `房间id:${id},当前人数:${players.length}`
|
||||
this.node.active = true
|
||||
}
|
||||
|
||||
handleClick() {
|
||||
EventManager.Instance.emit(EventEnum.RoomJoin, this.id)
|
||||
}
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/UI/RoomItemManager.ts.meta
Normal file
9
apps/client/assets/Scripts/UI/RoomItemManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "26da1a71-eb0d-463d-b2d6-fbc64bf0ab89",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
13
apps/client/assets/Scripts/UI/ShootManager.ts
Normal file
13
apps/client/assets/Scripts/UI/ShootManager.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
import { EventEnum } from '../Enum';
|
||||
import EventManager from '../Global/EventManager';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('ShootManager')
|
||||
export class ShootManager extends Component {
|
||||
shoot() {
|
||||
EventManager.Instance.emit(EventEnum.WeaponShoot)
|
||||
}
|
||||
|
||||
}
|
||||
|
9
apps/client/assets/Scripts/UI/ShootManager.ts.meta
Normal file
9
apps/client/assets/Scripts/UI/ShootManager.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "f4c4bbdf-77d3-44a3-88c2-29f40401dab7",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
12
apps/client/assets/Scripts/Utils.meta
Normal file
12
apps/client/assets/Scripts/Utils.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "f395bd07-24d1-424e-bf3c-3276de3d9d22",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
11
apps/client/assets/Scripts/Utils/index.ts
Normal file
11
apps/client/assets/Scripts/Utils/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { SpriteFrame } from "cc"
|
||||
|
||||
const INDEX_REG = /\((\d+)\)/
|
||||
|
||||
const getNumberWithinString = (str: string) => parseInt(str.match(INDEX_REG)?.[1] || '0')
|
||||
|
||||
export const sortSpriteFrame = (spriteFrame: Array<SpriteFrame>) =>
|
||||
spriteFrame.sort((a, b) => getNumberWithinString(a.name) - getNumberWithinString(b.name))
|
||||
|
||||
export const rad2Angle = (rad: number) => rad / Math.PI * 180
|
||||
|
9
apps/client/assets/Scripts/Utils/index.ts.meta
Normal file
9
apps/client/assets/Scripts/Utils/index.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2e280f2e-52a3-465a-acf5-fb412ef51d6b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
Reference in New Issue
Block a user