2025-03-18 15:47:09 +08:00
|
|
|
extends Node
|
|
|
|
|
|
|
|
@export var cols : int
|
|
|
|
@export var cell_size : Vector2i
|
|
|
|
@export var left_top_of_board : Marker2D
|
|
|
|
@export var init_card_anchos : Array[Marker2D]
|
|
|
|
|
|
|
|
@export var board_area : ReferenceRect
|
|
|
|
|
|
|
|
signal dragging_card
|
2025-03-20 00:56:38 +08:00
|
|
|
signal release_dragging_card
|
2025-03-18 15:47:09 +08:00
|
|
|
|
|
|
|
var has_mouse: bool = false
|
|
|
|
var dragged_card : Card
|
|
|
|
|
2025-03-20 00:56:38 +08:00
|
|
|
var hovered_cell_control : ColorRect
|
|
|
|
|
2025-03-18 15:47:09 +08:00
|
|
|
var hovered_cell : Vector2 = Global.invalid_cell_index
|
|
|
|
var board_info : Array = []
|
|
|
|
|
2025-03-20 00:56:38 +08:00
|
|
|
const GLOBAL_LEFT_TOP = "global-left-top"
|
|
|
|
const GLOBAL_CENTER = "global-center"
|
|
|
|
const INDEX = "index"
|
|
|
|
const AREA = "area"
|
|
|
|
const HOLD_CARD = "hold-card"
|
|
|
|
|
|
|
|
const hovered_color = "015aafb8"
|
|
|
|
|
2025-03-18 15:47:09 +08:00
|
|
|
func _ready() -> void:
|
|
|
|
init_game()
|
|
|
|
dragging_card.connect(_on_dragging_card)
|
2025-03-20 00:56:38 +08:00
|
|
|
release_dragging_card.connect(_on_release_dragging_card)
|
2025-03-18 15:47:09 +08:00
|
|
|
|
|
|
|
func init_game() -> void:
|
|
|
|
board_info = create_cells()
|
|
|
|
send_cards()
|
|
|
|
|
|
|
|
func create_cells() -> Array:
|
|
|
|
var cells_matrix = []
|
|
|
|
var init_pos = Vector2(left_top_of_board.global_position.x, left_top_of_board.global_position.y)
|
|
|
|
for i in range(0, cols):
|
|
|
|
var row = []
|
|
|
|
for j in range(0, cols):
|
|
|
|
var left_top_pos = Vector2(init_pos.x + i * cell_size.x, init_pos.y + j * cell_size.y)
|
|
|
|
var center_pos = Vector2(left_top_pos.x + cell_size.x / 2, left_top_pos.y + cell_size.y / 2)
|
2025-03-20 00:56:38 +08:00
|
|
|
var cell = setup_cell_rect(left_top_pos, cell_size)
|
|
|
|
$BoardCells.add_child(cell)
|
2025-03-18 15:47:09 +08:00
|
|
|
var dict = {
|
2025-03-20 00:56:38 +08:00
|
|
|
GLOBAL_LEFT_TOP : left_top_pos,
|
|
|
|
GLOBAL_CENTER : center_pos,
|
|
|
|
INDEX: Vector2i(i, j),
|
|
|
|
AREA : cell,
|
|
|
|
HOLD_CARD : null
|
2025-03-18 15:47:09 +08:00
|
|
|
}
|
|
|
|
row.append(dict)
|
|
|
|
cells_matrix.append(row)
|
|
|
|
return cells_matrix
|
|
|
|
|
2025-03-20 00:56:38 +08:00
|
|
|
func setup_cell_rect(left_top: Vector2, size: Vector2i) -> ColorRect:
|
|
|
|
var cell = ColorRect.new()
|
|
|
|
cell.color = Color(hovered_color)
|
|
|
|
cell.size = cell_size
|
|
|
|
cell.global_position = left_top
|
|
|
|
cell.visible = false
|
2025-03-18 15:47:09 +08:00
|
|
|
return cell
|
|
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
2025-03-20 00:56:38 +08:00
|
|
|
var cell = clamp_cell()
|
|
|
|
if hovered_cell_control != null:
|
|
|
|
hovered_cell_control.visible = false
|
|
|
|
if cell != null:
|
|
|
|
hovered_cell_control = cell
|
|
|
|
cell.visible = true
|
2025-03-18 15:47:09 +08:00
|
|
|
|
|
|
|
func _on_dragging_card(card: Card) -> void:
|
|
|
|
dragged_card = card
|
|
|
|
|
2025-03-20 00:56:38 +08:00
|
|
|
func _on_release_dragging_card(card: Card, index: Vector2i) -> void:
|
|
|
|
print(index)
|
|
|
|
dragged_card = null
|
|
|
|
|
2025-03-18 15:47:09 +08:00
|
|
|
func get_hover_cell_index() -> Vector2i:
|
|
|
|
var mouse_global_position = $MouseAnchor.get_global_mouse_position()
|
|
|
|
return get_cell_on_pos(mouse_global_position)
|
|
|
|
|
|
|
|
func get_cell_on_pos(pos: Vector2) -> Vector2i:
|
2025-03-20 00:56:38 +08:00
|
|
|
if pos.x < left_top_of_board.global_position.x or pos.y < left_top_of_board.global_position.y:
|
|
|
|
return Global.invalid_cell_index
|
2025-03-18 15:47:09 +08:00
|
|
|
var relative_pos = Vector2(pos.x - left_top_of_board.global_position.x, pos.y - left_top_of_board.global_position.y)
|
|
|
|
var index = Vector2i(int(relative_pos.x) / cell_size.x, int(relative_pos.y) / cell_size.y)
|
|
|
|
if index.x < 0 or index.x >= cols or index.y < 0 or index.y >= cols:
|
|
|
|
index = Global.invalid_cell_index
|
|
|
|
return index
|
|
|
|
|
2025-03-20 00:56:38 +08:00
|
|
|
func clamp_cell() -> ColorRect:
|
2025-03-18 15:47:09 +08:00
|
|
|
var index = get_hover_cell_index()
|
|
|
|
if index != Global.invalid_cell_index:
|
|
|
|
var cell_info = board_info[index.x][index.y]
|
2025-03-20 00:56:38 +08:00
|
|
|
var cell = cell_info[AREA]
|
|
|
|
if cell != null:
|
|
|
|
var control = cell as ColorRect
|
|
|
|
return control
|
|
|
|
return null
|
2025-03-18 15:47:09 +08:00
|
|
|
|
|
|
|
func send_cards() -> void:
|
|
|
|
var card_prefab = load("res://Scenes/Card.tscn")
|
|
|
|
for i in range(init_card_anchos.size()):
|
|
|
|
var pos = init_card_anchos[i].global_position
|
|
|
|
var card_instance = card_prefab.instantiate()
|
|
|
|
var card_size = (card_instance as Card).texture_normal.get_size()
|
|
|
|
card_instance.global_position = Vector2(pos.x - card_size.x / 2, pos.y - card_size.y / 2)
|
|
|
|
var val = randi() % 10
|
|
|
|
var cate_num = randi() % 4
|
|
|
|
card_instance.setup_card(cate_num, val)
|
|
|
|
$Cards.add_child(card_instance)
|
2025-03-20 00:56:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
func _on_board_area_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
|
|
|
if event is InputEventMouseButton:
|
|
|
|
if event.button_index == MOUSE_BUTTON_LEFT:
|
|
|
|
if not event.pressed:
|
|
|
|
try_place_card(dragged_card)
|
|
|
|
|
|
|
|
func try_place_card(card: Card) -> void:
|
|
|
|
var index = get_hover_cell_index()
|
|
|
|
if dragged_card == null or index == Global.invalid_cell_index:
|
|
|
|
return
|
|
|
|
var cell_info = board_info[index.x][index.y]
|
|
|
|
if cell_info[HOLD_CARD] == null:
|
|
|
|
var lt = cell_info[GLOBAL_LEFT_TOP]
|
|
|
|
card.global_position = lt
|
|
|
|
card.settled = true
|
|
|
|
cell_info[HOLD_CARD] = card
|
|
|
|
emit_signal("release_dragging_card", card, index)
|