april-2025/scripts/bullet.gd

25 lines
671 B
GDScript

class_name Bullet extends Area2D
const BULLET_SCENE: PackedScene = preload("res://scenes/bullet.tscn")
@export var SPEED = 700
var bearing: Vector2 = Vector2(1,0)
signal bulletHit(body, bullet)
static func create(pos: Vector2, bearing: Vector2) -> Bullet:
var instance = BULLET_SCENE.instantiate()
instance.position = pos
instance.bearing = bearing
return instance
func _physics_process(delta: float) -> void:
position += bearing * SPEED * delta
if position.x < 0 || position.x > 1152:
bearing.x *= -1
if position.y < 0 || position.y > 648:
bearing.y *= -1
func _on_body_entered(body: Node2D) -> void:
if not body is Player:
bulletHit.emit(body, self)