Files
hello-godot/scripts/enemy_medium.gd
T

54 lines
1.3 KiB
GDScript

class_name EnemyMedium
extends EnemyBase
## 中型轰炸机:从屏幕一侧横掠,正弦起伏,朝玩家方向瞄准射击。
const TEX := preload("res://assets/svg/enemy_medium.svg")
var side := 1 ## 1 = 从左向右,-1 = 从右向左
var base_y := 220.0
var vx := 110.0
var fire_interval := 1.6
var bullet_speed := 235.0
var _t := 0.0
var _fire_timer := 1.0
func setup(p_side: int, p_y: float, p_vx := 110.0, p_bullet_speed := 235.0, p_fire_interval := 1.6) -> void:
side = p_side
base_y = p_y
vx = p_vx
bullet_speed = p_bullet_speed
fire_interval = p_fire_interval
drops_powerup = true
position = Vector2(-70.0 if side == 1 else 610.0, base_y)
func _ready() -> void:
collision_layer = 4
collision_mask = 0
hp = 6
score_value = 400
hit_radius = 30.0
var sprite := Sprite2D.new()
sprite.texture = TEX
add_child(sprite)
var shape := CollisionShape2D.new()
var rect := RectangleShape2D.new()
rect.size = Vector2(54.0, 30.0)
shape.shape = rect
add_child(shape)
func _process(delta: float) -> void:
_t += delta
position.x += vx * float(side) * delta
position.y = base_y + sin(_t * 1.1) * 46.0
if (side == 1 and position.x > 610.0) or (side == -1 and position.x < -70.0):
queue_free()
return
_fire_timer -= delta
if _fire_timer <= 0.0:
_fire_timer = fire_interval
_fire_bullet(_aim_direction() * bullet_speed)