CatAction.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { _decorator, Color, Component, director, EventTouch, find, Input, Label, Node, Sprite, tween, UITransform, Vec2, Vec3 } from 'cc';
  2. import { HoleAction } from './HoleAction';
  3. import { PinAction } from './PinAction';
  4. import { ColorDate } from './ColorDate';
  5. import { AudioMgr } from './AudioMgr';
  6. import { Clips, events } from './Enums';
  7. import { Tools } from './Tools';
  8. import { Global } from './Global';
  9. import { CatColorRules } from './CatColorRules';
  10. const { ccclass, property } = _decorator;
  11. @ccclass('CatAction')
  12. export class CatAction extends Component {
  13. cat_color: ColorDate;
  14. default_position: Readonly<Vec3>;
  15. defalut_lock: boolean = false;//默认不锁
  16. @property({ type: Node })
  17. lock: Node;
  18. isLocked: boolean = false;
  19. onLoad(): void {
  20. //get default position
  21. this.default_position = this.node.getPosition().clone();
  22. //显示锁头
  23. this.defalut_lock = this.lock.active;
  24. }
  25. start() {
  26. this.node.on(Input.EventType.TOUCH_START, this.touch_start, this);
  27. }
  28. init_cat() {
  29. // console.log("init>>>>>>> slot>>>>>>>>>defalut_lock>>",this.defalut_lock,this.isLocked);
  30. this.set_lock_unlock(this.defalut_lock);
  31. this.clear_pins();
  32. // console.log("init slot,",this.isLocked);
  33. this.node.setPosition(this.default_position.x, this.default_position.y);
  34. if (this.isLocked) {
  35. //默认有锁定的,让其出现在默认位置
  36. return;
  37. }
  38. //set slot color
  39. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_0());
  40. if (!this.cat_color) {
  41. //没有颜色,了。
  42. return;
  43. }
  44. //set color
  45. this.set_img(this.cat_color, 255);
  46. }
  47. get_cat_color(): ColorDate {
  48. return this.cat_color;
  49. }
  50. set_lock_unlock(l: boolean) {
  51. if (l) {
  52. //lock
  53. this.isLocked = true;
  54. //隐藏 hole
  55. this.node.children.forEach(element => {
  56. if (element.getComponent(HoleAction)) {
  57. element.active = false;
  58. }
  59. });
  60. //修改图颜色,黑色吧
  61. this.set_img(ColorDate.new_bean(0, 0, 0, 0), 150);
  62. this.lock.active = true;
  63. } else {
  64. this.isLocked = false;
  65. //隐藏 hole
  66. this.node.children.forEach(element => {
  67. if (element.getComponent(HoleAction)) {
  68. element.active = true;
  69. }
  70. });
  71. this.lock.active = false;
  72. }
  73. }
  74. touch_start(e: EventTouch) {
  75. if (!this.isLocked) {
  76. return;
  77. }
  78. AudioMgr.ins.playSound(Clips.btn_1);
  79. // AudioMgr.ins.playSound(Clips.btn_1);
  80. Global.props_action.show_box(this);
  81. }
  82. update(deltaTime: number) {
  83. }
  84. private get_hole_arr(): Node[] {
  85. let hole_arr = [];
  86. this.node.children.forEach(element => {
  87. if (element.getComponent(HoleAction)) {
  88. hole_arr.push(element);
  89. }
  90. });
  91. return hole_arr;
  92. }
  93. public get_pin_num(): number {
  94. let pin_num = 0;
  95. this.node.children.forEach(element => {
  96. if (element.getComponent(PinAction)) {
  97. pin_num++;
  98. }
  99. });
  100. return pin_num;
  101. }
  102. public check_able_put(pin: PinAction): boolean {
  103. if (!this.cat_color) {
  104. return false;
  105. }
  106. if (this.isLocked) {
  107. return false;
  108. }
  109. if (pin.pin_color.id != this.cat_color.id) {
  110. return false;
  111. }
  112. let temp = this.get_temp_hole_num()
  113. if (temp > 0) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. //get temp hole num
  120. public get_temp_hole_num(): number {
  121. let pin_num = this.get_pin_num();
  122. let hole_arr = this.get_hole_arr();
  123. return hole_arr.length - pin_num;
  124. }
  125. private get_temp_position(): Vec3 {
  126. //钉子数量
  127. let pin_num = this.get_pin_num();
  128. let hole_arr = this.get_hole_arr();
  129. if (this.get_temp_hole_num() <= 0) {
  130. //钉子满了
  131. return null;
  132. }
  133. let target_hole_index = pin_num;
  134. return hole_arr[target_hole_index].position;
  135. }
  136. public put_pin(pin: PinAction, path: String): boolean {
  137. let target_hole_pos = this.get_temp_position();
  138. if (!target_hole_pos) {
  139. // 没找到孔的点
  140. // console.log("没找到孔的点");
  141. return false;
  142. }
  143. //
  144. let world_pos = pin.node.getWorldPosition();
  145. let target = this.node.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
  146. this.node.addChild(pin.node);
  147. pin.node.setPosition(target);
  148. // let target = find("Canvas").getComponent(UITransform).convertToNodeSpaceAR(hole_world_pos);
  149. pin.remove_collider();
  150. //这里限制一下,防止多个钉子一起放进来,都执行结束动作,只准许最后一个钉子完成后执行结束动作
  151. let run_completed = false;
  152. if (this.get_temp_hole_num() <= 0) {
  153. run_completed = true;
  154. }
  155. tween(pin.node)
  156. .to(0.35, { position: target_hole_pos }, Global.our_easing)
  157. .call(() => {
  158. //
  159. AudioMgr.ins.playSound(Clips.close_screw);
  160. if (run_completed) {
  161. //如果已经满了。
  162. this.full_complete(path + ">>put_pin");
  163. }
  164. })
  165. .start();
  166. return true;
  167. }
  168. private full_complete(path: String) {
  169. //获取奖励
  170. Global.coins_action.put_coins(this.get_pin_num(), this.node.getWorldPosition());
  171. let to_position = this.node.getPosition().clone();
  172. to_position.y = to_position.y + 300;
  173. tween(this.node)
  174. .to(0.25, { position: to_position }, Global.our_easing)
  175. .call(() => {
  176. AudioMgr.ins.playSound(Clips.complete_1);
  177. this.cat_color = null;
  178. Global.pin_prgress_computed(this.get_pin_num());
  179. this.clear_pins();
  180. director.emit(events.check_completed, this);
  181. }).start();
  182. }
  183. public clear_pins() {
  184. Tools.clearFromParent(this.node, PinAction);
  185. }
  186. private set_img(c: ColorDate, a: number) {
  187. let img = this.node.getChildByName("img").getComponent(Sprite);
  188. img.color = new Color(c.r, c.g, c.b, a);
  189. }
  190. public into_scence(pos: Vec2= new Vec2(-600, 0)) {
  191. if (this.isLocked) {
  192. //默认有锁定的,让其出现在默认位置
  193. this.node.setPosition(this.default_position.x, this.default_position.y);
  194. return;
  195. }
  196. //set slot color
  197. switch (Global.cur_lvl) {
  198. case 1:
  199. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_0());
  200. break;
  201. case 2:
  202. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_0());
  203. break;
  204. case 3:
  205. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_1());
  206. break;
  207. case 4:
  208. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_2());
  209. break;
  210. case 5:
  211. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_3());
  212. break;
  213. default:
  214. this.cat_color = CatColorRules.get_next_cat_color(CatColorRules.r_4());
  215. }
  216. if (!this.cat_color) {
  217. //没有颜色了。
  218. return;
  219. }
  220. console.log("into_scence ---->>>>>>>>> remain cat>>", CatColorRules.cat_color_arr.length);
  221. //set color
  222. this.set_img(this.cat_color, 255);
  223. this.node.setPosition(this.default_position.x + pos.x, this.default_position.y + pos.y);
  224. tween(this.node)
  225. .to(0.20, { position: this.default_position.clone() }, Global.our_easing)
  226. .call(() => {
  227. //飞到了自己的位置
  228. let pin_arr: PinAction[] = [];
  229. // bucket
  230. Global.bucket_action.get_pin_arr_by_color_id(this.cat_color.id, pin_arr);
  231. Global.layer_empty_action.get_pin_by_color(this.cat_color.id, pin_arr);
  232. pin_arr.forEach(element => {
  233. if (this.check_able_put(element)) {
  234. this.put_pin(element, "into_scence");
  235. }
  236. });
  237. })
  238. .start();
  239. }
  240. }