mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 19:41:29 +08:00
121 lines
3.7 KiB
JavaScript
121 lines
3.7 KiB
JavaScript
/**
|
|
* Control 场景类
|
|
*
|
|
* 允许用户控制角色,NPC 自动搜索并收集小草
|
|
*/
|
|
import BreatheLife from './BreatheLife.js'
|
|
import Unit from '../objects/Unit.js'
|
|
import SearchBehavior from '../behaviors/SearchBehavior.js'
|
|
export default class Control extends BreatheLife {
|
|
constructor(sceneKey = 'Control') {
|
|
// 场景相关
|
|
super(sceneKey)
|
|
this.nextScene = 'InfoBar'
|
|
}
|
|
preload() {
|
|
super.preload()
|
|
}
|
|
create() {
|
|
super.create()
|
|
|
|
// 设置 Control 场景的目标角色数量
|
|
this.targetCount = 2 // 目标角色数量
|
|
|
|
// 初始化输入事件
|
|
this.initInput()
|
|
|
|
// 添加随机NPC
|
|
this.addRandomNPCs()
|
|
}
|
|
|
|
update() {
|
|
super.update()
|
|
|
|
// 更新所有 NPC 的行为
|
|
this.NPCObjects.forEach(npc => {
|
|
if (npc.update) {
|
|
npc.update()
|
|
}
|
|
})
|
|
}
|
|
|
|
// Control 场景不再需要覆盖 initSceneTransition 方法
|
|
// 数组初始化由父类 BreatheLife.create() 中的 initSceneTransition() 完成
|
|
// targetCount 通过在 create() 中直接设置来覆盖
|
|
initInput() {
|
|
// 移除父类的输入事件监听器,避免重复监听
|
|
super.removeListener()
|
|
|
|
// 监听鼠标点击事件
|
|
this.input.on('pointerdown', () => {
|
|
// 获取点击位置的坐标
|
|
const { x, y } = this.input.activePointer
|
|
this.handleClick(x, y)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 处理点击事件
|
|
* @param {number} x - 点击的 x 坐标
|
|
* @param {number} y - 点击的 y 坐标
|
|
*/
|
|
handleClick(x, y) {
|
|
// 获取点击位置的精灵
|
|
const clickedSprite = this.getClickedSprite(x, y)
|
|
console.log("点击到的精灵:", clickedSprite)
|
|
if (clickedSprite && this.NPCObjects.includes(clickedSprite)) { // 如果点击到了角色
|
|
this.handleSpriteClick(clickedSprite)
|
|
}else if (this.NPCObjects.length < this.targetCount) {
|
|
// 如果角色数量不足目标数量,创建新的角色
|
|
const sprite = this.physics.add.sprite(x, y, 'player').setScale(1).setOrigin(0.5, 1)
|
|
sprite.setDepth(this.SpriteDepth) // 设置角色层级
|
|
this.NPCObjects.push(sprite) // 加入角色集合
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建NPC
|
|
* @param {number} x - NPC的 x 坐标
|
|
* @param {number} y - NPC的 y 坐标
|
|
*/
|
|
createNPC(x, y) {
|
|
// 使用 Unit 类创建 NPC
|
|
const npc = new Unit(this, x, y, 'player1-0')
|
|
npc.setType('npc')
|
|
npc.setDepth(this.SpriteDepth) // 设置NPC渲染层级
|
|
|
|
// 为 NPC 添加搜索行为
|
|
const searchBehavior = new SearchBehavior(npc, this.smallGrassObjects, {
|
|
searchDelay: 500,
|
|
moveSpeed: 200,
|
|
onCollect: (grass) => {
|
|
this.sound.play('coin-a')
|
|
},
|
|
onComplete: () => {
|
|
console.log('NPC 收集完所有小草')
|
|
}
|
|
})
|
|
npc.setBehavior(searchBehavior)
|
|
|
|
// 添加到NPC数组
|
|
this.NPCObjects.push(npc)
|
|
}
|
|
|
|
/**
|
|
* 添加随机NPC
|
|
*/
|
|
addRandomNPCs() {
|
|
// 随机生成NPC数量
|
|
const randomCount = 5
|
|
for (let i = 0; i < randomCount; i++) {
|
|
// 随机间隔随机位置生成NPC
|
|
const interval = Phaser.Math.Between(0, 500)
|
|
this.time.delayedCall(interval, () => {
|
|
const x = Phaser.Math.Between(0, this.game.config.width)
|
|
const y = Phaser.Math.Between(0, this.game.config.height)
|
|
this.createNPC(x, y)
|
|
})
|
|
}
|
|
}
|
|
}
|