Refactored to allow hotseat and multiplayer. TODO: Create interface class for webrtc/enet multiplayer

This commit is contained in:
Sch1nken 2024-03-22 01:37:05 +01:00
parent d688eaf9c6
commit 5fe8f22ccb
27 changed files with 798 additions and 135 deletions

View file

@ -24,8 +24,32 @@ var is_blacks_turn: bool = false
var should_disable: bool = false
func is_players_turn() -> bool:
return is_blacks_turn == is_black or GameData.debug
@rpc("call_local", "authority")
func set_authority(peer_id: int) -> void:
set_multiplayer_authority(peer_id, true)
pass
func is_players_turn() -> bool:
return is_blacks_turn == is_black
# Have a functions that checks if we're the "owner"
func is_owner() -> bool:
# TODO: Only use multiplayer authority
# According to https://docs.godotengine.org/en/stable/classes/class_offlinemultiplayerpeer.html
# Even without active multiplayer, the normal calls just work as expected
#if GameData.is_hot_seat:
# return true
#elif GameData.is_player_black == is_black: # we could use multiplayer authority here
# return true
#return false
return is_multiplayer_authority()
@rpc("call_remote", "any_peer")
func update_amount(amount: int) -> void:
tile_count = amount
update_tile_count_display()
func update_color(_is_black: bool) -> void:
is_black = _is_black
@ -37,10 +61,7 @@ func update_color(_is_black: bool) -> void:
update_tile_count_display()
insect_icon.texture = insect_resource.ui_texture
if GameData.is_hot_seat:
return
if is_black != is_blacks_turn and not GameData.debug:
if is_black != GameData.is_player_black:
disabled = true
return
@ -55,6 +76,7 @@ func update_tile_count_display() -> void:
func decrement_tile_count() -> void:
tile_count -= 1
update_tile_count_display()
update_amount.rpc(tile_count)
func disable() -> void:
disabled = true
@ -110,10 +132,12 @@ func _ready() -> void:
GameEvents.turn_started.connect(_on_turn_started)
name = insect_resource.tile_name
func _on_turn_started(turn_num: int, map: HexGrid, _is_blacks_turn: bool) -> void:
is_blacks_turn = _is_blacks_turn
should_disable = false
if turn_num >= 7 and not GameData.bees_placed.has(is_black): #has_bee_been_placed:
if turn_num >= 7 and not GameData.bees_placed.has(is_black):
if is_black == is_blacks_turn:
# if not bee has been placed for this player
# lock all buttons except the bee
@ -123,7 +147,8 @@ func _on_turn_started(turn_num: int, map: HexGrid, _is_blacks_turn: bool) -> voi
else:
#lock
should_disable = true
refresh_state()
func _on_insect_tile_deselected(tile: InsectTile) -> void:
@ -140,16 +165,18 @@ func _on_insect_selected(button: InsectButton, _is_black: bool) -> void:
disable()
func hover() -> void:
#if is_blacks_turn != is_black and not GameData.debug:
# return
if is_blacks_turn != is_black:
return
if not is_owner():
return
var tween = get_tree().create_tween()
tween.tween_property(hex, "position", Vector2(0, -16), 0.1).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_SPRING)
func unhover() -> void:
#if is_blacks_turn != is_black and not GameData.debug:
# return
#if GameData.is_player_black != is_black:
#return
var tween = get_tree().create_tween()
tween.tween_property(hex, "position", Vector2(0, 0), 0.25).set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_SPRING)
@ -167,17 +194,10 @@ func _on_mouse_exited():
unhover()
func _on_pressed():
# We can only press under these conditions
# Check if its the currents player turn (is black)
# If hotseat: we're done
# If multiplayer: Check if GameData.is_player_black is correct, this prevents
# the other player from using this button on their game instance
#GameData.is_hot_seat
if is_empty():
return
if not is_blacks_turn == is_black and not GameData.debug:
if not is_owner():
return
if not is_players_turn():