From 358d7c2eb44a6c12e43c7b5a6dc891293b1d0218 Mon Sep 17 00:00:00 2001 From: Rob Kelly Date: Sun, 17 Nov 2024 17:14:10 -0700 Subject: [PATCH] Extracted play management strategy to it's own resource --- src/world/play_manager/play_manager.gd | 23 +++++++++++++++++++ src/world/play_manager/round_robin_manager.gd | 14 +++++++++++ src/world/world.gd | 17 ++++---------- src/world/world.tscn | 11 +++++++-- 4 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 src/world/play_manager/play_manager.gd create mode 100644 src/world/play_manager/round_robin_manager.gd diff --git a/src/world/play_manager/play_manager.gd b/src/world/play_manager/play_manager.gd new file mode 100644 index 0000000..d6ee4f4 --- /dev/null +++ b/src/world/play_manager/play_manager.gd @@ -0,0 +1,23 @@ +class_name PlayManager extends Resource +## Abstract base type for strategies to manage the flow of gameplay + +## List of game player instances +@export var players: Array[WorldPlayer] = [] + + +func initialize() -> void: + for player: WorldPlayer in players: + player.shot_setup.finished.connect(_on_turn_finished) + on_initialization() + + +func on_initialization() -> void: + pass # Implemented in derived type + + +func on_turn_finished(_shot_setup: ShotSetup) -> void: + pass # Implemented in derived type + + +func _on_turn_finished(shot_setup: ShotSetup) -> void: + on_turn_finished(shot_setup) diff --git a/src/world/play_manager/round_robin_manager.gd b/src/world/play_manager/round_robin_manager.gd new file mode 100644 index 0000000..fafd5f5 --- /dev/null +++ b/src/world/play_manager/round_robin_manager.gd @@ -0,0 +1,14 @@ +class_name RoundRobinManager extends PlayManager +## Players take turns one after the other, looping back to the first player at the end. + + +func on_initialization() -> void: + print("IN ON_INITIALIZATION") + # Set first player as active + players[0].shot_setup.phase = ShotSetup.Phase.AIM + + +func on_turn_finished(source: ShotSetup) -> void: + print_debug("Shot finished for ", source.player.name) + players.push_back(players.pop_front()) + players[0].shot_setup.queue_restart() diff --git a/src/world/world.gd b/src/world/world.gd index ecd05a6..3a70013 100644 --- a/src/world/world.gd +++ b/src/world/world.gd @@ -6,7 +6,7 @@ class_name World extends Node @export var initial_level: PackedScene = load("res://levels/debug_level/debug_level.tscn") -@export var players: Array[WorldPlayer] = [] +@export var manager: PlayManager @export var spawn_group := "PlayerSpawn" @@ -22,13 +22,11 @@ func _ready() -> void: load_level(initial_level) # Spawn players in the level - for player: WorldPlayer in players: + for player: WorldPlayer in manager.players: _spawn_player(player) - # Set first player as active - if players: - players[0].shot_setup.phase = ShotSetup.Phase.AIM - else: + manager.initialize() + if not manager.players: push_warning("Warning: Starting game world with no players!") @@ -46,17 +44,10 @@ func _random_spawn() -> Node3D: func _spawn_player(player: WorldPlayer) -> void: var spawn_point := _random_spawn() var shot_setup := player.shot_setup - shot_setup.finished.connect(_on_shot_finished) shot_setup.global_transform = spawn_point.global_transform spawn_point.add_sibling(shot_setup) -func _on_shot_finished(source: ShotSetup) -> void: - print_debug("Shot finished for ", source.player.name) - players.push_back(players.pop_front()) - players[0].shot_setup.queue_restart() - - ## Instantiate and mantle the given level scene. ## ## This will free any currently-loaded level! diff --git a/src/world/world.tscn b/src/world/world.tscn index 618e7a0..4c94106 100644 --- a/src/world/world.tscn +++ b/src/world/world.tscn @@ -1,14 +1,21 @@ -[gd_scene load_steps=6 format=3 uid="uid://cwnwcd8kushl3"] +[gd_scene load_steps=9 format=3 uid="uid://cwnwcd8kushl3"] [ext_resource type="Script" path="res://src/world/world.gd" id="1_ybjyx"] +[ext_resource type="PackedScene" uid="uid://bm2o3mex10v11" path="res://levels/debug_level/debug_level.tscn" id="2_0xu5a"] [ext_resource type="PackedScene" uid="uid://c4ifdiohng830" path="res://src/ui/shot_hud/shot_hud.tscn" id="2_5b7qb"] [ext_resource type="Script" path="res://src/player/world_player.gd" id="2_e743i"] [ext_resource type="Script" path="res://src/ui/world_ui.gd" id="2_imewa"] [ext_resource type="Resource" uid="uid://crock3revdn73" path="res://src/player/debug_player.tres" id="3_pyw81"] +[ext_resource type="Script" path="res://src/world/play_manager/round_robin_manager.gd" id="5_h6mje"] + +[sub_resource type="Resource" id="Resource_rdjhi"] +script = ExtResource("5_h6mje") +players = Array[ExtResource("2_e743i")]([ExtResource("3_pyw81")]) [node name="World" type="Node" groups=["WorldGroup"]] script = ExtResource("1_ybjyx") -players = Array[ExtResource("2_e743i")]([ExtResource("3_pyw81")]) +initial_level = ExtResource("2_0xu5a") +manager = SubResource("Resource_rdjhi") [node name="Level" type="Node3D" parent="."] unique_name_in_owner = true