RoosterMoveCar.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.scheduleOnce(() => {
  75. this.updateScrewsLen();
  76. }, 0.2);
  77. }
  78. private onClickSet(): void {
  79. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(3), 1.0);
  80. const show = tgxUIMgr.inst.isShowing(UI_Setting);
  81. if (!show) {
  82. tgxUIMgr.inst.showUI(UI_Setting);
  83. }
  84. }
  85. onTouchCar(touchCar: Node) {
  86. let car = touchCar;
  87. let carComp = car.getComponent(CarCarColorsComponent);
  88. if (!carComp) return;
  89. const carWorldPos = car.getWorldPosition().clone();
  90. const objs = GameUtil.getWorldPositionAsVec2(car);
  91. const obje = GameUtil.calculateRayEnd(car, 1000);
  92. // const obje = this.createRaycastPosByDir(objs, car, carComp.carDir);
  93. // console.log('射线起点:', objs);
  94. // console.log('射线终点:', obje);
  95. // 射线检测
  96. let results = PhysicsSystem2D.instance.raycast(objs, obje, ERaycast2DType.Closest);
  97. if (results.length > 0) {
  98. const closestResult = results[0];
  99. const collider = results[0].collider;
  100. // console.log('碰撞器名称:', collider.name);
  101. // console.log('碰撞点世界坐标:', results[0].point);
  102. //碰到车
  103. if (collider.group == 1 << 1) {
  104. const halfLen = carComp.halfLen;
  105. let forward = null!;
  106. if (carComp.carDir == CarDir.TOP || carComp.carDir == CarDir.BOTTOM) {
  107. forward = new Vec3(0, 1, 0);
  108. } else {
  109. forward = new Vec3(1, 0, 0);
  110. }
  111. const carMovePos = carWorldPos.clone().add(forward.clone().multiplyScalar(closestResult.fraction - 5 * halfLen));
  112. tween(car).to(0.1, {
  113. worldPosition: carMovePos
  114. })
  115. .to(0.1, {
  116. worldPosition: carWorldPos
  117. })
  118. .call(() => {
  119. })
  120. .start()
  121. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(5), 1.0);
  122. }
  123. else {
  124. //碰到路
  125. const parkPoint = this.getEmptyParkPoint()
  126. if (parkPoint === null) {
  127. console.log("没有车位了")
  128. return
  129. }
  130. const targetWorldPos = results[0].point;
  131. // 顶部导航
  132. let tweenCar: Tween<Node> = tween(car)
  133. if (collider.node.name === "PhysicRoodTop") {
  134. // this.hitPointTween(car, parkPoint, tweenCar, collider.node)
  135. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node, parkPoint)
  136. this.topRoadTween(car, parkPoint, tweenCar)
  137. } else if (collider.node.name === "PhysicRoodLeft") {
  138. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/LeftPoint")
  139. // this.hitPointTween(car, targetPoint, tweenCar, collider.node);
  140. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  141. this.leftRoadTween(car, tweenCar)
  142. this.leftTopToRight(car, parkPoint, tweenCar)
  143. this.topRoadTween(car, parkPoint, tweenCar)
  144. } else if (collider.node.name === "PhysicRoodRight") {
  145. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/RightPoint")
  146. // this.hitPointTween(car, targetPoint, tweenCar, collider.node)
  147. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  148. this.rightRoadTween(car, tweenCar)
  149. this.rightTopToleft(car, parkPoint, tweenCar)
  150. this.topRoadTween(car, parkPoint, tweenCar)
  151. } else if (collider.node.name === "PhysicRoodBottom") {
  152. const hitPoint = find("Canvas/Scene/Grounds/PhysicRoodBottom")!;
  153. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  154. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  155. const toLeft = leftPoint.getWorldPosition().subtract(carWorldPos).normalize();
  156. const toRight = rightPoint.getWorldPosition().subtract(carWorldPos).normalize();
  157. if (Math.abs(toLeft.x) < Math.abs(toRight.x)) {
  158. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodBottom/LeftPoint")
  159. // this.hitPointTween(car, targetPoint, tweenCar, collider.node)
  160. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  161. this.bottomRoadTween(car, targetPoint, tweenCar)
  162. this.leftRoadTween(car, tweenCar)
  163. this.leftTopToRight(car, parkPoint, tweenCar)
  164. } else {
  165. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodBottom/RightPoint")
  166. // this.hitPointTween(car, targetPoint, tweenCar, collider.node)
  167. this.hitPointTween2(car, targetWorldPos, tweenCar, collider.node)
  168. this.bottomRoadTween(car, targetPoint, tweenCar)
  169. this.rightRoadTween(car, tweenCar)
  170. this.rightTopToleft(car, parkPoint, tweenCar)
  171. }
  172. this.topRoadTween(car, parkPoint, tweenCar)
  173. }
  174. parkPoint.name = "inuse"
  175. tweenCar.call(() => {
  176. const exhasut = car.getChildByName('Exhaust')!;
  177. const Exhaust1 = car.getChildByName('Exhaust1')!;
  178. if (exhasut) {
  179. exhasut.active = false;
  180. Exhaust1.active = false;
  181. }
  182. car.setParent(parkPoint, true);
  183. })
  184. .start()
  185. }
  186. } else {
  187. console.log('未检测到碰撞物体');
  188. }
  189. }
  190. onUpdateLeftNail() {
  191. this.updateScrewsLen();
  192. this.onUpdateLvl();
  193. }
  194. updateScrewsLen() {
  195. const levels = find('Canvas/Scene/Levels')!;
  196. const lbScrews = find('Canvas/GameUI/Pai/LbScrews')!.getComponent(Label);
  197. const children = levels.children;
  198. if (children.length <= 0)
  199. return;
  200. const levelComp = children[0].getComponent(LevelAction)!;
  201. const pins = levelComp.get_pin_color();
  202. console.log(`钉子数量:${pins.length}`)
  203. lbScrews.string = `${pins.length}`;
  204. }
  205. onUpdateLvl() {
  206. const lbScrews = find('Canvas/GameUI/TopLeft/TitleLvl/LbLvl')!.getComponent(Label);
  207. const { level } = LevelManager.instance.levelModel;
  208. lbScrews.string = `${level}`;
  209. }
  210. /** 获取停车位坐标*/
  211. getEmptyParkPoint(): Node {
  212. const points = find("Canvas/Scene/Parkings").children
  213. for (let i = 0; i < points.length; i++) {
  214. if (points[i].name === "empty") {
  215. return points[i]
  216. }
  217. }
  218. return null
  219. }
  220. /** 导航到碰撞点
  221. * @param car 车节点
  222. *@param targetWorldPos 碰撞点的世界坐标
  223. *@param tweenCar 车的tween动画
  224. *@param hitPoint street碰撞点
  225. */
  226. hitPointTween2(car: Node, targetWorldPos: Vec2, tweenCar: Tween<Node>, hitPoint: Node = null, parkPoint?: Node) {
  227. CarUnscrewAudioMgr.playOneShot(CarUnscrewAudioMgr.getMusicIdName(4), 1.0);
  228. const exhasut = car.getChildByName('Exhaust')!;
  229. const Exhaust1 = car.getChildByName('Exhaust1')!;
  230. if (exhasut) {
  231. exhasut.active = true;
  232. Exhaust1.active = true;
  233. }
  234. const targetV3 = new Vec3(targetWorldPos.x, targetWorldPos.y, 0);
  235. tweenCar
  236. .to(duration, { worldPosition: targetV3 }, { easing: 'quadInOut' })
  237. .call(() => {
  238. const carWorldPos = car.getWorldPosition().clone();
  239. const direction = targetV3.subtract(carWorldPos).normalize();
  240. // console.log('direction:', direction);
  241. let up: Vec3;
  242. if (Math.abs(direction.x) > Math.abs(direction.y)) {
  243. // 左右方向
  244. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  245. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  246. const toLeft = leftPoint.getWorldPosition().subtract(carWorldPos).normalize();
  247. const toRight = rightPoint.getWorldPosition().subtract(carWorldPos).normalize();
  248. if (Math.abs(toLeft.x) < Math.abs(toRight.x)) {
  249. up = new Vec3(0, 0, 1);
  250. } else {
  251. up = new Vec3(0, 0, -1);
  252. }
  253. } else {
  254. // 上下方向
  255. // console.log('hitPoint.name:', hitPoint.name);
  256. if (hitPoint.name == 'PhysicRoodBottom' || hitPoint.name == 'PhysicRoodTop') {
  257. if (hitPoint.name == 'PhysicRoodTop') {
  258. //车找相对停车位的方向
  259. const carWorldPos = car.getWorldPosition().clone();
  260. const parkWorldPos = parkPoint.getWorldPosition().clone();
  261. const direction = carWorldPos.subtract(parkWorldPos).normalize();
  262. if (direction.x > 0) {
  263. up = new Vec3(0, 0, 1);
  264. } else {
  265. up = new Vec3(0, 0, -1);
  266. }
  267. } else {
  268. const leftPoint = hitPoint.getChildByName('LeftPoint')!;
  269. const rightPoint = hitPoint.getChildByName('RightPoint')!;
  270. const toLeft = leftPoint.getWorldPosition().subtract(targetV3).normalize();
  271. const toRight = rightPoint.getWorldPosition().subtract(targetV3).normalize();
  272. if (Math.abs(toLeft.x) <= Math.abs(toRight.x)) {
  273. up = new Vec3(0, 0, 1);
  274. } else {
  275. up = new Vec3(0, 0, -1);
  276. }
  277. }
  278. } else {
  279. up = direction.y > 0 ? new Vec3(0, -1, 0) : new Vec3(0, 1, 0);
  280. }
  281. }
  282. car.lookAt(targetV3, up);
  283. })
  284. .start();
  285. }
  286. // 顶部导航
  287. topRoadTween(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  288. const targetWorldPos = targetPoint.worldPosition.clone();
  289. tweenCar
  290. .to(duration, {
  291. worldPosition: new Vec3(targetWorldPos.x, targetWorldPos.y - 70, targetWorldPos.z)
  292. })
  293. .delay(0.1)
  294. .to(duration, {
  295. eulerAngles: new Vec3(0, 0, 0)
  296. })
  297. .delay(0.1)
  298. .to(duration, {
  299. worldPosition: targetPoint.worldPosition
  300. })
  301. .call(() => {
  302. car.getComponent(CarCarColorsComponent)!.openCover();
  303. })
  304. }
  305. /** 顶部左边转向右*/
  306. leftTopToRight(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  307. tweenCar.to(0.2, {
  308. eulerAngles: new Vec3(0, 0, -90)
  309. })
  310. .start()
  311. }
  312. // 左边导航
  313. leftRoadTween(car: Node, tweenCar: Tween<Node>) {
  314. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/LeftPoint")
  315. tweenCar.to(duration, {
  316. worldPosition: targetPoint.getWorldPosition()
  317. })
  318. .call(() => {
  319. const carforward = car.forward.clone()
  320. tween(carforward).to(0.1, { x: -1, y: 0, z: 0 }, {
  321. onUpdate: () => {
  322. car.forward = carforward
  323. }
  324. }).start()
  325. })
  326. .delay(0.1)
  327. }
  328. /** 顶部右边转向左*/
  329. rightTopToleft(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  330. tweenCar.to(0.2, {
  331. eulerAngles: new Vec3(0, 0, 90)
  332. })
  333. .start()
  334. }
  335. // 右边导航
  336. rightRoadTween(car: Node, tweenCar: Tween<Node>) {
  337. const targetPoint = find("Canvas/Scene/Grounds/PhysicRoodTop/RightPoint")
  338. tweenCar.to(duration, {
  339. worldPosition: targetPoint.getWorldPosition()
  340. })
  341. .call(() => {
  342. const carforward = car.forward.clone()
  343. tween(carforward).to(0.1, { x: 1, y: 0, z: 0 }, {
  344. onUpdate: () => {
  345. car.forward = carforward
  346. }
  347. }).start()
  348. })
  349. .delay(0.1)
  350. }
  351. // 底部导航
  352. bottomRoadTween(car: Node, targetPoint: Node, tweenCar: Tween<Node>) {
  353. tweenCar.to(duration, {
  354. worldPosition: new Vec3(targetPoint.getWorldPosition().x, targetPoint.getWorldPosition().y + 20, targetPoint.getWorldPosition().z)
  355. })
  356. .call(() => {
  357. const carforward = car.forward.clone()
  358. tween(carforward).to(0.1, { x: 0, y: 0, z: 1 }, {
  359. onUpdate: () => {
  360. car.forward = carforward
  361. }
  362. }).start()
  363. })
  364. .delay(0.1)
  365. }
  366. }