CarBoxComponent.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { _decorator, CCInteger, Component, find, Label, Node, tween, v2, v3 } from 'cc';
  2. import { GameUtil } from '../GameUtil';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('CarBoxComponent')
  5. export class CarBoxComponent extends Component {
  6. @property(Label)
  7. label: Label = null
  8. @property(CCInteger)
  9. get num() {
  10. return this._num
  11. }
  12. set num(value) {
  13. this._num = value
  14. this.label.string = `${value}`
  15. }
  16. @property(CCInteger)
  17. private _num: number = 3
  18. isAnimateOut: boolean = false
  19. /** 盒子出车*/
  20. outCarTween() {
  21. console.log("outCarTween 盒子出车")
  22. this.isAnimateOut = true
  23. const car = this.node.getChildByName("cars").children[0]
  24. const rotation = car.angle;
  25. this.num -= 1
  26. // 根据角度计算方向向量
  27. let direction = v2(0, 0);
  28. if (rotation === -90) {
  29. direction = v2(1, 0); // 朝右
  30. } else if (rotation === 0) {
  31. direction = v2(0, 1); // 朝上
  32. } else if (rotation === 90) {
  33. direction = v2(-1, 0); // 朝左
  34. } else if (rotation === 180) {
  35. direction = v2(0, -1); // 朝下
  36. } else {
  37. const adjustedAngle = rotation - 90;
  38. direction = v2(-Math.cos(adjustedAngle * (Math.PI / 180)), Math.sin(-adjustedAngle * (Math.PI / 180)));
  39. }
  40. const objs = GameUtil.getWorldPositionAsVec2(car);
  41. const obje = objs.add(direction.multiplyScalar(300));
  42. car.setParent(find("Canvas/Scene/Levels").children[0], true);
  43. tween(car)
  44. .to(0.2, { worldPosition: v3(obje.x, obje.y, 0) })
  45. .call(() => {
  46. this.isAnimateOut = false
  47. })
  48. .start();
  49. }
  50. }