RoosterMoveCar.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. import { _decorator, Component, ERaycast2DType, find, Label, Node, NodeEventType, PhysicsSystem2D, Tween, tween, v2, v3, Vec2, Vec3 } from 'cc';
  2. import { EventDispatcher } from '../core_tgx/easy_ui_framework/EventDispatcher';
  3. import { CarColorsGlobalInstance } from './Script/CarColorsGlobalInstance';
  4. import { CarDir } from './Script/CarColorsGlobalTypes';
  5. import { CarCarColorsComponent } from './Script/Components/CarCarColorsComponent';
  6. import { GameEvent } from './Script/Enum/GameEvent';
  7. import { LevelManager } from './Script/Manager/LevelMgr';
  8. import { CarCarColorsSysterm } from './Script/Systems/CarCarColorsSysterm';
  9. import { GameUtil } from './Script/GameUtil';
  10. import { LevelAction } from './Script/LevelAction';
  11. import { GlobalConfig } from '../start/Config/GlobalConfig';
  12. import { AdvertMgr } from '../core_tgx/base/ad/AdvertMgr';
  13. import { Barricade } from './Script/Components/Barricade';
  14. import { CarUnscrewAudioMgr } from './Script/Manager/CarUnscrewAudioMgr';
  15. import { tgxUIMgr } from '../core_tgx/tgx';
  16. import { UI_Setting } from '../scripts/UIDef';
  17. const { ccclass, property } = _decorator;
  18. const duration = 0.3;
  19. @ccclass('RoosterMoveCar')
  20. export class RoosterMoveCar extends Component {
  21. onLoad() {
  22. CarUnscrewAudioMgr.initilize();
  23. CarUnscrewAudioMgr.play(CarUnscrewAudioMgr.getMusicIdName(2), 1.0);
  24. LevelManager.instance.initilizeModel();
  25. CarColorsGlobalInstance.instance.levels = find('Canvas/Scene/Levels')!;
  26. CarColorsGlobalInstance.instance.carSysterm = this.node.getComponent(CarCarColorsSysterm)!;
  27. CarColorsGlobalInstance.instance.loadPinPrefab();
  28. this.registerListener();
  29. }
  30. protected start(): void {
  31. this.startGame();
  32. }
  33. async startGame() {
  34. await LevelManager.instance.gameStart();
  35. }
  36. registerListener() {
  37. EventDispatcher.instance.on(GameEvent.EVENT_CLICK_CAR, this.onTouchCar, this);
  38. EventDispatcher.instance.on(GameEvent.EVENT_BATTLE_SUCCESS_LEVEL_UP, this.onUpdateLvl, this);
  39. EventDispatcher.instance.on(GameEvent.EVENT_UPDATE_LEFT_NAIL, this.onUpdateLeftNail, this);
  40. EventDispatcher.instance.on(GameEvent.EVENT_CHECK_ELEMENT_CHILDREN, this.onUpdateLeftNail, this);
  41. //UI监听
  42. const btnLock4 = find('Canvas/Scene/Parkings/empty-lock4')!;
  43. const btnLock5 = find('Canvas/Scene/Parkings/empty-lock5')!;
  44. const btnLock6 = find('Canvas/Scene/Parkings/empty-lock6')!;
  45. const btnRefresh = find('Canvas/GameUI/TopLeft/BtnRefresh')!;
  46. const btnSet = find('Canvas/GameUI/TopLeft/BtnSet')!;
  47. btnLock4.on(NodeEventType.TOUCH_END, () => this.onClickHandler(btnLock4), this);
  48. btnLock5.on(NodeEventType.TOUCH_END, () => this.onClickHandler(btnLock5), this);
  49. btnLock6.on(NodeEventType.TOUCH_END, () => this.onClickHandler(btnLock6), this);
  50. btnRefresh.on(NodeEventType.TOUCH_END, () => this.onClickRefresh(), this);
  51. btnSet.on(NodeEventType.TOUCH_END, () => this.onClickSet(), this);
  52. }
  53. private onClickHandler(bt: Node): void {
  54. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(3), 1.0);
  55. const setBtEmpty = () => {
  56. bt.name = 'empty';
  57. bt.getComponentInChildren(Barricade)!.tick();
  58. }
  59. if (!GlobalConfig.isDebug) {
  60. AdvertMgr.instance.showReawardVideo(() => {
  61. setBtEmpty();
  62. })
  63. } else {
  64. setBtEmpty();
  65. }
  66. }
  67. private onClickRefresh(): void {
  68. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(3), 1.0);
  69. Tween.stopAll();
  70. LevelManager.instance.clearLevelData();
  71. CarColorsGlobalInstance.instance.carSysterm.clearAll();
  72. const { level } = LevelManager.instance.levelModel;
  73. LevelManager.instance.loadLevel(level);
  74. this.updateScrewsLen();
  75. }
  76. private onClickSet(): void {
  77. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(3), 1.0);
  78. const show = tgxUIMgr.inst.isShowing(UI_Setting);
  79. if (!show) {
  80. tgxUIMgr.inst.showUI(UI_Setting);
  81. }
  82. }
  83. onTouchCar(touchCar: Node) {
  84. let car = touchCar;
  85. let carComp = car.getComponent(CarCarColorsComponent);
  86. if (!carComp) return;
  87. const carWorldPos = car.getWorldPosition().clone();
  88. const objs = GameUtil.getWorldPositionAsVec2(car);
  89. const obje = GameUtil.calculateRayEnd(car, 1000);
  90. // const obje = this.createRaycastPosByDir(objs, car, carComp.carDir);
  91. // console.log('射线起点:', objs);
  92. // console.log('射线终点:', obje);
  93. // 射线检测
  94. let results = PhysicsSystem2D.instance.raycast(objs, obje, ERaycast2DType.Closest);
  95. if (results.length > 0) {
  96. const closestResult = results[0];
  97. const collider = results[0].collider;
  98. // console.log('碰撞器名称:', collider.name);
  99. // console.log('碰撞点世界坐标:', results[0].point);
  100. //碰到车
  101. if (collider.group == 1 << 1) {
  102. const halfLen = carComp.halfLen;
  103. let forward = null!;
  104. if (carComp.carDir == CarDir.TOP || carComp.carDir == CarDir.BOTTOM) {
  105. forward = new Vec3(0, 1, 0);
  106. } else {
  107. forward = new Vec3(1, 0, 0);
  108. }
  109. const carMovePos = carWorldPos.clone().add(forward.clone().multiplyScalar(closestResult.fraction - 5 * halfLen));
  110. tween(car).to(0.1, {
  111. worldPosition: carMovePos
  112. })
  113. .to(0.1, {
  114. worldPosition: carWorldPos
  115. })
  116. .call(() => {
  117. })
  118. .start()
  119. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(5), 1.0);
  120. }
  121. else {
  122. //碰到路
  123. const parkPoint = this.getEmptyParkPoint()
  124. if (parkPoint === null) {
  125. console.log("没有车位了")
  126. return
  127. }
  128. const targetWorldPos = results[0].point;
  129. // 顶部导航
  130. let tweenCar: Tween<Node> = tween(car)
  131. if (collider.node.name === "PhysicRoodTop") {
  132. // this.hitPointTween(car, parkPoint, tweenCar, collider.node)
  133. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node, parkPoint)
  134. this.topRoadTween(car, parkPoint, tweenCar)
  135. } else if (collider.node.name === "PhysicRoodLeft") {
  136. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/LeftPoint")
  137. // this.hitPointTween(car, targetPoint, tweenCar, collider.node);
  138. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  139. this.leftRoadTween(car, tweenCar)
  140. this.leftTopToRight(car, parkPoint, tweenCar)
  141. this.topRoadTween(car, parkPoint, tweenCar)
  142. } else if (collider.node.name === "PhysicRoodRight") {
  143. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/RightPoint")
  144. // this.hitPointTween(car, targetPoint, tweenCar, collider.node)
  145. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  146. this.rightRoadTween(car, tweenCar)
  147. this.rightTopToleft(car, parkPoint, tweenCar)
  148. this.topRoadTween(car, parkPoint, tweenCar)
  149. } else if (collider.node.name === "PhysicRoodBottom") {
  150. const hitPoint = find("Canvas/Scene/Grounds/PhysicRoodBottom")!;
  151. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  152. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  153. const toLeft = leftPoint.getWorldPosition().subtract(carWorldPos).normalize();
  154. const toRight = rightPoint.getWorldPosition().subtract(carWorldPos).normalize();
  155. if (Math.abs(toLeft.x) < Math.abs(toRight.x)) {
  156. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodBottom/LeftPoint")
  157. // this.hitPointTween(car, targetPoint, tweenCar, collider.node)
  158. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  159. this.bottomRoadTween(car, targetPoint, tweenCar)
  160. this.leftRoadTween(car, tweenCar)
  161. this.leftTopToRight(car, parkPoint, tweenCar)
  162. } else {
  163. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodBottom/RightPoint")
  164. // this.hitPointTween(car, targetPoint, tweenCar, collider.node)
  165. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  166. this.bottomRoadTween(car, targetPoint, tweenCar)
  167. this.rightRoadTween(car, tweenCar)
  168. this.rightTopToleft(car, parkPoint, tweenCar)
  169. }
  170. this.topRoadTween(car, parkPoint, tweenCar)
  171. }
  172. parkPoint.name = "inuse"
  173. tweenCar.call(() => {
  174. const exhasut = car.getChildByName('Exhaust')!;
  175. if (exhasut) {
  176. exhasut.active = false;
  177. }
  178. car.setParent(parkPoint, true);
  179. })
  180. .start()
  181. }
  182. } else {
  183. console.log('未检测到碰撞物体');
  184. }
  185. }
  186. onUpdateLeftNail() {
  187. this.updateScrewsLen();
  188. this.onUpdateLvl();
  189. }
  190. updateScrewsLen() {
  191. const levels = find('Canvas/Scene/Levels')!;
  192. const lbScrews = find('Canvas/GameUI/Pai/LbScrews')!.getComponent(Label);
  193. const children = levels.children;
  194. if (children.length <= 0)
  195. return;
  196. const levelComp = children[0].getComponent(LevelAction)!;
  197. const pins = levelComp.get_pin_color();
  198. console.log(`钉子数量:${pins.length}`)
  199. lbScrews.string = `${pins.length}`;
  200. }
  201. onUpdateLvl() {
  202. const lbScrews = find('Canvas/GameUI/TopLeft/TitleLvl/LbLvl')!.getComponent(Label);
  203. const { level } = LevelManager.instance.levelModel;
  204. lbScrews.string = `${level}`;
  205. }
  206. /** 获取停车位坐标*/
  207. getEmptyParkPoint(): Node {
  208. const points = find("Canvas/Scene/Parkings").children
  209. for (let i = 0; i < points.length; i++) {
  210. if (points[i].name === "empty") {
  211. return points[i]
  212. }
  213. }
  214. return null
  215. }
  216. /** 导航到碰撞点
  217. * @param car 车节点
  218. *@param targetWorldPos 碰撞点的世界坐标
  219. *@param tweenCar 车的tween动画
  220. *@param hitPoint street碰撞点
  221. */
  222. hitPointTween2(car: Node, targetWorldPos: Vec2, tweenCar: Tween<Node>, hitPoint: Node = null, parkPoint?: Node) {
  223. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(4), 1.0);
  224. const exhasut = car.getChildByName('Exhaust')!;
  225. if (exhasut) {
  226. exhasut.active = true;
  227. }
  228. const targetV3 = new Vec3(targetWorldPos.x, targetWorldPos.y, 0);
  229. tweenCar
  230. .to(duration, { worldPosition: targetV3 }, { easing: 'quadInOut' })
  231. .call(() => {
  232. const carWorldPos = car.getWorldPosition().clone();
  233. const direction = targetV3.subtract(carWorldPos).normalize();
  234. // console.log('direction:', direction);
  235. let up: Vec3;
  236. if (Math.abs(direction.x) > Math.abs(direction.y)) {
  237. // 左右方向
  238. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  239. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  240. const toLeft = leftPoint.getWorldPosition().subtract(carWorldPos).normalize();
  241. const toRight = rightPoint.getWorldPosition().subtract(carWorldPos).normalize();
  242. if (Math.abs(toLeft.x) < Math.abs(toRight.x)) {
  243. up = new Vec3(0, 0, 1);
  244. } else {
  245. up = new Vec3(0, 0, -1);
  246. }
  247. } else {
  248. // 上下方向
  249. // console.log('hitPoint.name:', hitPoint.name);
  250. if (hitPoint.name == 'PhysicRoodBottom' || hitPoint.name == 'PhysicRoodTop') {
  251. if (hitPoint.name == 'PhysicRoodTop') {
  252. //车找相对停车位的方向
  253. const carWorldPos = car.getWorldPosition().clone();
  254. const parkWorldPos = parkPoint.getWorldPosition().clone();
  255. const direction = carWorldPos.subtract(parkWorldPos).normalize();
  256. if (direction.x > 0) {
  257. up = new Vec3(0, 0, 1);
  258. } else {
  259. up = new Vec3(0, 0, -1);
  260. }
  261. } else {
  262. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  263. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  264. const toLeft = leftPoint.getWorldPosition().subtract(targetV3).normalize();
  265. const toRight = rightPoint.getWorldPosition().subtract(targetV3).normalize();
  266. if (Math.abs(toLeft.x) <= Math.abs(toRight.x)) {
  267. up = new Vec3(0, 0, 1);
  268. } else {
  269. up = new Vec3(0, 0, -1);
  270. }
  271. }
  272. } else {
  273. up = direction.y > 0 ? new Vec3(0, -1, 0) : new Vec3(0, 1, 0);
  274. }
  275. }
  276. car.lookAt(targetV3, up);
  277. })
  278. .start();
  279. }
  280. // 顶部导航
  281. topRoadTween(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  282. const targetWorldPos = targetPoint.worldPosition.clone();
  283. tweenCar
  284. .to(duration, {
  285. worldPosition: new Vec3(targetWorldPos.x, targetWorldPos.y - 70, targetWorldPos.z)
  286. })
  287. .delay(0.1)
  288. .to(duration, {
  289. eulerAngles: new Vec3(0, 0, 0)
  290. })
  291. .delay(0.1)
  292. .to(duration, {
  293. worldPosition: targetPoint.worldPosition
  294. })
  295. .call(() => {
  296. car.getComponent(CarCarColorsComponent)!.openCover();
  297. })
  298. }
  299. /** 顶部左边转向右*/
  300. leftTopToRight(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  301. tweenCar.to(0.2, {
  302. eulerAngles: new Vec3(0, 0, -90)
  303. })
  304. .start()
  305. }
  306. // 左边导航
  307. leftRoadTween(car: Node, tweenCar: Tween<Node>) {
  308. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/LeftPoint")
  309. tweenCar.to(duration, {
  310. worldPosition: targetPoint.getWorldPosition()
  311. })
  312. .call(() => {
  313. const carforward = car.forward.clone()
  314. tween(carforward).to(0.1, { x: -1, y: 0, z: 0 }, {
  315. onUpdate: () => {
  316. car.forward = carforward
  317. }
  318. }).start()
  319. })
  320. .delay(0.1)
  321. }
  322. /** 顶部右边转向左*/
  323. rightTopToleft(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  324. tweenCar.to(0.2, {
  325. eulerAngles: new Vec3(0, 0, 90)
  326. })
  327. .start()
  328. }
  329. // 右边导航
  330. rightRoadTween(car: Node, tweenCar: Tween<Node>) {
  331. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/RightPoint")
  332. tweenCar.to(duration, {
  333. worldPosition: targetPoint.getWorldPosition()
  334. })
  335. .call(() => {
  336. const carforward = car.forward.clone()
  337. tween(carforward).to(0.1, { x: 1, y: 0, z: 0 }, {
  338. onUpdate: () => {
  339. car.forward = carforward
  340. }
  341. }).start()
  342. })
  343. .delay(0.1)
  344. }
  345. // 底部导航
  346. bottomRoadTween(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  347. tweenCar.to(duration, {
  348. worldPosition: new Vec3(targetPoint.getWorldPosition().x, targetPoint.getWorldPosition().y + 50, targetPoint.getWorldPosition().z)
  349. })
  350. .call(() => {
  351. const carforward = car.forward.clone()
  352. tween(carforward).to(0.1, { x: 0, y: 0, z: 1 }, {
  353. onUpdate: () => {
  354. car.forward = carforward
  355. }
  356. }).start()
  357. })
  358. .delay(0.1)
  359. }
  360. }