Mouse movement done

This commit is contained in:
bean 2025-03-20 00:56:38 +08:00
parent a68999c05e
commit 5629076291
4 changed files with 82 additions and 53 deletions

View File

@ -12,6 +12,7 @@ offset_left = -24.0
offset_top = -32.0 offset_top = -32.0
offset_right = 24.0 offset_right = 24.0
offset_bottom = 32.0 offset_bottom = 32.0
mouse_filter = 1
texture_normal = ExtResource("1_3p273") texture_normal = ExtResource("1_3p273")
script = ExtResource("1_0qqmn") script = ExtResource("1_0qqmn")
onion_texture = ExtResource("1_3p273") onion_texture = ExtResource("1_3p273")

View File

@ -1,8 +1,11 @@
[gd_scene load_steps=4 format=4 uid="uid://bdhkwmdjla3dy"] [gd_scene load_steps=5 format=4 uid="uid://bdhkwmdjla3dy"]
[ext_resource type="Script" uid="uid://cvjgu1sx802tc" path="res://Scripts/GameManager.gd" id="1_r150o"] [ext_resource type="Script" uid="uid://cvjgu1sx802tc" path="res://Scripts/GameManager.gd" id="1_r150o"]
[ext_resource type="TileSet" uid="uid://d1gtddd47nuvg" path="res://Assets/TileSets/Tilesets.tres" id="2_idj7w"] [ext_resource type="TileSet" uid="uid://d1gtddd47nuvg" path="res://Assets/TileSets/Tilesets.tres" id="2_idj7w"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_idj7w"]
size = Vector2(336, 450)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kln2b"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_kln2b"]
size = Vector2(48, 71) size = Vector2(48, 71)
@ -12,7 +15,7 @@ cols = 7
cell_size = Vector2i(48, 64) cell_size = Vector2i(48, 64)
left_top_of_board = NodePath("Tiles/Board/LeftTop") left_top_of_board = NodePath("Tiles/Board/LeftTop")
init_card_anchos = [NodePath("Tiles/Cards/Marker1"), NodePath("Tiles/Cards/Marker2"), NodePath("Tiles/Cards/Marker3"), NodePath("Tiles/Cards/Marker4")] init_card_anchos = [NodePath("Tiles/Cards/Marker1"), NodePath("Tiles/Cards/Marker2"), NodePath("Tiles/Cards/Marker3"), NodePath("Tiles/Cards/Marker4")]
board_area = NodePath("Tiles/Board/BoardArea") board_area = NodePath("")
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(-159, -32) position = Vector2(-159, -32)
@ -28,11 +31,11 @@ tile_set = ExtResource("2_idj7w")
[node name="LeftTop" type="Marker2D" parent="Tiles/Board"] [node name="LeftTop" type="Marker2D" parent="Tiles/Board"]
position = Vector2(-480, -256) position = Vector2(-480, -256)
[node name="BoardArea" type="ReferenceRect" parent="Tiles/Board"] [node name="BoardArea" type="Area2D" parent="Tiles/Board"]
offset_left = -480.0
offset_top = -256.0 [node name="CollisionShape2D" type="CollisionShape2D" parent="Tiles/Board/BoardArea"]
offset_right = -144.0 position = Vector2(-312, -33)
offset_bottom = 192.0 shape = SubResource("RectangleShape2D_idj7w")
[node name="Cards" type="TileMapLayer" parent="Tiles"] [node name="Cards" type="TileMapLayer" parent="Tiles"]
position = Vector2(-32, 0) position = Vector2(-32, 0)
@ -70,3 +73,5 @@ offset_right = 40.0
offset_bottom = 40.0 offset_bottom = 40.0
[node name="Cards" type="Node2D" parent="."] [node name="Cards" type="Node2D" parent="."]
[connection signal="input_event" from="Tiles/Board/BoardArea" to="." method="_on_board_area_input_event"]

View File

@ -47,8 +47,11 @@ func _on_button_down() -> void:
func _on_button_up() -> void: func _on_button_up() -> void:
has_mouse = false has_mouse = false
if !settled:
global_position = origin_pos
func _process(delta): func _process(delta):
pass if has_mouse and !settled:
#if has_mouse and !settled: var mouse_pos = get_global_mouse_position()
#global_position = global_position.lerp(get_global_mouse_position(), mouse_follow_speed*delta) var target_pos = Vector2(mouse_pos.x + 2, mouse_pos.y + 2)
global_position = global_position.lerp(target_pos, mouse_follow_speed*delta)

View File

@ -8,18 +8,28 @@ extends Node
@export var board_area : ReferenceRect @export var board_area : ReferenceRect
signal dragging_card signal dragging_card
signal release_dragging_card
var has_mouse: bool = false var has_mouse: bool = false
var dragged_card : Card var dragged_card : Card
var hovered_cell_control : ColorRect
var hovered_cell : Vector2 = Global.invalid_cell_index var hovered_cell : Vector2 = Global.invalid_cell_index
var board_info : Array = [] var board_info : Array = []
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"
func _ready() -> void: func _ready() -> void:
init_game() init_game()
dragging_card.connect(_on_dragging_card) dragging_card.connect(_on_dragging_card)
#board_area.mouse_entered.connect(_on_mouse_enter_board) release_dragging_card.connect(_on_release_dragging_card)
#board_area.mouse_exited.connect(_on_mouse_exit_board)
func init_game() -> void: func init_game() -> void:
board_info = create_cells() board_info = create_cells()
@ -33,73 +43,64 @@ func create_cells() -> Array:
for j in range(0, cols): 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 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) var center_pos = Vector2(left_top_pos.x + cell_size.x / 2, left_top_pos.y + cell_size.y / 2)
var cell = setup_cell_rect(center_pos, cell_size) var cell = setup_cell_rect(left_top_pos, cell_size)
var collision_box = cell.get_node("CollisionBox") as CollisionShape2D $BoardCells.add_child(cell)
var shape = collision_box.shape
var dict = { var dict = {
"global-left-top": Vector2(cell.global_position.x - shape.get_rect().size.x / 2, cell.global_position.y - shape.get_rect().size.y / 2), GLOBAL_LEFT_TOP : left_top_pos,
"index": Vector2i(i, j), GLOBAL_CENTER : center_pos,
"area": cell, INDEX: Vector2i(i, j),
"empty": true AREA : cell,
HOLD_CARD : null
} }
row.append(dict) row.append(dict)
cells_matrix.append(row) cells_matrix.append(row)
return cells_matrix return cells_matrix
func setup_cell_rect(left_top: Vector2, size: Vector2i) -> Area2D: func setup_cell_rect(left_top: Vector2, size: Vector2i) -> ColorRect:
var cell := Area2D.new() var cell = ColorRect.new()
cell.color = Color(hovered_color)
cell.size = cell_size
cell.global_position = left_top cell.global_position = left_top
$BoardCells.add_child(cell) cell.visible = false
# 添加 CollisionShape2D 节点
var collision_shape = CollisionShape2D.new()
collision_shape.name = "CollisionBox"
cell.add_child(collision_shape)
# 设置 CollisionShape2D 为矩形,大小为棋盘格大小
var shape = RectangleShape2D.new()
shape.size = size
collision_shape.shape = shape
return cell return cell
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
pass var cell = clamp_cell()
#if has_mouse and dragged_card != null: if hovered_cell_control != null:
#clamp_card_on_cell(dragged_card) hovered_cell_control.visible = false
if cell != null:
hovered_cell_control = cell
cell.visible = true
func _on_dragging_card(card: Card) -> void: func _on_dragging_card(card: Card) -> void:
dragged_card = card dragged_card = card
func _on_release_dragging_card(card: Card, index: Vector2i) -> void:
print(index)
dragged_card = null
func get_hover_cell_index() -> Vector2i: func get_hover_cell_index() -> Vector2i:
var mouse_global_position = $MouseAnchor.get_global_mouse_position() var mouse_global_position = $MouseAnchor.get_global_mouse_position()
return get_cell_on_pos(mouse_global_position) return get_cell_on_pos(mouse_global_position)
func get_cell_on_pos(pos: Vector2) -> Vector2i: func get_cell_on_pos(pos: Vector2) -> Vector2i:
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
var relative_pos = Vector2(pos.x - left_top_of_board.global_position.x, pos.y - left_top_of_board.global_position.y) 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) 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: if index.x < 0 or index.x >= cols or index.y < 0 or index.y >= cols:
index = Global.invalid_cell_index index = Global.invalid_cell_index
return index return index
#func _on_mouse_enter_board() -> void: func clamp_cell() -> ColorRect:
##if dragged_card != null:
##dragged_card.handle_pos = false
#has_mouse = true
#print("board enter")
#
#func _on_mouse_exit_board() -> void:
##if dragged_card != null:
##dragged_card.handle_pos = true
#has_mouse = false
#print("board exit")
func clamp_card_on_cell(card: Card) -> void:
var index = get_hover_cell_index() var index = get_hover_cell_index()
if index != Global.invalid_cell_index: if index != Global.invalid_cell_index:
var cell_info = board_info[index.x][index.y] var cell_info = board_info[index.x][index.y]
#var cell_area = cell_info.area var cell = cell_info[AREA]
var pos = cell_info["global-left-top"] if cell != null:
card.global_position = pos var control = cell as ColorRect
return control
return null
func send_cards() -> void: func send_cards() -> void:
var card_prefab = load("res://Scenes/Card.tscn") var card_prefab = load("res://Scenes/Card.tscn")
@ -112,3 +113,22 @@ func send_cards() -> void:
var cate_num = randi() % 4 var cate_num = randi() % 4
card_instance.setup_card(cate_num, val) card_instance.setup_card(cate_num, val)
$Cards.add_child(card_instance) $Cards.add_child(card_instance)
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)