Files
fps_project_1/scripts/shotgun_pellet.gd
2024-12-13 15:20:17 -06:00

65 lines
1.7 KiB
GDScript

extends RigidBody3D
@export var max_bounces : int = 10
@onready var collision_shape: CollisionShape3D = $CollisionShape3D
@onready var mesh: MeshInstance3D = $MeshInstance3D
@onready var material = mesh.get_surface_override_material(0)
var bullet_force_mod
var bullet_damage
var player_position
var bounces = 0
var start_time
var end_time
var start_velocity
const EMISSION_MAX : float = 200
const EMISSION_LIFETIME : float = 1.75 #in seconds
const VELOCITY_REQUIRED_TO_HIT : float = 10
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
visible = false
start_time = Time.get_ticks_msec()
end_time = start_time + (EMISSION_LIFETIME * 1000)
start_velocity = linear_velocity.length()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var current_time = (Time.get_ticks_msec())/end_time
material.emission_energy_multiplier = clamp(lerp(EMISSION_MAX,0.0,current_time),0,EMISSION_MAX)
var distance_from_player = abs(self.global_position - player_position)
if distance_from_player.length() > 1.5:
visible = true
func _on_body_entered(body: Node) -> void:
bounces += 1
if bounces >= max_bounces:
despawn()
if body != null and !body.is_in_group("player") and linear_velocity.length() >= VELOCITY_REQUIRED_TO_HIT:
if body.is_in_group("switch"):
body.hit(bullet_damage)
if body.has_method("hit") and !body.is_in_group("player"):
body.hit(1)
if body.is_in_group("breakable"):
var current_velocity = transform.basis * Vector3(0,0,-1 * bullet_force_mod)
body.breaking(current_velocity)
func despawn():
collision_shape.disabled = true
await get_tree().create_timer(1).timeout
self.queue_free()
func _on_timer_timeout() -> void:
despawn()