UI_Idioms.ts 5.6 KB

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