Files
fps_project_1/scripts/oildrum.gd

89 lines
2.6 KiB
GDScript

extends RigidBody3D
@export var hp : float = 3
@export var explosion : Resource
@export var blast_power : float = 10
@export_enum("Enemy", "Trap") var enemy_type: int
@onready var leak_audio: AudioStreamPlayer3D = $leak_audio
@onready var blast_radius_area: Area3D = $BlastRadius
@onready var radius_shape: CollisionShape3D = $BlastRadius/CollisionShape3D
var leak_hp = false
var blast_amount
const HP_LEAK_RATE = .5
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
add_to_group("persist")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if leak_hp:
hp -= delta * HP_LEAK_RATE
print("HP - ",hp)
if hp <= 0:
explode()
func save():
var save_dict = {
"filename" : get_scene_file_path(),
"parent" : get_parent().get_path(),
"pos_x" : position.x,
"pos_y" : position.y,
"pos_z" : position.z,
"rot_x" : rotation.x,
"rot_y" : rotation.y,
"rot_z" : rotation.z,
"hp" : hp,
"leak_hp" : leak_hp,
}
return save_dict
func hit(bullet_damage):
hp -= 1
leak_hp = true
leak_audio.play()
func explode():
#get all collision objects that can be moved
var bodies_in_blast = blast_radius_area.get_overlapping_bodies()
#for each object determine vector from center of blast radius
#apply linear velocity power as the inverse % of distance towards the edge (bonus points if using curve texture)
#break breakable objects
for body in bodies_in_blast:
#calculate blast power and direction
var blast_direction = (body.global_position - blast_radius_area.global_position).normalized()
var blast_amount = 1 - ((body.global_position - blast_radius_area.global_position).length() / radius_shape.shape.radius)
var blast_velocity = blast_direction * blast_power * blast_amount
#apply and/or damage
if body.get_class() == "RigidBody3D":
if body.is_in_group("breakable"):
body.breaking(blast_velocity)
else:
body.linear_velocity += blast_velocity
if body.is_in_group("player"):
body.velocity += clamp(blast_velocity,Vector3(-7,-7,-7),Vector3(7,7,7))
body.recoil.add_recoil(Vector3(1,.1,.1),10,10)
body.hit(1,self,enemy_type)
if body.has_method("hit") and !body.is_in_group("player"):
body.hit(1)
if body.is_in_group("enemy"):
body.knocked = true
body.stunned = true
print("knocked")
body.knocked_timer.start()
body.stunned_timer.start()
body.velocity += blast_velocity
#Spawn gpu particles
var explosionspawn = explosion.instantiate()
explosionspawn.position = self.global_position
explosionspawn.transform.basis = self.global_transform.basis
get_tree().get_root().add_child(explosionspawn)
queue_free()