123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877 |
- import { _decorator, Component, math, Node } from 'cc';
- import { Game } from '../../core/util_comp/Game';
- import { ch } from '../../ch/ch';
- import TableLoadUtil from '../../core/util/TableLoadUtil';
- import { ModuleDef } from '../../scripts/ModuleDef';
- import { TableUtil } from '../../module_extra/table_ts/TableUtil';
- import map from './Map';
- import Block, { BlockState } from './block/block';
- import { FMSType } from '../process/FMS';
- import { Link } from './Link';
- import { Lv, LvDir } from './Lv';
- import { UINotify } from '../../module_basic/ui_notify/UINotify';
- import player, { data_type, day_data_type, rand_type, week_data_type } from './Player';
- import get_new_head_icon from '../../core/util_class/HeadIcon';
- import { gui } from '../../core/ui/ui';
- interface event_protocol {
- show_path(path: { x: number, y: number }[]): void;
- game_start(): void;
- game_wait(): void;
- game_end(win: boolean): void;
- show_block_type(id: number, type: number): void;
- }
- export enum GameState {
- init = 0,
- wait = 1,
- match = 2,
- end = 3,
- move = 4,
- }
- const { ccclass, property } = _decorator;
- @ccclass('GameLink')
- export class GameLink extends Game<GameLink> {
- //
- private _bs: Map<number, Block> = new Map();
- public map: map = new map();
- public link: Link;
- public lv: Lv;
- public player: player;
- public head_icon = get_new_head_icon();
- public evt = ch.get_new_event<event_protocol>();
- public state: GameState = GameState.init;
- public ver_b: boolean = false;
- public vibrate: boolean = false;
- onLoad(): void {
- super.onLoad();
- this.vibrate = ch.storage.getBoolean('linkup_vibrate');
- // console.log(new Date(chsdk.date.now()));
- }
- public set_vibrate(b: boolean): void {
- this.vibrate = b;
- if (this.vibrate) {
- ch.storage.set('linkup_vibrate', 1);
- } else {
- ch.storage.remove('linkup_vibrate');
- }
- }
- public vibrateShort(): void {
- if (!this.vibrate) return;
- ch.sdk.vibrateShort();
- }
- public vibrateLong(): void {
- if (!this.vibrate) return;
- ch.sdk.vibrateLong();
- }
- update(deltaTime: number) {
- super.update(deltaTime);
- if (this.is_paused) return;
- const list = Array.from(this._bs.values());
- for (let i = 0; i < list.length; i++) {
- list[i].action(deltaTime);
- }
- if (this.state != GameState.wait) return;
- this.lv?.time_run(deltaTime);
- }
- async start() {
- this.loadTable();
- this.player = player.getInstance(ch.sdk.get_gid(), ch.sdk.get_uid().toString());
- this.player.init_user_info();
- this.link = new Link(this.map);
- this.lv = new Lv();
- await this.player.load();
- if (chsdk.checkFromSidebar() && this.player.get_is_favorite() == 0) {
- this.player.add_item(3, 1);//提示道具
- this.player.set_is_favorite();
- }
- if (this.player.get_bx_time() < 0) {
- this.player.set_bx_time(300);
- }
- GameLink.getInst().to_main();
- }
- to_main() {
- this.FMS.Change(FMSType.Main);
- }
- private _ch_lv: number | null;
- to_start(lv?: number | null) {
- this._ch_lv = lv;
- this.FMS.Change(FMSType.Start);
- }
- async game_start() {
- //this.resume();
- this.game_clear();
- this.player.set_item(1, 4);
- const floor = this.player.data.get(data_type.max_floor);
- const lv = this._ch_lv ?? this.player.day_data.get(day_data_type.lv);
- this._ch_lv = null;
- this.lv.init(floor <= 0 ? 1 : floor, lv <= 0 ? 1 : lv);
- ch.log.debug('开始关卡', floor, lv, this.lv.seed);
- this.ranodm.set_seed(this.lv.seed);
- if (this.lv.lv == 1) {
- this.random_novice();
- } else {
- this.random_all_map(10 - this.lv.lv);
- }
- //
- this.evt.emit(this.evt.key.game_start);
- //await this.delay.start(3);
- //this.lv.win();
- }
- async game_end() {
- if (this.loop_id) this.job.DeleteTimeTask(this.loop_id);
- this.loop_id = null;
- this.state = GameState.end;
- await this.delay.start(0.3);
- //this.pause();
- if (this.lv.is_win) {
- //连胜下标变换的判断
- this.player.set_ls(1);//连胜+1
- let res = GameLink.getInst().player.get_ls_index();
- //更新最大连胜下标
- let index = GameLink.getInst().player.get_max_ls_index();
- console.log('当前连胜下标' + res);
- console.log('当前最大连胜下标' + index);
- //GameLink.getInst().player.config[index]
- if (res >= index + 1) {
- GameLink.getInst().player.set_lsbx_redpoint(1);
- GameLink.getInst().player.set_max_ls_index(1);
- console.log('更新最大连胜下标' + GameLink.getInst().player.get_max_ls_index());
- }
- if (this.lv.lv == 10) {
- this.player.data.add(data_type.max_floor, 1);
- this.player.data.add(data_type.floor, 1);
- this.player.day_data.set(day_data_type.lv, 1);
- this.player.save_rank_floor();
- this.player.setDirty();
- this.player.save();
- } else {
- this.player.day_data.add(day_data_type.lv, 1);
- this.player.setDirty();
- }
- this.player.day_data.add(day_data_type.total_lv, 1);
- this.player.save_rank_total_lv();
- } else {
- this.player.week_data.set(week_data_type.ls, 0);//重置连胜
- this.player.day_data.add(day_data_type.try, 1);
- this.player.setDirty();
- }
- this.evt.emit(this.evt.key.game_end, this.lv.is_win);
- }
- async relife() {
- this.player.day_data.sub(day_data_type.try, -1);
- this.lv.relife();
- await this.delay.start(0.5);
- this.state = GameState.wait;
- this.resume();
- }
- game_clear(): void {
- this.removeAllBlock();
- this.map.dispose();
- this._bs.clear();
- this._choose_id_a = null;
- this._choose_id_b = null;
- }
- /**随机新手关*/
- private random_novice(): void {
- const x: number = 10;
- const y: number = 16;
- const max: number = 10;
- const type_count: number = 27;
- this.map.init(x, y);
- const by = this.ranodm.int(6, 8);
- const t = this.ranodm.int(0, type_count);
- const b1 = this.creatBlock(t);
- b1.SetCell(4, by);
- const b2 = this.creatBlock(t);
- b2.SetCell(5, by);
- const values: number[] = [];
- for (let i = 0; i < max; i += 2) {
- const t = this.ranodm.int(0, type_count);
- values.push(t);
- values.push(t);
- }
- for (let i = values.length - 1; i > 0; i--) {
- const j = this.ranodm.int(0, i + 1);
- [values[i], values[j]] = [values[j], values[i]];
- }
- const x_list = [2, 4, 5, 7];
- const y_list = [6, 7, 9];
- let n: number = 0;
- for (let i = 0; i < x_list.length; i++) {
- for (let j = 0; j < y_list.length; j++) {
- if (y_list[j] == by && (x_list[i] == 4 || x_list[i] == 5)) continue;
- const b = this.creatBlock(values[n]);
- b.SetCell(x_list[i], y_list[j]);
- n++;
- }
- }
- }
- /**随机整个地图方块,至少保证有几对连在一起*/
- private random_all_map(pairsToAdd: number): void {
- pairsToAdd = pairsToAdd <= 0 ? 0 : pairsToAdd;
- const x: number = 10;
- const y: number = 16;
- const max: number = ((x - 2) * (y - 2));
- const type_count: number = 27;
- this.map.init(x, y);
- //
- const types = Array.from({ length: type_count }, (_, i) => i);
- let values: number[] = [];
- for (let i = 0; i < types.length; i++) {
- const t = types[i];
- values.push(t);
- values.push(t);
- }
- for (let i = values.length; i < max; i += 2) {
- const t = this.ranodm.int(0, type_count);
- values.push(t);
- values.push(t);
- }
- let placedPairs = 0;
- while (placedPairs < pairsToAdd) {
- const row_a = this.ranodm.int(1, x - 1);
- const col_a = this.ranodm.int(1, y - 1);
- if (this.map.check_empty(row_a, col_a, 1)) {
- let row_b = row_a;
- let col_b = col_a;
- if (this.ranodm.dir() > 0) {
- row_b += this.ranodm.dir();
- } else {
- col_b += this.ranodm.dir();
- }
- if (this.map.check_empty(row_b, col_b, 1)) {
- const a = this.creatBlock(values[placedPairs * 2]);
- a.SetCell(row_a, col_a);
- const b = this.creatBlock(values[placedPairs * 2 + 1]);
- b.SetCell(row_b, col_b);
- placedPairs++;
- }
- }
- }
- values = values.slice(pairsToAdd * 2);
- for (let i = values.length - 1; i > 0; i--) {
- const j = this.ranodm.int(0, i + 1);
- [values[i], values[j]] = [values[j], values[i]];
- }
- let n: number = 0;
- for (let row = 1; row < x - 1; row++) {
- for (let col = 1; col < y - 1; col++) {
- if (this.map.check_empty(row, col)) {
- const b = this.creatBlock(values[n]);
- b.SetCell(row, col);
- n++;
- }
- }
- }
- //ch.log.debug(this.map);
- }
- //
- public async blockShowUp() {
- const k = 0.01;
- let c: number = 0;
- if (this.lv.dir == LvDir.none) {
- let values = this.getAllBlock();
- for (let i = values.length - 1; i > 0; i--) {
- const j = this.ranodm.int(0, i + 1);
- [values[i], values[j]] = [values[j], values[i]];
- }
- for (let i = 0; i < values.length; i += 2) {
- values[i].setState(BlockState.show);
- values[i + 1].setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- } else if (this.lv.dir == LvDir.xCenter) {
- const midRow = Math.floor(this.map.rows * 0.5);
- for (let offset = midRow - 1; offset >= 0; offset--) {
- for (let col = 1; col < this.map.cols - 1; col++) {
- const ida = this.map.get_id(midRow + offset, col);
- if (!ida) continue;
- this.getBlock(ida)?.setState(BlockState.show);
- const idb = this.map.get_id(midRow - offset - 1, col);
- if (!idb) continue;
- this.getBlock(idb)?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.xOut) {
- const midRow = Math.floor(this.map.rows * 0.5);
- for (let offset = 0; offset < midRow; offset++) {
- for (let col = 1; col < this.map.cols - 1; col++) {
- const ida = this.map.get_id(midRow + offset, col);
- if (!ida) continue;
- this.getBlock(ida)?.setState(BlockState.show);
- const idb = this.map.get_id(midRow - offset - 1, col);
- if (!idb) continue;
- this.getBlock(idb)?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.yCenter) {
- const midCol = Math.floor(this.map.cols * 0.5);
- for (let offset = midCol - 1; offset >= 0; offset--) {
- for (let row = 1; row < this.map.rows - 1; row++) {
- const ida = this.map.get_id(row, midCol + offset);
- if (!ida) continue;
- this.getBlock(ida)?.setState(BlockState.show);
- const idb = this.map.get_id(row, midCol - offset - 1);
- if (!idb) continue;
- this.getBlock(idb)?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.yOut) {
- const midCol = Math.floor(this.map.cols * 0.5);
- for (let offset = 0; offset < midCol; offset++) {
- for (let row = 1; row < this.map.rows - 1; row++) {
- const ida = this.map.get_id(row, midCol + offset);
- if (!ida) continue;
- this.getBlock(ida)?.setState(BlockState.show);
- const idb = this.map.get_id(row, midCol - offset - 1);
- if (!idb) continue;
- this.getBlock(idb)?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.up) {
- //向上
- for (let col = this.map.cols - 2; col >= 1; col--) {
- for (let row = this.map.rows - 2; row >= 1; row -= 2) {
- this.getBlock(this.map.get_id(row, col))?.setState(BlockState.show);
- this.getBlock(this.map.get_id(row - 1, col))?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.down) {
- //向下,从左到右遍历------------------------------------------------------------
- for (let col = 1; col < this.map.cols - 1; col++) {
- for (let row = 1; row < this.map.rows - 1; row += 2) {
- this.getBlock(this.map.get_id(row, col))?.setState(BlockState.show);
- this.getBlock(this.map.get_id(row + 1, col))?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.right) {
- //向右
- for (let row = 1; row < this.map.rows - 1; row++) {
- for (let col = 1; col < this.map.cols - 1; col += 2) {
- this.getBlock(this.map.get_id(row, col))?.setState(BlockState.show);
- this.getBlock(this.map.get_id(row, col + 1))?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- } else if (this.lv.dir == LvDir.left) {
- //向左
- for (let row = this.map.rows - 2; row >= 1; row--) {
- for (let col = 1; col < this.map.cols - 1; col += 2) {
- this.getBlock(this.map.get_id(row, col))?.setState(BlockState.show);
- this.getBlock(this.map.get_id(row, col + 1))?.setState(BlockState.show);
- await this.delay.start(k);
- c += k;
- }
- }
- }
- //------------------------------------------------
- let n = 1.5 - c;
- if (n <= 0) n = 0.2;
- await this.delay.start(n);
- for (let row = 1; row < this.map.rows - 1; row++) {
- for (let col = 1; col < this.map.cols - 1; col++) {
- this.getBlock(this.map.get_id(row, col))?.setState(BlockState.idle);
- }
- }
- this.state = GameState.wait;
- this.evt.emit(this.evt.key.game_wait);
- }
- //加载配置
- private loadTable(): void {
- ch.log.log_start("加载配置初始化");
- TableLoadUtil.preloadAll(ModuleDef.EXTRA, "table_json", async () => {
- }, TableUtil.set);
- }
- //
- public creatBlock(type: number): Block {
- const b = Block.Creat(type);
- this._bs.set(b.id, b);
- return b;
- }
- public getAllBlock(): Block[] {
- return Array.from(this._bs.values());
- }
- public getBlock(id: number): Block {
- if (!id) return null;
- return this._bs.get(id);
- }
- public removeBlock(id: number | Block): void {
- if (typeof id === 'number') {
- const t = this._bs.get(id);
- if (t) {
- this._bs.delete(id);
- t.dispose();
- }
- } else {
- this._bs.delete(id.id);
- id.dispose();
- }
- }
- public removeAllBlock(): void {
- this._bs.forEach((value, key) => {
- value.dispose();
- });
- this._bs.clear();
- }
- //
- public showBlockType(id: number | Block | null): void {
- if (!this.ver_b) return;
- if (this.state != GameState.wait) return;
- if (this._choose_id_a && this._choose_id_b) return;
- this._unchoose_a();
- if (id == null) {
- this.evt.emit(this.evt.key.show_block_type, 0, 0);
- return;
- }
- let b: Block;
- if (typeof id === 'number') {
- b = this._bs.get(id)
- } else {
- b = id;
- }
- this.evt.emit(this.evt.key.show_block_type, b.id, b.type);
- }
- //
- private _choose_id_a: number = null;
- private _choose_id_b: number = null;
- private _unchoose_a(): Block {
- if (!this._choose_id_a) return;
- const a = this.getBlock(this._choose_id_a);
- a.change_choose(0);
- this._choose_id_a = null;
- return a;
- }
- private _unchoose_b(): Block {
- if (!this._choose_id_b) return;
- const b = this.getBlock(this._choose_id_b);
- b.change_choose(0);
- this._choose_id_b = null;
- return b;
- }
- public async chooseBlock(id: number | Block) {
- if (this.state != GameState.wait) return;
- if (this._choose_id_a && this._choose_id_b) return;
- let b: Block;
- if (typeof id === 'number') {
- b = this._bs.get(id)
- } else {
- b = id;
- }
- if (b.state != BlockState.idle) return;
- b.change_choose(1);
- if (!this._choose_id_a) {
- this._choose_id_a = b.id;
- } else {
- const a = this.getBlock(this._choose_id_a);
- if (b.id === this._choose_id_a) {
- this._choose_id_a = null;
- a.change_choose(0);
- return;
- }
- this._choose_id_b = b.id;
- //
- const path = this.link.getMatch(a, b);
- if (path) {
- this.state = GameState.match;
- this.evt.emit(this.evt.key.show_path, path);
- } else {
- this._choose_id_a = null;
- this._choose_id_b = null;
- a.change_choose(0);
- b.change_choose(0);
- this.lv.change_life(-1);
- }
- }
- }
- public async eliminate() {
- const a = this._unchoose_a();
- const b = this._unchoose_b();
- const eliminate_point = [{ x: a.cx, y: a.cy }, { x: b.cx, y: b.cy }];
- this._choose_id_a = null;
- this._choose_id_b = null;
- this.removeBlock(a);
- this.removeBlock(b);
- this.un_tips();
- if (this._bs.size <= 0) {
- this.lv.win();
- } else {
- await this.move(eliminate_point);
- this.loop_ts();
- }
- }
- private loop_id: number;
- private async loop_ts() {
- if (!this.ver_b) return;
- if (this.loop_id) this.job.DeleteTimeTask(this.loop_id);
- this.loop_id = this.job.AddTimeTask(() => {
- if (this.state != GameState.wait) {
- this.job.DeleteTimeTask(this.loop_id);
- this.loop_id = null;
- return;
- }
- const bb = this.link.getLinkBlock();
- if (!bb) {
- UINotify.show('没有可消除的了,请洗牌');
- this.loop_ts();
- }
- }, 10);
- }
- private async move(list?: { x: number, y: number }[] | null): Promise<void> {
- this.state = GameState.move;
- switch (this.lv.dir) {
- case LvDir.left:
- if (list) {
- if (list[0].y == list[1].y) {
- const xx = Math.min(list[0].x, list[1].x);
- list = [{ x: xx, y: list[0].y }];
- }
- } else {
- list = [];
- for (let i = 1; i < this.map.cols - 1; i++) {
- list.push({ x: 1, y: i })
- }
- }
- for (let i = 0; i < list.length; i++) {
- const x = 1;
- const y = list[i].y;
- const move_list: Block[] = [];
- for (let n = x; n < this.map.rows - 1; n++) {
- const id = this.map.get_id(n, y);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x + n, y)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.right:
- if (list) {
- if (list[0].y == list[1].y) {
- const xx = Math.max(list[0].x, list[1].x);
- list = [{ x: xx, y: list[0].y }]
- }
- } else {
- list = [];
- for (let i = 1; i < this.map.cols - 1; i++) {
- list.push({ x: this.map.rows - 1, y: i })
- }
- }
- for (let i = 0; i < list.length; i++) {
- const x = this.map.rows - 1;
- const y = list[i].y;
- const move_list: Block[] = [];
- for (let n = x; n > 0; n--) {
- const id = this.map.get_id(n, y);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x - n - 1, y)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.up:
- if (list) {
- if (list[0].x === list[1].x) list = [{ x: list[0].x, y: 1 }];
- } else {
- list = [];
- for (let i = 1; i < this.map.rows - 1; i++) {
- list.push({ x: i, y: 1 })
- }
- }
- for (let i = 0; i < list.length; i++) {
- const y = 1;
- const x = list[i].x;
- const move_list: Block[] = [];
- for (let n = y; n < this.map.cols; n++) {
- const id = this.map.get_id(x, n);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x, y + n)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.down:
- if (list) {
- if (list[0].x === list[1].x) list = [{ x: list[0].x, y: this.map.cols - 1 }];
- } else {
- list = [];
- for (let i = 1; i < this.map.rows - 1; i++) {
- list.push({ x: i, y: this.map.cols - 1 })
- }
- }
- for (let i = 0; i < list.length; i++) {
- const y = this.map.cols - 1;
- const x = list[i].x;
- const move_list: Block[] = [];
- for (let n = y; n > 0; n--) {
- const id = this.map.get_id(x, n);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x, y - 1 - n)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.xCenter:
- if (list) {
- if (list[0].y == list[1].y) list = [list[0]];
- } else {
- list = [];
- for (let i = 1; i < this.map.cols - 1; i++) list.push({ x: this.map.rows - 1, y: i });
- }
- for (let i = 0; i < list.length; i++) {
- const k = this.map.rows * 0.5;
- const y = list[i].y;
- const move_list: Block[] = [];
- for (let n = k; n < this.map.rows - 1; n++) {
- const id = this.map.get_id(n, y);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(k + n, y)) move_list[n].setState(BlockState.move);
- }
- move_list.length = 0;
- for (let n = k - 1; n > 0; n--) {
- const id = this.map.get_id(n, y);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(k - 1 - n, y)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.xOut:
- if (list) {
- if (list[0].y == list[1].y) list = [list[0]];
- } else {
- list = [];
- for (let i = 1; i < this.map.cols - 1; i++) list.push({ x: this.map.rows - 1, y: i });
- }
- for (let i = 0; i < list.length; i++) {
- const k = this.map.rows * 0.5;
- const y = list[i].y;
- const move_list: Block[] = [];
- for (let n = this.map.rows - 1; n >= k; n--) {
- const id = this.map.get_id(n, y);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(this.map.rows - 2 - n, y)) move_list[n].setState(BlockState.move);
- }
- move_list.length = 0;
- for (let n = 1; n < k; n++) {
- const id = this.map.get_id(n, y);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(n + 1, y)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.yCenter:
- if (list) {
- if (list[0].x == list[1].x) list = [list[0]];
- } else {
- list = [];
- for (let i = 1; i < this.map.rows - 1; i++) list.push({ x: i, y: this.map.cols - 1 });
- }
- for (let i = 0; i < list.length; i++) {
- const k = this.map.cols * 0.5;
- const x = list[i].x;
- const move_list: Block[] = [];
- for (let n = k; n < this.map.cols - 1; n++) {
- const id = this.map.get_id(x, n);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x, k + n)) move_list[n].setState(BlockState.move);
- }
- move_list.length = 0;
- for (let n = k - 1; n > 0; n--) {
- const id = this.map.get_id(x, n);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x, k - 1 - n)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- case LvDir.yOut:
- if (list) {
- if (list[0].x == list[1].x) list = [list[0]];
- } else {
- list = [];
- for (let i = 1; i < this.map.rows - 1; i++) list.push({ x: i, y: this.map.cols - 1 });
- }
- for (let i = 0; i < list.length; i++) {
- const k = this.map.cols * 0.5; // 中心行
- const x = list[i].x;
- const move_list: Block[] = [];
- // 检查当前块下方的所有格子
- for (let n = this.map.cols - 1; n >= k; n--) {
- const id = this.map.get_id(x, n);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- // 移动下方块
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x, this.map.cols - 2 - n)) move_list[n].setState(BlockState.move);
- }
- //清空移动列表
- move_list.length = 0;
- //检查当前块上方的所有格子
- for (let n = 1; n < k; n++) {
- const id = this.map.get_id(x, n);
- if (id) {
- const block = this.getBlock(id);
- move_list.push(block);
- }
- }
- // 移动上方块
- for (let n = 0; n < move_list.length; n++) {
- if (move_list[n].SetCell(x, n + 1)) move_list[n].setState(BlockState.move);
- }
- }
- break;
- }
- if (this.lv.dir != LvDir.none) {
- await this.delay.start(0.3);
- }
- this.state = GameState.wait;
- }
- private _tipsa: number;
- private _tipsb: number;
- private un_tips(): void {
- if (this._tipsa) {
- this.getBlock(this._tipsa)?.change_choose(0);
- this._tipsa = null;
- }
- if (this._tipsb) {
- this.getBlock(this._tipsb)?.change_choose(0);
- this._tipsb = null;
- }
- }
- /**提示*/
- public tips(): boolean {
- if (this.state != GameState.wait) return false;
- if (this._choose_id_a && this._choose_id_b) return false;
- this._unchoose_a();
- const bb = this.link.getLinkBlock();
- if (bb) {
- bb.a.change_choose(2);
- bb.b.change_choose(2);
- this._tipsa = bb.a.id;
- this._tipsb = bb.b.id;
- this.lv.record.item1++;
- return true;
- } else {
- notify('没有可消除的了,请洗牌');
- return false;
- }
- }
- /**刷新*/
- public sx(): boolean {
- if (this.state != GameState.wait) return false;
- if (this._choose_id_a && this._choose_id_b) return false;
- this.un_tips();
- this._unchoose_a();
- const bs = this.getAllBlock();
- let ts: number[] = []
- for (let i = 0; i < bs.length; i++) {
- ts.push(bs[i].type);
- }
- for (let i = ts.length - 1; i > 0; i--) {
- const j = Math.floor(Math.random() * (i + 1));
- [ts[i], ts[j]] = [ts[j], ts[i]];
- }
- for (let i = 0; i < bs.length; i++) {
- bs[i].change_type(ts[i]);
- }
- this.lv.record.item2++;
- return true;
- }
- //
- public add_hp(): boolean {
- if (this.lv.life >= 6) {
- notify('生命值已达到上限');
- return false;
- }
- this.lv.change_life(3);
- this.lv.record.item3++;
- return true;
- }
- //
- public change_dir(): boolean {
- if (this.state != GameState.wait) return false;
- if (this._choose_id_a && this._choose_id_b) return false;
- this.un_tips();
- this.lv.change_dir();
- if (this.lv.dir == LvDir.none) return false;
- this.move();
- return true;
- }
- }
- export function notify(msg: string): void {
- UINotify.show(msg);
- }
- export function button_sound(): void {
- ch.audio.playOneShot('sounds/sfx_button');
- }
- export function ani_ui(node: Node, end: number = 1.0): void {
- gui.scale_elasticOut_anim(node, 1.2, 0.5, end);
- }
- export function repair_ani_ui(node: Node, end: number = 1.15): void {
- gui.scale_elasticOut_anim(node, 1.2, 0.5, end);
- }
|