mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-30 00:41:29 +08:00
feat(SS) 添加AI移动逻辑
This commit is contained in:
1 parent
48960d2a0c
commit
5a595d72e1
2 files changed
+65
-5
No files matched your search
@@ -45,6 +45,8 @@ export default class BreatheLife extends WorldLand {
|
||||
// 覆盖输入
|
||||
this.initInput()
|
||||
|
||||
// 添加随机小草
|
||||
this.addRandomSmallGrass()
|
||||
}
|
||||
initSceneTransition() {
|
||||
// 初始化角色计数器,用于跟踪创建的角色数量
|
||||
@@ -79,10 +81,10 @@ export default class BreatheLife extends WorldLand {
|
||||
const clickedSprite = this.getClickedSprite(x, y)
|
||||
console.log("点击到的精灵:", clickedSprite)
|
||||
// 如果满足条件则切换到下一场景
|
||||
if (this.spriteCount >= this.targetCount && this.spriteTextureCount >= this.targetCount && this.spriteAnimCount >= this.targetCount) {
|
||||
this.scene.start(this.nextScene)
|
||||
}else if (clickedSprite) { // 如果点击到了精灵
|
||||
if (clickedSprite) { // 如果点击到了精灵
|
||||
this.handleSpriteClick(clickedSprite)
|
||||
}else if (this.spriteCount >= this.targetCount && this.spriteTextureCount >= this.targetCount && this.spriteAnimCount >= this.targetCount) {
|
||||
this.scene.start(this.nextScene)
|
||||
}else if (this.spriteCount < 3) {
|
||||
// 创建一个新的角色 sprite,缩放为1倍,并设置原点为中心底部
|
||||
const sprite = this.physics.add.sprite(x, y, 'player').setScale(1).setOrigin(0.5, 1)
|
||||
@@ -129,6 +131,8 @@ export default class BreatheLife extends WorldLand {
|
||||
this.addSpriteAnimation(clickedSprite) // 添加角色动画
|
||||
}else if (['player1-1', 'player1-2'].includes(clickedSprite.texture.key)){ // 如果点击的角色纹理已经是player0-0,则添加音效
|
||||
this.sound.play('coin-b') // 播放移动音效
|
||||
// 添加搜索逻辑,自动检测最近的小草
|
||||
this.searchAndAddSmallGrass(clickedSprite)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,4 +168,60 @@ export default class BreatheLife extends WorldLand {
|
||||
sprite.anims.play('player-move')
|
||||
this.spriteAnimCount++
|
||||
}
|
||||
|
||||
// 重写创建小草方法,移除条件判断和计数器
|
||||
createSmallGrass(x, y) {
|
||||
// 创建瓦片图实例
|
||||
const tile = this.add.image(x, y, 'smallGrass');
|
||||
tile.setDepth(this.mapDepth); // 设置深度,确保在UI层级之下
|
||||
tile.setScrollFactor(0); // 固定瓦片,不随相机移动
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
searchAndAddSmallGrass(clickedSprite, collectedGrass = 0) {
|
||||
const smallGrassTiles = this.children.getChildren().filter(child =>
|
||||
child instanceof Phaser.GameObjects.Image && child.texture.key === 'smallGrass'
|
||||
)
|
||||
|
||||
if (smallGrassTiles.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
let nearestGrass = null
|
||||
let minDistance = Infinity
|
||||
|
||||
for (const grass of smallGrassTiles) {
|
||||
const distance = Phaser.Math.Distance.Between(
|
||||
clickedSprite.x, clickedSprite.y,
|
||||
grass.x, grass.y
|
||||
)
|
||||
|
||||
if (distance < minDistance) {
|
||||
minDistance = distance
|
||||
nearestGrass = grass
|
||||
}
|
||||
}
|
||||
|
||||
if (nearestGrass) {
|
||||
this.tweens.add({
|
||||
targets: clickedSprite,
|
||||
x: nearestGrass.x,
|
||||
y: nearestGrass.y,
|
||||
duration: 1000,
|
||||
ease: 'Power2',
|
||||
onComplete: () => {
|
||||
nearestGrass.destroy()
|
||||
this.sound.play('coin-a')
|
||||
this.searchAndAddSmallGrass(clickedSprite, collectedGrass + 1)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ export default class WorldLand extends WorldTime {
|
||||
this.input.on('pointerdown', () => {
|
||||
// 获取点击位置的坐标
|
||||
const { x, y } = this.input.activePointer
|
||||
this.createTile(x, y)
|
||||
this.createSmallGrass(x, y)
|
||||
})
|
||||
|
||||
// 监听键盘按键事件,按键后执行场景过渡动画
|
||||
@@ -102,7 +102,7 @@ export default class WorldLand extends WorldTime {
|
||||
console.log(`草地铺满屏幕完成,使用了 ${tilesX}x${tilesY} 个瓦片`)
|
||||
}
|
||||
|
||||
createTile(x, y) {
|
||||
createSmallGrass(x, y) {
|
||||
if (this.smallGrassCount >= 5) {
|
||||
this.scene.start(this.nextScene);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user