From 2232c9148a3ac3a344ce7dd228a98dec20f6e369 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: Sun, 14 Dec 2025 15:06:22 +0800 Subject: [PATCH] =?UTF-8?q?refactor(MathTool):=20=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E9=9A=8F=E6=9C=BA=E6=95=B0=E7=9B=B8=E5=85=B3=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=BB=A5=E6=8F=90=E9=AB=98=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 randv2_range 重命名为 randomVector2In,randc_from 重命名为 randomChoiceFrom,randc_from_weights 重命名为 randChoiceWeightsFrom,并新增 randomRingPoint 方法 --- scripts/Contents/AIPresets/Entity.gd | 2 +- scripts/Contents/Bullets/MushroomPickaxe.gd | 4 ++-- scripts/Contents/Characters/Bear.gd | 12 ++++++------ scripts/Contents/Characters/Chick.gd | 2 +- scripts/Contents/Characters/KukeMC.gd | 2 +- scripts/Contents/Characters/MTY.gd | 2 +- scripts/Contents/Panels/GameOver.gd | 2 +- scripts/Contents/Wave.gd | 2 +- scripts/Contents/Weapons/WhiteSoul.gd | 2 +- scripts/Statemachine/EntityBase.gd | 16 ++++++++-------- scripts/Tools/Managers/CameraManager.gd | 2 +- scripts/Tools/MathTool.gd | 10 ++++++---- 12 files changed, 30 insertions(+), 28 deletions(-) diff --git a/scripts/Contents/AIPresets/Entity.gd b/scripts/Contents/AIPresets/Entity.gd index d459c6a..a19b116 100644 --- a/scripts/Contents/AIPresets/Entity.gd +++ b/scripts/Contents/AIPresets/Entity.gd @@ -17,5 +17,5 @@ static func distanceAction(entity: EntityBase, target: EntityBase, minDistance: if minDistance <= distance and distance <= maxDistance: action.call() static func weightAttack(entity: EntityBase, indexes: Array[int], weight: Array[int], chargeUp: Callable): - var method = MathTool.randc_from_weights(indexes, weight) + var method = MathTool.randChoiceWeightsFrom(indexes, weight) entity.tryAttack(method, chargeUp.call(method)) diff --git a/scripts/Contents/Bullets/MushroomPickaxe.gd b/scripts/Contents/Bullets/MushroomPickaxe.gd index d6a3fca..6f51963 100644 --- a/scripts/Contents/Bullets/MushroomPickaxe.gd +++ b/scripts/Contents/Bullets/MushroomPickaxe.gd @@ -8,7 +8,7 @@ func succeedToHit(_dmg, entity): if MathTool.rate(rate): for i in randi_range(1, count): ItemDropped.generate( - MathTool.randc_from([ItemStore.ItemType.BASEBALL, ItemStore.ItemType.BASKETBALL, ItemStore.ItemType.BEACHBALL]), + MathTool.randomChoiceFrom([ItemStore.ItemType.BASEBALL, ItemStore.ItemType.BASKETBALL, ItemStore.ItemType.BEACHBALL]), randi_range(1, count), - entity.position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset) + entity.position + MathTool.randomVector2In(GameRule.itemDroppedSpawnOffset) ) diff --git a/scripts/Contents/Characters/Bear.gd b/scripts/Contents/Characters/Bear.gd index 541becc..f8015aa 100644 --- a/scripts/Contents/Characters/Bear.gd +++ b/scripts/Contents/Characters/Bear.gd @@ -52,11 +52,11 @@ func attack(type): if !is_instance_valid(currentFocusedBoss): return false for bullet in BulletBase.generate(ComponentManager.getBullet("ArrowSeven"), self, findWeaponAnchor("normal"), deg_to_rad(randf_range(0, 360))): bullet.tracer = currentFocusedBoss - bullet.position += MathTool.randv2_range(50) + bullet.position += MathTool.randomVector2In(50) await TickTool.millseconds(50) return false elif type == 1: - await sprintTo(currentFocusedBoss.position - Vector2(MathTool.randc_from([-300, 300]), 300), 0.25) + await sprintTo(currentFocusedBoss.position - Vector2(MathTool.randomChoiceFrom([-300, 300]), 300), 0.25) var count = randi_range(6, 8) for i in range(count): BulletBase.generate(ComponentManager.getBullet("SunDance"), self, weaponPos, deg_to_rad(360.0 / count * i)) @@ -66,7 +66,7 @@ func attack(type): bullet.rotation = 360 / 13.0 * i elif type == 3: if !is_instance_valid(currentFocusedBoss): return false - await sprintTo(currentFocusedBoss.position - Vector2(MathTool.randc_from([500, -500]), 0), 0.25) + await sprintTo(currentFocusedBoss.position - Vector2(MathTool.randomChoiceFrom([500, -500]), 0), 0.25) sprintParticle.emitting = true canRunAi = false currentInvinsible = true @@ -76,7 +76,7 @@ func attack(type): await trySprint() sprintParticle.emitting = false canRunAi = true - await sprintTo(currentFocusedBoss.position + MathTool.randv2_range(400), 0.25) + await sprintTo(currentFocusedBoss.position + MathTool.randomVector2In(400), 0.25) currentInvinsible = false return false elif type == 4: @@ -102,8 +102,8 @@ func attack(type): for i in 16: if !is_instance_valid(currentFocusedBoss): return false for bullet in BulletBase.generate(ComponentManager.getBullet("LightGun"), self, currentFocusedBoss.position, 0): - bullet.position += MathTool.randv2_range(600) - bullet.look_at(currentFocusedBoss.position + MathTool.randv2_range(50)) + bullet.position += MathTool.randomVector2In(600) + bullet.look_at(currentFocusedBoss.position + MathTool.randomVector2In(50)) await TickTool.millseconds(100) return false elif type == 7: diff --git a/scripts/Contents/Characters/Chick.gd b/scripts/Contents/Characters/Chick.gd index 11ead35..8f9f99a 100644 --- a/scripts/Contents/Characters/Chick.gd +++ b/scripts/Contents/Characters/Chick.gd @@ -34,7 +34,7 @@ func attack(type): if type == 0: var weaponPos = findWeaponAnchor("normal") for i in randi_range(7, 16): - BulletBase.generate(ComponentManager.getBullet("Diamond"), self, weaponPos + MathTool.randv2_range(20), rotation + deg_to_rad(randf_range(-90, 90))) + BulletBase.generate(ComponentManager.getBullet("Diamond"), self, weaponPos + MathTool.randomVector2In(20), rotation + deg_to_rad(randf_range(-90, 90))) elif type == 1: var laserCount = randi_range(2, 4) for i in laserCount: diff --git a/scripts/Contents/Characters/KukeMC.gd b/scripts/Contents/Characters/KukeMC.gd index 71aa934..ed5c9d6 100644 --- a/scripts/Contents/Characters/KukeMC.gd +++ b/scripts/Contents/Characters/KukeMC.gd @@ -34,7 +34,7 @@ func attack(type): await TickTool.millseconds(randi_range(10, 50)) elif type == 1 and health < fields[FieldStore.Entity.MAX_HEALTH] * 0.5 and canSummon: for i in randi_range(1, 2): - var child = EntityBase.generate(ComponentManager.getCharacter("KukeChild"), position + MathTool.randv2_range(500)) + var child = EntityBase.generate(ComponentManager.getCharacter("KukeChild"), position + MathTool.randomVector2In(500)) child.currentFocusedBoss = currentFocusedBoss child.masterMine = self elif type == 2: diff --git a/scripts/Contents/Characters/MTY.gd b/scripts/Contents/Characters/MTY.gd index 0dc8fb5..cfbf0c0 100644 --- a/scripts/Contents/Characters/MTY.gd +++ b/scripts/Contents/Characters/MTY.gd @@ -18,5 +18,5 @@ func attack(type: int): func sprint(): var target = BulletTool.findClosetBulletCanDamage(position, get_tree(), self) if is_instance_valid(target): - var dir = (target.position - position).rotated(MathTool.randc_from([-1, 1]) * deg_to_rad(90)) + var dir = (target.position - position).rotated(MathTool.randomChoiceFrom([-1, 1]) * deg_to_rad(90)) move(dir.normalized() * sprintMultiplier, true) diff --git a/scripts/Contents/Panels/GameOver.gd b/scripts/Contents/Panels/GameOver.gd index e3f09e0..c673de4 100644 --- a/scripts/Contents/Panels/GameOver.gd +++ b/scripts/Contents/Panels/GameOver.gd @@ -6,5 +6,5 @@ extends FullscreenPanelBase func beforeOpen(args: Array = []): audio.play() - var reasonTemplate = MathTool.randc_from(GameRule.deadReasons) + var reasonTemplate = MathTool.randomChoiceFrom(GameRule.deadReasons) deadreason.text = ("[color=gray]" + reasonTemplate + "凶手是[b]%s[/b]的[b]%s[/b]。[/color]") % args diff --git a/scripts/Contents/Wave.gd b/scripts/Contents/Wave.gd index cc6b255..15a6ce1 100644 --- a/scripts/Contents/Wave.gd +++ b/scripts/Contents/Wave.gd @@ -88,7 +88,7 @@ static func spawn(center: Vector2) -> Array: var wave: Wave = data[i] for j in range(entityCountOf(wave)): var currentWave = wave.duplicate() - currentWave.entityPosition = MathTool.randv2_range(500) + center + currentWave.entityPosition = MathTool.randomRingPoint(200, 1000) + center result.append(currentWave) return result static func next(waves: Array): diff --git a/scripts/Contents/Weapons/WhiteSoul.gd b/scripts/Contents/Weapons/WhiteSoul.gd index 5d2f3fa..f453b34 100644 --- a/scripts/Contents/Weapons/WhiteSoul.gd +++ b/scripts/Contents/Weapons/WhiteSoul.gd @@ -11,7 +11,7 @@ func attack(entity: EntityBase): var myPos = get_global_mouse_position() + QuickUI.getWindowSize() * Vector2(randf_range(-1, 1) * 0.25, -1) for j in BulletBase.generate(ComponentManager.getBullet("WhiteSoul"), entity, myPos, - myPos.angle_to_point(get_global_mouse_position() + MathTool.randv2_range(readStore("radius"))) + myPos.angle_to_point(get_global_mouse_position() + MathTool.randomVector2In(readStore("radius"))) ): if j is BulletBase: j.baseDamage = readStore("atk") diff --git a/scripts/Statemachine/EntityBase.gd b/scripts/Statemachine/EntityBase.gd index 05c4b93..699d34a 100644 --- a/scripts/Statemachine/EntityBase.gd +++ b/scripts/Statemachine/EntityBase.gd @@ -171,7 +171,7 @@ func _process(_delta): statebar.levelLabel.text = str(level) func _physics_process(_delta: float) -> void: if !isPlayer() && !currentFocusedBoss: - currentFocusedBoss = MathTool.randc_from(get_tree().get_nodes_in_group("players")) + currentFocusedBoss = MathTool.randomChoiceFrom(get_tree().get_nodes_in_group("players")) animatree.set("parameters/blend_position", lerpf(animatree.get("parameters/blend_position"), lastDirection, 0.2)) if sprinting: if sprintAi(): @@ -234,7 +234,7 @@ func takeDamage(baseDamage: float, crit: bool = false, perfectMiss: bool = false var resultDamage = baseDamage + baseDamage * int(crit) * fields.get(FieldStore.Entity.CRIT_DAMAGE) health -= resultDamage healthChanged.emit(health) - DamageLabel.create(resultDamage, crit || perfectMiss, damageAnchor.global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset)) + DamageLabel.create(resultDamage, crit || perfectMiss, damageAnchor.global_position + MathTool.randomVector2In(GameRule.damageLabelSpawnOffset)) if health <= 0: tryDie(null) return resultDamage @@ -261,7 +261,7 @@ func bulletHit(bullet: BulletBase, crit: bool): hit.emit(damage, bullet, crit) health -= damage healthChanged.emit(health) - DamageLabel.create(damage, crit || perfectMiss, damageAnchor.global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset)) + DamageLabel.create(damage, crit || perfectMiss, damageAnchor.global_position + MathTool.randomVector2In(GameRule.damageLabelSpawnOffset)) if isBoss and bullet.launcher.isPlayer(): bullet.launcher.setBoss(self) if health <= 0: @@ -338,24 +338,24 @@ func tryDie(by: BulletBase = null): var item = drops[drop] var count = ceil(randf_range(dropCounts[drop].x, dropCounts[drop].y)) for i in range(count): - ItemDropped.generate(item, randi_range(1, 2 * round(sqrt(GameRule.difficulty - GameRule.difficultyRange.x + 1))), position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset)) + ItemDropped.generate(item, randi_range(1, 2 * round(sqrt(GameRule.difficulty - GameRule.difficultyRange.x + 1))), position + MathTool.randomVector2In(GameRule.itemDroppedSpawnOffset)) if MathTool.rate( GameRule.appleDropRate + by.launcher.fields.get(FieldStore.Entity.DROP_APPLE_RATE) + GameRule.appleDropRateInfluenceByLuckValue * by.launcher.fields[FieldStore.Entity.LUCK_VALUE] ) or isBoss: for i in randi_range(appleCount.x, appleCount.y): - ItemDropped.generate(ItemStore.ItemType.APPLE, 1, position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset)) + ItemDropped.generate(ItemStore.ItemType.APPLE, 1, position + MathTool.randomVector2In(GameRule.itemDroppedSpawnOffset)) ItemDropped.generate( ItemStore.ItemType.BEACHBALL, fields[FieldStore.Entity.MAX_HEALTH] * randf_range(1 - GameRule.beachballOffset, 1 + GameRule.beachballOffset), - position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset) + position + MathTool.randomVector2In(GameRule.itemDroppedSpawnOffset) ) if isBoss: ItemDropped.generate( ItemStore.ItemType.SOUL, randi_range(1, 2), - position + MathTool.randv2_range(GameRule.itemDroppedSpawnOffset) + position + MathTool.randomVector2In(GameRule.itemDroppedSpawnOffset) ) if isPlayer(): if UIState.player == self: @@ -452,7 +452,7 @@ func sprint(): pass func heal(count: float): health += count - DamageLabel.create(-count, false, damageAnchor.global_position + MathTool.randv2_range(GameRule.damageLabelSpawnOffset)) + DamageLabel.create(-count, false, damageAnchor.global_position + MathTool.randomVector2In(GameRule.damageLabelSpawnOffset)) return count func register(): pass diff --git a/scripts/Tools/Managers/CameraManager.gd b/scripts/Tools/Managers/CameraManager.gd index 07c65e1..d99a111 100644 --- a/scripts/Tools/Managers/CameraManager.gd +++ b/scripts/Tools/Managers/CameraManager.gd @@ -13,7 +13,7 @@ func _ready(): func _physics_process(_delta): if is_instance_valid(UIState.player): position = UIState.player.position - position += MathTool.randv2_range(shakeIntensity) + position += MathTool.randomVector2In(shakeIntensity) static func shake(millseconds: float, intensity: float = 10, steper: Callable = func(currentValue, _totalValue, _restPercent): return currentValue): var startTime = WorldManager.getTime() diff --git a/scripts/Tools/MathTool.gd b/scripts/Tools/MathTool.gd index dbc5219..1e3947d 100644 --- a/scripts/Tools/MathTool.gd +++ b/scripts/Tools/MathTool.gd @@ -2,21 +2,23 @@ class_name MathTool static func rate(value: float) -> bool: return randf() < value -static func randv2_range(offset: float): +static func randomVector2In(offset: float): return Vector2( randf_range(-offset, offset), randf_range(-offset, offset) ) -static func randc_from(array: Array): +static func randomRingPoint(innerRadius: float, outerRadius: float): + return Vector2.from_angle(randf_range(0, 2 * PI)) * randf_range(innerRadius, outerRadius) +static func randomChoiceFrom(array: Array): return array[randi() % array.size()] -static func randc_from_weights(indexes: Array, weights: Array[int]): +static func randChoiceWeightsFrom(indexes: Array, weights: Array[int]): var totals: Array = [] var index = 0 for i in indexes: for j in range(weights[index]): totals.append(i) index += 1 - return randc_from(totals) + return randomChoiceFrom(totals) static func signBeforeStr(value: float): return ("+" if value > 0 else "-" if value < 0 else "") + str(abs(value)) static func percent(value: float):