8127c787f8
Interval dropped from 1.0s +- 0.6 to 0.4s +- 0.25 (about 2.5 bullets per second instead of 1), which measured 13 drops in 6 seconds versus 6 before. bullet.gd gained stop_on_obstacle (default false): the probe raycast is disabled, so rain bullets fall straight through terrain and ropes and are only recycled by their lifetime or by dropping past despawn_y. Verified a bullet falling from y=190 goes through the floor at y=225 and despawns at y=340. Also picks up the editor changes: the rope tiles were removed from level 3 and a few nodes gained unique ids.
65 lines
1.9 KiB
GDScript
65 lines
1.9 KiB
GDScript
extends Node2D
|
||
|
||
## 子弹雨:从天上随机横坐标往下落子弹,密度可以调。
|
||
##
|
||
## 落下来的子弹**不会被任何东西挡住**(bullet 的 stop_on_obstacle 默认关掉),
|
||
## 会一直穿到关卡底下才回收。
|
||
|
||
const BULLET := preload("res://scenes/prefabs/enemies/bullet.tscn")
|
||
|
||
@export var enabled: bool = true
|
||
## 平均多少秒落一颗(数值越大越稀疏)
|
||
@export var interval: float = 0.4
|
||
## 间隔随机浮动范围(0 = 完全固定节奏)
|
||
@export var interval_jitter: float = 0.25
|
||
## 开场后第一次落下的延迟
|
||
@export var start_delay: float = 1.0
|
||
## 生成高度(相对自己的 y,负数 = 从屏幕上方外面)
|
||
@export var spawn_y: float = -30.0
|
||
## 水平撒落范围:以自己的 x 为中心,左右各一半
|
||
@export var span: float = 468.0
|
||
## 下落速度
|
||
@export var fall_speed: float = 70.0
|
||
@export var damage: int = 1
|
||
@export var bullet_color: Color = Color(0.55, 0.8, 1.0)
|
||
## 固定随机种子(0 = 每次运行都不同)
|
||
@export var random_seed: int = 0
|
||
|
||
var _timer: float = 0.0
|
||
var _rng := RandomNumberGenerator.new()
|
||
var dropped: int = 0
|
||
|
||
|
||
func _ready() -> void:
|
||
add_to_group("bullet_rain")
|
||
if random_seed != 0:
|
||
_rng.seed = random_seed
|
||
else:
|
||
_rng.randomize()
|
||
_timer = start_delay
|
||
|
||
|
||
func _physics_process(delta: float) -> void:
|
||
if not enabled:
|
||
return
|
||
_timer -= delta
|
||
if _timer > 0.0:
|
||
return
|
||
_timer = maxf(interval + _rng.randf_range(-interval_jitter, interval_jitter), 0.05)
|
||
drop()
|
||
|
||
|
||
func drop() -> void:
|
||
var bullet := BULLET.instantiate()
|
||
# 这几个属性必须在 add_child 之前设好
|
||
bullet.bullet_color = bullet_color
|
||
bullet.speed = fall_speed
|
||
bullet.damage = damage
|
||
bullet.stop_on_obstacle = false
|
||
get_parent().add_child(bullet)
|
||
bullet.global_position = Vector2(
|
||
global_position.x + _rng.randf_range(-span * 0.5, span * 0.5),
|
||
global_position.y + spawn_y)
|
||
bullet.launch(Vector2(0.0, 1.0), fall_speed)
|
||
dropped += 1
|