Change GameManager to use RPCs to sync state
This commit is contained in:
+155
-22
@@ -1,19 +1,142 @@
|
|||||||
class_name GameManager
|
|
||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
const STAR_DAY_FREQUENCY: int = 3
|
const STAR_DAY_FREQUENCY: int = 3
|
||||||
|
|
||||||
static var money: int = 1000
|
var money: int = 0:
|
||||||
static var day_number: int = 1
|
get:
|
||||||
static var meals_in_play: Array[String]
|
return money
|
||||||
static var sides_in_play: Array[String]
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify money directly.")
|
||||||
|
return
|
||||||
|
money = max(0, value)
|
||||||
|
_sync_money.rpc(money)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable") # Server updates clients
|
||||||
|
func _sync_money(new_amount: int) -> void:
|
||||||
|
money = new_amount
|
||||||
|
|
||||||
|
|
||||||
|
var day_number: int = 1:
|
||||||
|
get:
|
||||||
|
return day_number
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify day_number directly.")
|
||||||
|
return
|
||||||
|
day_number = value
|
||||||
|
_sync_day_number.rpc(day_number)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_day_number(new_value: int) -> void:
|
||||||
|
day_number = new_value
|
||||||
|
|
||||||
|
|
||||||
|
var meals_in_play: Array[String] = []:
|
||||||
|
get:
|
||||||
|
return meals_in_play
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify meals_in_play directly.")
|
||||||
|
return
|
||||||
|
meals_in_play = value
|
||||||
|
_sync_meals_in_play.rpc(meals_in_play)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_meals_in_play(new_value: Array[String]) -> void:
|
||||||
|
meals_in_play = new_value
|
||||||
|
|
||||||
|
|
||||||
|
var sides_in_play: Array[String] = []:
|
||||||
|
get:
|
||||||
|
return sides_in_play
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify sides_in_play directly.")
|
||||||
|
return
|
||||||
|
sides_in_play = value
|
||||||
|
_sync_sides_in_play.rpc(sides_in_play)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_sides_in_play(new_value: Array[String]) -> void:
|
||||||
|
sides_in_play = new_value
|
||||||
|
|
||||||
|
|
||||||
# GameConfig - difficulty settings
|
# GameConfig - difficulty settings
|
||||||
static var day_length_seconds: float = 60.0
|
var day_length_seconds: float = 60.0:
|
||||||
static var customers_per_day: float = 5.0
|
get:
|
||||||
static var customers_count_increase_per_day: float = 1.0
|
return day_length_seconds
|
||||||
static var group_min_size: int = 1
|
set(value):
|
||||||
static var group_max_size: int = 2
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify day_length_seconds directly.")
|
||||||
|
return
|
||||||
|
day_length_seconds = value
|
||||||
|
_sync_day_length_seconds.rpc(day_length_seconds)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_day_length_seconds(new_value: float) -> void:
|
||||||
|
day_length_seconds = new_value
|
||||||
|
|
||||||
|
|
||||||
|
var customers_per_day: float = 5.0:
|
||||||
|
get:
|
||||||
|
return customers_per_day
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify customers_per_day directly.")
|
||||||
|
return
|
||||||
|
customers_per_day = value
|
||||||
|
_sync_customers_per_day.rpc(customers_per_day)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_customers_per_day(new_value: float) -> void:
|
||||||
|
customers_per_day = new_value
|
||||||
|
|
||||||
|
|
||||||
|
var customers_count_increase_per_day: float = 1.0:
|
||||||
|
get:
|
||||||
|
return customers_count_increase_per_day
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify customers_count_increase_per_day directly.")
|
||||||
|
return
|
||||||
|
customers_count_increase_per_day = value
|
||||||
|
_sync_customers_count_increase_per_day.rpc(customers_count_increase_per_day)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_customers_count_increase_per_day(new_value: float) -> void:
|
||||||
|
customers_count_increase_per_day = new_value
|
||||||
|
|
||||||
|
|
||||||
|
var group_min_size: int = 1:
|
||||||
|
get:
|
||||||
|
return group_min_size
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify group_min_size directly.")
|
||||||
|
return
|
||||||
|
group_min_size = value
|
||||||
|
_sync_group_min_size.rpc(group_min_size)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_group_min_size(new_value: int) -> void:
|
||||||
|
group_min_size = new_value
|
||||||
|
|
||||||
|
|
||||||
|
var group_max_size: int = 2:
|
||||||
|
get:
|
||||||
|
return group_max_size
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify group_max_size directly.")
|
||||||
|
return
|
||||||
|
group_max_size = value
|
||||||
|
_sync_group_max_size.rpc(group_max_size)
|
||||||
|
|
||||||
|
@rpc("authority", "call_remote", "reliable")
|
||||||
|
func _sync_group_max_size(new_value: int) -> void:
|
||||||
|
group_max_size = new_value
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
enum GameState {
|
enum GameState {
|
||||||
@@ -22,19 +145,24 @@ enum GameState {
|
|||||||
BUILDING
|
BUILDING
|
||||||
}
|
}
|
||||||
|
|
||||||
static var game_state: GameState = GameState.BUILDING: set = _set_game_state
|
var game_state: GameState = GameState.BUILDING:
|
||||||
|
get:
|
||||||
|
return game_state
|
||||||
|
set(value):
|
||||||
|
if not multiplayer.is_server():
|
||||||
|
push_warning("Clients cannot modify game_state directly.")
|
||||||
|
return
|
||||||
|
game_state = value
|
||||||
|
_sync_game_state.rpc(game_state)
|
||||||
|
Signals.game_state_changed.emit(game_state)
|
||||||
|
|
||||||
static func _set_game_state(value: GameState):
|
@rpc("authority", "call_remote", "reliable")
|
||||||
game_state = value
|
func _sync_game_state(new_value: GameState) -> void:
|
||||||
Signals.game_state_changed.emit(value)
|
game_state = new_value
|
||||||
|
Signals.game_state_changed.emit(game_state)
|
||||||
|
|
||||||
|
|
||||||
static func _restart():
|
func get_random_meal() -> String:
|
||||||
print("restart Game")
|
|
||||||
game_state = GameState.RUNNING
|
|
||||||
|
|
||||||
|
|
||||||
static func get_random_meal() -> String:
|
|
||||||
if meals_in_play.size() == 0:
|
if meals_in_play.size() == 0:
|
||||||
push_error("GameManager: get_random_meal() called but meals_in_play is empty")
|
push_error("GameManager: get_random_meal() called but meals_in_play is empty")
|
||||||
return ""
|
return ""
|
||||||
@@ -43,7 +171,7 @@ static func get_random_meal() -> String:
|
|||||||
return meals_in_play[rand_index]
|
return meals_in_play[rand_index]
|
||||||
|
|
||||||
|
|
||||||
static func get_random_side() -> String:
|
func get_random_side() -> String:
|
||||||
print("GameManager: get_random_side() ")
|
print("GameManager: get_random_side() ")
|
||||||
if sides_in_play.size() == 0:
|
if sides_in_play.size() == 0:
|
||||||
push_error("GameManager: get_random_side() called but sides_in_play is empty")
|
push_error("GameManager: get_random_side() called but sides_in_play is empty")
|
||||||
@@ -53,7 +181,12 @@ static func get_random_side() -> String:
|
|||||||
return sides_in_play[rand_index]
|
return sides_in_play[rand_index]
|
||||||
|
|
||||||
|
|
||||||
static func game_over():
|
func _restart():
|
||||||
|
print("restart Game")
|
||||||
|
game_state = GameState.RUNNING
|
||||||
|
|
||||||
|
|
||||||
|
func game_over():
|
||||||
print("Game Over")
|
print("Game Over")
|
||||||
game_state = GameState.GAME_OVER
|
game_state = GameState.GAME_OVER
|
||||||
Signals.game_over.emit()
|
Signals.game_over.emit()
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ XRToolsRumbleManager="*uid://by853dk86g1qw"
|
|||||||
NetworkManager="*res://Net/network_manager.gd"
|
NetworkManager="*res://Net/network_manager.gd"
|
||||||
GlobalKeyEvents="*uid://c60unagog5oi1"
|
GlobalKeyEvents="*uid://c60unagog5oi1"
|
||||||
Signals="*uid://fuux6cxfjwdc"
|
Signals="*uid://fuux6cxfjwdc"
|
||||||
|
GameManager="*uid://y4sol8l8vih1"
|
||||||
|
|
||||||
[debug]
|
[debug]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user