FLiteMap.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import FF from "../FF";
  2. const { ccclass, property } = cc._decorator;
  3. const CALC_RECT_WIDTH = 20;
  4. const CLEAR_LINE_WIDTH = 20;
  5. /**
  6. * 小地图
  7. */
  8. @ccclass
  9. export default class FLiteMap extends cc.Component {
  10. @property(cc.Node)
  11. maskNode: cc.Node = null;
  12. @property(cc.Node)
  13. mIcon: cc.Node = null;
  14. @property(cc.Sprite)
  15. mMapSprite: cc.Sprite = null;
  16. //网格记录
  17. private grid: Array<Array<number>> = new Array<Array<number>>();
  18. private stencil:cc.Graphics;
  19. private ff: FF;
  20. /**
  21. * 绘制的点
  22. */
  23. public tempDrawPoint: cc.Vec2 = null;
  24. onLoad() {
  25. let mask: any = this.maskNode.getComponent(cc.Mask);
  26. this.stencil = mask._graphics;
  27. this.stencil.lineWidth = CLEAR_LINE_WIDTH;
  28. this.stencil.lineCap = cc.Graphics.LineCap.ROUND;
  29. this.stencil.lineJoin = cc.Graphics.LineJoin.ROUND;
  30. this.stencil.strokeColor = cc.color(255, 255, 255, 255);
  31. for (let i = 0; i < this.node.height; i++) {
  32. this.grid.push([]);
  33. for (let j = 0; j < this.node.width; j++) {
  34. this.grid[i].push(0)
  35. }
  36. }
  37. // this.grid[0][0] = 1;
  38. // cc.log('xxx 1= ',this.grid[0][0]);
  39. // cc.log('xxx 2= ',this.grid[0][1]);
  40. // cc.log('xxx 3= ',this.grid[1][0]);
  41. }
  42. public loadMap(icon:string){
  43. cc.resources.load('icon/map/'+icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
  44. if(err){
  45. cc.error(err);
  46. }else{
  47. this.mMapSprite.spriteFrame = spriteFrame;
  48. }
  49. } );
  50. }
  51. public initMap(ff:FF){
  52. this.ff = ff;
  53. let widget = this.node.getComponent(cc.Widget);
  54. widget.target = this.ff.main.node;
  55. }
  56. public transform(pos:cc.Vec2){
  57. let x = Math.floor(pos.x*this.node.width/this.ff.mMap.node.width);
  58. let y = Math.floor(pos.y*this.node.height/this.ff.mMap.node.height);
  59. return cc.v2(x,y);
  60. }
  61. clearMask(pos:cc.Vec2) {
  62. // 地图坐标转化为小地图坐标
  63. let tpos = this.transform(pos);
  64. if(this.grid[tpos.y][tpos.x] == 1){
  65. return
  66. }
  67. if (this.tempDrawPoint == null) {
  68. // 只有一个点,用圆来清除涂层
  69. this.stencil.circle(tpos.x, tpos.y, 5);
  70. this.stencil.fill();
  71. this.tempDrawPoint = tpos;
  72. this.mIcon.setPosition(tpos);
  73. this.grid[tpos.y][tpos.x] = 1;
  74. } else if (this.tempDrawPoint.x == tpos.x && this.tempDrawPoint.y == tpos.y) {
  75. } else {
  76. // 存在多个点,用线段来清除涂层
  77. let prevPos = this.tempDrawPoint;
  78. let curPos = tpos;
  79. this.stencil.moveTo(prevPos.x, prevPos.y);
  80. this.stencil.lineTo(curPos.x, curPos.y);
  81. this.stencil.stroke();
  82. this.tempDrawPoint = tpos;
  83. this.mIcon.setPosition(tpos);
  84. this.grid[tpos.y][tpos.x] = 1;
  85. }
  86. }
  87. reset() {
  88. let mask: any = this.maskNode.getComponent(cc.Mask);
  89. mask._graphics.clear();
  90. }
  91. }