Side-Scrolling-Shooter/Project/Scripts/bullet.gd
2025-04-15 11:58:44 -07:00

40 lines
1.3 KiB
GDScript

extends RigidBody2D
var Speed = 1200
var DespawnTimer: float = 5
var timer: float = 0
@export var EnemyBullet: bool = false
# rotato is here to fix a fucking bug. Thanks engine!
@export var rotato: float
var Direction: Vector2 = Vector2(0, 0)
func _integrate_forces(_state: PhysicsDirectBodyState2D):
self.linear_velocity = Direction * Speed
self.rotation_degrees = rotato
# Direction using trig
Direction = Vector2(cos(deg_to_rad(rotato)), sin(deg_to_rad(rotato)))
# Flip it correctly when rotated
if rotato > 90 and rotato <= 270:
get_node("Sprite2D").flip_v = true
func _physics_process(delta: float):
# Despawn to stop game from lagging
timer += delta
if timer >= DespawnTimer:
self.queue_free()
func _on_body_entered(body: Node) -> void:
# Bullets collide into each other sometimes. Remove it by putting bullets into a group.
if !body.is_in_group("Bullet") and EnemyBullet != true and body.is_in_group("Enemy"):
body.set("Health", body.get("Health") - 1)
get_node("AudioStreamPlayer").play()
DespawnTimer = timer + 1
# Don't disappear the bullet, the Audio needs to play!
self.collision_layer = 0b0000
self.collision_mask = 0b0000
self.visible = false
# for the player
elif !body.is_in_group("Bullet") and EnemyBullet == true and body.is_in_group("Player"):
body.TakeDamage()