GameLocalData.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { UserMgr } from "../../module_basic/scripts/UserMgr";
  2. import {
  3. IFood,
  4. IGameData,
  5. IGamePlayer,
  6. } from "../../module_basic/shared/protocols/public/game/GameTypeDef";
  7. import { IRoomData } from "../../module_basic/shared/types/RoomData";
  8. import { GameLocalScene } from "./GameSceneLocal";
  9. export class SinglePlayerData implements IGamePlayer {
  10. uid: string;
  11. color: number;
  12. playerId: number;
  13. roleName: string;
  14. location: string;
  15. transform: number[];
  16. reviveTime: number;
  17. protectedTime: number;
  18. weight: number;
  19. radius: number;
  20. speed: number;
  21. teamId: number;
  22. state: "" | "moving";
  23. }
  24. export class GameLocalData {
  25. createLocalGameData(p: IGamePlayer): IGameData {
  26. let gameData: IGameData = {
  27. mapWidth: GameLocalScene.MAP_WIDTH,
  28. mapHeight: GameLocalScene.MAP_HEIGHT,
  29. defaultPlayerCellRadius: 10,
  30. gameState: "playing",
  31. gameStateRemainingTime: 300,
  32. players: [p],
  33. foodList: {},
  34. teamsWeights: {},
  35. };
  36. return gameData;
  37. }
  38. mockIGamePlayer(): IGamePlayer {
  39. // UserMgr.inst.uid = "local-me-uid-xxx-001";
  40. let a: SinglePlayerData = new SinglePlayerData();
  41. a.uid = UserMgr.inst.uid;
  42. a.color = 3;
  43. a.playerId = 1;
  44. a.roleName = "Local";
  45. a.transform = [550, 500, 0, 0];
  46. a.reviveTime = 0;
  47. a.protectedTime = 0;
  48. a.weight = 30;
  49. a.radius = 200;
  50. a.speed = 500;
  51. a.teamId = 1;
  52. a.state = "moving";
  53. return a;
  54. }
  55. private _autoFoodId = 1;
  56. newFoodData(): IFood {
  57. return {
  58. id: this._autoFoodId++,
  59. x:
  60. Math.random() * GameLocalScene.MAP_WIDTH - GameLocalScene.MAP_WIDTH / 2,
  61. y:
  62. Math.random() * GameLocalScene.MAP_HEIGHT -
  63. GameLocalScene.MAP_HEIGHT / 2,
  64. type: Math.floor(Math.random() * 4),
  65. color: this.getRandomColor(),
  66. radius: Math.random() * 40 + 10,
  67. };
  68. }
  69. private static _ins: GameLocalData = null;
  70. public static get ins(): GameLocalData {
  71. if (!this._ins) {
  72. this._ins = new GameLocalData();
  73. }
  74. return this._ins;
  75. }
  76. private getRandomColor(): number {
  77. // 方法1:返回随机数字
  78. return Math.floor(Math.random() * 0xffffff);
  79. // 方法2:从预定义颜色中选择
  80. const colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff, 0x00ffff];
  81. return colors[Math.floor(Math.random() * colors.length)];
  82. }
  83. createLocalRoomData(uid: string): IRoomData {
  84. let a = {
  85. id: "local-room-id-xxx-001",
  86. displayId: "local-room-id-xxx-001",
  87. gameType: "local",
  88. name: "Local",
  89. maxUser: 10,
  90. userList: [
  91. {
  92. uid: uid,
  93. name: "Local",
  94. visualId: 1,
  95. gender: 1,
  96. introduction: "Local",
  97. isOnline: true,
  98. ready: true,
  99. },
  100. ],
  101. maxPlayerNum: 2,
  102. playerNum: 1,
  103. isPlaying: true,
  104. messages: [],
  105. };
  106. return a;
  107. }
  108. }