lq_collide.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import ccclass = cc._decorator.ccclass;
  2. import property = cc._decorator.property;
  3. import requireComponent = cc._decorator.requireComponent;
  4. import menu = cc._decorator.menu;
  5. import Component = cc.Component;
  6. import Enum = cc.Enum;
  7. import Size = cc.Size;
  8. import Vec2 = cc.Vec2;
  9. import Graphics = cc.Graphics;
  10. import Node = cc.Node;
  11. import macro = cc.macro;
  12. import Color = cc.Color;
  13. import {LQCollideShape, LQCollideStatus} from "../lq_base/data/lq_const";
  14. import {LQCollideConfig, LQCollideInfoList} from "./lq_collide_config";
  15. import {LQCollideSystem} from "./lq_collide_system";
  16. import {LQRect} from "../lq_base/data/lq_data";
  17. import {LQCollideBase} from "./lq_collide_base";
  18. import {LQGameUtil} from "../lq_base/util/lq_game_util";
  19. @ccclass
  20. @requireComponent(LQCollideBase)
  21. @menu("lq/collide")
  22. export class LQCollide extends Component {
  23. @property({displayName: '绘制形状'})
  24. get draw_collide(): boolean {
  25. return this._draw_collide;
  26. }
  27. set draw_collide(value: boolean) {
  28. this._draw_collide = value;
  29. this.draw_shape();
  30. }
  31. @property
  32. protected _draw_collide: boolean = false;
  33. @property({
  34. tooltip: '碰撞形状,None就是无敌,不参与碰撞',
  35. type: Enum(LQCollideShape),
  36. displayName: '碰撞形状'
  37. })
  38. get collide_shape(): LQCollideShape {
  39. return this._collide_shape;
  40. }
  41. set collide_shape(value: LQCollideShape) {
  42. this._collide_shape = value;
  43. this.draw_shape();
  44. }
  45. @property()
  46. public _collide_shape: LQCollideShape = LQCollideShape.Rect;
  47. @property({
  48. type: Enum(LQCollideInfoList), tooltip: '碰撞类别',
  49. displayName: '碰撞类别'
  50. })
  51. get collide_group_index() {
  52. if (this._collide_group_index === -1) {
  53. this._collide_group_index = LQCollideSystem.get_info_by_id(this.collide_group_id).index;
  54. }
  55. return this._collide_group_index;
  56. }
  57. set collide_group_index(value) {
  58. if (this._collide_group_index === value) {
  59. return;
  60. }
  61. this._collide_group_index = value;
  62. this.collide_group_id = LQCollideSystem.get_group_by_index(value).id;
  63. }
  64. @property({serializable: false})
  65. private _collide_group_index = -1;
  66. @property({visible: false})
  67. protected collide_group_id: number = 0;
  68. @property({
  69. tooltip: 'collide半径',
  70. visible() {
  71. // @ts-ignore
  72. return this._collide_shape === LQCollideShape.Circle;
  73. },
  74. displayName: '半径'
  75. })
  76. get radius(): number {
  77. return this._radius;
  78. }
  79. set radius(value: number) {
  80. this._radius = value;
  81. this.draw_shape();
  82. }
  83. @property()
  84. protected _radius: number = 50;
  85. @property({
  86. tooltip: 'collide长宽',
  87. visible() {
  88. // @ts-ignore
  89. return this._collide_shape === LQCollideShape.Rect;
  90. },
  91. displayName: '长宽'
  92. })
  93. get size(): Size {
  94. return this._size;
  95. }
  96. set size(value: Size) {
  97. this._size = value;
  98. if (this.world_rect) {
  99. this.world_rect.width = value.width;
  100. this.world_rect.height = value.height;
  101. this.world_rect.half_width = value.width * 0.5;
  102. this.world_rect.half_height = value.height * 0.5;
  103. }
  104. this.draw_shape();
  105. }
  106. @property()
  107. protected _size: Size = new Size(100, 100);
  108. @property({displayName: '位置偏移'})
  109. get offset(): Vec2 {
  110. return this._offset;
  111. }
  112. set offset(value: Vec2) {
  113. this._offset = value;
  114. this.draw_shape();
  115. }
  116. @property({
  117. type: Vec2,
  118. visible() {
  119. // @ts-ignore
  120. return this._collide_shape === LQCollideShape.Polygon;
  121. },
  122. displayName: '多边形碰撞点'
  123. })
  124. get polygon_points(): Vec2[] {
  125. return this._polygon_points;
  126. }
  127. set polygon_points(value: Vec2[]) {
  128. this._polygon_points = value;
  129. this.draw_shape();
  130. }
  131. @property()
  132. public _polygon_points: Vec2[] = [new Vec2(-45, -45), new Vec2(45, -45), new Vec2(60, 40), new Vec2(0, 70), new Vec2(-60, 40)];
  133. //collide碰撞位置偏移
  134. @property()
  135. public _offset: Vec2 = new Vec2(0, 0);
  136. @property({displayName: '自定义字符串'})
  137. public data_string: string = '';
  138. //每个collide的id唯一
  139. public collide_id: number = 0;
  140. //状态
  141. public collide_status: LQCollideStatus = LQCollideStatus.Idle;
  142. //是否可碰撞
  143. public is_enable: boolean = true;
  144. //是否开启碰撞前后的函数
  145. public is_open_func: boolean = true;
  146. //碰撞类别
  147. public collide_category = 0;
  148. //碰撞筛选
  149. public collide_mask = 0;
  150. //缓存多边形碰撞数据
  151. public cache_polygon_points: number[];
  152. //绘制collide形状组件
  153. private _debugDrawer!: Graphics;
  154. public world_rect!: LQRect;
  155. public collide_map: { [key: number]: { collide: LQCollide, status: 1 | 2 } } = {};
  156. public follow_target_category: number | undefined;
  157. private static id_maker: number = 1;
  158. //检测绘制组件是否添加
  159. private checkDebugDrawValid() {
  160. if (!this._debugDrawer || !this._debugDrawer.isValid) {
  161. let node = this.node.getChildByName('Collide');
  162. if (!node) {
  163. node = new Node('Collide');
  164. node.zIndex = macro.MAX_ZINDEX;
  165. this.node.addChild(node);
  166. // @ts-ignore
  167. node._objFlags = 1096;
  168. this._debugDrawer = node.addComponent(Graphics);
  169. this._debugDrawer.lineWidth = 3;
  170. this._debugDrawer.strokeColor = new Color(255, 0, 0);
  171. this._debugDrawer.fillColor = new Color(255, 0, 0);
  172. } else {
  173. this._debugDrawer = node.getComponent(Graphics);
  174. }
  175. }
  176. }
  177. //绘制形状
  178. protected draw_shape() {
  179. if (!this._draw_collide) {
  180. if (this._debugDrawer) {
  181. this._debugDrawer.clear();
  182. }
  183. return;
  184. }
  185. this.checkDebugDrawValid();
  186. this._debugDrawer.clear();
  187. let o1 = {key: 'scaleX', value: this.node.scaleX};
  188. let o2 = {key: 'scaleY', value: this.node.scaleY};
  189. LQGameUtil.recursion_node_property(this.node, o1);
  190. LQGameUtil.recursion_node_property(this.node, o2);
  191. if (o1.value === 0 || o2.value === 0) {
  192. return;
  193. }
  194. this._debugDrawer.node.scaleX = 1 / o1.value;
  195. this._debugDrawer.node.scaleY = 1 / o2.value;
  196. switch (this._collide_shape) {
  197. case LQCollideShape.Circle:
  198. this._debugDrawer.circle(+this._offset.x, +this._offset.y, this._radius);
  199. this._debugDrawer.stroke();
  200. break;
  201. case LQCollideShape.Rect:
  202. this._debugDrawer.moveTo(-this._size.width * 0.5 + this._offset.x, -this._size.height * 0.5 + this._offset.y);
  203. this._debugDrawer.lineTo(-this._size.width * 0.5 + this._offset.x, +this._size.height * 0.5 + this._offset.y);
  204. this._debugDrawer.lineTo(this._size.width * 0.5 + this._offset.x, +this._size.height * 0.5 + this._offset.y);
  205. this._debugDrawer.lineTo(this._size.width * 0.5 + this._offset.x, -this._size.height * 0.5 + this._offset.y);
  206. this._debugDrawer.lineTo(-this._size.width * 0.5 + this._offset.x, -this._size.height * 0.5 + this._offset.y);
  207. this._debugDrawer.stroke();
  208. break;
  209. case LQCollideShape.Polygon:
  210. this._debugDrawer.moveTo(this._polygon_points[0].x + this._offset.x, this._polygon_points[0].y + this._offset.y);
  211. for (let i = 1; i < this._polygon_points.length; i++) {
  212. this._debugDrawer.lineTo(this._polygon_points[i].x + this._offset.x, this._polygon_points[i].y + this._offset.y);
  213. }
  214. this._debugDrawer.lineTo(this._polygon_points[0].x + this._offset.x, this._polygon_points[0].y + this._offset.y);
  215. this._debugDrawer.stroke();
  216. break;
  217. }
  218. }
  219. //仅用于矩形
  220. public update_size(width: number, height: number) {
  221. this._size.width = width;
  222. this.world_rect.width = width;
  223. this.world_rect.half_width = width * 0.5;
  224. this._size.height = height;
  225. this.world_rect.height = height;
  226. this.world_rect.half_height = height * 0.5;
  227. this.draw_shape();
  228. }
  229. public init_lq_collide() {
  230. this.world_rect = new LQRect(0, 0, this._size.width, this._size.height);
  231. this.draw_shape();
  232. const info = LQCollideSystem.get_info_by_id(this.collide_group_id);
  233. this.collide_mask = info.mask;
  234. this.collide_category = info.category;
  235. this.collide_id = LQCollide.id_maker++;
  236. }
  237. public enable_lq_collide() {
  238. if (this.collide_status === LQCollideStatus.Live) {
  239. console.warn(this.node.name + '重复添加');
  240. return;
  241. }
  242. this.is_enable = true;
  243. this.collide_status = LQCollideStatus.Live;
  244. LQCollideSystem.add_collide(this);
  245. }
  246. public disable_lq_collide() {
  247. if (this.collide_status !== LQCollideStatus.Live) {
  248. return;
  249. }
  250. this.is_enable = false;
  251. this.collide_status = LQCollideStatus.Idle;
  252. LQCollideSystem.remove_collide(this);
  253. }
  254. public update_lq_collide() {
  255. }
  256. // @ts-ignore
  257. public on_collide(collide: LQCollide): void {
  258. if (LQCollideConfig.switch_print_log) {
  259. console.log(this.node.name + ' collide');
  260. }
  261. }
  262. //@ts-ignore
  263. public on_enter(collide: LQCollide) {
  264. if (LQCollideConfig.switch_print_log) {
  265. console.log(this.node.name + ' on_enter');
  266. }
  267. }
  268. //@ts-ignore
  269. public on_exit(collide: LQCollide) {
  270. if (LQCollideConfig.switch_print_log) {
  271. console.log(this.node.name + ' on_exit');
  272. }
  273. }
  274. }