mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 19:51:29 +08:00
119 lines
4.2 KiB
JavaScript
119 lines
4.2 KiB
JavaScript
/**
|
|
* WorldLand 场景类
|
|
*
|
|
* 初始引导场景,显示空白背景并在用户交互或自动延迟后
|
|
* 执行过渡动画切换到下一个场景
|
|
*/
|
|
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.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')) // 初始化时间,白天黑夜状态
|
|
|
|
this.createTilemap() // 创建瓦片地图
|
|
|
|
this.initFrameRateText() // 初始化帧率显示文本
|
|
}
|
|
|
|
createTilemap() {
|
|
// 获取当前屏幕大小
|
|
const screenWidth = this.cameras.main.width;
|
|
const screenHeight = this.cameras.main.height;
|
|
|
|
// 计算需要的瓦片数量
|
|
const tilesX = Math.ceil(screenWidth / this.tileSize);
|
|
const tilesY = Math.ceil(screenHeight / this.tileSize);
|
|
|
|
// 草地铺满屏幕
|
|
for (let y = 0; y < tilesY; y++) {
|
|
for (let x = 0; x < tilesX; x++) {
|
|
// 使用草地瓦片
|
|
const tile = this.add.image(
|
|
x * this.tileSize + this.tileSize / 2,
|
|
y * this.tileSize + this.tileSize / 2,
|
|
'grass'
|
|
);
|
|
tile.setDepth(this.mapDepth); // 设置深度,确保在UI层级之下
|
|
}
|
|
}
|
|
|
|
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) {
|
|
this.scene.start(this.nextScene);
|
|
}
|
|
// 创建瓦片图实例
|
|
const tile = this.add.image(x, y, 'smallGrass');
|
|
tile.setDepth(this.mapDepth); // 设置深度,确保在UI层级之下
|
|
tile.setScrollFactor(0); // 固定瓦片,不随相机移动
|
|
// 增加小草计数器
|
|
this.smallGrassCount++;
|
|
}
|
|
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)}`) // 更新帧率显示文本
|
|
}
|
|
}
|