123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import FF from "../FF";
- const { ccclass, property } = cc._decorator;
- const CALC_RECT_WIDTH = 20;
- const CLEAR_LINE_WIDTH = 20;
- @ccclass
- export default class FLiteMap extends cc.Component {
- @property(cc.Node)
- maskNode: cc.Node = null;
- @property(cc.Node)
- mIcon: cc.Node = null;
- @property(cc.Sprite)
- mMapSprite: cc.Sprite = null;
-
- private grid: Array<Array<number>> = new Array<Array<number>>();
- private stencil:cc.Graphics;
- private ff: FF;
-
- public tempDrawPoint: cc.Vec2 = null;
- onLoad() {
- let mask: any = this.maskNode.getComponent(cc.Mask);
- this.stencil = mask._graphics;
- this.stencil.lineWidth = CLEAR_LINE_WIDTH;
- this.stencil.lineCap = cc.Graphics.LineCap.ROUND;
- this.stencil.lineJoin = cc.Graphics.LineJoin.ROUND;
- this.stencil.strokeColor = cc.color(255, 255, 255, 255);
- for (let i = 0; i < this.node.height; i++) {
- this.grid.push([]);
- for (let j = 0; j < this.node.width; j++) {
- this.grid[i].push(0)
- }
- }
-
-
-
-
- }
- public loadMap(icon:string){
- cc.resources.load('icon/map/'+icon, cc.SpriteFrame, (err, spriteFrame:cc.SpriteFrame) =>{
- if(err){
- cc.error(err);
- }else{
- this.mMapSprite.spriteFrame = spriteFrame;
- }
- } );
- }
- public initMap(ff:FF){
- this.ff = ff;
- let widget = this.node.getComponent(cc.Widget);
- widget.target = this.ff.main.node;
- }
- public transform(pos:cc.Vec2){
- let x = Math.floor(pos.x*this.node.width/this.ff.mMap.node.width);
- let y = Math.floor(pos.y*this.node.height/this.ff.mMap.node.height);
- return cc.v2(x,y);
- }
- clearMask(pos:cc.Vec2) {
-
- let tpos = this.transform(pos);
- if(this.grid[tpos.y][tpos.x] == 1){
- return
- }
- if (this.tempDrawPoint == null) {
-
- this.stencil.circle(tpos.x, tpos.y, 5);
- this.stencil.fill();
- this.tempDrawPoint = tpos;
- this.mIcon.setPosition(tpos);
- this.grid[tpos.y][tpos.x] = 1;
- } else if (this.tempDrawPoint.x == tpos.x && this.tempDrawPoint.y == tpos.y) {
- } else {
-
- let prevPos = this.tempDrawPoint;
- let curPos = tpos;
- this.stencil.moveTo(prevPos.x, prevPos.y);
- this.stencil.lineTo(curPos.x, curPos.y);
- this.stencil.stroke();
- this.tempDrawPoint = tpos;
- this.mIcon.setPosition(tpos);
- this.grid[tpos.y][tpos.x] = 1;
- }
-
- }
- reset() {
- let mask: any = this.maskNode.getComponent(cc.Mask);
- mask._graphics.clear();
- }
- }
|