RoosterMoveCar.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 async onClickRefresh(): Promise<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. await GameUtil.delay(0.2);
  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. const Exhaust1 = car.getChildByName('Exhaust1')!;
  176. if (exhasut) {
  177. exhasut.active = false;
  178. Exhaust1.active = false;
  179. }
  180. car.setParent(parkPoint, true);
  181. })
  182. .start()
  183. }
  184. } else {
  185. console.log('未检测到碰撞物体');
  186. }
  187. }
  188. onUpdateLeftNail() {
  189. this.updateScrewsLen();
  190. this.onUpdateLvl();
  191. }
  192. updateScrewsLen() {
  193. const levels = find('Canvas/Scene/Levels')!;
  194. const lbScrews = find('Canvas/GameUI/Pai/LbScrews')!.getComponent(Label);
  195. const children = levels.children;
  196. if (children.length <= 0)
  197. return;
  198. const levelComp = children[0].getComponent(LevelAction)!;
  199. const pins = levelComp.get_pin_color();
  200. // console.log(`钉子数量:${pins.length}`)
  201. lbScrews.string = `${pins.length}`;
  202. }
  203. onUpdateLvl() {
  204. const lbScrews = find('Canvas/GameUI/TitleLvl/LbLvl')!.getComponent(Label);
  205. const { level } = LevelManager.instance.levelModel;
  206. lbScrews.string = `${level}`;
  207. }
  208. /** 获取停车位坐标*/
  209. getEmptyParkPoint(): Node {
  210. const points = find("Canvas/Scene/Parkings").children
  211. for (let i = 0; i < points.length; i++) {
  212. if (points[i].name === "empty") {
  213. return points[i]
  214. }
  215. }
  216. return null
  217. }
  218. /** 导航到碰撞点
  219. * @param car 车节点
  220. *@param targetWorldPos 碰撞点的世界坐标
  221. *@param tweenCar 车的tween动画
  222. *@param hitPoint street碰撞点
  223. */
  224. hitPointTween2(car: Node, targetWorldPos: Vec2, tweenCar: Tween<Node>, hitPoint: Node = null, parkPoint?: Node) {
  225. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(4), 1.0);
  226. const exhasut = car.getChildByName('Exhaust')!;
  227. const Exhaust1 = car.getChildByName('Exhaust1')!;
  228. if (exhasut) {
  229. exhasut.active = true;
  230. Exhaust1.active = true;
  231. }
  232. const targetV3 = new Vec3(targetWorldPos.x, targetWorldPos.y, 0);
  233. tweenCar
  234. .to(duration, { worldPosition: targetV3 }, { easing: 'quadInOut' })
  235. .call(() => {
  236. const carWorldPos = car.getWorldPosition().clone();
  237. const direction = targetV3.subtract(carWorldPos).normalize();
  238. // console.log('direction:', direction);
  239. let up: Vec3;
  240. if (Math.abs(direction.x) > Math.abs(direction.y)) {
  241. // 左右方向
  242. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  243. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  244. const toLeft = leftPoint.getWorldPosition().subtract(carWorldPos).normalize();
  245. const toRight = rightPoint.getWorldPosition().subtract(carWorldPos).normalize();
  246. if (Math.abs(toLeft.x) < Math.abs(toRight.x)) {
  247. up = new Vec3(0, 0, 1);
  248. } else {
  249. up = new Vec3(0, 0, -1);
  250. }
  251. } else {
  252. // 上下方向
  253. // console.log('hitPoint.name:', hitPoint.name);
  254. if (hitPoint.name == 'PhysicRoodBottom' || hitPoint.name == 'PhysicRoodTop') {
  255. if (hitPoint.name == 'PhysicRoodTop') {
  256. //车找相对停车位的方向
  257. const carWorldPos = car.getWorldPosition().clone();
  258. const parkWorldPos = parkPoint.getWorldPosition().clone();
  259. const direction = carWorldPos.subtract(parkWorldPos).normalize();
  260. if (direction.x > 0) {
  261. up = new Vec3(0, 0, 1);
  262. } else {
  263. up = new Vec3(0, 0, -1);
  264. }
  265. } else {
  266. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  267. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  268. const toLeft = leftPoint.getWorldPosition().subtract(targetV3).normalize();
  269. const toRight = rightPoint.getWorldPosition().subtract(targetV3).normalize();
  270. if (Math.abs(toLeft.x) <= Math.abs(toRight.x)) {
  271. up = new Vec3(0, 0, 1);
  272. } else {
  273. up = new Vec3(0, 0, -1);
  274. }
  275. }
  276. } else {
  277. up = direction.y > 0 ? new Vec3(0, -1, 0) : new Vec3(0, 1, 0);
  278. }
  279. }
  280. car.lookAt(targetV3, up);
  281. })
  282. .start();
  283. }
  284. // 顶部导航
  285. topRoadTween(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  286. const targetWorldPos = targetPoint.worldPosition.clone();
  287. tweenCar
  288. .to(duration, {
  289. worldPosition: new Vec3(targetWorldPos.x, targetWorldPos.y - 70, targetWorldPos.z)
  290. })
  291. .delay(0.1)
  292. .to(duration, {
  293. eulerAngles: new Vec3(0, 0, 0)
  294. })
  295. .delay(0.1)
  296. .to(duration, {
  297. worldPosition: targetPoint.worldPosition
  298. })
  299. .call(() => {
  300. car.getComponent(CarCarColorsComponent)!.openCover();
  301. })
  302. }
  303. /** 顶部左边转向右*/
  304. leftTopToRight(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  305. tweenCar.to(0.2, {
  306. eulerAngles: new Vec3(0, 0, -90)
  307. })
  308. .start()
  309. }
  310. // 左边导航
  311. leftRoadTween(car: Node, tweenCar: Tween<Node>) {
  312. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/LeftPoint")
  313. tweenCar.to(duration, {
  314. worldPosition: targetPoint.getWorldPosition()
  315. })
  316. .call(() => {
  317. const carforward = car.forward.clone()
  318. tween(carforward).to(0.1, { x: -1, y: 0, z: 0 }, {
  319. onUpdate: () => {
  320. car.forward = carforward
  321. }
  322. }).start()
  323. })
  324. .delay(0.1)
  325. }
  326. /** 顶部右边转向左*/
  327. rightTopToleft(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  328. tweenCar.to(0.2, {
  329. eulerAngles: new Vec3(0, 0, 90)
  330. })
  331. .start()
  332. }
  333. // 右边导航
  334. rightRoadTween(car: Node, tweenCar: Tween<Node>) {
  335. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/RightPoint")
  336. tweenCar.to(duration, {
  337. worldPosition: targetPoint.getWorldPosition()
  338. })
  339. .call(() => {
  340. const carforward = car.forward.clone()
  341. tween(carforward).to(0.1, { x: 1, y: 0, z: 0 }, {
  342. onUpdate: () => {
  343. car.forward = carforward
  344. }
  345. }).start()
  346. })
  347. .delay(0.1)
  348. }
  349. // 底部导航
  350. bottomRoadTween(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  351. tweenCar.to(duration, {
  352. worldPosition: new Vec3(targetPoint.getWorldPosition().x, targetPoint.getWorldPosition().y + 20, targetPoint.getWorldPosition().z)
  353. })
  354. .call(() => {
  355. const carforward = car.forward.clone()
  356. tween(carforward).to(0.1, { x: 0, y: 0, z: 1 }, {
  357. onUpdate: () => {
  358. car.forward = carforward
  359. }
  360. }).start()
  361. })
  362. .delay(0.1)
  363. }
  364. }