223 lines
7.4 KiB
GDScript
223 lines
7.4 KiB
GDScript
extends Control
|
|
|
|
@export var radial_stamina : bool = false
|
|
|
|
## VIEWPORT
|
|
var viewportWidth
|
|
var viewportHeight
|
|
|
|
var current_stam_bar
|
|
var stam_bar_visible : bool = false
|
|
var interact_visible : bool = false
|
|
var health_bar_start_pos
|
|
var money_count : int = 0
|
|
|
|
var crosshair_target
|
|
|
|
@onready var level_control = get_tree().current_scene
|
|
@onready var player = level_control.player
|
|
@onready var stamina_bar: TextureProgressBar = $StaminaBar
|
|
@onready var stamina_bar_2: ProgressBar = $StaminaBar2
|
|
@onready var health_bar: ProgressBar = $HealthBar
|
|
@onready var ammo_counter: HBoxContainer = $GunInfo/VBoxContainer/AmmoCounter
|
|
@onready var gun_name: Label = $"GunInfo/VBoxContainer/Gun Name"
|
|
@onready var ammo_current: Label = $GunInfo/VBoxContainer/AmmoCounter/AmmoCurrent
|
|
@onready var ammo_reserve: Label = $GunInfo/VBoxContainer/AmmoCounter/AmmoReserve
|
|
@onready var gun_info: MarginContainer = $GunInfo
|
|
@onready var money: Label = $Money
|
|
@onready var crosshair: TextureRect = $Crosshair
|
|
@onready var crosshair_center: TextureRect = $CrosshairCenter
|
|
@onready var pickup_item_indicator = preload("res://assets/pickup_item_indicator.tscn")
|
|
|
|
const FULL_WHITE = Color(1, 1, 1, 1)
|
|
const TRANSPARENT = Color(1, 1, 1, 0)
|
|
const RED_COLOR = Color(1, 0, 0)
|
|
const ORANGE_COLOR = Color(0.822, 0.318, 0.086)
|
|
const GREEN_COLOR = Color(0, 0.608, 0.172)
|
|
|
|
const STAM_BAR_MAX_OPACITY = 1.0
|
|
const CROSSHAIR_SIZE = Vector2(40,40)
|
|
|
|
var pickup_notifs = []
|
|
var can_spawn = true
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
# Get Viewport size
|
|
viewportWidth = get_viewport().size.x
|
|
viewportHeight = get_viewport().size.y
|
|
|
|
|
|
SignalBus.player_hit.connect(player_hit)
|
|
SignalBus.shot_fired.connect(crosshair_size_change)
|
|
|
|
money_count = GameGlobals.money
|
|
|
|
health_bar.max_value = level_control.gamemode.start_health
|
|
|
|
health_bar_start_pos = health_bar.position
|
|
|
|
crosshair_target = CROSSHAIR_SIZE
|
|
|
|
if radial_stamina:
|
|
current_stam_bar = stamina_bar
|
|
stamina_bar_2.visible = false
|
|
else:
|
|
current_stam_bar = stamina_bar_2
|
|
stamina_bar.visible = false
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
player = level_control.player
|
|
|
|
if player != null:
|
|
# Crosshair
|
|
crosshair_target = lerp(crosshair_target,CROSSHAIR_SIZE,delta * 5)
|
|
crosshair.size = lerp(crosshair.size, crosshair_target,delta * 20)
|
|
crosshair.position = Vector2(viewportWidth/2,viewportHeight/2) + (crosshair.size/-2)
|
|
crosshair_center.position = Vector2(viewportWidth/2,viewportHeight/2) + (crosshair_center.scale * crosshair_center.size/-2)
|
|
stamina_bar.scale = (crosshair.size/CROSSHAIR_SIZE) * Vector2(.185,.185)
|
|
stamina_bar.position = Vector2(viewportWidth/2,viewportHeight/2) + (stamina_bar.scale * stamina_bar.size/-2)
|
|
#hide crosshair and ammo when no gun
|
|
if player.gun == null:
|
|
crosshair.visible = false
|
|
gun_info.visible = false
|
|
else:
|
|
crosshair.visible = true
|
|
gun_info.visible = true
|
|
|
|
#HEALTH
|
|
health_bar.value = GameGlobals.health
|
|
if GameGlobals.health <= 2:
|
|
change_color(health_bar,RED_COLOR,10,delta)
|
|
health_bar.position = health_bar_start_pos + shake_element(15)
|
|
elif GameGlobals.health < ((level_control.gamemode.start_health / 2) + 1):
|
|
change_color(health_bar,ORANGE_COLOR,10,delta)
|
|
else:
|
|
change_color(health_bar,FULL_WHITE,10,delta)
|
|
|
|
#MONEY
|
|
if money_count < int(GameGlobals.money):
|
|
money_count += 1
|
|
change_color(money,GREEN_COLOR,10,delta)
|
|
elif money_count > int(GameGlobals.money):
|
|
change_color(money,RED_COLOR,10,delta)
|
|
money_count -= 1
|
|
else:
|
|
change_color(money,FULL_WHITE,10,delta)
|
|
|
|
money.text = "$" + str(money_count)
|
|
if player.remaining_stamina/level_control.gamemode.max_stamina >= .99:
|
|
stam_bar_visible = false
|
|
else:
|
|
stam_bar_visible = true
|
|
current_stam_bar.value = player.remaining_stamina
|
|
|
|
if player.gun != null:
|
|
if GameGlobals.gun_ammo.has(player.gun.gun_name) and GameGlobals.gun_ammo[player.gun.gun_name] != null:
|
|
ammo_counter.visible = true
|
|
ammo_current.text = str(GameGlobals.gun_ammo[player.gun.gun_name]).pad_zeros(2)
|
|
lerp_color(ammo_current,RED_COLOR,FULL_WHITE,GameGlobals.gun_ammo[player.gun.gun_name],player.gun.max_ammo,.5)
|
|
else:
|
|
ammo_counter.visible = false
|
|
if GameGlobals.ammo_reserve.has(str(player.gun.ammo_type)):
|
|
ammo_reserve.text = str(GameGlobals.ammo_reserve[str(player.gun.ammo_type)]).pad_zeros(3)
|
|
lerp_color(ammo_reserve,RED_COLOR,FULL_WHITE,GameGlobals.ammo_reserve[str(player.gun.ammo_type)],player.gun.max_ammo*2,.5)
|
|
else:
|
|
ammo_reserve.text = "-"
|
|
else:
|
|
fade_in_out(ammo_current,1,false,10,delta)
|
|
fade_in_out(ammo_reserve,1,false,10,delta)
|
|
fade_in_out(crosshair,1,false,10,delta)
|
|
if player.gun != null:
|
|
gun_name.text = player.gun.gun_name
|
|
gun_name.visible = true
|
|
else:
|
|
gun_name.visible = false
|
|
|
|
|
|
if player.remaining_stamina < 25:
|
|
change_color(current_stam_bar,RED_COLOR,10,delta)
|
|
change_color(crosshair,RED_COLOR,10,delta)
|
|
else:
|
|
change_color(current_stam_bar,FULL_WHITE,10,delta)
|
|
change_color(crosshair,FULL_WHITE,10,delta)
|
|
|
|
if player.interact_ray.is_colliding():
|
|
if player.interact_ray.get_collider() != null:
|
|
if player.interact_ray.get_collider().is_in_group("interact"):
|
|
interact_visible = true
|
|
else:
|
|
interact_visible = false
|
|
else:
|
|
interact_visible = false
|
|
|
|
if interact_visible == true:
|
|
change_color(crosshair,GREEN_COLOR,10,delta)
|
|
else:
|
|
change_color(crosshair,FULL_WHITE,10,delta)
|
|
|
|
if player.ads:
|
|
if player.gun != null:
|
|
if player.gun.ads:
|
|
fade_in_out(crosshair,1,false,5,delta)
|
|
fade_in_out(current_stam_bar,.001,true,5,delta)
|
|
else:
|
|
fade_in_out(crosshair,1,true,5,delta)
|
|
else:
|
|
fade_in_out(crosshair,1,true,5,delta)
|
|
|
|
## FADE ELEMENTS IN AND OUT
|
|
fade_in_out(current_stam_bar,STAM_BAR_MAX_OPACITY,stam_bar_visible,5,delta)
|
|
|
|
## SPAWN NOTIFICATIONS
|
|
spawn_notifs()
|
|
|
|
func crosshair_size_change():
|
|
crosshair_target += Vector2(20,20)
|
|
|
|
|
|
|
|
func shake_element(amount):
|
|
var rand_x = randf_range(-amount,amount)
|
|
var rand_y = randf_range(-amount,amount)
|
|
return Vector2(rand_x,rand_y)
|
|
|
|
func lerp_color(element,colorA,colorB,cur_value,max_value,active_percent):
|
|
var value : float = float(cur_value)/float(max_value)
|
|
if value <= active_percent:
|
|
element.modulate = lerp(colorA,colorB,value)
|
|
else:
|
|
element.modulate = colorB
|
|
|
|
func change_color(element,color,speed,delta):
|
|
element.modulate = lerp(element.modulate, Color(color.r,color.g,color.b,element.modulate.a), (delta * speed)/Engine.time_scale)
|
|
|
|
func fade_in_out(element,MAX_OPACITY,visible,speed,delta):
|
|
var element_color = element.modulate
|
|
if visible:
|
|
element.modulate = lerp(element_color, Color(element_color.r,element_color.g,element_color.b,MAX_OPACITY),(delta * speed)/Engine.time_scale)
|
|
else:
|
|
element.modulate = lerp(element_color, Color(element_color.r,element_color.g,element_color.b,0),(delta * speed)/Engine.time_scale)
|
|
|
|
func pick_up_notif(type,ammo_type,value):
|
|
var pickup_notif = pickup_item_indicator.instantiate()
|
|
pickup_notif.pickup_type = type
|
|
pickup_notif.ammo_type = ammo_type
|
|
pickup_notif.value = value
|
|
pickup_notif.position = money.position + Vector2(150,-50)
|
|
pickup_notif.scale = Vector2(1,1)
|
|
pickup_notifs.append(pickup_notif)
|
|
|
|
func spawn_notifs():
|
|
if pickup_notifs.size() > 0 and can_spawn:
|
|
can_spawn = false
|
|
var msg = pickup_notifs.pop_front()
|
|
add_child(msg)
|
|
await get_tree().create_timer(.2).timeout
|
|
can_spawn = true
|
|
|
|
func player_hit():
|
|
pass
|
|
#self.position += wiggle_element(25)
|