"use strict";
cc._RFpush(module, '30b044ZrbBPW7YXSqNsg/Rd', 'Game');
// Scripts\Game.js

cc.Class({
    'extends': cc.Component,

    properties: {
        Col: 0,
        Row: 0,
        Padding: 0,
        SpacingX: 0,
        SpacingY: 0,
        cell: cc.Prefab
    },

    // use this for initialization
    onLoad: function onLoad() {
        this.beginX = 0;
        this.beginY = 0;
        this.pSet = []; //用来存坐标
        var ele = cc.instantiate(this.cell);
        this.eleSize = ele.getContentSize();
        this.buildCoordinateSet();
        this.init();
        this.touch();
    },
    init: function init() {
        var node = this.node;
        this.cells = [];
        var pSet = this.pSet;
        for (var i = 0; i < this.Row; i++) {
            var arr1 = [];
            for (var j = 0; j < this.Col; j++) {
                var ele = cc.instantiate(this.cell);
                ele.setPosition(pSet[i][j].x, pSet[i][j].y);
                node.addChild(ele, 0, "ele");
                var com = ele.getComponent('cell');
                com.pos = cc.v2(i, j);
                arr1.push(ele);
            }
            this.cells.push(arr1);
        }
    },
    PositionToPos: function PositionToPos(x, y) {
        //屏幕坐标转矩阵坐标
        var pos = cc.v2(Math.floor((x - this.beginX) / (this.eleSize.width + this.SpacingX) + 0.5), Math.floor((y - this.beginY) / (this.eleSize.height + this.SpacingY) + 0.5));
        return pos;
    },
    touch: function touch() {
        this.node.on('touchstart', function (event) {
            //传回节点位置
            cc.log('touchstart');
            var p = this.node.convertToNodeSpace(event.getLocation());
            var p2 = this.PositionToPos(p.x, p.y);
            cc.log('p', p, 'p2', p2);
            var cell = this.cells[p2.x][p2.y];
            cell.active = false;
            cc.log(cell);
        }, this);
        this.node.on('touchmove', function (event) {
            cc.log('touchmove');
        }, this);
        this.node.on('touchend', function (event) {
            cc.log('touchend');
        }, this);
    },
    buildCoordinateSet: function buildCoordinateSet() {
        this.beginX = (this.node.width - (this.Row - 1) * (this.SpacingX + this.eleSize.width)) / 2;
        this.beginY = this.Padding + this.eleSize.height / 2;

        for (var i = 0; i < this.Row; i++) {
            var arr = [];
            for (var j = 0; j < this.Col; j++) {
                var position = cc.v2(this.beginX + i * (this.eleSize.width + this.SpacingX), this.beginY + j * (this.eleSize.height + this.SpacingY));
                arr.push(position);
            }
            this.pSet.push(arr);
        }
    }

});

cc._RFpop();