feat(SS) 添加游戏时间

This commit is contained in:
chicrobot committed 2026-02-04 18:46:11 +08:00
1 parent ad84198db5
commit 2616dc21f1
5 files changed
+427 -3

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
/**
* WorldLand 场景类
*
* 初始引导场景,显示空白背景并在用户交互或自动延迟后
* 执行过渡动画切换到下一个场景
*/
import GameTimeManager from '../../systems/GameTimeManager.js'
export default class WorldLand extends Phaser.Scene {
/**
* 构造函数
* 初始化场景并设置场景键名为 'WorldLand'
*/
constructor() {
super('WorldLand')
this.nextScene = 'BreatheLife'
this.timeManager = new GameTimeManager() // 创建游戏时间管理器实例
this.UIDepth = 100 // UI层级深度,确保在其他元素之上
this.maskDepth = 99 // 黑夜遮罩层级,低于UI层级
}
create() {
// 设置场景背景颜色为深灰色
this.cameras.main.setBackgroundColor('#999999')
// 监听鼠标点击事件,点击后执行场景过渡动画
this.input.on('pointerdown', () => {
this.scene.start(this.nextScene);
})
// 监听键盘按键事件,按键后执行场景过渡动画
this.input.keyboard.on('keydown', () => {
this.scene.start(this.nextScene);
})
this.initDayNight(this.toGameTime('1999-12-30 23:57:00')) // 初始化时间,白天黑夜状态
this.initFrameRateText() // 初始化帧率显示文本
}
/**
* 更新游戏时间
* 每帧调用,刷新游戏时间
*/
update() {
this.showFrameRate() // 显示当前帧率
this.updateGameTime() // 更新游戏时间
this.displayTime() // 显示当前游戏时间
this.updateDayNight() // 更新昼夜状态
}
initFrameRateText() {
// 创建帧率显示文本
this.frameRateText = this.add.text(
20, 60, '', {
fontSize: '20px',
fill: '#ffffff',
fontFamily: 'Arial'
}
).setScrollFactor(0).setDepth(this.UIDepth) // 固定文本,不随相机移动,UI层级
}
showFrameRate() {
// 获取当前帧率
const frameRate = this.game.loop.actualFps
// 更新帧率显示文本
this.frameRateText.setText(`Frame Rate: ${frameRate.toFixed(2)}`)
}
/**
* 初始化昼夜状态
* 设置初始为白天,添加事件监听切换状态
*/
initDayNight(gameTime) {
this.timeManager.init(gameTime) // 初始化时间管理器
// 创建时间显示文本
this.timeText = this.add.text(
20, 20, '', {
fontSize: '20px',
fill: '#ffffff',
fontFamily: 'Arial'
}
).setScrollFactor(0).setDepth(this.UIDepth) // 固定文本,不随相机移动,UI层级
// 添加黑夜遮罩
this.nightMask = this.add.rectangle(
0, 0,
this.cameras.main.width,
this.cameras.main.height,
0x000000
).setOrigin(0, 0).setScrollFactor(0).setDepth(this.maskDepth) // 固定遮罩,不随相机移动,遮罩层级
this.nightMask.alpha = 0 // 初始设置为透明
}
/**
* 更新游戏时间
* 每帧调用,基于现实时间流逝更新游戏时间
*/
updateGameTime() {
this.timeManager.updateGameTime() // 使用时间管理器更新游戏时间
}
/**
* 获取游戏中的日期
* 根据游戏时间戳计算游戏年月日时分秒,格式为YYYY-MM-DD HH:mm:ss
*/
getGameDate() {
return this.timeManager.getGameDate() // 使用时间管理器获取游戏日期
}
/**
* 转换游戏时间字符串为游戏时间戳
* 接收时间字符串,格式为YYYY-MM-DD HH:mm:ss,返回游戏时间戳(单位:秒)
*/
toGameTime(gameTimeString) {
return this.timeManager.toGameTime(gameTimeString) // 使用时间管理器转换游戏时间
}
/**
* 显示当前时间
* 每帧调用,更新显示时间
*/
displayTime() {
const timeString = this.getGameDate().dateString
const day = this.timeManager.getDayNightState() ? '白天' : '黑夜'
// 更新显示时间
this.timeText.setText(`${day} ${timeString}`)
}
/**
* 更新昼夜状态
* 每帧调用,根据游戏时间切换白天黑夜状态
*/
updateDayNight() {
const isDay = this.timeManager.updateDayNight()
// 根据昼夜状态更新遮罩
if (this.nightMask) {
this.nightMask.alpha = isDay ? 0 : 0.5
}
}
}