mlpcardgame/Scenes/Components/Card.gd

130 lines
3.7 KiB
GDScript3
Raw Normal View History

2019-05-07 18:56:26 +00:00
extends Area
2019-05-04 21:42:07 +00:00
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()
signal card_dropped_anim()
2019-05-05 23:35:58 +00:00
signal card_menu()
2019-05-20 21:52:38 +00:00
signal card_moved(origin, direction, t)
signal card_clicked()
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
var cardID := "" setget set_cardid
2019-05-07 18:56:26 +00:00
var playerID := 0
var inHand := false
var inZone := false
var zoneName = ""
var cardMat = preload("res://MLPAssets/Placeholders/CardMat.tres")
var ready = false
2019-05-04 23:18:55 +00:00
2019-05-20 21:52:38 +00:00
onready var border := $CardModel/Border
2019-05-11 19:36:44 +00:00
onready var animation := $CardModel/Border/AnimationPlayer
onready var handanim := $CardModel/Border/HandAnimPlayer # Hack to get hand animations off the main queue
onready var cardImage := $CardModel/Border/CardImage
onready var texture: Texture = cardImage.get_surface_material(0)
func set_cardid(val: String):
cardID = val
if ready:
Resources.get_card(val, self, "_card_fetched")
func _ready():
Resources.get_card(cardID, self, "_card_fetched")
ready = true
2019-05-04 23:18:55 +00:00
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")
# warning-ignore:unused_argument
2019-05-04 23:18:55 +00:00
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:
emit_signal("card_picked")
2019-05-07 18:56:26 +00:00
# Disable input for now
input_ray_pickable = false
2019-05-05 23:09:19 +00:00
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-20 21:52:38 +00:00
var denom := Vector3.UP.dot(direction)
var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom;
emit_signal("card_moved", 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
2019-05-12 21:37:44 +00:00
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and clickable():
2019-05-05 23:09:19 +00:00
# Update click status
clicked = event.pressed
2019-05-06 22:18:10 +00:00
2019-05-05 23:09:19 +00:00
# Check for card dropped
if not clicked and lifted:
emit_signal("card_dropped")
2019-05-07 18:56:26 +00:00
input_ray_pickable = true
2019-05-05 23:09:19 +00:00
lifted = false
2019-05-06 22:18:10 +00:00
2019-05-05 23:09:19 +00:00
# Check double click
if event.doubleclick and not inHand and not lifted and not animation.is_playing():
2019-05-20 21:52:38 +00:00
emit_signal("card_clicked")
func _check_drop_anim(anim_name):
if anim_name == "drop":
emit_signal("card_dropped_anim")
# warning-ignore:unused_argument
func _card_fetched(result: int, response_code: int, headers: PoolStringArray, body: PoolByteArray):
assert(result == HTTPRequest.RESULT_SUCCESS)
var img = Image.new()
2019-05-27 22:57:40 +00:00
var err = img.load_webp_from_buffer(body)
2019-05-12 21:37:44 +00:00
assert(err == OK)
# Depending if card pic is a problem or not, rotate the card image mesh
if img.get_width() > img.get_height():
cardImage.scale = Vector3(0.45,1,0.319)
cardImage.rotation.y = PI/2
else:
cardImage.scale = Vector3(0.319,1,0.45)
cardImage.rotation.y = 0
var mat = cardMat.duplicate()
texture = ImageTexture.new()
texture.create_from_image(img)
mat.albedo_texture = texture
cardImage.set_surface_material(0, mat)
func tween_move_to(targetPos: Vector3, duration: float = 0.1):
$Tween.interpolate_property(self, "translation",
translation, targetPos, duration,
Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
func tween_rotate(targetRot: Vector3, duration: float = 0.1):
$Tween.interpolate_property(self, "rotation",
rotation, targetRot, duration,
Tween.TRANS_CUBIC, Tween.EASE_IN_OUT)
func tween():
$Tween.start()
func disableInput():
$CollisionShape.disabled = true
func enableInput():
$CollisionShape.disabled = false
func clickable() -> bool:
#TODO Better control over if a card can be taken or not
if playerID == 0:
return true
return false
func reset_transform():
$CardModel.translation = Vector3.ZERO
$CardModel.rotation = Vector3.ZERO