UI_Idioms.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { _decorator, Component, DynamicAtlasManager, instantiate, Label, macro, Node, Prefab, ScrollView, Size, UITransform, Vec2, Vec3 } from 'cc';
  2. import { Container_Manager } from 'db://assets/Scripts/Container_Manager';
  3. import { UI_Idiom } from './UI_Idiom';
  4. import { Cube_Infor } from 'db://assets/Scripts/Cube_Infor';
  5. import ui_base from 'db://assets/core/ui/ui_base';
  6. import { ModuleDef } from 'db://assets/Scripts/ModuleDef';
  7. import { GameUILayers } from 'db://assets/core/ui/ui';
  8. import { Layout_Idioms } from './Layout_Idioms';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('UI_Idioms')
  11. export class UI_Idioms extends ui_base {
  12. count: number =5;
  13. idioms: UI_Idiom[] = [];
  14. constructor() {
  15. super(ModuleDef.GAME, 'ui/UI_Idioms/ScrollView', GameUILayers.HUD, Layout_Idioms);
  16. }
  17. protected async onCreated() {
  18. this.init();
  19. }
  20. public init() {
  21. const layout = this.getLayout<Layout_Idioms>();
  22. console.log("init");
  23. for (let i = 0; i < this.count; i++) {
  24. let node = instantiate(layout.idiom_prefab);
  25. node.parent = layout.content;
  26. console.log("label" + Container_Manager.instance.idioms[i].idiom);
  27. node.getComponent(UI_Idiom).txt.string = Container_Manager.instance.idioms[i].idiom;
  28. node.getComponent(UI_Idiom).piece_1_word = Container_Manager.instance.idioms[i].piece_1_word;
  29. node.getComponent(UI_Idiom).piece_2_word = Container_Manager.instance.idioms[i].piece_2_word;
  30. this.idioms.push(node.getComponent(UI_Idiom));
  31. }
  32. }
  33. //高亮显示
  34. public light_Show(cube_infor: Cube_Infor) {
  35. const layout = this.getLayout<Layout_Idioms>();
  36. let txt_length = cube_infor.Text.length;
  37. this.idioms.forEach(element => {
  38. if (element.piece_1_word === cube_infor.Text) {
  39. element.hud_sp.node.active = true;
  40. element.hud_sp.node.getComponent(UITransform).contentSize = new Size(20 * txt_length, 20);
  41. let target = 0;
  42. element.hud_sp.node.position = new Vec3(31 + target * 20, -16, 0);
  43. let pos = new Vec2(element.node.position.x, element.node.position.y);
  44. this.scrollToTarget(pos);
  45. }
  46. else if (element.piece_2_word === cube_infor.Text) {
  47. element.hud_sp.node.active = true;
  48. element.hud_sp.node.getComponent(UITransform).contentSize = new Size(20 * txt_length, 20);
  49. let target = 4 - txt_length;
  50. element.hud_sp.node.position = new Vec3(31 + target * 20, -16, 0);
  51. let pos = new Vec2(element.node.position.x, element.node.position.y);
  52. this.scrollToTarget(pos);
  53. }
  54. });
  55. }
  56. //取消高亮
  57. public light_Hide(cube1: Cube_Infor, cube2: Cube_Infor) {
  58. const layout = this.getLayout<Layout_Idioms>();
  59. this.idioms = this.idioms.filter(element => {
  60. let shouldRemove = false;
  61. let hasMatch = false;
  62. // 检查与 cube1 相关的成语
  63. if (cube1.Text === element.piece_1_word) {
  64. for (const cubeInfor of Container_Manager.instance.idiom_combine.keys()) {
  65. if (cubeInfor.txt.string === cube1.Text) {
  66. hasMatch = true;
  67. break;
  68. }
  69. }
  70. // 只有当前字没有参与成语时才移除提示
  71. if (!hasMatch) {
  72. element.hud_sp.node.active = false; // 销毁UI中的成语提示
  73. }
  74. }
  75. // 检查与 cube2 相关的成语
  76. if (cube2.Text === element.piece_2_word) {
  77. for (const cubeInfor of Container_Manager.instance.idiom_combine.keys()) {
  78. if (cubeInfor.txt.string === cube2.Text) {
  79. hasMatch = true;
  80. break;
  81. }
  82. }
  83. // 只有当前字没有参与成语时才移除提示
  84. if (!hasMatch) {
  85. element.hud_sp.node.active = false; // 销毁UI中的成语提示
  86. }
  87. }
  88. // 检查是否能组合成一个成语
  89. if (cube1.Text + cube2.Text === element.txt.string) {
  90. element.node.destroy(); // 销毁节点
  91. shouldRemove = true; // 如果已组合,移除该成语
  92. }
  93. // 保留未被组合的元素
  94. return !shouldRemove;
  95. });
  96. // 监测所有字是否仍然需要高亮显示
  97. this.idioms.forEach(element => {
  98. // 检查每个字是否仍然在任何成语组合中
  99. let shouldDisplay = false;
  100. // 遍历容器中的所有成语组合,看看当前字是否仍然参与任何组合
  101. for (const cubeInfor of Container_Manager.instance.idiom_combine.keys()) {
  102. if (cubeInfor.txt.string === element.piece_1_word || cubeInfor.txt.string === element.piece_2_word) {
  103. shouldDisplay = true;
  104. break;
  105. }
  106. }
  107. // 如果该字还参与成语,则保持其提示显示
  108. if (shouldDisplay) {
  109. element.hud_sp.node.active = true;
  110. }
  111. });
  112. }
  113. public scrollToTarget(pos: Vec2) {
  114. const layout = this.getLayout<Layout_Idioms>();
  115. layout.scrollView.scrollTo(pos, 0.2, true);
  116. }
  117. public all_light_Hide(){
  118. this.idioms.forEach(element => {
  119. element.hud_sp.node.active = false;
  120. });
  121. }
  122. }