2024-03-05 15:47:11 +01:00
|
|
|
extends TextureButton
|
2024-03-14 19:30:18 +01:00
|
|
|
class_name InsectButton
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
@onready var hex = $Hex
|
|
|
|
|
@onready var tile_count_label = $Hex/TileCountLabel
|
|
|
|
|
@onready var insect_icon = $Hex/InsectIcon
|
2024-03-16 02:36:45 +01:00
|
|
|
var BEE = load("res://Tile/Prefabs/Bee.tres")
|
2024-03-15 18:05:58 +01:00
|
|
|
@export var insect_resource: TileResource = BEE
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
const HEX_BLACK = preload("res://InsectTiles/Assets/UI/hex_black.svg")
|
|
|
|
|
const HEX_WHITE = preload("res://InsectTiles/Assets/UI/hex_white.svg")
|
|
|
|
|
|
|
|
|
|
@export var is_black: bool = false
|
|
|
|
|
|
2024-03-16 02:36:45 +01:00
|
|
|
var disable_amount_display: bool = false
|
|
|
|
|
|
2024-03-05 15:47:11 +01:00
|
|
|
var tile_count: int = 1
|
|
|
|
|
|
|
|
|
|
var selected: bool = false
|
|
|
|
|
var hovered: bool = false
|
2024-03-08 00:22:29 +01:00
|
|
|
var deactivated: bool = false
|
|
|
|
|
|
2024-03-16 19:56:22 +01:00
|
|
|
var is_blacks_turn: bool = false
|
|
|
|
|
|
|
|
|
|
var should_disable: bool = false
|
|
|
|
|
|
2024-03-22 01:37:05 +01:00
|
|
|
@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()
|
2024-03-16 19:56:22 +01:00
|
|
|
|
2024-03-15 18:05:58 +01:00
|
|
|
func update_color(_is_black: bool) -> void:
|
|
|
|
|
is_black = _is_black
|
|
|
|
|
if is_black:
|
|
|
|
|
hex.texture = HEX_BLACK
|
|
|
|
|
else:
|
|
|
|
|
hex.texture = HEX_WHITE
|
|
|
|
|
|
|
|
|
|
update_tile_count_display()
|
|
|
|
|
insect_icon.texture = insect_resource.ui_texture
|
|
|
|
|
|
2024-03-22 01:37:05 +01:00
|
|
|
if is_black != GameData.is_player_black:
|
2024-03-15 18:05:58 +01:00
|
|
|
disabled = true
|
|
|
|
|
return
|
|
|
|
|
|
2024-03-08 00:22:29 +01:00
|
|
|
func is_empty() -> bool:
|
|
|
|
|
return tile_count <= 0
|
|
|
|
|
|
|
|
|
|
func update_tile_count_display() -> void:
|
|
|
|
|
tile_count_label.text = str(tile_count)
|
|
|
|
|
if is_empty():
|
|
|
|
|
disable()
|
|
|
|
|
|
|
|
|
|
func decrement_tile_count() -> void:
|
|
|
|
|
tile_count -= 1
|
|
|
|
|
update_tile_count_display()
|
2024-03-22 01:37:05 +01:00
|
|
|
update_amount.rpc(tile_count)
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
func disable() -> void:
|
2024-03-08 00:22:29 +01:00
|
|
|
disabled = true
|
2024-03-05 15:47:11 +01:00
|
|
|
deactivated = true
|
|
|
|
|
|
|
|
|
|
var tween = get_tree().create_tween()
|
|
|
|
|
tween.tween_property(self, "modulate", Color.DIM_GRAY, 0.15)
|
|
|
|
|
|
|
|
|
|
func enable() -> void:
|
|
|
|
|
disabled = false
|
2024-03-08 00:22:29 +01:00
|
|
|
deactivated = false
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
var tween = get_tree().create_tween()
|
|
|
|
|
tween.tween_property(self, "modulate", Color.WHITE, 0.15)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_placement_cancelled() -> void:
|
2024-03-14 19:30:18 +01:00
|
|
|
refresh_state()
|
2024-03-08 00:22:29 +01:00
|
|
|
|
2024-03-14 19:30:18 +01:00
|
|
|
func _on_insect_placed(resource: TileResource, _is_black: bool, pos: Vector4i) -> void:
|
|
|
|
|
refresh_state()
|
|
|
|
|
|
|
|
|
|
if insect_resource == resource and _is_black == is_black:
|
|
|
|
|
decrement_tile_count()
|
2024-03-05 15:47:11 +01:00
|
|
|
|
2024-03-14 19:30:18 +01:00
|
|
|
func refresh_state() -> void:
|
2024-03-08 00:22:29 +01:00
|
|
|
selected = false
|
2024-03-05 15:47:11 +01:00
|
|
|
|
2024-03-16 19:56:22 +01:00
|
|
|
if should_disable:
|
|
|
|
|
disable()
|
|
|
|
|
|
|
|
|
|
if not is_empty() and not should_disable:
|
2024-03-05 15:47:11 +01:00
|
|
|
enable()
|
2024-03-08 00:22:29 +01:00
|
|
|
|
2024-03-14 19:30:18 +01:00
|
|
|
if hovered and not is_empty():
|
2024-03-08 00:22:29 +01:00
|
|
|
hover()
|
|
|
|
|
else:
|
|
|
|
|
unhover()
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
2024-03-08 00:22:29 +01:00
|
|
|
func _ready() -> void:
|
2024-03-15 18:05:58 +01:00
|
|
|
update_color(is_black)
|
2024-03-08 00:22:29 +01:00
|
|
|
|
2024-03-16 02:36:45 +01:00
|
|
|
if disable_amount_display:
|
|
|
|
|
$Hex/TileCountLabel.visible = false
|
|
|
|
|
|
2024-03-05 15:47:11 +01:00
|
|
|
GameEvents.insect_selected.connect(_on_insect_selected)
|
|
|
|
|
GameEvents.insect_placement_cancelled.connect(_on_placement_cancelled)
|
|
|
|
|
GameEvents.insect_placed.connect(_on_insect_placed)
|
2024-03-14 19:30:18 +01:00
|
|
|
GameEvents.insect_tile_selected.connect(_on_insect_tile_selected)
|
|
|
|
|
GameEvents.insect_tile_moved.connect(_on_insect_tile_moved)
|
|
|
|
|
GameEvents.insect_tile_deselected.connect(_on_insect_tile_deselected)
|
|
|
|
|
|
2024-03-16 19:56:22 +01:00
|
|
|
GameEvents.turn_started.connect(_on_turn_started)
|
|
|
|
|
|
2024-03-22 01:37:05 +01:00
|
|
|
name = insect_resource.tile_name
|
|
|
|
|
|
2024-03-16 19:56:22 +01:00
|
|
|
func _on_turn_started(turn_num: int, map: HexGrid, _is_blacks_turn: bool) -> void:
|
|
|
|
|
is_blacks_turn = _is_blacks_turn
|
|
|
|
|
should_disable = false
|
2024-03-22 01:37:05 +01:00
|
|
|
if turn_num >= 7 and not GameData.bees_placed.has(is_black):
|
2024-03-19 23:16:29 +01:00
|
|
|
if is_black == is_blacks_turn:
|
2024-03-16 19:56:22 +01:00
|
|
|
# if not bee has been placed for this player
|
|
|
|
|
# lock all buttons except the bee
|
|
|
|
|
if is_same(BEE, insect_resource):
|
|
|
|
|
# don't lock
|
|
|
|
|
should_disable = false
|
|
|
|
|
else:
|
|
|
|
|
#lock
|
|
|
|
|
should_disable = true
|
2024-03-22 01:37:05 +01:00
|
|
|
|
|
|
|
|
|
2024-03-16 19:56:22 +01:00
|
|
|
refresh_state()
|
|
|
|
|
|
2024-03-14 19:30:18 +01:00
|
|
|
func _on_insect_tile_deselected(tile: InsectTile) -> void:
|
|
|
|
|
refresh_state()
|
2024-03-05 15:47:11 +01:00
|
|
|
|
2024-03-14 19:30:18 +01:00
|
|
|
func _on_insect_tile_selected(tile: InsectTile) -> void:
|
|
|
|
|
disable()
|
|
|
|
|
|
|
|
|
|
func _on_insect_tile_moved(tile: InsectTile, to: Vector4i) -> void:
|
|
|
|
|
refresh_state()
|
|
|
|
|
|
|
|
|
|
func _on_insect_selected(button: InsectButton, _is_black: bool) -> void:
|
|
|
|
|
if button != self:
|
2024-03-05 15:47:11 +01:00
|
|
|
disable()
|
|
|
|
|
|
|
|
|
|
func hover() -> void:
|
2024-03-22 01:37:05 +01:00
|
|
|
if is_blacks_turn != is_black:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if not is_owner():
|
|
|
|
|
return
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
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:
|
2024-03-22 01:37:05 +01:00
|
|
|
#if GameData.is_player_black != is_black:
|
|
|
|
|
#return
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
func _on_mouse_entered():
|
2024-03-08 00:22:29 +01:00
|
|
|
hovered = true
|
|
|
|
|
|
|
|
|
|
if not deactivated:
|
|
|
|
|
hover()
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
func _on_mouse_exited():
|
|
|
|
|
hovered = false
|
|
|
|
|
|
2024-03-08 00:22:29 +01:00
|
|
|
if not selected:
|
|
|
|
|
unhover()
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
func _on_pressed():
|
2024-03-08 00:22:29 +01:00
|
|
|
if is_empty():
|
2024-03-05 15:47:11 +01:00
|
|
|
return
|
|
|
|
|
|
2024-03-22 01:37:05 +01:00
|
|
|
if not is_owner():
|
2024-03-16 02:36:45 +01:00
|
|
|
return
|
|
|
|
|
|
2024-03-16 19:56:22 +01:00
|
|
|
if not is_players_turn():
|
|
|
|
|
return
|
|
|
|
|
|
2024-03-08 00:22:29 +01:00
|
|
|
selected = true
|
2024-03-05 15:47:11 +01:00
|
|
|
|
2024-03-14 19:30:18 +01:00
|
|
|
GameEvents.insect_selected.emit(self, is_black)
|
2024-03-05 15:47:11 +01:00
|
|
|
|
|
|
|
|
release_focus()
|
|
|
|
|
|
|
|
|
|
func _input(event):
|
2024-03-30 01:43:38 +01:00
|
|
|
if Input.is_action_just_pressed("deselect_tile"):
|
2024-03-05 15:47:11 +01:00
|
|
|
release_focus()
|
|
|
|
|
return
|