Mouse movement done
This commit is contained in:
parent
a68999c05e
commit
5629076291
@ -12,6 +12,7 @@ offset_left = -24.0
|
||||
offset_top = -32.0
|
||||
offset_right = 24.0
|
||||
offset_bottom = 32.0
|
||||
mouse_filter = 1
|
||||
texture_normal = ExtResource("1_3p273")
|
||||
script = ExtResource("1_0qqmn")
|
||||
onion_texture = ExtResource("1_3p273")
|
||||
|
@ -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="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"]
|
||||
size = Vector2(48, 71)
|
||||
|
||||
@ -12,7 +15,7 @@ cols = 7
|
||||
cell_size = Vector2i(48, 64)
|
||||
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")]
|
||||
board_area = NodePath("Tiles/Board/BoardArea")
|
||||
board_area = NodePath("")
|
||||
|
||||
[node name="Camera2D" type="Camera2D" parent="."]
|
||||
position = Vector2(-159, -32)
|
||||
@ -28,11 +31,11 @@ tile_set = ExtResource("2_idj7w")
|
||||
[node name="LeftTop" type="Marker2D" parent="Tiles/Board"]
|
||||
position = Vector2(-480, -256)
|
||||
|
||||
[node name="BoardArea" type="ReferenceRect" parent="Tiles/Board"]
|
||||
offset_left = -480.0
|
||||
offset_top = -256.0
|
||||
offset_right = -144.0
|
||||
offset_bottom = 192.0
|
||||
[node name="BoardArea" type="Area2D" parent="Tiles/Board"]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Tiles/Board/BoardArea"]
|
||||
position = Vector2(-312, -33)
|
||||
shape = SubResource("RectangleShape2D_idj7w")
|
||||
|
||||
[node name="Cards" type="TileMapLayer" parent="Tiles"]
|
||||
position = Vector2(-32, 0)
|
||||
@ -70,3 +73,5 @@ offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
|
||||
[node name="Cards" type="Node2D" parent="."]
|
||||
|
||||
[connection signal="input_event" from="Tiles/Board/BoardArea" to="." method="_on_board_area_input_event"]
|
||||
|
@ -47,8 +47,11 @@ func _on_button_down() -> void:
|
||||
|
||||
func _on_button_up() -> void:
|
||||
has_mouse = false
|
||||
if !settled:
|
||||
global_position = origin_pos
|
||||
|
||||
func _process(delta):
|
||||
pass
|
||||
#if has_mouse and !settled:
|
||||
#global_position = global_position.lerp(get_global_mouse_position(), mouse_follow_speed*delta)
|
||||
if has_mouse and !settled:
|
||||
var mouse_pos = get_global_mouse_position()
|
||||
var target_pos = Vector2(mouse_pos.x + 2, mouse_pos.y + 2)
|
||||
global_position = global_position.lerp(target_pos, mouse_follow_speed*delta)
|
||||
|
@ -8,18 +8,28 @@ extends Node
|
||||
@export var board_area : ReferenceRect
|
||||
|
||||
signal dragging_card
|
||||
signal release_dragging_card
|
||||
|
||||
var has_mouse: bool = false
|
||||
var dragged_card : Card
|
||||
|
||||
var hovered_cell_control : ColorRect
|
||||
|
||||
var hovered_cell : Vector2 = Global.invalid_cell_index
|
||||
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:
|
||||
init_game()
|
||||
dragging_card.connect(_on_dragging_card)
|
||||
#board_area.mouse_entered.connect(_on_mouse_enter_board)
|
||||
#board_area.mouse_exited.connect(_on_mouse_exit_board)
|
||||
release_dragging_card.connect(_on_release_dragging_card)
|
||||
|
||||
func init_game() -> void:
|
||||
board_info = create_cells()
|
||||
@ -33,73 +43,64 @@ func create_cells() -> Array:
|
||||
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)
|
||||
var cell = setup_cell_rect(center_pos, cell_size)
|
||||
var collision_box = cell.get_node("CollisionBox") as CollisionShape2D
|
||||
var shape = collision_box.shape
|
||||
var cell = setup_cell_rect(left_top_pos, cell_size)
|
||||
$BoardCells.add_child(cell)
|
||||
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),
|
||||
"index": Vector2i(i, j),
|
||||
"area": cell,
|
||||
"empty": true
|
||||
GLOBAL_LEFT_TOP : left_top_pos,
|
||||
GLOBAL_CENTER : center_pos,
|
||||
INDEX: Vector2i(i, j),
|
||||
AREA : cell,
|
||||
HOLD_CARD : null
|
||||
}
|
||||
row.append(dict)
|
||||
cells_matrix.append(row)
|
||||
return cells_matrix
|
||||
|
||||
func setup_cell_rect(left_top: Vector2, size: Vector2i) -> Area2D:
|
||||
var cell := Area2D.new()
|
||||
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
|
||||
$BoardCells.add_child(cell)
|
||||
|
||||
# 添加 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
|
||||
cell.visible = false
|
||||
return cell
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
pass
|
||||
#if has_mouse and dragged_card != null:
|
||||
#clamp_card_on_cell(dragged_card)
|
||||
var cell = clamp_cell()
|
||||
if hovered_cell_control != null:
|
||||
hovered_cell_control.visible = false
|
||||
if cell != null:
|
||||
hovered_cell_control = cell
|
||||
cell.visible = true
|
||||
|
||||
func _on_dragging_card(card: Card) -> void:
|
||||
dragged_card = card
|
||||
|
||||
func _on_release_dragging_card(card: Card, index: Vector2i) -> void:
|
||||
print(index)
|
||||
dragged_card = null
|
||||
|
||||
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:
|
||||
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 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
|
||||
|
||||
#func _on_mouse_enter_board() -> void:
|
||||
##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:
|
||||
func clamp_cell() -> ColorRect:
|
||||
var index = get_hover_cell_index()
|
||||
if index != Global.invalid_cell_index:
|
||||
var cell_info = board_info[index.x][index.y]
|
||||
#var cell_area = cell_info.area
|
||||
var pos = cell_info["global-left-top"]
|
||||
card.global_position = pos
|
||||
var cell = cell_info[AREA]
|
||||
if cell != null:
|
||||
var control = cell as ColorRect
|
||||
return control
|
||||
return null
|
||||
|
||||
func send_cards() -> void:
|
||||
var card_prefab = load("res://Scenes/Card.tscn")
|
||||
@ -112,3 +113,22 @@ func send_cards() -> void:
|
||||
var cate_num = randi() % 4
|
||||
card_instance.setup_card(cate_num, val)
|
||||
$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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user