mlpcardgame/Scenes/Components/Card.gd

82 lines
2 KiB
GDScript3
Raw Normal View History

2019-05-04 21:42:07 +00:00
extends Spatial
class_name Card
2019-05-04 22:01:00 +00:00
signal card_selected()
signal card_unselected()
2019-05-05 19:19:39 +00:00
signal card_picked()
signal card_dropped()
2019-05-05 23:35:58 +00:00
signal card_menu()
2019-05-04 21:42:07 +00:00
2019-05-05 23:09:19 +00:00
var clicked := false
var exhausted := false
2019-05-04 23:18:55 +00:00
var lifted := false
2019-05-05 23:35:58 +00:00
var flipped := false
export var cardID := ""
export var playerID := 0
2019-05-05 19:19:39 +00:00
export var inHand := false
2019-05-04 23:18:55 +00:00
onready var animation := $Border/AnimationPlayer
2019-05-04 21:42:07 +00:00
func _mouse_hover():
2019-05-04 22:01:00 +00:00
emit_signal("card_selected")
2019-05-04 21:42:07 +00:00
2019-05-04 22:01:00 +00:00
func _mouse_blur():
2019-05-04 23:18:55 +00:00
emit_signal("card_unselected")
func _input_event(camera, event, click_position, click_normal, shape_idx):
2019-05-05 23:35:58 +00:00
# Mouse motion
2019-05-05 23:09:19 +00:00
if clicked and event is InputEventMouseMotion:
if not lifted:
animation.play("lift")
if inHand:
animation.advance(1.0)
emit_signal("card_picked")
lifted = true
2019-05-04 23:18:55 +00:00
var origin: Vector3 = camera.project_ray_origin(event.position)
var direction: Vector3 = camera.project_ray_normal(event.position)
2019-05-05 19:19:39 +00:00
if inHand:
transform.origin = Vector3.ZERO + Vector3.RIGHT * direction * 3.0
else:
var denom := Vector3.UP.dot(direction)
var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom;
transform.origin = origin + direction * t
2019-05-05 23:35:58 +00:00
# Right click
if event is InputEventMouseButton and event.pressed and event.button_index == BUTTON_RIGHT:
# Show menu
emit_signal("card_menu")
# Left click
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
2019-05-05 23:09:19 +00:00
# Update click status
clicked = event.pressed
# Check for card dropped
if not clicked and lifted:
if inHand:
animation.play("drop")
else:
animation.play_backwards("lift")
emit_signal("card_dropped")
2019-05-05 23:09:19 +00:00
lifted = false
# Check double click
if event.doubleclick and not lifted and not animation.is_playing():
if exhausted:
animation.play_backwards("tap")
exhausted = false
else:
animation.play("tap")
2019-05-05 23:35:58 +00:00
exhausted = true
2019-05-06 11:12:57 +00:00
func reset_transform():
$Border.transform.origin = Vector3.ZERO
2019-05-05 23:35:58 +00:00
func _menu_action(id: int):
match id:
0: # Flip
if flipped:
animation.play_backwards("flip")
flipped = false
else:
animation.play("flip")
flipped = true