Skip to content

Commit 5f09671

Browse files
committed
1 parent 8209f8d commit 5f09671

File tree

7 files changed

+91
-2
lines changed

7 files changed

+91
-2
lines changed

assets/CardSlot.png

3.95 KB
Loading

project/CardManager.gd

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
extends Node2D
22

33
const COLLISION_MASK_CARD = 1
4+
const COLLISION_MASK_CARD_SLOT = 2
45

56
var card_being_dragged : Node2D
67
var screen_size : Vector2
@@ -30,14 +31,21 @@ func _input(event: InputEvent) -> void:
3031
if card:
3132
start_drag(card)
3233
else:
33-
finish_drag()
34+
if card_being_dragged:
35+
finish_drag()
3436

3537
func start_drag(card: Node2D):
3638
card.scale = Vector2(1, 1)
3739
card_being_dragged = card
3840

3941
func finish_drag():
4042
card_being_dragged.scale = Vector2(1.05, 1.05)
43+
var card_slot_found = raycast_check_for_card_slot()
44+
if card_slot_found and not card_slot_found.card_in_slot:
45+
# Card dropped in empty slot.
46+
card_being_dragged.position = card_slot_found.position
47+
card_being_dragged.get_node("Area2D/CollisionShape2D").disabled = true
48+
card_slot_found.card_in_slot = true
4149
card_being_dragged = null
4250

4351

@@ -71,6 +79,19 @@ func highlight_card(card: Node2D, hovered: bool):
7179
card.z_index = 1
7280

7381

82+
func raycast_check_for_card_slot() -> Variant:
83+
var space_state = get_world_2d().direct_space_state
84+
var parameters = PhysicsPointQueryParameters2D.new()
85+
parameters.position = get_global_mouse_position()
86+
parameters.collide_with_areas = true
87+
parameters.collision_mask = COLLISION_MASK_CARD_SLOT
88+
var result = space_state.intersect_point(parameters)
89+
if result.size() > 0:
90+
var slot : Node2D = result[0].collider.get_parent()
91+
return slot
92+
return null
93+
94+
7495
func raycast_check_for_card() -> Variant:
7596
var space_state = get_world_2d().direct_space_state
7697
var parameters = PhysicsPointQueryParameters2D.new()

project/CardSlot.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extends Node2D
2+
3+
4+
var card_in_slot = false

project/CardSlot.png

3.95 KB
Loading

project/CardSlot.png.import

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="CompressedTexture2D"
5+
uid="uid://fccvay6su1fs"
6+
path="res://.godot/imported/CardSlot.png-ce2a75163d8c7ecc316afc7cba9c1a17.ctex"
7+
metadata={
8+
"vram_texture": false
9+
}
10+
11+
[deps]
12+
13+
source_file="res://CardSlot.png"
14+
dest_files=["res://.godot/imported/CardSlot.png-ce2a75163d8c7ecc316afc7cba9c1a17.ctex"]
15+
16+
[params]
17+
18+
compress/mode=0
19+
compress/high_quality=false
20+
compress/lossy_quality=0.7
21+
compress/hdr_compression=1
22+
compress/normal_map=0
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
28+
process/fix_alpha_border=true
29+
process/premult_alpha=false
30+
process/normal_map_invert_y=false
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

project/CardSlot.tscn

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[gd_scene load_steps=4 format=3 uid="uid://cmc8cw3l15t6n"]
2+
3+
[ext_resource type="Texture2D" uid="uid://fccvay6su1fs" path="res://CardSlot.png" id="1_gtpn6"]
4+
[ext_resource type="Script" path="res://CardSlot.gd" id="1_sedjj"]
5+
6+
[sub_resource type="RectangleShape2D" id="RectangleShape2D_ve5sy"]
7+
size = Vector2(178, 263)
8+
9+
[node name="CardSlot" type="Node2D"]
10+
script = ExtResource("1_sedjj")
11+
12+
[node name="CardSlotImage" type="Sprite2D" parent="."]
13+
texture = ExtResource("1_gtpn6")
14+
15+
[node name="Area2D" type="Area2D" parent="."]
16+
collision_layer = 2
17+
collision_mask = 2
18+
19+
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
20+
position = Vector2(0, -0.5)
21+
shape = SubResource("RectangleShape2D_ve5sy")

project/Main.tscn

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://dct70tba5rujv"]
1+
[gd_scene load_steps=4 format=3 uid="uid://dct70tba5rujv"]
22

33
[ext_resource type="PackedScene" uid="uid://ba127t8k4b0xh" path="res://Card.tscn" id="1_8uu4h"]
44
[ext_resource type="Script" path="res://CardManager.gd" id="2_2bkmx"]
5+
[ext_resource type="PackedScene" uid="uid://cmc8cw3l15t6n" path="res://CardSlot.tscn" id="3_setnr"]
56

67
[node name="Main" type="Node2D"]
78

@@ -13,3 +14,11 @@ position = Vector2(135, 189)
1314

1415
[node name="Card2" parent="CardManager" instance=ExtResource("1_8uu4h")]
1516
position = Vector2(1018, 467)
17+
18+
[node name="CardSlot" parent="." instance=ExtResource("3_setnr")]
19+
position = Vector2(402, 188)
20+
scale = Vector2(1.3, 1.3)
21+
22+
[node name="CardSlot2" parent="." instance=ExtResource("3_setnr")]
23+
position = Vector2(695, 191)
24+
scale = Vector2(1.3, 1.3)

0 commit comments

Comments
 (0)