exports card loading handling into its own node and script

This commit is contained in:
ShyProton 2025-04-28 02:48:46 -04:00
parent 2f5185d22c
commit a7aad7d99f
5 changed files with 90 additions and 74 deletions

View File

@ -34,6 +34,14 @@ func check_hover() -> void:
_on_mouse_exited()
func error(error_type: String) -> String:
return "ERROR::CARD::%s::%s::%s::\n" % [card_id, card_name, error_type]
func colission_size() -> Vector2:
return $Area2D/CollisionShape2D.shape.size
func _physics_process(delta: float) -> void:
focused = hovered or dragging
@ -61,72 +69,3 @@ func _on_mouse_exited() -> void:
Input.set_default_cursor_shape(Input.CURSOR_ARROW)
$TweenController.scale(1.0)
func _card_error(error_type: String) -> String:
return "ERROR::CARD::%s::%s::%s::\n" % [card_id, card_name, error_type]
func _ready() -> void:
var load_status = _load_card()
if load_status != OK:
# TODO: No need to push another error as the failure state of loading does that already,
# if the card is not cached, perhaps a placeholder blank card can be used instead?
# Setting that up can be put here later...
push_error("Failed to load card.")
func _load_card() -> Error:
if _load_data() != OK:
return FAILED
if _load_image() != OK:
return FAILED
return OK
func _load_data() -> Error:
var cached_json = FileAccess.get_file_as_string("user://card_cache/" + card_id + "/card.json")
if cached_json.is_empty():
push_error("%s\nCard json data was not found in cache" % _card_error("CACHE"))
return FAILED
var card_json = JSON.parse_string(cached_json)
if card_json == null:
push_error("%s\nCard json data is could not be parsed as valid json" % _card_error("DATA"))
return FAILED
card_name = card_json["name"]
card_type = card_json["type_line"]
oracle_text = card_json["oracle_text"]
return OK
func _load_image() -> Error:
# NOTE: Assuming we're going with using the .png cards on board.
var cached_img = FileAccess.get_file_as_bytes("user://card_cache/" + card_id + "/card.png")
if cached_img.is_empty():
push_error("%sCard on-board image was not found in cache" % _card_error("CACHE"))
return FAILED
var image = Image.new()
var image_status: Error = image.load_png_from_buffer(cached_img)
if image_status != OK:
push_error("%sCard on-board image failed to load correctly" % _card_error("IMAGE"))
return FAILED
var size = $Area2D/CollisionShape2D.shape.size
image.resize(int(size.x), int(size.y), Image.INTERPOLATE_LANCZOS)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
$Sprite2D.texture = image_texture
return OK

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=5 format=3 uid="uid://cah3mvdnom1xg"]
[gd_scene load_steps=6 format=3 uid="uid://cah3mvdnom1xg"]
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://scenes/card/card.gd" id="1_kikvd"]
[ext_resource type="Script" uid="uid://bkk0pyypi1id7" path="res://scenes/card/tween.gd" id="2_imta7"]
[ext_resource type="Script" uid="uid://dhgk6fhw8oua0" path="res://scenes/card/input.gd" id="3_vtcvk"]
[ext_resource type="Script" uid="uid://vckbno504iay" path="res://scenes/card/load.gd" id="4_g65cd"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kikvd"]
size = Vector2(125, 175)
@ -17,11 +18,14 @@ script = ExtResource("1_kikvd")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource("RectangleShape2D_kikvd")
[node name="TweenController" type="Node2D" parent="."]
[node name="TweenController" type="Node" parent="."]
script = ExtResource("2_imta7")
[node name="InputHandler" type="Node2D" parent="."]
[node name="InputHandler" type="Node" parent="."]
script = ExtResource("3_vtcvk")
[node name="DataLoader" type="Node" parent="."]
script = ExtResource("4_g65cd")
[connection signal="mouse_entered" from="Area2D" to="." method="_on_mouse_entered"]
[connection signal="mouse_exited" from="Area2D" to="." method="_on_mouse_exited"]

View File

@ -1,4 +1,4 @@
extends Node2D
extends Node
var card: Node
var tween_controller: Node
@ -21,7 +21,7 @@ func handle_inputs(delta: float) -> void:
if Input.is_action_just_pressed("SELECT"):
card.dragging = true
Input.set_default_cursor_shape(Input.CURSOR_DRAG)
card.mouse_offset = get_global_mouse_position() - card.global_position
card.mouse_offset = card.get_global_mouse_position() - card.global_position
if Input.is_action_just_released("SELECT"):
card.dragging = false
card.check_hover()

72
scenes/card/load.gd Normal file
View File

@ -0,0 +1,72 @@
extends Node
var card: Node
func _ready() -> void:
card = get_parent()
if _load_card() != OK:
# TODO: No need to push another error as the failure state of loading does that already,
# if the card is not cached, perhaps a placeholder blank card can be used instead?
# Setting that up can be put here later...
push_error("Failed to load card.")
func _load_card() -> Error:
if _load_data() != OK:
return FAILED
if _load_image() != OK:
return FAILED
return OK
func _load_data() -> Error:
var cached_json = FileAccess.get_file_as_string(
"user://card_cache/" + card.card_id + "/card.json"
)
if cached_json.is_empty():
push_error("%s\nCard json data was not found in cache" % card.error("CACHE"))
return FAILED
var card_json = JSON.parse_string(cached_json)
if card_json == null:
push_error("%s\nCard json data is could not be parsed as valid json" % card.error("DATA"))
return FAILED
card.card_name = card_json["name"]
card.card_type = card_json["type_line"]
card.oracle_text = card_json["oracle_text"]
return OK
func _load_image() -> Error:
# NOTE: Assuming we're going with using the .png cards on board.
var cached_img = FileAccess.get_file_as_bytes("user://card_cache/" + card.card_id + "/card.png")
if cached_img.is_empty():
push_error("%sCard on-board image was not found in cache" % card.error("CACHE"))
return FAILED
var image = Image.new()
var image_status: Error = image.load_png_from_buffer(cached_img)
if image_status != OK:
push_error("%sCard on-board image failed to load correctly" % card.error("IMAGE"))
return FAILED
var size = card.colission_size()
image.resize(int(size.x), int(size.y), Image.INTERPOLATE_LANCZOS)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
var card_sprite = card.get_node("Sprite2D")
card_sprite.texture = image_texture
return OK

1
scenes/card/load.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://vckbno504iay