refactor(SS) 重构故事场景,采用继承方式维护场景

This commit is contained in:
chicrobot committed 2026-02-06 20:31:05 +08:00
1 parent d3669bd44e
commit 48960d2a0c
3 files changed
+114 -102

No files matched your search

+51 -39
View File
@@ -7,43 +7,76 @@
import WorldTime from './WorldTime.js'
export default class WorldLand extends WorldTime {
/**
* 构造函数
* 初始化场景并设置场景键名
* @param {string} sceneKey - 场景键名
*/
constructor(sceneKey = 'WorldLand') {
// 场景相关
super(sceneKey)
this.nextScene = 'BreatheLife'
this.UIDepth = 100 // UI层级深度,确保在其他元素之上
this.maskDepth = 99 // 黑夜遮罩层级,低于UI层级
this.mapDepth = 98 // 地图层级,确保在UI层级之下
// 层级相关
this.mapDepth = 0 // 地图层级
// 地图相关
this.tileCount = 234 // 瓦片总数,从tile_0000到tile_0233
this.tileSize = 16 // 瓦片大小
this.mapWidth = 18; // 瓦片图行数
this.mapHeight = 13; // 瓦片图列数
}
preload() {
// 加载草地瓦片图
this.load.image(`grass`, `assets/PNG/Tiles/Tiles/tile_0024.png`)
this.load.image(`smallGrass`, `assets/PNG/Tiles/Tiles/tile_0044.png`)
}
create() {
// 设置场景背景颜色为深灰色
this.cameras.main.setBackgroundColor('#999999')
// 初始化计数器,用于跟踪创建的小草数量
this.smallGrassCount = 0
this.initInput() // 初始化输入事件
this.initDayNight(this.toGameTime('2000-01-01 17:57:00')) // 初始化时间,白天黑夜状态
create() {
super.create()
// 初始化场景切换条件
this.initSceneTransition()
// 覆盖输入
this.initInput()
this.createTilemap() // 创建瓦片地图
this.initFrameRateText() // 初始化帧率显示文本
}
initSceneTransition() {
// 初始化计数器,用于跟踪创建的小草数量
this.smallGrassCount = 0
}
initInput() {
// 移除父类的输入事件监听器,避免重复监听
super.removeListener()
// 监听鼠标点击事件,点击后创建一个新的小草
this.input.on('pointerdown', () => {
// 获取点击位置的坐标
const { x, y } = this.input.activePointer
this.createTile(x, y)
})
// 监听键盘按键事件,按键后执行场景过渡动画
this.input.keyboard.on('keydown', () => {
this.scene.start(this.nextScene);
})
}
removeListener() {
// 移除鼠标点击事件监听器
this.input.off('pointerdown');
// 移除键盘按键事件监听器
this.input.keyboard.off('keydown');
}
update() {
this.showFrameRate() // 显示当前帧率
this.updateGameTime() // 更新游戏时间
this.displayTime() // 显示当前游戏时间
this.updateDayNight() // 更新昼夜状态
}
createTilemap() {
// 获取当前屏幕大小
const screenWidth = this.cameras.main.width;
@@ -69,29 +102,8 @@ export default class WorldLand extends WorldTime {
console.log(`草地铺满屏幕完成,使用了 ${tilesX}x${tilesY} 个瓦片`)
}
update() {
this.showFrameRate() // 显示当前帧率
this.updateGameTime() // 更新游戏时间
this.displayTime() // 显示当前游戏时间
this.updateDayNight() // 更新昼夜状态
}
initInput() {
// 监听鼠标点击事件,点击后创建一个新的小草
this.input.on('pointerdown', () => {
// 获取点击位置的坐标
const { x, y } = this.input.activePointer
this.createTile(x, y)
})
// 监听键盘按键事件,按键后执行场景过渡动画
this.input.keyboard.on('keydown', () => {
this.scene.start(this.nextScene);
})
}
createTile(x, y) {
if (this.smallGrassCount >= 10) {
if (this.smallGrassCount >= 5) {
this.scene.start(this.nextScene);
}
// 创建瓦片图实例