ElementAction.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { _decorator, BoxCollider2D, CircleCollider2D, Color, Component, director, instantiate, Material, Node, PolygonCollider2D, Prefab, Rect, RigidBody2D, Sprite, UIRenderer, UITransform, Vec4, view } from 'cc';
  2. import { HoleAction } from './HoleAction';
  3. import { PinAction } from './PinAction';
  4. import { PoolMgr } from './PoolMagr';
  5. import { ColorDate } from './ColorDate';
  6. import { Global } from './Global';
  7. import { events } from './Enums';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('ElementAction')
  10. export class ElementAction extends Component {
  11. // cur_color:ColorDate = null;
  12. start() {
  13. }
  14. update(deltaTime: number) {
  15. let currentPosition = this.node.getPosition()
  16. if (currentPosition.y < - view.getVisibleSize().height / 2) {
  17. this.node.removeFromParent();
  18. director.emit(events.remove_element,this);
  19. }
  20. }
  21. /**
  22. * X关 ,钉子总数
  23. * @returns
  24. */
  25. public get_hole_num(): number {
  26. let hole_num: number = 0;
  27. this.node.children.forEach(element => {
  28. if (element.getComponent(HoleAction)) {
  29. hole_num++;
  30. }
  31. });
  32. return hole_num;
  33. }
  34. public init_element(group_id: number, ele_color: ColorDate) {
  35. this.node.getComponent(RigidBody2D).group = group_id;
  36. this.node.getComponents(BoxCollider2D).forEach(element => {
  37. element.group = group_id;
  38. });
  39. this.node.getComponents(CircleCollider2D).forEach(element => {
  40. element.group = group_id;
  41. });
  42. this.node.getComponents(PolygonCollider2D).forEach(element => {
  43. element.group = group_id;
  44. });
  45. //set img color
  46. // this.cur_color = ele_color;
  47. this.node.children.forEach(element => {
  48. if (element.name == "img") {
  49. let img = element.getComponent(Sprite);
  50. img.color = new Color(ele_color.r, ele_color.g, ele_color.b, 190);
  51. }
  52. });
  53. }
  54. public init_pin(group_id: number, color_pin_arr: ColorDate[]) {
  55. //获取 hole
  56. this.node.children.forEach(element => {
  57. if (element.getComponent(HoleAction)) {
  58. let new_pin = instantiate(PoolMgr.ins.getPrefab("pin"));
  59. this.node.addChild(new_pin);
  60. new_pin.setPosition(element.position);
  61. new_pin.getComponent(PinAction).init_date(group_id, color_pin_arr.shift());
  62. }
  63. });
  64. }
  65. get_pin_color(arr:PinAction[]){
  66. this.node.children.forEach(pin_node => {
  67. // pin_node.getComponent(PinAction)?.get_pin_color(arr);
  68. let pin_action = pin_node.getComponent(PinAction)
  69. if(pin_action&&pin_action.pin_color){
  70. arr.push(pin_node.getComponent(PinAction));
  71. }
  72. });
  73. }
  74. }