GameTips.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import EventName from "../EventName/EventName";
  8. import LocalData from "../LocalData";
  9. import MyComponent from "../Template/MyComponent";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export default class GameTips extends MyComponent {
  13. @property(cc.Label)
  14. Hight: cc.Label = null;
  15. @property(cc.Label)
  16. Left: cc.Label = null;
  17. @property(cc.Label)
  18. Right: cc.Label = null;
  19. // LIFE-CYCLE CALLBACKS:
  20. onLoad() {
  21. super.onLoad()
  22. }
  23. start() {
  24. this.regEvent(EventName.Update_CurrentHight, this.Update_CurrentHight, this)
  25. this.regEvent(EventName.YaoGanMove, this.YaoGanMove, this)
  26. }
  27. Update_CurrentHight(hightNum: number) {
  28. this.Hight.string = `${hightNum.toString()} M`
  29. if (LocalData.getInstance().getMaxRecord() < hightNum) {
  30. LocalData.getInstance().setMaxRecord(hightNum, '=')
  31. }
  32. }
  33. // YaoGanEnd(dir: cc.Vec3, power: number) {
  34. // // const result = parseFloat((power * 100).toFixed(2));
  35. // // this.History.string = `历史:${this.GetAngle(dir)}° , ${result}%`
  36. // }
  37. YaoGanMove(dir: cc.Vec3, power: number) {
  38. const result = parseFloat((power * 100).toFixed(0));
  39. this.Left.string = `${this.GetAngle(dir)}°`
  40. this.Right.string = `${this.FixLength(result)}%`
  41. }
  42. GetAngle(dir: cc.Vec3) {
  43. //根据朝向计算出夹角弧度
  44. var angle = dir.signAngle(cc.v2(0, 1));
  45. //将弧度转换为欧拉角
  46. // (angle / Math.PI * 180).toFixed(0);
  47. return this.FixLength((angle / Math.PI * 180).toFixed(0).toString())
  48. // let numStr = (angle / Math.PI * 180).toFixed(0).toString();
  49. // let spacesToAdd = 3 - numStr.length;
  50. // let paddedStr = "";
  51. // for (let i = 0; i < spacesToAdd; i++) {
  52. // paddedStr += " ";
  53. // }
  54. // return paddedStr + numStr;
  55. }
  56. FixLength(numStr) {
  57. let spacesToAdd = 4 - numStr.length;
  58. let paddedStr = "";
  59. for (let i = 0; i < spacesToAdd; i++) {
  60. paddedStr += " ";
  61. }
  62. return paddedStr + numStr;
  63. }
  64. onDestroy(): void {
  65. super.onDestroy()
  66. }
  67. }