mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-30 07:41:29 +08:00
fix(SS) 修复会重复采集同一物品的问题
This commit is contained in:
1 parent
5a595d72e1
commit
6b07a4a096
1 file changed
+57
-34
@@ -54,6 +54,7 @@ export default class BreatheLife extends WorldLand {
|
||||
this.spriteTextureCount = 0
|
||||
this.spriteAnimCount = 0
|
||||
this.targetCount = 3 // 目标角色数量
|
||||
this.smallGrassObjects = [] // 维护小草集合
|
||||
}
|
||||
initInput() {
|
||||
// 移除父类的输入事件监听器,避免重复监听
|
||||
@@ -65,10 +66,17 @@ export default class BreatheLife extends WorldLand {
|
||||
const { x, y } = this.input.activePointer
|
||||
this.handleClick(x, y)
|
||||
})
|
||||
|
||||
// 监听自定义收集完成事件,当所有小草都被收集时触发
|
||||
this.events.on('collectAllSmallGrass', () => {
|
||||
this.scene.start(this.nextScene)
|
||||
})
|
||||
}
|
||||
removeListener() {
|
||||
// 移除鼠标点击事件监听器
|
||||
this.input.off('pointerdown');
|
||||
// 移除自定义收集完成事件监听器
|
||||
this.events.off('collectAllSmallGrass')
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,14 +154,6 @@ export default class BreatheLife extends WorldLand {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建角色动画
|
||||
// sprite.anims.create({
|
||||
// key: 'player1-0',
|
||||
// frames: sprite.anims.generateFrameNumbers('player1-0', { start: 0, end: 7 }),
|
||||
// frameRate: 10,
|
||||
// repeat: -1
|
||||
// })
|
||||
|
||||
// 创建移动动画
|
||||
this.anims.create({
|
||||
key: 'player-move',
|
||||
@@ -171,55 +171,78 @@ export default class BreatheLife extends WorldLand {
|
||||
|
||||
// 重写创建小草方法,移除条件判断和计数器
|
||||
createSmallGrass(x, y) {
|
||||
// 创建瓦片图实例
|
||||
const tile = this.add.image(x, y, 'smallGrass');
|
||||
tile.setDepth(this.mapDepth); // 设置深度,确保在UI层级之下
|
||||
tile.setScrollFactor(0); // 固定瓦片,不随相机移动
|
||||
// 创建
|
||||
const smallGrass = this.add.image(x, y, 'smallGrass');
|
||||
smallGrass.setDepth(this.mapDepth); // 设置深度,确保在UI层级之下
|
||||
smallGrass.setScrollFactor(0); // 固定,不随相机移动
|
||||
return smallGrass
|
||||
}
|
||||
|
||||
// 新增方法:随机添加50个小草
|
||||
addRandomSmallGrass() {
|
||||
for (let i = 0; i < 50; i++) {
|
||||
// 屏幕范围内随机生成小草位置
|
||||
const x = Phaser.Math.Between(0, this.cameras.main.width)
|
||||
const y = Phaser.Math.Between(0, this.cameras.main.height)
|
||||
this.createSmallGrass(x, y)
|
||||
const smallGrass = this.createSmallGrass(x, y)
|
||||
// 保存小草对象到数组
|
||||
this.smallGrassObjects.push(smallGrass)
|
||||
}
|
||||
}
|
||||
searchAndAddSmallGrass(clickedSprite, collectedGrass = 0) {
|
||||
const smallGrassTiles = this.children.getChildren().filter(child =>
|
||||
child instanceof Phaser.GameObjects.Image && child.texture.key === 'smallGrass'
|
||||
)
|
||||
|
||||
if (smallGrassTiles.length === 0) {
|
||||
/**
|
||||
* 搜索并添加小草
|
||||
* @param {Phaser.GameObjects.Sprite} clickedSprite - 点击的角色精灵
|
||||
*/
|
||||
searchAndAddSmallGrass(clickedSprite) {
|
||||
// 搜索所有小草
|
||||
// const smallGrass = this.children.getChildren().filter(child =>
|
||||
// child instanceof Phaser.GameObjects.Image && child.texture.key === 'smallGrass'
|
||||
// )
|
||||
const smallGrass = this.smallGrassObjects
|
||||
// 过滤未标记的小草
|
||||
const unmarkedGrass = smallGrass.filter(grass => grass.alpha === 1)
|
||||
|
||||
// 如果没有小草,直接返回
|
||||
if (smallGrass.length === 0) {
|
||||
// 所有小草都已收集,触发事件
|
||||
this.events.emit('collectAllSmallGrass')
|
||||
return
|
||||
}
|
||||
|
||||
let nearestGrass = null
|
||||
let minDistance = Infinity
|
||||
|
||||
for (const grass of smallGrassTiles) {
|
||||
// 查找最近的小草
|
||||
let nearestGrass = null // 最近的小草
|
||||
let minDistance = Infinity // 最近距离,初始为无穷大
|
||||
// 遍历所有小草,找到最近的一个
|
||||
for (const grass of unmarkedGrass) {
|
||||
// 计算当前小草与角色的距离
|
||||
const distance = Phaser.Math.Distance.Between(
|
||||
clickedSprite.x, clickedSprite.y,
|
||||
grass.x, grass.y
|
||||
)
|
||||
|
||||
// 如果当前小草距离更近,更新最近的小草
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance
|
||||
nearestGrass = grass
|
||||
}
|
||||
}
|
||||
|
||||
// 如果找到最近的小草,移动角色到小草位置
|
||||
if (nearestGrass) {
|
||||
// 标记小草为已收集
|
||||
nearestGrass.setAlpha(0.5)
|
||||
// 角色移动到小草位置
|
||||
this.tweens.add({
|
||||
targets: clickedSprite,
|
||||
x: nearestGrass.x,
|
||||
y: nearestGrass.y,
|
||||
duration: 1000,
|
||||
ease: 'Power2',
|
||||
targets: clickedSprite, // 角色精灵
|
||||
x: nearestGrass.x, // 目标小草位置x
|
||||
y: nearestGrass.y, // 目标小草位置y
|
||||
duration: 1000, // 移动时间1秒
|
||||
ease: 'Power2', // 移动缓动效果
|
||||
onComplete: () => {
|
||||
nearestGrass.destroy()
|
||||
this.sound.play('coin-a')
|
||||
this.searchAndAddSmallGrass(clickedSprite, collectedGrass + 1)
|
||||
// 从数组中移除已收集的小草
|
||||
this.smallGrassObjects = this.smallGrassObjects.filter(
|
||||
obj => obj !== nearestGrass
|
||||
)
|
||||
nearestGrass.destroy() // 移动完成后销毁小草
|
||||
this.sound.play('coin-a') // 播放收集小草音效
|
||||
this.searchAndAddSmallGrass(clickedSprite) // 递归调用,继续搜索下一个小草
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user