|
@@ -37,65 +37,69 @@ export default class UEGridMap extends UEBase {
|
|
startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
|
|
startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
|
|
));
|
|
));
|
|
row.push(cell);
|
|
row.push(cell);
|
|
- cell.Init(idx);
|
|
|
|
|
|
+ cell.Init({
|
|
|
|
+ id: idx, type: 1,
|
|
|
|
+ zIndex: idx
|
|
|
|
+ }, idx);
|
|
idx++;
|
|
idx++;
|
|
}
|
|
}
|
|
this.gridMap.push(row);
|
|
this.gridMap.push(row);
|
|
}
|
|
}
|
|
- this.initEvent();
|
|
|
|
|
|
+ this.InitEvent();
|
|
}
|
|
}
|
|
|
|
|
|
- initEvent(): void {
|
|
|
|
- this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
|
|
|
|
- this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
|
|
|
|
- this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
|
|
|
|
- this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
|
|
|
|
|
|
+ InitEvent(): void {
|
|
|
|
+ this.gridLayer.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this);
|
|
|
|
+ this.gridLayer.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this);
|
|
|
|
+ this.gridLayer.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this);
|
|
|
|
+ this.gridLayer.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchEnd, this);
|
|
}
|
|
}
|
|
|
|
|
|
- private onTouchStart(event: cc.Event.EventTouch): void {
|
|
|
|
- const touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
- const cell = this.getCellByPos(touchPos);
|
|
|
|
- if (cell && cell.hasItem()) {
|
|
|
|
|
|
+ private OnTouchStart(event: cc.Event.EventTouch): void {
|
|
|
|
+ const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
+ const cell = this.GetCellByPos(touchPos);
|
|
|
|
+ if (cell && cell.CanDrag()) {
|
|
this.isDragging = true;
|
|
this.isDragging = true;
|
|
this.dragStartPos = touchPos;
|
|
this.dragStartPos = touchPos;
|
|
this.selectedCell = cell;
|
|
this.selectedCell = cell;
|
|
- cell.startDrag();
|
|
|
|
|
|
+ cell.StartDrag();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private onTouchMove(event: cc.Event.EventTouch): void {
|
|
|
|
|
|
+ private OnTouchMove(event: cc.Event.EventTouch): void {
|
|
if (!this.isDragging || !this.selectedCell) return;
|
|
if (!this.isDragging || !this.selectedCell) return;
|
|
- const touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
- this.selectedCell.updateDragPosition(cc.v3(touchPos.x, touchPos.y));
|
|
|
|
|
|
+ const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
+ this.selectedCell.UpdateDragPosition(cc.v3(touchPos.x, touchPos.y));
|
|
}
|
|
}
|
|
|
|
|
|
- private onTouchEnd(event: cc.Event.EventTouch): void {
|
|
|
|
|
|
+ private OnTouchEnd(event: cc.Event.EventTouch): void {
|
|
if (!this.isDragging || !this.selectedCell) return;
|
|
if (!this.isDragging || !this.selectedCell) return;
|
|
- const touchPos = this.node.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
- const targetCell = this.getCellByPos(touchPos);
|
|
|
|
|
|
+ const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
|
|
|
|
+ const targetCell = this.GetCellByPos(touchPos);
|
|
|
|
|
|
if (targetCell && targetCell !== this.selectedCell) {
|
|
if (targetCell && targetCell !== this.selectedCell) {
|
|
- this.tryMergeItems(this.selectedCell, targetCell);
|
|
|
|
|
|
+ this.TryMergeItems(this.selectedCell, targetCell);
|
|
}
|
|
}
|
|
|
|
|
|
- this.selectedCell.endDrag();
|
|
|
|
|
|
+ this.selectedCell.EndDrag();
|
|
this.isDragging = false;
|
|
this.isDragging = false;
|
|
this.selectedCell = null;
|
|
this.selectedCell = null;
|
|
}
|
|
}
|
|
|
|
|
|
- private getCellByPos(pos: cc.Vec2): UECell | null {
|
|
|
|
- const row = Math.floor(-pos.y / GridConstant.CELL_HEIGHT);
|
|
|
|
- const col = Math.floor(pos.x / GridConstant.CELL_WIDTH);
|
|
|
|
|
|
+ private GetCellByPos(pos: cc.Vec2): UECell | null {
|
|
|
|
+ const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
|
|
|
|
+ const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
|
|
|
|
+
|
|
|
|
+ const row = Math.floor((pos.x - startX) / GridConstant.CELL_WIDTH);
|
|
|
|
+ const col = Math.floor((startY - pos.y) / GridConstant.CELL_HEIGHT);
|
|
|
|
|
|
- if (row >= 0 && row < GridConstant.ROW && col >= 0 && col < GridConstant.COL) {
|
|
|
|
- return this.gridMap[row][col];
|
|
|
|
|
|
+ if (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
|
|
|
|
+ return this.gridMap[col][row];
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- private tryMergeItems(fromCell: UECell, toCell: UECell): void {
|
|
|
|
- if (fromCell.canMergeWith(toCell)) {
|
|
|
|
- toCell.mergeFrom(fromCell);
|
|
|
|
- }
|
|
|
|
|
|
+ private TryMergeItems(fromCell: UECell, toCell: UECell): void {
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|