From b6c1e58f35f03a7c30eae8f92a00d2dc748be6e4 Mon Sep 17 00:00:00 2001 From: yangwen Date: Mon, 19 May 2025 20:58:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B9=BF=E6=92=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/resources/GameOver.lh | 335 ++++++++++++++++++++++++-------- src/models/EventManager.ts | 50 +++++ src/models/EventManager.ts.meta | 3 + src/views/GameOver.ts | 20 +- src/views/Stage.ts | 17 +- 5 files changed, 342 insertions(+), 83 deletions(-) create mode 100644 src/models/EventManager.ts create mode 100644 src/models/EventManager.ts.meta diff --git a/assets/resources/GameOver.lh b/assets/resources/GameOver.lh index cdb2dae..587087b 100644 --- a/assets/resources/GameOver.lh +++ b/assets/resources/GameOver.lh @@ -11,78 +11,272 @@ "right": 0, "top": 0, "bottom": 0, + "_$comp": [ + { + "_$type": "72a5ccd3-8177-451c-b2ce-cf1b42f7e6d4", + "scriptPath": "../src/views/GameOver.ts", + "btn_second": { + "_$ref": "0w6x8zzw" + }, + "btn_restart": { + "_$ref": "y7aniaui" + }, + "btn_new": { + "_$ref": "eepsaznr" + } + } + ], "_$child": [ { - "_$id": "li6t446m", - "_$type": "Image", - "name": "bg", - "x": 90, - "y": 404, - "width": 900, - "height": 1112, + "_$id": "0a0ovv4z", + "_$type": "Box", + "name": "center", + "x": 96, + "y": 420, + "width": 888, + "height": 1080, "_mouseState": 2, "centerX": 0, "centerY": 0, - "skin": "res://6ad19646-ed7a-431c-a50e-65c2bcb40751", - "sizeGrid": "40,60,50,60,0", - "color": "#ffffff" - }, - { - "_$id": "sco68sr2", - "_$type": "Button", - "name": "btn_restart", - "x": 172, - "y": 1032, - "width": 736, - "height": 152, - "_mouseState": 2, - "centerX": 0, - "centerY": 148, - "stateNum": 1, - "skin": "res://2e8c9585-e650-4494-a329-807fd2d2a532", - "sizeGrid": "24,40,24,40,0", - "label": "重新开始", - "labelFont": "", - "labelSize": 60, - "labelBold": true, - "labelColors": "#1d5cdc,#1d5cdc,#1d5cdc", - "labelAlign": "center", - "labelVAlign": "middle" - }, - { - "_$id": "p8wk9udp", - "_$type": "Button", - "name": "btn_new", - "x": 172, - "y": 1234, - "width": 736, - "height": 152, - "_mouseState": 2, - "centerX": 0, - "centerY": 350, - "stateNum": 1, - "skin": "res://2e8c9585-e650-4494-a329-807fd2d2a532", - "sizeGrid": "24,40,24,40,0", - "label": "新游戏", - "labelFont": "", - "labelSize": 60, - "labelBold": true, - "labelColors": "#1d5cdc,#1d5cdc,#1d5cdc", - "labelAlign": "center", - "labelVAlign": "middle" + "_$child": [ + { + "_$id": "xs71kk61", + "_$type": "Sprite", + "name": "Sprite", + "width": 888, + "height": 1080, + "_gcmds": [ + { + "_$type": "DrawRoundRectCmd", + "x": 0, + "y": 0, + "width": 1, + "height": 1, + "lt": 20, + "rt": 20, + "lb": 20, + "rb": 20, + "percent": true, + "lineWidth": 1, + "fillColor": "#FFFFFF" + } + ] + }, + { + "_$id": "xz2d429f", + "_$type": "VBox", + "name": "buttons", + "x": 72, + "y": 420, + "width": 744, + "height": 600, + "centerX": 0, + "centerY": 180, + "space": 48, + "align": "center", + "_$child": [ + { + "_$id": "0w6x8zzw", + "_$type": "Sprite", + "name": "btn_second", + "width": 744, + "height": 156, + "_gcmds": [ + { + "_$type": "DrawRoundRectCmd", + "x": 0, + "y": 0, + "width": 1, + "height": 1, + "lt": 20, + "rt": 20, + "lb": 20, + "rb": 20, + "percent": true, + "lineWidth": 1, + "fillColor": "rgba(29, 92, 220, 1)" + } + ], + "_$child": [ + { + "_$id": "23po992p", + "_$type": "Sprite", + "name": "Sprite", + "x": 68, + "y": 51, + "width": 60, + "height": 54, + "_gcmds": [ + { + "_$type": "DrawRoundRectCmd", + "x": 0, + "y": 0, + "width": 1, + "height": 1, + "lt": 10, + "rt": 10, + "lb": 10, + "rb": 10, + "percent": true, + "lineWidth": 1, + "fillColor": "#FFFFFF" + } + ], + "_$child": [ + { + "_$id": "7i8eqgi1", + "_$type": "Sprite", + "name": "Sprite", + "x": 16, + "y": 12, + "width": 32, + "height": 32, + "_gcmds": [ + { + "_$type": "DrawPolyCmd", + "x": 0, + "y": 0, + "points": [ + 0, + 0, + 32, + 16, + 0, + 32 + ], + "lineWidth": 1, + "fillColor": "rgba(29, 92, 220, 1)" + } + ] + } + ] + } + ] + }, + { + "_$id": "y7aniaui", + "_$type": "Sprite", + "name": "btn_restart", + "y": 204, + "width": 744, + "height": 156, + "alpha": 0.18, + "_gcmds": [ + { + "_$type": "DrawRoundRectCmd", + "x": 0, + "y": 0, + "width": 1, + "height": 1, + "lt": 20, + "rt": 20, + "lb": 20, + "rb": 20, + "percent": true, + "lineWidth": 1, + "fillColor": "rgba(29, 92, 220, 1)" + } + ] + }, + { + "_$id": "eepsaznr", + "_$type": "Sprite", + "name": "btn_new", + "y": 408, + "width": 744, + "height": 156, + "alpha": 0.18, + "_gcmds": [ + { + "_$type": "DrawRoundRectCmd", + "x": 0, + "y": 0, + "width": 1, + "height": 1, + "lt": 20, + "rt": 20, + "lb": 20, + "rb": 20, + "percent": true, + "lineWidth": 1, + "fillColor": "rgba(29, 92, 220, 1)" + } + ] + } + ] + }, + { + "_$id": "17a1hg41", + "_$type": "VBox", + "name": "labels", + "x": 72, + "y": 420, + "width": 744, + "height": 600, + "centerX": 0, + "centerY": 180, + "space": 48, + "align": "center", + "_$child": [ + { + "_$id": "4fv9wmfi", + "_$type": "Label", + "name": "Label(1)", + "width": 744, + "height": 156, + "text": "Second Chace", + "fontSize": 72, + "color": "rgba(255, 255, 255, 1)", + "bold": true, + "align": "center", + "valign": "middle", + "padding": "0,0,0,0" + }, + { + "_$id": "o6codx3p", + "_$type": "Label", + "name": "Label(2)", + "y": 204, + "width": 744, + "height": 156, + "text": "Restart", + "fontSize": 72, + "color": "rgba(29, 92, 220, 1)", + "bold": true, + "align": "center", + "valign": "middle", + "padding": "0,0,0,0" + }, + { + "_$id": "2r6iotpo", + "_$type": "Label", + "name": "Label(3)", + "y": 408, + "width": 744, + "height": 156, + "text": "New Game", + "fontSize": 72, + "color": "rgba(29, 92, 220, 1)", + "bold": true, + "align": "center", + "valign": "middle", + "padding": "0,0,0,0" + } + ] + } + ] }, { "_$id": "jkqju6ok", "_$type": "Label", "name": "Label", "x": 172, - "y": 510, + "y": 511, "width": 736, "height": 100, "centerX": 0, - "centerY": -400, - "text": "游戏结束", - "font": "", + "centerY": -399, + "text": "Game Over", "fontSize": 80, "color": "rgba(0, 0, 0, 1)", "bold": true, @@ -94,32 +288,19 @@ "_$id": "39t7ygbe", "_$type": "Label", "name": "Label(1)", - "x": 172, - "y": 667, - "width": 736, + "x": 186, + "y": 644, + "width": 709, "height": 300, "centerX": 0, - "centerY": -143, - "text": "你犯了3个错误所以你输掉了游戏", - "font": "", - "fontSize": 60, + "centerY": -166, + "text": "You lost the game because you made 3 mistakes", + "fontSize": 50, "color": "rgba(123, 129, 140, 1)", "align": "center", "valign": "top", "wordWrap": true, "padding": "0,0,0,0" } - ], - "_$comp": [ - { - "_$type": "72a5ccd3-8177-451c-b2ce-cf1b42f7e6d4", - "scriptPath": "../src/views/GameOver.ts", - "btn_restart": { - "_$ref": "sco68sr2" - }, - "btn_new": { - "_$ref": "p8wk9udp" - } - } ] } \ No newline at end of file diff --git a/src/models/EventManager.ts b/src/models/EventManager.ts new file mode 100644 index 0000000..f85f6cb --- /dev/null +++ b/src/models/EventManager.ts @@ -0,0 +1,50 @@ + + + +export const EVENT_TYPES = { + NOT_UPDATE_MISTAKE: "NOT_UPDATE_MISTAKE",//更新分数 +} + + +export class EventManager { + + private static instance: EventManager + public static getInstance(): EventManager { + if (!EventManager.instance) { + EventManager.instance = new EventManager() + } + return EventManager.instance + } + + private listeners: Map> = new Map() + + public RegisterEvent(eventID: string, func: any): void { + if (this.listeners.has(eventID) == false) { + this.listeners.set(eventID, new Array()) + } + var list: Array = this.listeners.get(eventID) + var find = list.indexOf(func) + if (find < 0) { + list.push(func) + } + } + + public UnregisterEvent(eventID: string, func: any): void { + if (this.listeners.has(eventID)) { + var list: Array = this.listeners.get(eventID) + var find = list.indexOf(func) + if (find >= 0) { + list.splice(find, 1) + } + } + } + + public DispatchEvent(eventID: string, arg: any): void { + if (this.listeners.has(eventID)) { + var list: Array = this.listeners.get(eventID) + for (var i=0; i { + var doStage = StorageManager.getInstance().loadStage(config.DEFAULT_STAGE_ID) + doStage.set_mistake(doStage.get_mistake()-1) + EventManager.getInstance().DispatchEvent(EVENT_TYPES.NOT_UPDATE_MISTAKE, "adsf") + this.owner.destroy() + }) + this.btn_restart.on(Laya.Event.CLICK, this, (evt: Laya.Event) => { var doStage = StorageManager.getInstance().loadStage(config.DEFAULT_STAGE_ID) var difficulty = doStage.get_difficulty() diff --git a/src/views/Stage.ts b/src/views/Stage.ts index f43f965..b7db43c 100644 --- a/src/views/Stage.ts +++ b/src/views/Stage.ts @@ -6,6 +6,7 @@ import { DOStage } from "../models/DOStage"; import { config } from "../constants/config"; import { StorageManager } from "../models/StorageManager"; import { Utility_ConvertSecondToString } from "../utils/utility"; +import { EVENT_TYPES, EventManager } from "../models/EventManager"; const { regClass, property } = Laya; @@ -54,9 +55,9 @@ export class Stage extends Laya.Script { private data: DOStage; private selectedBlock: Block; - - //组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次 + private static _instance: Stage onAwake(): void { + Stage._instance = this this.btn_back.on(Laya.Event.CLICK, this, ()=>{ UIManager.getInstance().loadHomeUI(); UIManager.getInstance().closeStageUI(); @@ -97,6 +98,18 @@ export class Stage extends Laya.Script { this.btn_hint_normal = this.btn_hint.getChildByName("Image").getChildByName("normal") as Laya.Box this.btn_hint_ad = this.btn_hint.getChildByName("Image").getChildByName("ad") as Laya.Box this.btn_hint_label = this.btn_hint.getChildByName("Image").getChildByName("normal").getChildByName("label") as Laya.Label + + EventManager.getInstance().RegisterEvent(EVENT_TYPES.NOT_UPDATE_MISTAKE, Stage.EventUpdateMistak) + } + + onDestroy(): void { + EventManager.getInstance().UnregisterEvent(EVENT_TYPES.NOT_UPDATE_MISTAKE, Stage.EventUpdateMistak) + } + + public static EventUpdateMistak(arg: any): void { + if (Stage._instance) { + Stage._instance.onUpdateMistake() + } } public onLoadStage(doStage: DOStage): void {