GameSceneLocal.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import { _decorator, Component, Node } from "cc";
  2. import { GameCamera } from "./GameCamera";
  3. import { Material } from "cc";
  4. import { GameEvent, GameMgr } from "../../module_basic/scripts/GameMgr";
  5. import { UITransform } from "cc";
  6. import { size } from "cc";
  7. import { Sprite } from "cc";
  8. import { Vec2 } from "cc";
  9. import { renderer } from "cc";
  10. import {
  11. IFood,
  12. IGameData,
  13. IGamePlayer,
  14. } from "../../module_basic/shared/protocols/public/game/GameTypeDef";
  15. import { instantiate } from "cc";
  16. import { GameResMgr } from "./GameResMgr";
  17. import { Cell } from "./Cell";
  18. import { UserMgr } from "../../module_basic/scripts/UserMgr";
  19. import { PlayerMovement2D } from "./PlayerMovement2D";
  20. import { PlayerController } from "./PlayerController";
  21. import { UIGameJoystick } from "../ui_game_joystick/UIGameJoystick";
  22. import { UIGameHUD } from "../ui_game_hud/UIGameHUD";
  23. import { UIGameRank } from "../ui_game_rank/UIGameRank";
  24. import { RoomMgr } from "../../module_basic/scripts/RoomMgr";
  25. import { IRoomData, IUserData } from "../../module_basic/shared/types/RoomData";
  26. import { UserInfo } from "../../module_basic/shared/types/UserInfo";
  27. import { color } from "cc";
  28. import { GameLocalData } from "./GameLocalData";
  29. import { director } from "cc";
  30. import { SceneUtil } from "../../core_tgx/base/SceneUtils";
  31. import { ModuleDef } from "../../scripts/ModuleDef";
  32. import { SceneDef } from "../../scripts/SceneDef";
  33. import { GameSceneUtil } from "../../module_basic/scripts/GameSceneUtil";
  34. import { UIGameResult } from "../../module_ball/ui_game/UIGameResult";
  35. const { ccclass, property } = _decorator;
  36. @ccclass("GameLocalScene")
  37. export class GameLocalScene extends Component {
  38. @property(Node) mapRoot: Node;
  39. @property(Node) background: Node;
  40. @property(Node) objectsRoot: Node;
  41. @property(GameCamera) gameCamera: GameCamera;
  42. @property(Material) mapMaterial: Material;
  43. public static MAP_WIDTH = 10000;
  44. public static MAP_HEIGHT = 10000;
  45. private _cellMap: { [key: number]: Cell } = {};
  46. private _foodMap: { [key: number]: Node } = {};
  47. private _foodInfoMap: { [key: number]: IFood } = {};
  48. private _foodPool: Node[] = [];
  49. private _backgroundPass: renderer.Pass;
  50. private _handleEdgeFactor: number;
  51. private _selfPlayer: Cell = null;
  52. start() {
  53. let localUser: UserInfo = {
  54. uid: "local-me-uid-xxx-001",
  55. };
  56. UserMgr.inst.setUserInfo(localUser);
  57. let roomData: IRoomData = GameLocalData.ins.createLocalRoomData(
  58. localUser.uid
  59. );
  60. RoomMgr.inst.onNet_RoomDataSyncPush({
  61. data: roomData,
  62. });
  63. tgx.UIMgr.inst.showUI(UIGameJoystick);
  64. tgx.UIMgr.inst.showUI(UIGameHUD);
  65. // tgx.UIMgr.inst.showUI(UIGameRank);
  66. let selfPlayerInfo = GameLocalData.ins.mockIGamePlayer();
  67. let gameData = GameLocalData.ins.createLocalGameData(selfPlayerInfo);
  68. GameMgr.inst.localMode(gameData);
  69. GameMgr.inst.onNet_PlayerComesPush({
  70. player: selfPlayerInfo,
  71. });
  72. this.initBackground();
  73. this.createCell(selfPlayerInfo);
  74. let self = this;
  75. let intIn = setInterval(() => {
  76. if (
  77. !GameMgr.inst.gameData ||
  78. GameMgr.inst.gameData.gameState == "gameover"
  79. ) {
  80. clearInterval(intIn);
  81. return;
  82. }
  83. self.createFoods();
  84. }, 1000);
  85. director.on(GameEvent.GAME_EXIT, this.onGameExit, this);
  86. }
  87. onGameExit() {
  88. tgx.UIMgr.inst.closeAll();
  89. GameMgr.inst.localGameOver();
  90. UIGameResult.show();
  91. }
  92. protected onDestroy(): void {
  93. director.off(GameEvent.GAME_EXIT, this.onGameExit, this);
  94. }
  95. initBackground() {
  96. const extBorder = 4000;
  97. let gameData = GameMgr.inst.gameData;
  98. const realWidth = gameData.mapWidth + extBorder;
  99. const realHeight = gameData.mapHeight + extBorder;
  100. this.background.getComponent(UITransform).contentSize = size(
  101. realWidth,
  102. realHeight
  103. );
  104. //@en initialize the material parameters of the background.
  105. //@zh 初始化地图背景材质
  106. const halfWidth = gameData.mapWidth / 2;
  107. const halfHeight = gameData.mapHeight / 2;
  108. let spriteFrame = this.background.getComponent(Sprite).spriteFrame;
  109. let uvScale = new Vec2();
  110. let scaler = 0.6;
  111. //@en use uv scale to adapt the map size
  112. //@zh 使用uv缩放来适配地图大小
  113. uvScale.x = (realWidth / spriteFrame.width) * scaler;
  114. uvScale.y = (realHeight / spriteFrame.height) * scaler;
  115. this.mapMaterial.setProperty("uvScale", uvScale);
  116. this.mapMaterial.setProperty(
  117. "halfMapSize",
  118. new Vec2(halfWidth, halfHeight)
  119. );
  120. //this.mapMaterial.setProperty('edgeColor', new Vec4(1.0,0.0,0.0,1.0));
  121. //this.mapMaterial.setProperty('fadeOutRange', 1000.0);
  122. //@en save the material parameter handle for efficient setting of material parameters in update
  123. //@zh 保存材质参数相关句柄,方便在update中高效率地设置材质参数
  124. this._backgroundPass = this.mapMaterial.passes[0];
  125. this._handleEdgeFactor = this._backgroundPass.getHandle("edgeFactor");
  126. }
  127. tmpColor = color();
  128. createFoods() {
  129. let createCount = 1000 - Object.keys(this._foodMap).length;
  130. if (createCount <= 0) {
  131. return;
  132. }
  133. if (createCount > 100) {
  134. createCount = 100;
  135. }
  136. for (let i = 0; i < createCount; i++) {
  137. this.createFood(GameLocalData.ins.newFoodData());
  138. }
  139. }
  140. protected update(dt: number): void {
  141. let value = Math.sin(Date.now() / 50 / Math.PI);
  142. value = value * 0.5 + 0.5;
  143. this._backgroundPass.setUniform(this._handleEdgeFactor, value);
  144. this.checkFoodAndEat();
  145. }
  146. async createCell(player: IGamePlayer) {
  147. let newCell = instantiate(GameResMgr.inst.cellPrefab);
  148. this.objectsRoot.addChild(newCell);
  149. newCell.setWorldPosition(
  150. player.transform[0],
  151. player.transform[1],
  152. player.transform[2]
  153. );
  154. let cellComp = newCell.getComponent(Cell);
  155. cellComp.setPlayer(player);
  156. this._cellMap[player.playerId] = cellComp;
  157. if (player.uid == UserMgr.inst.uid) {
  158. this._selfPlayer = cellComp;
  159. let mv2d = newCell.addComponent(PlayerMovement2D);
  160. mv2d.needRotation = false;
  161. mv2d.moveSpeed = player.speed;
  162. mv2d.setPrecision(10);
  163. this.gameCamera.target = cellComp;
  164. newCell.addComponent(PlayerController);
  165. if (player.state == "moving") {
  166. mv2d.onMovement(player.transform[3], 1.0);
  167. }
  168. }
  169. }
  170. async createFood(fd: IFood) {
  171. // console.log("createFood", fd);
  172. let newFood = this._foodPool.pop();
  173. if (!newFood) {
  174. newFood = new Node();
  175. newFood.addComponent(Sprite);
  176. }
  177. this._foodInfoMap[fd.id] = fd;
  178. this.mapRoot.addChild(newFood);
  179. newFood.name = "food_" + fd.id;
  180. this._foodMap[fd.id] = newFood;
  181. let foodSprites = GameResMgr.inst.foodSprites;
  182. let sprf = foodSprites[fd.type % foodSprites.length];
  183. let spr = newFood.getComponent(Sprite);
  184. spr.spriteFrame = sprf;
  185. this.tmpColor.fromHEX(fd.color.toString(16));
  186. spr.color = this.tmpColor;
  187. let scale = (fd.radius * 2) / sprf.rect.width;
  188. newFood.setScale(scale, scale, 1.0);
  189. newFood.setRotationFromEuler(0, 0, Math.random() * 360);
  190. newFood.setWorldPosition(fd.x, fd.y, 0);
  191. }
  192. checkFoodAndEat() {
  193. if (!this._selfPlayer) {
  194. return;
  195. }
  196. for (let foodId in this._foodMap) {
  197. let foodNode = this._foodMap[foodId];
  198. let foodPos = foodNode.getWorldPosition();
  199. let playerPos = this._selfPlayer.node.getWorldPosition();
  200. let distance = Vec2.distance(foodPos, playerPos);
  201. let ddd = this._selfPlayer.player.radius / 1.5;
  202. if (distance < ddd) {
  203. delete this._foodMap[foodId];
  204. foodNode.removeFromParent();
  205. this._foodPool.push(foodNode);
  206. let oneAdd = this._foodInfoMap[foodId].radius / 10;
  207. delete this._foodInfoMap[foodId];
  208. this._selfPlayer.setRadius(this._selfPlayer.player.radius + oneAdd);
  209. }
  210. }
  211. }
  212. }