123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import UEBase from "../../frameWork/compment/UEBase";
- import { GridConstant } from "./GridConstant";
- import UECell from "./UECell";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class UEGridMap extends UEBase {
- static readonly BundleKey: string = "gridMap";
- static readonly PrefabUrl: string = "UEGridMap";
- static readonly CLS: string = "UEGridMap";
- @property(cc.Prefab)
- cellPrefab: cc.Prefab = null!;
- @property(cc.Node)
- gridCenter: cc.Node = null;
- @property(cc.Node)
- gridLayer: cc.Node = null;
- gridMap: UECell[][] = [];
- private isDragging: boolean = false;
- private dragStartPos: cc.Vec2 = cc.v2(0, 0);
- private selectedCell: UECell = null!;
- Init() {
- const startX = -(GridConstant.ROW * GridConstant.CELL_WIDTH) / 2;
- const startY = (GridConstant.COL * GridConstant.CELL_HEIGHT) / 2;
- this.gridLayer.setContentSize(GridConstant.CELL_WIDTH * GridConstant.ROW, GridConstant.CELL_HEIGHT * GridConstant.COL);
- let idx = 0;
- for (let i = 0; i < GridConstant.COL; i++) {
- let row = [];
- for (let j = 0; j < GridConstant.ROW; j++) {
- let cell = cc.instantiate(this.cellPrefab).getComponent(UECell);
- this.gridLayer.addChild(cell.node);
- cell.node.width = GridConstant.CELL_WIDTH;
- cell.node.height = GridConstant.CELL_HEIGHT;
- cell.node.setPosition(cc.v2(
- startX + j * GridConstant.CELL_WIDTH + GridConstant.CELL_WIDTH / 2,
- startY - i * GridConstant.CELL_HEIGHT - GridConstant.CELL_HEIGHT / 2
- ));
- row.push(cell);
- cell.Init({
- id: idx, type: 1,
- zIndex: idx
- }, idx);
- idx++;
- }
- this.gridMap.push(row);
- }
- 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);
- }
- 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.dragStartPos = touchPos;
- this.selectedCell = cell;
- cell.StartDrag();
- }
- }
- private OnTouchMove(event: cc.Event.EventTouch): void {
- if (!this.isDragging || !this.selectedCell) return;
- const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
- this.selectedCell.UpdateDragPosition(cc.v3(touchPos.x, touchPos.y));
- }
- private OnTouchEnd(event: cc.Event.EventTouch): void {
- if (!this.isDragging || !this.selectedCell) return;
- const touchPos = this.gridLayer.convertToNodeSpaceAR(event.getLocation());
- const targetCell = this.GetCellByPos(touchPos);
- if (targetCell && targetCell !== this.selectedCell) {
- this.TryMergeItems(this.selectedCell, targetCell);
- }
- this.selectedCell.EndDrag();
- this.isDragging = false;
- this.selectedCell = null;
- }
- 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 (col >= 0 && col < GridConstant.COL && row >= 0 && row < GridConstant.ROW) {
- return this.gridMap[col][row];
- }
- return null;
- }
- private TryMergeItems(fromCell: UECell, toCell: UECell): void {
- }
- }
|