Files
hello-godot/scripts/player_bullet.gd
T

38 lines
1.1 KiB
GDScript

class_name PlayerBullet
extends Area2D
## 玩家子弹:沿自身方向直线飞行(斜射弹共享此类),命中敌机造成伤害。
const SPEED := 760.0
const TEX := preload("res://assets/svg/bullet_player.svg")
var direction := Vector2.UP
func _ready() -> void:
collision_layer = 2
# 4 空中敌机 + 32 地面/海面单位(只被打,不与玩家机体碰撞)
collision_mask = 4 | 32
var sprite := Sprite2D.new()
sprite.texture = TEX
add_child(sprite)
var shape := CollisionShape2D.new()
var rect := RectangleShape2D.new()
rect.size = Vector2(6.0, 16.0)
shape.shape = rect
add_child(shape)
# 旋转节点本身:贴图与碰撞矩形一起对齐弹道方向
rotation = Vector2.UP.angle_to(direction)
area_entered.connect(_on_area_entered)
func _process(delta: float) -> void:
position += direction * SPEED * delta
if global_position.y < -30.0 or global_position.x < -30.0 or global_position.x > 570.0:
queue_free()
func _on_area_entered(area: Area2D) -> void:
if area is EnemyBase:
(area as EnemyBase).apply_damage(1)
queue_free()