游戏结束

This commit is contained in:
2025-05-14 12:17:47 +08:00
parent 2cc01c5570
commit 830bf2bb29
27 changed files with 405 additions and 113 deletions

View File

@@ -3,6 +3,8 @@
export const respath = {
home_ui_res: "resources/Home.lh",
stage_ui_res: "resources/Stage.lh",
gameover_ui_res: "resources/GameOver.lh",
gamedone_ui_res: "resources/GameDone.lh",
score_add_ui_res: "resources/ScoreAdd.lh",
}

View File

@@ -73,6 +73,16 @@ export class StorageManager {
return new DOStage(dataStage, levelStr)
}
public cleanStage(): void {
if (this.stageID && this.stageID.length > 0) {
if (this.stageMap.has(this.stageID)) {
this.stageMap.delete(this.stageID)
}
Laya.LocalStorage.removeItem(this.stageID)
this.stageID = ""
}
}
public saveStage(): void {
if (this.changed) {
this.changed = false

View File

@@ -22,6 +22,8 @@ export class UIManager extends Laya.Script {
return UIManager._instance
}
private stage: Stage
public loadHome(): void {
Laya.loader.load(respath.home_ui_res).then((go)=>{
var prefab = go.create()
@@ -29,11 +31,39 @@ export class UIManager extends Laya.Script {
})
}
public loadStage(): void {
Laya.loader.load(respath.stage_ui_res).then((go)=>{
public loadStage(stageID: string, clean: boolean=false): void {
if (clean) {
StorageManager.getInstance().cleanStage()
}
if (this.stage) {
this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
}
else {
Laya.loader.load(respath.stage_ui_res).then((go)=>{
var prefab = go.create()
this.stage = this.owner.addChild(prefab).getComponent(Stage)
this.stage.onLoadStage(StorageManager.getInstance().loadStage(stageID))
})
}
}
public closeStage(): void {
if (this.stage) {
this.stage.owner.destroy()
this.stage = null
}
}
public loadGameOver(): void {
Laya.loader.load(respath.gameover_ui_res).then((go)=>{
var prefab = go.create()
var stage = this.owner.addChild(prefab).getComponent(Stage)
stage.onLoadStage(StorageManager.getInstance().loadStage("first"))
this.owner.addChild(prefab)
})
}
public loadGameDone(): void {
Laya.loader.load(respath.gamedone_ui_res).then((go)=>{
var prefab = go.create()
this.owner.addChild(prefab)
})
}

View File

@@ -30,23 +30,22 @@ export class Candy extends Laya.Script {
this.updateCandy()
}
public getLeft(): number {
return this.data.get_left();
}
updateCandy(): void {
this.owner.active = this.getLeft() > 0
if (this.data.get_show() > 0) {
this.label_show.text = this.data.get_show().toString()
}
else {
this.label_show.text = ""
}
if (this.getLeft() > 0) {
this.label_left.text = this.getLeft().toString();
if (this.data.get_left() > 0) {
this.label_left.text = this.data.get_left().toString();
this.owner.visible = true
}
else {
this.label_left.text = ""
this.owner.visible = false
this.label_show.text = ""
}
}
}

22
src/views/GameDone.ts Normal file
View File

@@ -0,0 +1,22 @@
import { UIManager } from "../models/UIManager";
import { StorageManager } from "../models/StorageManager";
const { regClass, property } = Laya;
@regClass()
export class GameDone extends Laya.Script {
declare owner : Laya.Box;
@property(Laya.Button)
public btn_new: Laya.Button
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
this.btn_new.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
UIManager.getInstance().closeStage();
UIManager.getInstance().loadHome();
StorageManager.getInstance().cleanStage()
this.owner.destroy()
})
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "13e020d1-5ab8-4082-80ca-c0d208342103"
}

29
src/views/GameOver.ts Normal file
View File

@@ -0,0 +1,29 @@
import { UIManager } from "../models/UIManager";
const { regClass, property } = Laya;
@regClass()
export class GameOver extends Laya.Script {
declare owner : Laya.Box;
@property(Laya.Button)
public btn_restart: Laya.Button
@property(Laya.Button)
public btn_new: Laya.Button
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
this.btn_restart.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
UIManager.getInstance().loadStage("first", true);
this.owner.destroy()
})
this.btn_new.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
UIManager.getInstance().loadStage("first", true);
this.owner.destroy()
})
}
}

View File

@@ -0,0 +1,3 @@
{
"uuid": "72a5ccd3-8177-451c-b2ce-cf1b42f7e6d4"
}

View File

@@ -16,7 +16,7 @@ export class Home extends Laya.Script {
onAwake(): void {
this.btn_start.on(Laya.Event.CLICK, this, (evt: Laya.Event) => {
console.log("btn_start=", evt);
UIManager.getInstance().loadStage();
UIManager.getInstance().loadStage("first");
this.owner.destroy()
})

View File

@@ -67,7 +67,7 @@ export class Stage extends Laya.Script {
onAwake(): void {
this.btn_back.on(Laya.Event.CLICK, this, ()=>{
UIManager.getInstance().loadHome();
this.owner.destroy()
UIManager.getInstance().closeStage();
})
for (var i=0; i<this.obj_blocks.numChildren; i++) {
var hbox = this.obj_blocks.getChildAt(i);
@@ -131,6 +131,7 @@ export class Stage extends Laya.Script {
for (var i=0; i<blocks.length; i++) {
var doBlock = blocks[i]
var block = this.blocks[i]
block.setButtonSkin(this.img_block_normal.skin)
block.setBlock(doBlock)
}
var candys = doStage.get_candys()
@@ -231,10 +232,10 @@ export class Stage extends Laya.Script {
if (index >= list.length)
index = list.length -1
var block = list[index]
block.setShowNumber(block.getData().get_correct())
this.onUpdateScore()
this.onSetBlock(block)
this.selectedBlock = block
var correct = block.getData().get_correct()
var candy = this.candys[correct-1]
this.onApplyCandy(candy)
}
}
@@ -267,10 +268,11 @@ export class Stage extends Laya.Script {
this.onUpdateScore()
this.onSetBlock(this.selectedBlock)
if (b) {
var left = candy.getLeft();
var left = candy.getData().get_left();
candy.setLeft(left-1);
if (this.getIsComplete()) {
console.log("完成关卡")
UIManager.getInstance().loadGameDone()
}
}
else {//失败
@@ -278,6 +280,7 @@ export class Stage extends Laya.Script {
this.onUpdateMistake()
if (this.data.get_mistake() >= this.mistake_all) {
//关卡失败
UIManager.getInstance().loadGameOver()
}
}
}