123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import GameDataCenter from "../../data/GameDataCenter";
- import ResSprite from "../../frameWork/compment/ResSprite";
- import UEBase from "../../frameWork/compment/UEBase";
- import EventMng from "../../manager/EventMng";
- import { HcInfoGeziInfo, HcType, HcUnlock } from "../../shared/hc/PtlHcInfo";
- import { GridConstant } from "./GridConstant";
- import { GridEvent } from "./GridEvent";
- import UECube from "./UECube";
- const { ccclass, property } = cc._decorator;
- export interface I_CellData {
- x: number,
- y: number,
- idx: number;
- unlock: HcUnlock;
- }
- @ccclass
- export default class UECell extends UEBase {
- static readonly BundleKey: string = "gridMap";
- static readonly PrefabUrl: string = "UECell";
- static readonly CLS: string = "UECell";
- @property(cc.Node)
- private node_select: cc.Node = null!;
- @property(ResSprite)
- private sp_quality: ResSprite = null;
- @property(cc.Node)
- private node_lock: cc.Node = null;
- @property(cc.Prefab)
- cubePrefab: cc.Prefab = null!;
- cellData: I_CellData = null!;
- ueCube: UECube = null;
- Init(cellData: I_CellData) {
- this.cellData = cellData;
- this.node.zIndex = cellData.idx;
- this.SetLock();
- EventMng.on(GridEvent.HC_CELL_CHANGE, this.OnHcCellChange, this);
- EventMng.on(GridEvent.HC_CELL_SELECT, this.OnHcCellSelect, this);
- }
- /** 监听格子选中 */
- OnHcCellSelect(idx: number) {
- if (!idx) {
- this.SetSelect(false);
- } else if (idx == this.cellData.idx) {
- this.SetSelect(true);
- }
- }
- /** 监听格子变化 */
- OnHcCellChange(data: { idx: number, item: HcInfoGeziInfo }) {
- if (data.idx == this.cellData.idx) {
- if (data.item.type == HcType.wu) {
- this.ClearCube();
- } else {
- }
- }
- }
- /** 创建棋子 */
- CreateCube(info: HcInfoGeziInfo) {
- let cube = cc.instantiate(this.cubePrefab).getComponent(UECube);
- GameDataCenter.gridMap.ueGridMap.GetCubeLayer().addChild(cube.node);
- cube.node.width = GridConstant.CELL_WIDTH;
- cube.node.height = GridConstant.CELL_WIDTH;
- let pos = GameDataCenter.gridMap.GetPosByVec(this.cellData.x, this.cellData.y);
- cube.node.setPosition(pos);
- let idx = (this.cellData.x + 1) * 10 + (this.cellData.y + 1);
- cube.Init({
- type: info.type,
- id: info.correlationId,
- idx: idx
- })
- this.ueCube = cube;
- this.SetQuality();
- this.SetLock();
- return cube;
- }
- SetCube(cube: UECube) {
- this.ueCube = cube;
- this.SetQuality();
- }
- SetQuality() {
- if (this.ueCube && !this.IsLock()) {
- this.ueCube.SetZIndex(this.cellData.idx);
- let quality = this.ueCube.GetQuality();
- this.sp_quality.setSpriteFrame('gridMap', `qualityBg/Img_zjm_diban0${quality}`);
- } else {
- this.sp_quality.setSpriteFrame('gridMap', `qualityBg/Img_zjm_diban00`);
- }
- }
- IsLock(): boolean {
- return this.cellData.unlock == HcUnlock.off;
- }
- ChangeLockState(unlock: HcUnlock) {
- this.cellData.unlock = unlock;
- this.SetLock();
- }
- SetLock() {
- if (this.cellData.unlock == HcUnlock.off) {
- this.node_lock.active = true;
- if (this.ueCube) {
- this.ueCube.node.active = false;
- }
- } else {
- this.node_lock.active = false;
- if (this.ueCube) {
- this.ueCube.node.active = true;
- }
- }
- }
- GetZIndex() {
- return this.cellData.idx;
- }
- SetSelect(isSelect: boolean) {
- this.node_select.active = isSelect;
- }
- /** 是否有物品可以拖动 */
- CanDrag(): boolean {
- return !this.IsEmpty() && this.ueCube.CanDrag() && !this.IsLock();
- }
- GetCube(): UECube {
- return this.ueCube;
- }
- /** 该棋格是否是空的 */
- IsEmpty(): boolean {
- return this.ueCube == null;
- }
- /** 清空棋格里的棋子 */
- ClearCube() {
- this.ueCube.unUse();
- this.ueCube = null;
- this.SetQuality();
- }
- MoveCubeToCell(toCell: UECell) {
- this.ueCube.MoveToNewPos(toCell.node.position.clone());
- }
- }
|