163 lines
4.4 KiB
TypeScript
Raw Normal View History

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