-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_world.gd
More file actions
170 lines (128 loc) · 5.23 KB
/
demo_world.gd
File metadata and controls
170 lines (128 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
## DemoWorld: Demonstration script for Miniworld MOO-like architecture
##
## This script creates a simple test world to showcase the core systems:
## - WorldObject creation and management
## - Component composition (Actor, Memory, Location)
## - Command execution and parsing
## - Event propagation between objects
## - Room navigation and connectivity
##
## The demo automatically creates a two-room world, spawns a player,
## and executes a series of test commands to demonstrate functionality.
##
## Dependencies:
## - WorldKeeper: Object registry and lifecycle manager
## - ActorComponent: Command execution capability
## - LocationComponent: Room navigation system
## - MemoryComponent: Experience storage
##
## Notes:
## - Press SPACE to restart the demo
## - Commands execute on a 1-second timer for readability
extends Node2D
## The player's WorldObject instance
var player: WorldObject
## The starting room WorldObject
var lobby: WorldObject
## The connected garden room WorldObject
var garden: WorldObject
func _ready() -> void:
"""Initialize the demo world and run test sequence.
Waits one frame for autoloads to initialize, then creates
the world, player, and executes demonstration commands.
"""
# Give the world a moment to initialize
await get_tree().process_frame
_create_world()
_create_player()
_test_commands()
func _create_world() -> void:
"""Create a simple world with two connected rooms.
Constructs the Lobby and Garden rooms, adds LocationComponents
to make them navigable, and creates bidirectional exits between them.
"""
print("\n=== Creating World ===")
# Create the Lobby room
lobby = WorldKeeper.create_room("The Lobby", "A comfortable entrance hall with plush seating and warm lighting.")
print("Created: %s" % lobby)
# Add location component to make it a proper room
var lobby_location: LocationComponent = LocationComponent.new()
lobby.add_component("location", lobby_location)
# Create the Garden room
garden = WorldKeeper.create_room("The Garden", "A peaceful garden filled with flowers and the sound of trickling water.")
print("Created: %s" % garden)
var garden_location: LocationComponent = LocationComponent.new()
garden.add_component("location", garden_location)
# Connect the rooms with bidirectional exits
lobby_location.add_exit("garden", garden)
lobby_location.add_exit("north", garden)
garden_location.add_exit("lobby", lobby)
garden_location.add_exit("south", lobby)
print("Connected rooms with exits")
func _create_player() -> void:
"""Create a player character with Actor and Memory components.
Constructs a player WorldObject, adds Actor capability for command
execution and Memory for storing experiences, then places the
player in the lobby.
"""
print("\n=== Creating Player ===")
player = WorldKeeper.create_object("player", "Wanderer")
print("Created: %s" % player)
# Add Actor component (can execute commands)
var actor_comp: ActorComponent = ActorComponent.new()
player.add_component("actor", actor_comp)
# Add Memory component (can remember things)
var memory_comp: MemoryComponent = MemoryComponent.new()
player.add_component("memory", memory_comp)
# Place player in the lobby
player.move_to(lobby)
print("Player moved to: %s" % lobby.name)
func _test_commands() -> void:
"""Execute a sequence of test commands to demonstrate functionality.
Runs through LOOK, SAY, GO, and EMOTE commands with 1-second delays
between each. After completion, displays the player's accumulated
memories, world statistics, and the object hierarchy tree.
"""
print("\n=== Testing Commands ===\n")
await get_tree().create_timer(1.0).timeout
var actor_comp: ActorComponent = player.get_component("actor") as ActorComponent
# Test LOOK command
print("Player executes: LOOK")
var result: Dictionary = actor_comp.execute_command("look")
print("Result: %s\n" % result.message)
await get_tree().create_timer(1.0).timeout
# Test SAY command
print("Player executes: SAY Hello, world!")
result = actor_comp.execute_command("say", ["Hello,", "world!"])
print("Result: %s\n" % result.message)
await get_tree().create_timer(1.0).timeout
# Test GO command
print("Player executes: GO garden")
result = actor_comp.execute_command("go", ["garden"])
print("Result: %s\n" % result.message)
await get_tree().create_timer(1.0).timeout
# Test EMOTE command
print("Player executes: EMOTE waves at the flowers")
result = actor_comp.execute_command("emote", ["waves", "at", "the", "flowers"])
print("Result: %s\n" % result.message)
await get_tree().create_timer(1.0).timeout
# Display player's accumulated memories
print("=== Player Memories ===")
var memory_comp: MemoryComponent = player.get_component("memory") as MemoryComponent
print(memory_comp.format_memories_as_text())
# Display world statistics
print("\n=== World Stats ===")
print(WorldKeeper.get_stats())
# Display object hierarchy tree
print("\n=== World Tree ===")
WorldKeeper.print_world_tree()
func _process(_delta: float) -> void:
"""Handle input for demo restart.
Monitors for SPACE key press to reload the scene and restart
the demonstration from the beginning.
Args:
_delta: Time elapsed since previous frame (unused)
"""
# Press SPACE to run the demo again
if Input.is_action_just_pressed("ui_accept"):
get_tree().reload_current_scene()