refactored bullet script, broke shotgun pellets which will need to be fixed

This commit is contained in:
derek
2025-03-25 16:55:22 -05:00
parent 3e3a0d2192
commit 8d0f925be6
24 changed files with 1283 additions and 122 deletions

View File

@@ -12,9 +12,9 @@ func _ready() -> void:
center = Vector2(sub_viewport.size.x/2,sub_viewport.size.y/2)
func _draw() -> void:
print("CHARACTER HEALTH : ")
var health_percentage : float = health_bar_sprite.character.health/health_bar_sprite.character.start_health
#background
#draw background
draw_line(Vector2(0,center.y),Vector2(sub_viewport.size.x,center.y),Color(.5,.5,.5,.75),MARGIN * 2,true)
#health

View File

@@ -4,75 +4,13 @@ extends Projectile
@onready var mesh = $Cylinder
@onready var particles = $GPUParticles3D
@onready var enemy_particles = $GPUParticlesEnemy
@onready var hit_indicator = $Audio/HitIndicator
@onready var ray: RayCast3D = $RayCast3D
@onready var water_leak : Resource = preload("res://assets/water_leak.tscn")
# Called when the node enters the scene tree for the first time.
func _ready():
visible = false
linear_velocity += transform.basis * Vector3(0, 0, -bullet_speed) + player_velocity
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
angular_velocity = Vector3(0,0,0)
var distance_from_player = abs(self.global_position - player_position)
if distance_from_player.length() > 2:
visible = true
if ray.is_colliding():
var body = ray.get_collider()
if body != null and !body.is_in_group("player"):
mesh.visible = false
ray.enabled = false
if body.has_method("hit"):
hit_indicator.play()
enemy_particles.emitting = true
body.hit(bullet_damage)
#bullethole effect
body.add_child(instance_bullethole)
instance_bullethole.global_transform.origin = ray.get_collision_point()
if (abs(ray.get_collision_normal().y) > 0.99):
instance_bullethole.look_at(ray.get_collision_point() + ray.get_collision_normal(), Vector3(0,0,1))
else:
instance_bullethole.look_at(ray.get_collision_point() + ray.get_collision_normal())
instance_bullethole.rotation.z = deg_to_rad(randf_range(0,360))
#range target
if body.is_in_group("range_target"):
body.add_marker(ray.get_collision_point(),ray.global_rotation)
# Leaking effect
if body.is_in_group("leak"):
var leakspawn = water_leak.instantiate()
body.add_child(leakspawn)
leakspawn.global_transform.origin = ray.get_collision_point()
if (abs(ray.get_collision_normal().y) > 0.99):
leakspawn.look_at(ray.get_collision_point() + ray.get_collision_normal(), Vector3(0,0,1))
else:
leakspawn.look_at(ray.get_collision_point() + ray.get_collision_normal())
leakspawn.rotation.z = deg_to_rad(randf_range(0,360))
if body.is_in_group("switch"):
body.hit(bullet_damage)
if body.is_in_group("breakable"):
var current_velocity = transform.basis * Vector3(0,0,-1 * bullet_force_mod)
ray.get_collider().breaking(current_velocity)
queue_free()
func _on_body_entered(body: Node) -> void:
if !body.is_in_group("player"):
await get_tree().create_timer(.1).timeout
mesh.visible = false
func despawn():
queue_free()

View File

@@ -1,4 +1,5 @@
extends Sprite3D
class_name HealthBar
@export var character : Node
@onready var health_bar: Control = $SubViewport/HealthBar

View File

@@ -2,4 +2,8 @@ extends Area3D
func _on_body_exited(body: Node3D) -> void:
print("PLAYER OUT OF BOUNDS")
get_tree().current_scene.die()
var level_control = get_tree().current_scene
if level_control.gamemode.die_on_leaving_bounds == true:
get_tree().current_scene.die()
else:
body.global_position = body.last_ground_pos + Vector3(0,10,0)

View File

@@ -36,6 +36,8 @@ var moving_fast_top_speed = 0.0
var mouse_input : Vector2
var ads : bool
var last_ground_pos
@export_group("Game Settings")
@export var AUDIO = true
@export_group("Player Movement")
@@ -473,6 +475,7 @@ func _physics_process(delta):
if GameGlobals.health <= 0:
level_control.die()
cache_ground_pos()
focus_on_target()
joypad_look()
aim_down_sights(delta)
@@ -511,6 +514,10 @@ func joypad_look():
head.rotate_x(-yAxis * JOYSTICK_SENSITIVITY * 1)
head.rotation.x = clamp(head.rotation.x, deg_to_rad(-90), deg_to_rad(85))
func cache_ground_pos():
if is_on_floor():
last_ground_pos = global_position
func punch():
if interact_ray.is_colliding():
if interact_ray.get_collider().has_method("hit"):

View File

@@ -3,6 +3,8 @@ class_name Projectile
@export var collision_raycast_3d : RayCast3D
const BULLET_HOLE = preload("res://assets/bullet_hole.tscn")
var player_position
var player_velocity
var bullet_active = true
@@ -16,13 +18,61 @@ var blast_radius
func _ready() -> void:
visible = false
func _process(delta: float) -> void:
var distance_from_player = abs(self.global_position - player_position)
func _physics_process(delta: float) -> void:
#make bullet visible when far enough from player
if !visible:
var distance_from_player = abs(self.global_position - player_position)
if distance_from_player.length() > 2.0:
visible = true
if distance_from_player.length() > 2:
visible = true
if collision_raycast_3d != null:
if collision_raycast_3d.is_colliding():
var body = collision_raycast_3d.get_collider()
damage(body)
collision_ray_length_adj()
func collision_ray():
func damage(body):
if body != null and !body.is_in_group("player"):
if collision_raycast_3d != null:
collision_raycast_3d.enabled = false
if body.has_method("hit"):
SignalBus.emit_signal("enemy_hit")
body.hit(bullet_damage)
#bullethole effect, eventually add group test for different bullet hole textures
add_normal_decal(body,BULLET_HOLE)
#mark range target
if body.is_in_group("range_target") and collision_raycast_3d != null:
body.add_marker(collision_raycast_3d.get_collision_point(),collision_raycast_3d.global_rotation)
#start leaking effect
if body.is_in_group("leak") and body.water_leak != null:
add_normal_decal(body,body.water_leak)
#break breakables
if body.is_in_group("breakable"):
var current_velocity = transform.basis * Vector3(0,0,-1 * bullet_force_mod)
collision_raycast_3d.get_collider().breaking(current_velocity)
queue_free()
func add_normal_decal(body,instance):
if collision_raycast_3d != null:
var decal_spawn = instance.instantiate()
body.add_child(decal_spawn)
decal_spawn.global_transform.origin = collision_raycast_3d.get_collision_point()
if (abs(collision_raycast_3d.get_collision_normal().y) > 0.99):
decal_spawn.look_at(collision_raycast_3d.get_collision_point() + collision_raycast_3d.get_collision_normal(), Vector3(0,0,1))
else:
decal_spawn.look_at(collision_raycast_3d.get_collision_point() + collision_raycast_3d.get_collision_normal())
decal_spawn.rotation.z = deg_to_rad(randf_range(0,360))
func collision_ray_length_adj():
if collision_raycast_3d != null:
#do looking towards velocity
#do lenghth of distance traveled next frame

View File

@@ -8,25 +8,18 @@ extends Projectile
# Called when the node enters the scene tree for the first time.
func _ready():
linear_velocity += transform.basis * Vector3(0, 0, -bullet_speed)
blast_radius = radius_shape.shape.radius
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
if !body.is_in_group("player"):
#move objects
explode()
#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()
#move objects
explode()
#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()
func explode():
#get all collision objects that can be moved

View File

@@ -9,7 +9,6 @@ extends Projectile
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
@@ -17,39 +16,22 @@ 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)
if linear_velocity.length() >= VELOCITY_REQUIRED_TO_HIT:
damage(body)
func despawn():
collision_shape.disabled = true

View File

@@ -48,12 +48,14 @@ func _ready():
if weapon_info.weapon_type != 1:
casings_chamber_last = weapon_info.max_ammo
if weapon_info.fire_mode == 0:
cycle_count_start = 1
elif weapon_info.fire_mode == 1:
cycle_count_start = 1
elif weapon_info.fire_mode == 2:
cycle_count_start = 3
match weapon_info.fire_mode:
0:
cycle_count_start = 1
1:
cycle_count_start = 1
2:
cycle_count_start = 3
cycle_count = cycle_count_start
# Called every frame. 'delta' is the elapsed time since the previous frame.
@@ -252,22 +254,24 @@ func projectile_initialize():
instance_bullet.transform.basis = player.bullet_ray.global_transform.basis
else:
instance_bullet.transform.basis = barrel_ray.global_transform.basis
instance_bullet.bullet_speed = weapon_info.bullet_speed
instance_bullet.player_velocity = player.velocity * transform.basis
instance_bullet.bullet_damage = weapon_info.bullet_damage
instance_bullet.blast_power = weapon_info.blast_power
instance_bullet.bullet_force_mod = weapon_info.bullet_force_mod
if weapon_info.bullet.bullet_hole != null:
instance_bullet.instance_bullethole = weapon_info.bullet.bullet_hole.instantiate()
instance_bullet.player_position = player.global_position
instance_bullet.linear_velocity += instance_bullet.transform.basis * Vector3(0, 0, -weapon_info.bullet_speed) + player.velocity
return instance_bullet
func bullet_fire():
SignalBus.emit_signal("shot_fired",weapon_info.crosshair_jump_amount)
audio_fire.play()
get_tree().current_scene.add_child(projectile_initialize())
var bullet_spawn = projectile_initialize()
get_tree().current_scene.add_child(bullet_spawn)
spawn_casing()
vibration()
if chamber != null:
revolver_chamber_rot_amt += weapon_info.chamber_rot_amount
if weapon_info.smoke_enabled: