2022-12-06 23:10:03 +08:00
|
|
|
import { Node, Prefab, SpriteFrame, clamp } from 'cc'
|
2022-11-27 23:23:47 +08:00
|
|
|
import Singleton from '../Base/Singleton'
|
2022-12-04 22:10:30 +08:00
|
|
|
import { EntityTypeEnum, IBullet, IClientInput, InputTypeEnum, IRoom, IState, toFixed } from '../Common'
|
2022-12-03 20:06:57 +08:00
|
|
|
import { ActorManager } from '../Entity/Actor/ActorManager'
|
2022-11-27 23:23:47 +08:00
|
|
|
import { BulletManager } from '../Entity/Bullet/BulletManager'
|
2022-12-03 20:06:57 +08:00
|
|
|
import { EventEnum } from '../Enum'
|
2022-11-27 23:23:47 +08:00
|
|
|
import { JoyStickManager } from '../UI/JoyStickManager'
|
|
|
|
import EventManager from './EventManager'
|
|
|
|
|
|
|
|
|
|
|
|
const PLAYER_SPEED = 100
|
|
|
|
const BULLET_SPEED = 600
|
|
|
|
|
|
|
|
const WEAPON_DAMAGE = 5
|
|
|
|
|
|
|
|
const PLAYER_RADIUS = 50
|
|
|
|
const BULLET_RADIUS = 10
|
|
|
|
|
2022-12-07 22:24:46 +08:00
|
|
|
const mapW = 960
|
|
|
|
const mapH = 640
|
|
|
|
|
2022-11-27 23:23:47 +08:00
|
|
|
export default class DataManager extends Singleton {
|
|
|
|
static get Instance() {
|
|
|
|
return super.GetInstance<DataManager>()
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:24:46 +08:00
|
|
|
//登陆数据
|
|
|
|
myPlayerId = 1
|
|
|
|
|
|
|
|
//大厅数据
|
|
|
|
roomInfo: IRoom
|
|
|
|
|
|
|
|
//游戏数据
|
2022-12-01 22:26:41 +08:00
|
|
|
stage: Node
|
2022-11-27 23:23:47 +08:00
|
|
|
jm: JoyStickManager
|
|
|
|
|
|
|
|
prefabMap: Map<string, Prefab> = new Map()
|
|
|
|
textureMap: Map<string, SpriteFrame[]> = new Map()
|
2022-12-03 20:06:57 +08:00
|
|
|
actorMap: Map<number, ActorManager> = new Map()
|
2022-11-27 23:23:47 +08:00
|
|
|
bulletMap: Map<number, BulletManager> = new Map()
|
|
|
|
|
2022-12-07 22:24:46 +08:00
|
|
|
reset() {
|
|
|
|
this.stage = null
|
|
|
|
this.jm = null
|
|
|
|
this.actorMap.clear()
|
|
|
|
this.bulletMap.clear()
|
|
|
|
this.prefabMap.clear()
|
|
|
|
this.textureMap.clear()
|
2022-12-01 22:26:41 +08:00
|
|
|
}
|
|
|
|
|
2022-11-27 23:23:47 +08:00
|
|
|
state: IState = {
|
|
|
|
players: [{
|
|
|
|
id: 2,
|
|
|
|
nickname: "哈哈1",
|
|
|
|
position: {
|
|
|
|
x: -200,
|
|
|
|
y: -200
|
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
x: 1,
|
|
|
|
y: 0
|
|
|
|
},
|
|
|
|
hp: 100,
|
2022-12-03 20:06:57 +08:00
|
|
|
type: EntityTypeEnum.Actor1,
|
2022-11-27 23:23:47 +08:00
|
|
|
weaponType: EntityTypeEnum.Weapon1,
|
|
|
|
bulletType: EntityTypeEnum.Bullet1,
|
|
|
|
}, {
|
|
|
|
id: 1,
|
|
|
|
nickname: "哈哈2",
|
|
|
|
position: {
|
|
|
|
x: 200,
|
|
|
|
y: 200
|
|
|
|
},
|
|
|
|
direction: {
|
|
|
|
x: 0,
|
|
|
|
y: -1
|
|
|
|
},
|
|
|
|
hp: 100,
|
2022-12-03 20:06:57 +08:00
|
|
|
type: EntityTypeEnum.Actor2,
|
2022-11-27 23:23:47 +08:00
|
|
|
weaponType: EntityTypeEnum.Weapon2,
|
|
|
|
bulletType: EntityTypeEnum.Bullet2,
|
|
|
|
}],
|
|
|
|
bullets: [],
|
|
|
|
nextBulletId: 1
|
|
|
|
}
|
|
|
|
|
2022-12-03 20:06:57 +08:00
|
|
|
applyInput(input: IClientInput) {
|
2022-11-27 23:23:47 +08:00
|
|
|
switch (input.type) {
|
2022-12-03 20:06:57 +08:00
|
|
|
case InputTypeEnum.ActorMove: {
|
2022-11-27 23:23:47 +08:00
|
|
|
const { direction: { x, y }, dt, id } = input
|
|
|
|
const player = this.state.players.find(e => e.id === id)
|
|
|
|
if (!player) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-04 22:10:30 +08:00
|
|
|
player.position.x += toFixed(x * PLAYER_SPEED * dt)
|
|
|
|
player.position.y += toFixed(y * PLAYER_SPEED * dt)
|
2022-12-06 23:10:03 +08:00
|
|
|
|
2022-12-07 22:24:46 +08:00
|
|
|
player.position.x = clamp(player.position.x, -mapW / 2, mapW / 2)
|
|
|
|
player.position.y = clamp(player.position.y, -mapH / 2, mapH / 2)
|
2022-12-06 23:10:03 +08:00
|
|
|
|
2022-11-27 23:23:47 +08:00
|
|
|
player.direction = { x, y }
|
|
|
|
break
|
|
|
|
}
|
2022-12-03 20:06:57 +08:00
|
|
|
case InputTypeEnum.WeaponShoot: {
|
2022-11-27 23:23:47 +08:00
|
|
|
const { owner, position, direction } = input
|
|
|
|
const bullet: IBullet = {
|
|
|
|
id: this.state.nextBulletId++,
|
|
|
|
owner,
|
|
|
|
position,
|
|
|
|
direction,
|
2022-12-03 20:06:57 +08:00
|
|
|
type: this.actorMap.get(owner).bulletType
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
this.state.bullets.push(bullet)
|
|
|
|
|
2022-12-01 22:26:41 +08:00
|
|
|
EventManager.Instance.emit(EventEnum.BulletBorn, owner)
|
2022-11-27 23:23:47 +08:00
|
|
|
break
|
|
|
|
}
|
2022-12-03 20:06:57 +08:00
|
|
|
case InputTypeEnum.TimePast: {
|
2022-11-27 23:23:47 +08:00
|
|
|
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) {
|
2022-12-01 22:26:41 +08:00
|
|
|
EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
|
2022-12-04 22:10:30 +08:00
|
|
|
x: toFixed((player.position.x + bullet.position.x) / 2),
|
|
|
|
y: toFixed((player.position.y + bullet.position.y) / 2),
|
2022-11-27 23:23:47 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
player.hp -= WEAPON_DAMAGE
|
|
|
|
bullets.splice(i, 1)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-12-07 22:24:46 +08:00
|
|
|
if (Math.abs(bullet.position.x) > mapW / 2 || Math.abs(bullet.position.y) > mapH / 2) {
|
2022-12-01 22:26:41 +08:00
|
|
|
EventManager.Instance.emit(EventEnum.ExplosionBorn, bullet.id, {
|
|
|
|
x: bullet.position.x,
|
|
|
|
y: bullet.position.y,
|
|
|
|
})
|
|
|
|
bullets.splice(i, 1)
|
|
|
|
}
|
|
|
|
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const bullet of this.state.bullets) {
|
2022-12-04 22:10:30 +08:00
|
|
|
bullet.position.x += toFixed(bullet.direction.x * BULLET_SPEED * dt)
|
|
|
|
bullet.position.y += toFixed(bullet.direction.y * BULLET_SPEED * dt)
|
2022-11-27 23:23:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|