open-fpsz/weapons/space_gun/projectile.gd
2024-04-11 17:44:13 +00:00

36 lines
1 KiB
GDScript

extends Node3D
const speed : float = 68.0 # m/s
var velocity : Vector3 = Vector3.ZERO
const lifespan : float = 5.0 # in seconds
@onready var shape_cast = $ShapeCast3D
@onready var game = get_tree().get_current_scene()
const EXPLOSION = preload("res://weapons/space_gun/projectile_explosion.tscn")
func _ready():
var lifespan_timer = Timer.new()
lifespan_timer.set_wait_time(lifespan)
lifespan_timer.set_one_shot(true)
lifespan_timer.set_autostart(true)
lifespan_timer.timeout.connect(self_destruct)
add_child(lifespan_timer)
func self_destruct():
explode(position)
func explode(spawn_location):
var spawned_explosion = EXPLOSION.instantiate()
spawned_explosion.position = spawn_location
game.add_child(spawned_explosion)
queue_free()
func _physics_process(delta):
var previous_position = global_position
global_position += velocity * delta
shape_cast.target_position = to_local(previous_position)
if shape_cast.is_colliding():
var contact_point = shape_cast.collision_result[0].point
explode(contact_point)