Extracted play management strategy to it's own resource

This commit is contained in:
Rob Kelly 2024-11-17 17:14:10 -07:00
parent 3d1da45673
commit 358d7c2eb4
4 changed files with 50 additions and 15 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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!

View File

@ -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