Implemented basic notification system for weapons changing.
This commit is contained in:
parent
a6d137bf7a
commit
18dc548dd6
16 changed files with 300 additions and 2 deletions
|
|
@ -2,5 +2,6 @@ extends Node
|
|||
|
||||
@warning_ignore_start("unused_signal") # since otherwise Godot will throw a warning that the signal is unused in current scope
|
||||
|
||||
signal weapon_changed(weapon_name: String, player_node: Node2D, player_sprite_size: Vector2, origin_offset: float)
|
||||
|
||||
@warning_ignore_restore("unused_signal") # put any future signals you add between the two ignore annotations
|
||||
@warning_ignore_restore("unused_signal")
|
||||
|
|
|
|||
89
scripts/notification_display.gd
Normal file
89
scripts/notification_display.gd
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
class_name NotificationDisplay extends Control
|
||||
|
||||
@export var icon: TextureRect
|
||||
@export var label: Label
|
||||
@onready var _container: HBoxContainer = $HBoxContainer
|
||||
|
||||
var _player: Node2D = null
|
||||
var _sprite_size: Vector2 = Vector2.ZERO
|
||||
var _origin_offset: float = 0.0
|
||||
var _follow_player: bool = false
|
||||
|
||||
func show_notification(weapon_name: String, player_node: Node2D, player_sprite_size: Vector2, origin_offset: float) -> void:
|
||||
label.add_theme_font_size_override("font_size", 8)
|
||||
label.text = weapon_name
|
||||
|
||||
# Auto-load icon from graphics folder matching the weapon name
|
||||
var icon_path = "res://graphics/" + weapon_name.to_lower() + ".png"
|
||||
if ResourceLoader.exists(icon_path):
|
||||
icon.texture = load(icon_path)
|
||||
else:
|
||||
icon.texture = null
|
||||
|
||||
_player = player_node
|
||||
_player.tree_exiting.connect(queue_free)
|
||||
_sprite_size = player_sprite_size
|
||||
_origin_offset = origin_offset
|
||||
_follow_player = true
|
||||
|
||||
# Wait one frame for layout to resolve shrink-to-fit widths
|
||||
await get_tree().process_frame
|
||||
|
||||
# Use container's natural width, but fix height at 16px
|
||||
size = Vector2(_container.size.x, 16.0)
|
||||
|
||||
# Position using origin offset for Y, to the right of player for X
|
||||
_update_position()
|
||||
modulate.a = 1.0
|
||||
|
||||
# Fade out and free
|
||||
var tween = create_tween()
|
||||
tween.tween_property(self, "modulate:a", 0.0, 2.0)
|
||||
tween.tween_callback(_on_fade_complete)
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if _follow_player and _player:
|
||||
_update_position()
|
||||
|
||||
|
||||
func _update_position() -> void:
|
||||
if _player == null:
|
||||
return
|
||||
|
||||
var player_pos = _player.position
|
||||
|
||||
# Y position uses the weapon origin offset from player center
|
||||
var notif_y = player_pos.y + _origin_offset - (size.y / 2.0)
|
||||
|
||||
# X position is 10px to the right of player's sprite edge
|
||||
var right_edge = player_pos.x + (_sprite_size.x / 2.0)
|
||||
var notif_x = right_edge + 10.0
|
||||
|
||||
var is_flipped = false
|
||||
|
||||
# Clamp to screen bounds — flip to left if it would go off-screen right
|
||||
var viewport_size = get_viewport_rect().size
|
||||
if notif_x + size.x > viewport_size.x:
|
||||
is_flipped = true
|
||||
var left_edge = player_pos.x - (_sprite_size.x / 2.0)
|
||||
notif_x = left_edge - 10.0 - size.x
|
||||
|
||||
# Swap icon/label order so the icon is always closest to the player
|
||||
if is_flipped:
|
||||
_container.move_child(label, 0)
|
||||
_container.move_child(icon, 1)
|
||||
else:
|
||||
_container.move_child(icon, 0)
|
||||
_container.move_child(label, 1)
|
||||
|
||||
# Final clamp: ensure the entire notification is within screen bounds
|
||||
notif_x = clamp(notif_x, 0, viewport_size.x - size.x)
|
||||
notif_y = clamp(notif_y, 0, viewport_size.y - size.y)
|
||||
|
||||
position = Vector2(notif_x, notif_y)
|
||||
|
||||
|
||||
func _on_fade_complete() -> void:
|
||||
_follow_player = false
|
||||
queue_free()
|
||||
1
scripts/notification_display.gd.uid
Normal file
1
scripts/notification_display.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://3a0cypjsx5cq
|
||||
23
scripts/notification_manager.gd
Normal file
23
scripts/notification_manager.gd
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class_name NotificationManager extends Node
|
||||
|
||||
@export var notification_scene: PackedScene
|
||||
|
||||
var _active_notification: NotificationDisplay = null
|
||||
|
||||
func _ready() -> void:
|
||||
EventBus.weapon_changed.connect(_on_weapon_changed)
|
||||
|
||||
|
||||
func _on_weapon_changed(weapon_name: String, player_node: Node2D, player_sprite_size: Vector2, origin_offset: float) -> void:
|
||||
if notification_scene == null:
|
||||
return
|
||||
|
||||
# Free any existing notification so the new one can fade in cleanly
|
||||
if _active_notification:
|
||||
_active_notification.queue_free()
|
||||
_active_notification = null
|
||||
|
||||
var notification = notification_scene.instantiate()
|
||||
add_child(notification)
|
||||
_active_notification = notification
|
||||
notification.show_notification(weapon_name, player_node, player_sprite_size, origin_offset)
|
||||
1
scripts/notification_manager.gd.uid
Normal file
1
scripts/notification_manager.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://boce4kgerpff2
|
||||
|
|
@ -15,6 +15,13 @@ func _ready() -> void:
|
|||
return
|
||||
|
||||
|
||||
func _emit_weapon_changed() -> void:
|
||||
var ship: Sprite2D = get_parent().get_node("Ship")
|
||||
var sprite_size = ship.get_rect().size
|
||||
var origin_offset = weapon_data.origin if weapon_data else 0.0
|
||||
EventBus.weapon_changed.emit(weapon_data.shot_name, get_parent(), sprite_size, origin_offset)
|
||||
|
||||
|
||||
func get_bullet_scene() -> PackedScene:
|
||||
return weapon_data.bullet_scene if weapon_data else null
|
||||
|
||||
|
|
@ -27,6 +34,8 @@ func select_weapon_by_name(name: String) -> bool:
|
|||
_current_weapon_index = i
|
||||
weapon_data = available_weapons[i]
|
||||
print("Switched to: ", name)
|
||||
|
||||
_emit_weapon_changed()
|
||||
return true
|
||||
return false
|
||||
|
||||
|
|
@ -39,3 +48,5 @@ func cycle_weapon() -> void: # Used for testing weapon cycling
|
|||
_current_weapon_index = (_current_weapon_index + 1) % available_weapons.size()
|
||||
weapon_data = available_weapons[_current_weapon_index]
|
||||
print("Switched to: ", weapon_data.shot_name)
|
||||
|
||||
_emit_weapon_changed()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue