Game state management & grunk counter

This commit is contained in:
Rob Kelly 2025-03-07 19:26:12 -07:00
parent faaf48d6a7
commit 143f35e761
12 changed files with 114 additions and 2 deletions

View File

@ -15,6 +15,10 @@ run/main_scene="res://levels/sandbox/sandbox.tscn"
config/features=PackedStringArray("4.4", "Forward Plus")
run/max_fps=60
[autoload]
GameManager="*res://src/game/game_manager.tscn"
[debug]
gdscript/warnings/untyped_declaration=2

7
src/game/game.gd Normal file
View File

@ -0,0 +1,7 @@
class_name Game extends Node
## Interface to the game as an application.
## Handy typed singleton access.
static var manager: GameManagerType:
get():
return GameManager

1
src/game/game.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://dxl25lkyped4

6
src/game/game.tscn Normal file
View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://qpq2cm1hgeha"]
[ext_resource type="Script" uid="uid://dxl25lkyped4" path="res://src/game/game.gd" id="1_qnjlk"]
[node name="Game" type="Node"]
script = ExtResource("1_qnjlk")

13
src/game/game_manager.gd Normal file
View File

@ -0,0 +1,13 @@
class_name GameManagerType extends Node
## Autoloaded singleton encapsulating game state.
signal grunk_collected(delta: float)
var grunk_tank := 0.0
var alert_level := 0
func collect_grunk(delta: float) -> void:
grunk_tank += delta
grunk_collected.emit(delta)

View File

@ -0,0 +1 @@
uid://c1i5gnht15x0e

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=3 uid="uid://cnrtgmanw40ei"]
[ext_resource type="Script" uid="uid://c1i5gnht15x0e" path="res://src/game/game_manager.gd" id="1_08e4a"]
[node name="GameManager" type="Node"]
script = ExtResource("1_08e4a")

View File

@ -0,0 +1,20 @@
extends HBoxContainer
## Does a funky lil bump when you collect tha grunk
const COUNTER_BUMP_RATE := 0.3
@onready var counter: Label = %Counter
func _ready() -> void:
Game.manager.grunk_collected.connect(on_grunk_collected)
counter.pivot_offset = Vector2(0, counter.size.y)
func on_grunk_collected(delta: float) -> void:
counter.text = str(int(Game.manager.grunk_tank))
counter.scale = Vector2.ONE + Vector2.ONE * clampf(delta / 128.0, 0.1, 1.0)
func _process(_delta: float) -> void:
counter.scale = counter.scale.lerp(Vector2.ONE, COUNTER_BUMP_RATE)

View File

@ -0,0 +1 @@
uid://dspsijt4ysg4x

View File

@ -0,0 +1,30 @@
[gd_scene load_steps=3 format=3 uid="uid://cq8qcp5xg41e0"]
[ext_resource type="Theme" uid="uid://b07fevr214mmr" path="res://src/ui/hud/hud_theme.tres" id="1_fwos1"]
[ext_resource type="Script" uid="uid://dspsijt4ysg4x" path="res://src/ui/hud/grunk_counter/grunk_counter.gd" id="2_jfqlp"]
[node name="GrunkCounter" type="HBoxContainer"]
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -45.0
offset_right = 134.0
grow_vertical = 0
theme = ExtResource("1_fwos1")
alignment = 2
script = ExtResource("2_jfqlp")
[node name="Label" type="Label" parent="."]
texture_filter = 6
layout_mode = 2
size_flags_horizontal = 0
text = "GRUNK: "
[node name="Counter" type="Label" parent="."]
unique_name_in_owner = true
texture_filter = 6
layout_mode = 2
size_flags_horizontal = 4
size_flags_vertical = 8
text = " 0"
horizontal_alignment = 2

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=3 format=3 uid="uid://dq1x21tq06dud"]
[gd_scene load_steps=4 format=3 uid="uid://dq1x21tq06dud"]
[ext_resource type="Theme" uid="uid://b07fevr214mmr" path="res://src/ui/hud/hud_theme.tres" id="1_lirk3"]
[ext_resource type="Script" uid="uid://lrsv0185bfu" path="res://src/ui/hud/player_hud.gd" id="2_j6lpx"]
[ext_resource type="PackedScene" uid="uid://cq8qcp5xg41e0" path="res://src/ui/hud/grunk_counter/grunk_counter.tscn" id="3_5be8f"]
[node name="PlayerHUD" type="Control"]
layout_mode = 3
@ -45,3 +46,21 @@ offset_right = 20.0
grow_horizontal = 2
grow_vertical = 0
text = "Press [E] to use"
[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -77.0
offset_right = 123.0
grow_vertical = 0
theme_override_constants/margin_left = 32
theme_override_constants/margin_top = 32
theme_override_constants/margin_right = 32
theme_override_constants/margin_bottom = 32
[node name="GrunkCounter" parent="MarginContainer" instance=ExtResource("3_5be8f")]
layout_mode = 2
size_flags_horizontal = 0
size_flags_vertical = 8

View File

@ -218,8 +218,12 @@ func _process(_delta: float) -> void:
_mutex.lock()
var new_total := _clear_total
_mutex.unlock()
if abs(new_total - _prev_clear_total) > CLEAR_TOTAL_EPSILON:
var delta := new_total - _prev_clear_total
if abs(delta) > CLEAR_TOTAL_EPSILON:
clear_total_updated.emit(new_total)
# XXX due to fp error, this will drift from the "true count" over time
# but it probably won't matter :shrug:
Game.manager.collect_grunk(delta)
_prev_clear_total = new_total
# If paint_continuous wasn't called last frame, stop the current polyline.