Skip to content

Commit 7c945c4

Browse files
committed
Add a basic texture for the top app bar. Refactor timer code to have basic view model layering.
1 parent c89745c commit 7c945c4

File tree

1,043 files changed

+19173
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,043 files changed

+19173
-44
lines changed

entities/time_component.gd

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extends Node2D
2+
3+
@onready var view_model = TimerViewModel.new()
4+
5+
var time_since_last_update = 0
6+
var time = Time.get_unix_time_from_system()
7+
8+
func _ready():
9+
ViewModelRegistry.register(ViewModelRegistry.Keys.TIMER, view_model)
10+
11+
func _process(delta: float):
12+
time_since_last_update += delta
13+
if time_since_last_update > 1:
14+
time_since_last_update = 0
15+
time += 86400 # seconds in a day
16+
view_model.set_time(time)

entities/time_component.tscn

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://b1x0140gst6wf"]
2+
3+
[ext_resource type="Script" path="res://entities/time_component.gd" id="1_bhnnc"]
4+
5+
[node name="TimeComponent" type="Node2D"]
6+
script = ExtResource("1_bhnnc")

project.godot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ run/main_scene="res://scenes/main/main.tscn"
1515
config/features=PackedStringArray("4.2", "Forward Plus")
1616
config/icon="res://icon.svg"
1717

18+
[autoload]
19+
20+
VMRegistry="*res://ui/viewmodels/vm_registry.gd"
21+
1822
[display]
1923

2024
window/size/viewport_width=1280

scenes/main/main.tscn

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
[gd_scene load_steps=5 format=3 uid="uid://d3nj6rt0cm53e"]
1+
[gd_scene load_steps=6 format=3 uid="uid://d3nj6rt0cm53e"]
22

33
[ext_resource type="PackedScene" uid="uid://xlfbl7fowoic" path="res://scenes/worldmap/world_map.tscn" id="1_cjno1"]
44
[ext_resource type="PackedScene" uid="uid://bnqxch8qylr8l" path="res://ui/widgets/hud.tscn" id="2_jggq6"]
55
[ext_resource type="PackedScene" uid="uid://2hgdqurtbrxg" path="res://scenes/common/player_controller.tscn" id="3_jr0xa"]
66
[ext_resource type="PackedScene" uid="uid://dcnty7qxod06" path="res://scenes/components/camera_component.tscn" id="4_xha1d"]
7+
[ext_resource type="PackedScene" uid="uid://b1x0140gst6wf" path="res://entities/time_component.tscn" id="5_6js18"]
78

89
[node name="World" type="Node2D"]
910

11+
[node name="Components" type="Node" parent="."]
12+
13+
[node name="CameraComponent" parent="Components" instance=ExtResource("4_xha1d")]
14+
15+
[node name="TimeComponent" parent="Components" instance=ExtResource("5_6js18")]
16+
1017
[node name="WorldMap" parent="." instance=ExtResource("1_cjno1")]
1118
position = Vector2(0, 1)
1219

@@ -19,7 +26,3 @@ position = Vector2(0, 1)
1926
[node name="PlayerController" parent="Controllers" node_paths=PackedStringArray("hud", "camera_component") instance=ExtResource("3_jr0xa")]
2027
hud = NodePath("../../CanvasLayer/HUD")
2128
camera_component = NodePath("../../Components/CameraComponent")
22-
23-
[node name="Components" type="Node" parent="."]
24-
25-
[node name="CameraComponent" parent="Components" instance=ExtResource("4_xha1d")]

ui/viewmodels/vm_base.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
extends Node
2+
class_name ViewModelBase

ui/viewmodels/vm_registry.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extends Node
2+
class_name ViewModelRegistry
3+
4+
enum Keys { TIMER }
5+
static var registry = {}
6+
7+
static func register(key: Keys, view_model: ViewModelBase):
8+
registry[key] = view_model
9+
10+
static func retrieve(key: Keys) -> ViewModelBase:
11+
return registry.get(key)

ui/viewmodels/vm_timer.gd

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
extends ViewModelBase
2+
class_name TimerViewModel
3+
4+
signal on_formatted_time_updated (new_time: String)
5+
6+
func set_time(new_timestamp: int):
7+
var now_dict := Time.get_datetime_dict_from_unix_time(new_timestamp)
8+
var formatted_datetime = _format_datetime(now_dict)
9+
on_formatted_time_updated.emit(formatted_datetime)
10+
11+
func _format_datetime(datetime: Dictionary) -> String:
12+
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
13+
var day = datetime["day"]
14+
var year = datetime["year"]
15+
var month_name = months[datetime["month"] - 1]
16+
17+
var suffix = "th"
18+
if day % 10 == 1 and day % 100 != 11:
19+
suffix = "st"
20+
elif day % 10 == 2 and day % 100 != 12:
21+
suffix = "nd"
22+
elif day % 10 == 3 and day % 100 != 13:
23+
suffix = "rd"
24+
25+
return "%s %d%s, %d" % [month_name, day, suffix, year]

ui/widgets/GameTimeLabel.gd

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
11
extends Label
22

3-
# todo: move all of this out of the view and make it not hard coded
4-
var time_since_last_update = 0
5-
var time = Time.get_unix_time_from_system()
3+
var view_model: TimerViewModel
64

75
func _ready() -> void:
8-
var now_dict := Time.get_datetime_dict_from_unix_time(time)
9-
text = format_datetime(now_dict)
6+
view_model = ViewModelRegistry.retrieve(ViewModelRegistry.Keys.TIMER) as TimerViewModel
7+
view_model.on_formatted_time_updated.connect(_handle_formatted_datetime_changed)
108

11-
func _process(delta: float):
12-
time_since_last_update += delta
13-
if time_since_last_update > 1:
14-
time_since_last_update = 0
15-
time += 86400 # seconds in a day
16-
var time_dict := Time.get_datetime_dict_from_unix_time(time)
17-
text = format_datetime(time_dict)
18-
19-
func format_datetime(datetime: Dictionary) -> String:
20-
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
21-
var day = datetime["day"]
22-
var year = datetime["year"]
23-
var month_name = months[datetime["month"] - 1]
24-
25-
var suffix = "th"
26-
if day % 10 == 1 and day % 100 != 11:
27-
suffix = "st"
28-
elif day % 10 == 2 and day % 100 != 12:
29-
suffix = "nd"
30-
elif day % 10 == 3 and day % 100 != 13:
31-
suffix = "rd"
32-
33-
return "%s %d%s, %d" % [month_name, day, suffix, year]
9+
func _handle_formatted_datetime_changed(new_datetime: String):
10+
text = new_datetime

ui/widgets/hud.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ size_flags_vertical = 3
1414

1515
[node name="TopAppBar" parent="." instance=ExtResource("1_kowid")]
1616
layout_mode = 1
17-
scale = Vector2(1.5, 1.5)

ui/widgets/top_app_bar.tscn

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://ci04dr1xobg8"]
1+
[gd_scene load_steps=4 format=3 uid="uid://ci04dr1xobg8"]
22

33
[ext_resource type="Theme" uid="uid://dxgsaua2q7y8i" path="res://ui/themes/hud.tres" id="2_7qrg5"]
44
[ext_resource type="Script" path="res://ui/widgets/GameTimeLabel.gd" id="3_08rmb"]
5+
[ext_resource type="Texture2D" uid="uid://uwdw62p64s6" path="res://vendor/kenney_ui-pack-pixel-adventure/Tiles/Large tiles/Thick outline/tile_0028.png" id="3_rb1vo"]
56

67
[node name="TopAppBar" type="Control"]
78
layout_mode = 3
8-
anchors_preset = 5
9-
anchor_left = 0.5
10-
anchor_right = 0.5
9+
anchors_preset = 10
10+
anchor_right = 1.0
11+
offset_bottom = 50.0
1112
grow_horizontal = 2
1213

13-
[node name="GameTimeLabel" type="Label" parent="."]
14-
layout_mode = 0
15-
offset_left = 87.0
16-
offset_top = 7.0
17-
offset_right = 244.0
18-
offset_bottom = 31.0
14+
[node name="Background" type="NinePatchRect" parent="."]
15+
layout_mode = 1
16+
anchors_preset = 10
17+
anchor_right = 1.0
18+
offset_bottom = 50.0
19+
grow_horizontal = 2
20+
texture = ExtResource("3_rb1vo")
21+
patch_margin_left = 11
22+
patch_margin_top = 11
23+
patch_margin_right = 11
24+
patch_margin_bottom = 11
25+
26+
[node name="GameTimeLabel" type="Label" parent="Background"]
27+
layout_mode = 1
28+
anchors_preset = 6
29+
anchor_left = 1.0
30+
anchor_top = 0.5
31+
anchor_right = 1.0
32+
anchor_bottom = 0.5
33+
offset_left = -157.0
34+
offset_top = -12.0
35+
offset_bottom = 12.0
36+
grow_horizontal = 0
37+
grow_vertical = 2
1938
theme = ExtResource("2_7qrg5")
2039
text = "January 1st, 2022"
2140
script = ExtResource("3_08rmb")

0 commit comments

Comments
 (0)