diff --git a/scenes/card/card.gd b/scenes/card/card.gd index f509bc0..e2cd00b 100644 --- a/scenes/card/card.gd +++ b/scenes/card/card.gd @@ -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 diff --git a/scenes/card/card.tscn b/scenes/card/card.tscn index 2e0bf98..d6a58e4 100644 --- a/scenes/card/card.tscn +++ b/scenes/card/card.tscn @@ -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"] diff --git a/scenes/card/input.gd b/scenes/card/input.gd index 29fb8fd..e643983 100644 --- a/scenes/card/input.gd +++ b/scenes/card/input.gd @@ -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() diff --git a/scenes/card/load.gd b/scenes/card/load.gd new file mode 100644 index 0000000..5603660 --- /dev/null +++ b/scenes/card/load.gd @@ -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 diff --git a/scenes/card/load.gd.uid b/scenes/card/load.gd.uid new file mode 100644 index 0000000..b9bcf60 --- /dev/null +++ b/scenes/card/load.gd.uid @@ -0,0 +1 @@ +uid://vckbno504iay