From f5db5829a21f5f68647ed1a5d8b2814004977803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=A8=E8=90=BD=E5=9F=BA=E5=9B=B4=E8=99=BE?= <3161880837@qq.com> Date: Sat, 6 Sep 2025 18:33:11 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=BF=E6=8D=A2PresetAIs?= =?UTF-8?q?=E4=B8=BAPresetBulletAI=EF=BC=8C=E4=BC=98=E5=8C=96=E5=AD=90?= =?UTF-8?q?=E5=BC=B9AI=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Presets/PresetAIs.gd => AIPresets/Bullet.gd} | 2 +- scripts/Contents/AIPresets/Entity.gd | 10 ++++++++++ scripts/Contents/Bullets/ChickLaser.gd | 2 +- scripts/Contents/Bullets/ChickSprint.gd | 2 +- scripts/Contents/Bullets/Diamond.gd | 4 ++-- scripts/Contents/Bullets/FireScan.gd | 2 +- scripts/Contents/Bullets/LGBTBullet.gd | 4 ++-- scripts/Contents/Bullets/PurpleCrystal.gd | 2 +- scripts/Contents/Bullets/Star.gd | 2 +- scripts/Contents/Bullets/VectorStar.gd | 2 +- scripts/Contents/Characters/Chick.gd | 2 +- scripts/Contents/Characters/Hen.gd | 2 +- scripts/Statemachine/BulletBase.gd | 4 ++-- scripts/Tools/MathTool.gd | 11 +++++++++++ 14 files changed, 36 insertions(+), 15 deletions(-) rename scripts/Contents/{Bullets/Presets/PresetAIs.gd => AIPresets/Bullet.gd} (94%) create mode 100644 scripts/Contents/AIPresets/Entity.gd diff --git a/scripts/Contents/Bullets/Presets/PresetAIs.gd b/scripts/Contents/AIPresets/Bullet.gd similarity index 94% rename from scripts/Contents/Bullets/Presets/PresetAIs.gd rename to scripts/Contents/AIPresets/Bullet.gd index 10cf42a..057b865 100644 --- a/scripts/Contents/Bullets/Presets/PresetAIs.gd +++ b/scripts/Contents/AIPresets/Bullet.gd @@ -1,4 +1,4 @@ -class_name PresetAIs +class_name PresetBulletAI static func lockLauncher(bullet: BulletBase, launcher: EntityBase, onTexture: bool = false): bullet.position = launcher.texture.global_position if onTexture else launcher.position diff --git a/scripts/Contents/AIPresets/Entity.gd b/scripts/Contents/AIPresets/Entity.gd new file mode 100644 index 0000000..4733ca7 --- /dev/null +++ b/scripts/Contents/AIPresets/Entity.gd @@ -0,0 +1,10 @@ +class_name PresetEntityAI + +static func follow(entity: EntityBase, target: EntityBase, minDistance: float = 100): + var delta = target.position - entity.position + var distanceOffset = abs(delta.length() - minDistance) + if distanceOffset > entity.fields[FieldStore.Entity.MOVEMENT_SPEED] * 10: + if delta.length() < minDistance: + entity.move(-delta) + else: + entity.move(delta) diff --git a/scripts/Contents/Bullets/ChickLaser.gd b/scripts/Contents/Bullets/ChickLaser.gd index 1bf862c..30feeca 100644 --- a/scripts/Contents/Bullets/ChickLaser.gd +++ b/scripts/Contents/Bullets/ChickLaser.gd @@ -5,4 +5,4 @@ func register(): penerate = 1 func ai(): rotation_degrees += 1 - PresetAIs.lockLauncher(self, launcher, true) + PresetBulletAI.lockLauncher(self, launcher, true) diff --git a/scripts/Contents/Bullets/ChickSprint.gd b/scripts/Contents/Bullets/ChickSprint.gd index e584f2f..649af3a 100644 --- a/scripts/Contents/Bullets/ChickSprint.gd +++ b/scripts/Contents/Bullets/ChickSprint.gd @@ -6,7 +6,7 @@ func register(): damage = 20 penerate = 1 func ai(): - PresetAIs.lockLauncher(self, launcher, true) + PresetBulletAI.lockLauncher(self, launcher, true) if !launcher.sprinting: tryDestroy() func destroy(beacuseMap: bool): diff --git a/scripts/Contents/Bullets/Diamond.gd b/scripts/Contents/Bullets/Diamond.gd index 420feba..93f1f4e 100644 --- a/scripts/Contents/Bullets/Diamond.gd +++ b/scripts/Contents/Bullets/Diamond.gd @@ -7,6 +7,6 @@ func register(): damage = 2 func ai(): canDamageSelf = !(timeLived() >= traceTime) - PresetAIs.forward(self, rotation) + PresetBulletAI.forward(self, rotation) if timeLived() < traceTime: - PresetAIs.trace(self, launcher.currentFocusedBoss.getTrackingAnchor(), 0.05) + PresetBulletAI.trace(self, launcher.currentFocusedBoss.getTrackingAnchor(), 0.05) diff --git a/scripts/Contents/Bullets/FireScan.gd b/scripts/Contents/Bullets/FireScan.gd index fb094ba..120c619 100644 --- a/scripts/Contents/Bullets/FireScan.gd +++ b/scripts/Contents/Bullets/FireScan.gd @@ -6,4 +6,4 @@ func register(): damage = 5 func ai(): - PresetAIs.forward(self, rotation) + PresetBulletAI.forward(self, rotation) diff --git a/scripts/Contents/Bullets/LGBTBullet.gd b/scripts/Contents/Bullets/LGBTBullet.gd index 112fe43..fe0c097 100644 --- a/scripts/Contents/Bullets/LGBTBullet.gd +++ b/scripts/Contents/Bullets/LGBTBullet.gd @@ -11,7 +11,7 @@ func ai(): speed *= 1.05 speed = clamp(speed, 0, 20) if is_instance_valid(tracer) and timeLived() < maxTraceTime: - PresetAIs.trace(self, tracer.getTrackingAnchor(), clamp(speed / 50 * tracePower, 0, 1)) - PresetAIs.forward(self, rotation) + PresetBulletAI.trace(self, tracer.getTrackingAnchor(), clamp(speed / 50 * tracePower, 0, 1)) + PresetBulletAI.forward(self, rotation) func destroy(_b): EffectController.create(preload("res://components/Effects/LGBTBoom.tscn"), position).shot() diff --git a/scripts/Contents/Bullets/PurpleCrystal.gd b/scripts/Contents/Bullets/PurpleCrystal.gd index a24f4a6..211a646 100644 --- a/scripts/Contents/Bullets/PurpleCrystal.gd +++ b/scripts/Contents/Bullets/PurpleCrystal.gd @@ -2,7 +2,7 @@ extends BulletBase class_name PurpleCrystal func ai(): - PresetAIs.forward(self, rotation) + PresetBulletAI.forward(self, rotation) func destroy(_beacuseMap: bool): var eff = EffectController.create(preload("res://components/Effects/PurpleCrystalExplosion.tscn"), global_position) eff.rotation = rotation diff --git a/scripts/Contents/Bullets/Star.gd b/scripts/Contents/Bullets/Star.gd index ceb4b6e..9b4bc4c 100644 --- a/scripts/Contents/Bullets/Star.gd +++ b/scripts/Contents/Bullets/Star.gd @@ -4,4 +4,4 @@ class_name Star func register(): damage = 5 func ai(): - PresetAIs.forward(self, rotation) + PresetBulletAI.forward(self, rotation) diff --git a/scripts/Contents/Bullets/VectorStar.gd b/scripts/Contents/Bullets/VectorStar.gd index bb0cbdf..b5cb10d 100644 --- a/scripts/Contents/Bullets/VectorStar.gd +++ b/scripts/Contents/Bullets/VectorStar.gd @@ -9,7 +9,7 @@ var rotateSpeed: float = 1 func ai(): texture.rotation_degrees += rotateSpeed rotateSpeed += 0.25 - PresetAIs.forward(self, rotation) + PresetBulletAI.forward(self, rotation) if timeLived() <= forwardTime: speed = 10 * ((forwardTime - timeLived()) / forwardTime) elif forwarded: diff --git a/scripts/Contents/Characters/Chick.gd b/scripts/Contents/Characters/Chick.gd index a474dfe..85840d0 100644 --- a/scripts/Contents/Characters/Chick.gd +++ b/scripts/Contents/Characters/Chick.gd @@ -17,7 +17,7 @@ func spawn(): texture.play("walk") func ai(): - move(currentFocusedBoss.position - position) + PresetEntityAI.follow(self, currentFocusedBoss, 0) if currentFocusedBoss.position.distance_to(position) < 200: tryAttack(2) elif currentFocusedBoss.position.distance_to(position) < 700: diff --git a/scripts/Contents/Characters/Hen.gd b/scripts/Contents/Characters/Hen.gd index 47f2ae6..fd82229 100644 --- a/scripts/Contents/Characters/Hen.gd +++ b/scripts/Contents/Characters/Hen.gd @@ -8,7 +8,7 @@ func register(): func ai(): attackCooldownMap[0] = randi_range(1500, 4000) - move(currentFocusedBoss.position - position) + PresetEntityAI.follow(self, currentFocusedBoss, 300) tryAttack(0) func attack(type): if type == 0: diff --git a/scripts/Statemachine/BulletBase.gd b/scripts/Statemachine/BulletBase.gd index 539417d..26eb64a 100644 --- a/scripts/Statemachine/BulletBase.gd +++ b/scripts/Statemachine/BulletBase.gd @@ -62,9 +62,9 @@ func _physics_process(_delta: float) -> void: var targetEntity = EntityTool.findClosetEntity(position, get_tree(), !launcher.isPlayer(), launcher.isPlayer(), - [launcher] ) + [launcher]) if is_instance_valid(targetEntity): - PresetAIs.trace( + PresetBulletAI.trace( self, targetEntity.getTrackingAnchor(), launcher.fields.get(FieldStore.Entity.BULLET_TRACE) / 10 diff --git a/scripts/Tools/MathTool.gd b/scripts/Tools/MathTool.gd index d3f0983..ecdc5ab 100644 --- a/scripts/Tools/MathTool.gd +++ b/scripts/Tools/MathTool.gd @@ -15,3 +15,14 @@ static func percent(value: float): return value / 100 static func shrimpRate(value: float): return floor(value) + int(rate(value - floor(value))) +static func getClosestIntersection(a: Vector2, b: Vector2, r: float) -> Vector2: + var ab = b - a + var distance = ab.length() + if distance < 0.00001: + return a + Vector2.RIGHT * r + var abNormalized = ab / distance + var distanceB = distance + if distanceB <= r: + return b + var intersection = a + abNormalized * r + return intersection