150 lines
4.1 KiB
GDScript
150 lines
4.1 KiB
GDScript
extends Area
|
|
|
|
class_name Card
|
|
|
|
signal card_selected()
|
|
signal card_unselected()
|
|
signal card_picked()
|
|
signal card_dropped()
|
|
signal card_dropped_anim()
|
|
signal card_menu()
|
|
|
|
var clicked := false
|
|
var exhausted := false
|
|
var lifted := false
|
|
var flipped := false
|
|
var cardID := "" setget set_cardid
|
|
var playerID := 0
|
|
var inHand := false
|
|
var inZone := false
|
|
var zoneName = ""
|
|
var cardMat = preload("res://MLPAssets/Placeholders/CardMat.tres")
|
|
var ready = false
|
|
|
|
onready var animation := $CardModel/Border/AnimationPlayer
|
|
onready var cardImage := $CardModel/Border/CardImage
|
|
onready var texture: Texture = cardImage.get_surface_material(0)
|
|
|
|
func set_cardid(val: String):
|
|
cardID = val
|
|
if ready:
|
|
Remote.fetch(Remote.IMGURL + val + ".jpg", self, "_card_fetched")
|
|
|
|
func _ready():
|
|
Remote.fetch(Remote.IMGURL + cardID + ".jpg", self, "_card_fetched")
|
|
ready = true
|
|
|
|
func _mouse_hover():
|
|
emit_signal("card_selected")
|
|
|
|
func _mouse_blur():
|
|
emit_signal("card_unselected")
|
|
|
|
# warning-ignore:unused_argument
|
|
func _input_event(camera, event, click_position, click_normal, shape_idx):
|
|
# Mouse motion
|
|
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)
|
|
if inHand:
|
|
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
|
|
if flipped:
|
|
$CardModel/Border.translation = Vector3(0, 0.01, 0)
|
|
$CardModel/Border.rotation = Vector3(0, 0, PI)
|
|
else:
|
|
$CardModel/Border.translation = Vector3.ZERO
|
|
$CardModel/Border.rotation = Vector3.ZERO
|
|
else:
|
|
var denom := Vector3.UP.dot(direction)
|
|
var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom;
|
|
translation = origin + direction * t
|
|
# 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:
|
|
# Update click status
|
|
clicked = event.pressed
|
|
|
|
# Check for card dropped
|
|
if not clicked and lifted:
|
|
emit_signal("card_dropped")
|
|
input_ray_pickable = true
|
|
lifted = false
|
|
|
|
# Check double click
|
|
if event.doubleclick and not inHand and not lifted and not animation.is_playing():
|
|
if exhausted:
|
|
animation.play_backwards("tap")
|
|
exhausted = false
|
|
else:
|
|
animation.play("tap")
|
|
exhausted = true
|
|
|
|
func reset_transform():
|
|
$CardModel.translation = Vector3.ZERO
|
|
$CardModel.rotation = Vector3.ZERO
|
|
|
|
func _menu_action(id: int):
|
|
match id:
|
|
0: # Flip
|
|
if flipped:
|
|
animation.play_backwards("flip")
|
|
flipped = false
|
|
else:
|
|
animation.play("flip")
|
|
flipped = true
|
|
|
|
func _check_drop_anim(anim_name):
|
|
if anim_name == "drop":
|
|
emit_signal("card_dropped_anim")
|
|
|
|
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
|
|
|
|
# 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()
|
|
var err = img.load_jpg_from_buffer(body)
|
|
# 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
|
|
assert(err == OK)
|
|
var mat = cardMat.duplicate()
|
|
texture = ImageTexture.new()
|
|
texture.create_from_image(img)
|
|
mat.albedo_texture = texture
|
|
cardImage.set_surface_material(0, mat)
|