Add snapping to zones

This commit is contained in:
Hamcha 2019-05-07 20:56:26 +02:00
parent f398d6cefa
commit df81649b53
Signed by: hamcha
GPG key ID: A40413D21021EAEE
6 changed files with 99 additions and 26 deletions

View file

@ -37,11 +37,14 @@ material/0 = null
[node name="Deck" parent="Cards" instance=ExtResource( 5 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -4.30889, 0, 3.52858 )
title = "Deck"
zoneName = "player1.deck"
[node name="Graveyard" parent="Cards" instance=ExtResource( 5 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5.23371, 0, 3.52858 )
title = "Graveyard"
zoneName = "player1.graveyard"
[node name="Banished" parent="Cards" instance=ExtResource( 5 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5.23371, 0, 2.40401 )
title = "Banished"
zoneName = "player1.banished"

View file

@ -1,4 +1,4 @@
extends Spatial
extends Area
class_name Card
@ -13,9 +13,11 @@ var clicked := false
var exhausted := false
var lifted := false
var flipped := false
export var cardID := ""
export var playerID := 0
export var inHand := false
var cardID := ""
var playerID := 0
var inHand := false
var inZone := false
var zoneName = ""
onready var animation := $Border/AnimationPlayer
@ -30,6 +32,8 @@ func _input_event(camera, event, click_position, click_normal, shape_idx):
if clicked and event is InputEventMouseMotion:
if not lifted:
emit_signal("card_picked")
# Disable input for now
input_ray_pickable = false
lifted = true
var origin: Vector3 = camera.project_ray_origin(event.position)
var direction: Vector3 = camera.project_ray_normal(event.position)
@ -37,6 +41,9 @@ func _input_event(camera, event, click_position, click_normal, shape_idx):
translation = Vector3(direction.x * 3.0, 0.25, -0.25)
# Fix rotation if coming from hand
rotation = Vector3.ZERO
elif inZone:
translation = Vector3.ZERO
rotation = Vector3.ZERO
else:
var denom := Vector3.UP.dot(direction)
var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom;
@ -53,6 +60,7 @@ func _input_event(camera, event, click_position, click_normal, shape_idx):
# Check for card dropped
if not clicked and lifted:
emit_signal("card_dropped")
input_ray_pickable = true
lifted = false
# Check double click

View file

@ -1,9 +1,21 @@
extends Spatial
extends Area
class_name Stack
signal hover()
signal blur()
export var title: String
export var zoneName: String
func _ready():
set_title(title)
func set_title(title: String):
$LabelViewport/Label.text = title
$LabelViewport/Label.text = title
func _mouse_entered():
emit_signal("hover")
func _mouse_exited():
emit_signal("blur")

View file

@ -1,21 +1,28 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=6 format=2]
[ext_resource path="res://Scenes/Components/Stack.gd" type="Script" id=1]
[sub_resource type="DynamicFontData" id=2]
[sub_resource type="BoxShape" id=4]
extents = Vector3( 0.424389, 0.024053, 0.546632 )
[sub_resource type="DynamicFontData" id=1]
font_path = "res://UIAssets/Fonts/Catamaran-Bold.ttf"
[sub_resource type="DynamicFont" id=3]
[sub_resource type="DynamicFont" id=2]
outline_size = 2
outline_color = Color( 0, 0, 0, 0.54902 )
font_data = SubResource( 2 )
font_data = SubResource( 1 )
[sub_resource type="ViewportTexture" id=1]
[sub_resource type="ViewportTexture" id=3]
viewport_path = NodePath("LabelViewport")
[node name="Stack" type="Spatial"]
[node name="Stack" type="Area"]
script = ExtResource( 1 )
[node name="CollisionShape" type="CollisionShape" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0239744, 0 )
shape = SubResource( 4 )
[node name="CSGBox" type="CSGBox" parent="."]
transform = Transform( 0.85598, 0, 0, 0, 1, 0, 0, 0, 0.871019, 0, 0, 0 )
width = 0.946586
@ -50,11 +57,13 @@ usage = 0
[node name="Label" type="Label" parent="LabelViewport"]
anchor_right = 1.0
anchor_bottom = 1.0
custom_fonts/font = SubResource( 3 )
custom_fonts/font = SubResource( 2 )
align = 1
valign = 1
[node name="LabelSprite" type="Sprite3D" parent="."]
transform = Transform( 0.5853, 0.810817, -3.54419e-008, 0, -4.37114e-008, -1, -0.810817, 0.5853, -2.55843e-008, 1.05839e-009, 0.0298628, 7.64017e-010 )
texture = SubResource( 1 )
texture = SubResource( 3 )
region_rect = Rect2( 0, 0, 30, 30 )
[connection signal="mouse_entered" from="." to="." method="_mouse_entered"]
[connection signal="mouse_exited" from="." to="." method="_mouse_exited"]

View file

@ -13,21 +13,18 @@ export var mouseHandThreshold = 0.85
var holdingCard: Card = null
var focusedCard: Card = null
var currentZone: Stack = null
var mouseOrigin: Vector2
var lastCameraTransform: Transform
onready var zones: Dictionary = {
"player1.deck": $Cards/Deck,
"player1.banished": $Cards/Banished,
"player1.graveyard": $Cards/Graveyard
}
func _ready():
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, true)
add_card("ff36", 0, false)
reorder_hand()
@ -52,9 +49,11 @@ func _process(delta: float):
camera.transform.origin = lastCameraTransform.origin - Vector3(mousePos.x, 0, mousePos.y)
# If holding a card, move it between board/hand
if holdingCard != null:
# Check if we're selecting our hand while holding a card
var relPos: Vector2 = absMousePos / get_viewport().size
var xMargin = 1.0-HAND_SCREEN_PERC
var selectingHand: bool = relPos.y > mouseHandThreshold and (relPos.x > xMargin/2 and relPos.x < (1.0-xMargin/2))
# Move card in/out hand
if selectingHand and not holdingCard.inHand:
holdingCard.inHand = true
call_deferred("reparent", holdingCard, cards, hand)
@ -62,6 +61,42 @@ func _process(delta: float):
holdingCard.inHand = false
call_deferred("reparent", holdingCard, hand, cards)
# Move card to/from zone
if currentZone != null:
if holdingCard.inZone:
if holdingCard.zoneName != currentZone.zoneName:
# Move from old zone to new
call_deferred("reparent", holdingCard, zones[holdingCard.zoneName], currentZone)
holdingCard.zoneName = currentZone.zoneName
else:
holdingCard.inZone = true
holdingCard.zoneName = currentZone.zoneName
if holdingCard.inHand:
call_deferred("reparent", holdingCard, hand, currentZone)
else:
call_deferred("reparent", holdingCard, cards, currentZone)
elif holdingCard.inZone:
# Move from zone to hand/field
if holdingCard.inHand:
call_deferred("reparent", holdingCard, zones[holdingCard.zoneName], hand)
else:
call_deferred("reparent", holdingCard, zones[holdingCard.zoneName], cards)
holdingCard.inZone = false
holdingCard.zoneName = ""
func _physics_process(delta):
# Do some raycast magic that normal Godot events cannot get (like ignoring items)
var space_state := get_world().direct_space_state
var absMousePos := get_viewport().get_mouse_position()
var from: Vector3 = camera.project_ray_origin(absMousePos)
var to: Vector3 = from + camera.project_ray_normal(absMousePos) * 1000
var result := space_state.intersect_ray(from, to, [self, holdingCard], 0x7FFFFFFF, true, true)
if result:
if result.collider is Stack:
currentZone = result.collider
else:
currentZone = null
func _card_picked(card: Card):
holdingCard = card
holdingCard.animation.play("lift")

View file

@ -14,14 +14,20 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://Scenes/Scripts/Board.gd"
}, {
"base": "Spatial",
"base": "Area",
"class": "Card",
"language": "GDScript",
"path": "res://Scenes/Components/Card.gd"
}, {
"base": "Area",
"class": "Stack",
"language": "GDScript",
"path": "res://Scenes/Components/Stack.gd"
} ]
_global_script_class_icons={
"Board": "",
"Card": ""
"Card": "",
"Stack": ""
}
[application]