CatAction.ts 8.7 KB

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