wip: reworking cards to use sprites and fixing up some stuff related to them

This commit is contained in:
ShyProton 2025-04-27 20:05:28 -04:00
parent d1ef1d6f4f
commit 7cddb502b4
10 changed files with 148 additions and 76 deletions

View File

@ -9,12 +9,13 @@ var _emitted_done = 0
signal fetch_start
var _emitted_start = 0
var _consts = preload("res://data/consts.gd")
func _all_downloads_done() -> bool:
return _emitted_done == _emitted_start
func _setup_cache_in_mem():
var file = FileAccess.open("user://bulk.json", FileAccess.READ)
_bulk_data = JSON.parse_string(file.get_as_text())
@ -34,14 +35,16 @@ func setup() -> Error:
_setup_cache_in_mem()
return OK
func _init() -> void:
_req_headers = PackedStringArray(["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"])
_req_headers = PackedStringArray(
["User-Agent: " + _consts.APP_NAME + "/" + _consts.APP_VERSION, "Accept: */*"]
)
fetch_done.connect(_on_end_emit)
fetch_start.connect(_on_start_emit)
func _on_start_emit() -> void:
_emitted_start += 1
@ -49,6 +52,7 @@ func _on_start_emit() -> void:
func _on_end_emit() -> void:
_emitted_done += 1
func has_emitted_all() -> bool:
return _emitted_start == _emitted_done
@ -63,6 +67,7 @@ func _get_dict_from_file(filepath: String) -> Dictionary:
return data
## get_card_data_from_name
##
## _name: String [br]
@ -99,7 +104,9 @@ func _get_card_data_from_bulk(field: String, search_query: String) -> Dictionary
dir.make_dir_recursive("user://card_cache/" + selected_entry["id"] + "/")
dir = null
var file = FileAccess.open("user://card_cache/" + selected_entry["id"] + "/card.json", FileAccess.WRITE)
var file = FileAccess.open(
"user://card_cache/" + selected_entry["id"] + "/card.json", FileAccess.WRITE
)
file.store_line(JSON.stringify(selected_entry, "\t"))
file.close()
@ -107,6 +114,7 @@ func _get_card_data_from_bulk(field: String, search_query: String) -> Dictionary
return selected_entry
func _fetch_card_img(data: Dictionary) -> Error:
fetch_start.emit()
if FileAccess.file_exists("user://card_cache/" + data["id"] + "card.png"):
@ -138,6 +146,7 @@ func _fetch_card_img(data: Dictionary) -> Error:
return OK
func get_bulk_data(force: bool) -> Error:
if FileAccess.file_exists("user://bulk.json"):
if force:
@ -164,7 +173,6 @@ func get_bulk_data(force: bool) -> Error:
push_error(_cache_error("PARSING") + "Failed to parse the Scryfall card results.")
return FAILED
error = httpr.request(card_content["download_uri"], _req_headers)
if error != OK:
push_error(_cache_error("GET_REQUEST") + "An error occurred in the Scryfall request.")
@ -189,7 +197,10 @@ func get_bulk_data(force: bool) -> Error:
return OK
func _notification(what):
if what == NOTIFICATION_PREDELETE:
if !_all_downloads_done():
push_error("ERR::MEM::CACHE\nCache being deleted before all threads have finished processing!")
push_error(
"ERR::MEM::CACHE\nCache being deleted before all threads have finished processing!"
)

57
card.gd
View File

@ -5,12 +5,7 @@ extends TextureRect
## Represents an instance of a card to be displayed on the tabletop.
## Contains helper text for the text, the cards ID, and the image path.
enum pivot {
ROTATE_0,
ROTATE_90,
ROTATE_180,
ROTATE_270
}
enum pivot { ROTATE_0, ROTATE_90, ROTATE_180, ROTATE_270 }
var current_pivot = pivot.ROTATE_0
@ -19,14 +14,16 @@ var card_name: String
var card_type: String
var oracle_text: String
var is_dragging = false
var is_pivot = false
# Tween trackers for distinguishing which state the card is in.
var is_focused = false # Is the card a focus?
var is_dragging = false # Is the card currently being dragged?
var is_travelling = false # Is the card moving to a location?
var is_pivot = false # Is the card currently waiting to be tapped/untapped?
var delay = 5.0
var mouse_offset: Vector2
func _pivot() -> int:
var deg: int
match current_pivot:
@ -42,11 +39,23 @@ func _pivot() -> int:
func _physics_process(delta: float) -> void:
if is_dragging == true:
var tween = get_tree().create_tween()
tween.tween_property(self, "position", get_global_mouse_position() - mouse_offset, delay * delta)
if is_pivot == true:
_tweening(delta)
func _tweening(delta: float) -> void:
var tween = get_tree().create_tween()
if is_focused:
tween.tween_property(self, "scale", Vector2(1.05, 1.05), delta * delay)
else:
tween.tween_property(self, "scale", Vector2(1.0, 1.0), delta * delay)
if is_dragging:
tween.tween_property(
self, "position", get_global_mouse_position() - mouse_offset, delay * delta
)
if is_pivot:
tween.tween_property(self, "rotation_degrees", _pivot(), delta * delay)
is_pivot = false
@ -62,9 +71,25 @@ func _gui_input(event: InputEvent) -> void:
else:
is_dragging = false
MOUSE_BUTTON_RIGHT:
# TODO: Tooltip menu for right-button clicking on cards.
pass
func _on_mouse_entered() -> void:
is_focused = true
func _on_mouse_exited() -> void:
if not is_dragging:
is_focused = false
func travel_to(position: Vector2) -> Error:
# TODO: Check whether position to travel to is within the confines of where this card can go.
return OK
func _unhandled_key_input(event: InputEvent) -> void:
if not event.is_action_pressed("default_action"):
return
@ -76,6 +101,7 @@ func _unhandled_key_input(event: InputEvent) -> void:
is_pivot = true
set_pivot_offset(size / 2)
func _card_error(error_type: String) -> String:
return "ERROR::CARD::%s::%s::%s::\n" % [card_id, card_name, error_type]
@ -140,16 +166,11 @@ func _load_image() -> Error:
push_error("%sCard on-board image failed to load correctly" % _card_error("IMAGE"))
return FAILED
# TODO: Get the size from the node or some constant variable.
image.resize(int(size.x), int(size.y), Image.INTERPOLATE_LANCZOS)
var image_texture = ImageTexture.new()
image_texture.set_image(image)
#expand_mode = TextureRect.EXPAND_FIT_WIDTH
texture = image_texture
return OK

View File

@ -1,7 +1,9 @@
[gd_scene load_steps=2 format=3 uid="uid://cah3mvdnom1xg"]
[gd_scene load_steps=3 format=3 uid="uid://cah3mvdnom1xg"]
[ext_resource type="Script" uid="uid://b3yqd1qu7dyq" path="res://card.gd" id="1_kikvd"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kikvd"]
[node name="Card" type="TextureRect"]
offset_left = 794.0
offset_top = 79.0
@ -9,3 +11,11 @@ offset_right = 919.0
offset_bottom = 254.0
mouse_default_cursor_shape = 2
script = ExtResource("1_kikvd")
[node name="Area2D" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource("RectangleShape2D_kikvd")
[connection signal="mouse_entered" from="." to="." method="_on_mouse_entered"]
[connection signal="mouse_exited" from="." to="." method="_on_mouse_exited"]

View File

@ -6,6 +6,7 @@ var _decklist
var cards: String
func _init(_cards: String) -> void:
cards = _cards
_decklist = Dictionary()

15
hand.gd Normal file
View File

@ -0,0 +1,15 @@
extends StaticBody2D
var cards: Array[Node] = []
var _card_class = preload("res://card.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

1
hand.gd.uid Normal file
View File

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

View File

@ -19,6 +19,7 @@ func _init(_decklist: Dictionary) -> void:
for i in _num:
lib_cards.push_back(card)
func add_cards(cards: Array, top: bool) -> void:
for card in cards:
add_card(card, top)

View File

@ -7,13 +7,16 @@ var fields: Array[Node] = []
var decks: Array[Dictionary]
func _load_decks():
if !FileAccess.file_exists("user://decks.json"):
return # no loaded decks
var file = FileAccess.open("user://decks.json", FileAccess.READ)
decks = JSON.parse_string(file.get_as_text())
file.close()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
# The first field in the array will be the player's own field.

View File

@ -1,12 +1,21 @@
[gd_scene load_steps=3 format=3 uid="uid://cx0vga81xwckh"]
[gd_scene load_steps=5 format=3 uid="uid://cx0vga81xwckh"]
[ext_resource type="Script" uid="uid://w2rqm1u7p7im" path="res://player.gd" id="1_4flbx"]
[ext_resource type="Script" uid="uid://dvu4gdhqjejeo" path="res://hand.gd" id="2_i3pqv"]
[ext_resource type="Script" uid="uid://bc51go8t8uvts" path="res://library.gd" id="2_onrkg"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_onrkg"]
size = Vector2(1920, 200)
[node name="Player" type="Node2D"]
script = ExtResource("1_4flbx")
[node name="Hand" type="Node2D" parent="."]
[node name="Hand" type="StaticBody2D" parent="."]
script = ExtResource("2_i3pqv")
[node name="CollisionArea" type="CollisionShape2D" parent="Hand"]
position = Vector2(960, 980)
shape = SubResource("RectangleShape2D_onrkg")
[node name="Library" type="Node2D" parent="."]
script = ExtResource("2_onrkg")

View File

@ -4,12 +4,13 @@ var player_class = preload("res://player.gd")
var deck_input = preload("res://deck_input.gd")
var _caching = preload("res://caching.gd")
var cards = "1 All That Glitters\n1 Ancestral Mask\n1 Angelic Destiny\n1 Arcane Signet\n1 Archon of Sun's Grace\n1 Austere Command\n1 Banishing Light\n1 Bear Umbra\n1 Blossoming Sands\n1 Canopy Vista\n1 Celestial Mantle\n1 Collective Resistance\n1 Command Tower\n1 Danitha Capashen, Paragon\n1 Danitha, New Benalia's Light\n1 Darksteel Mutation\n1 Daybreak Coronet\n1 Destiny Spinner\n1 Eidolon of Blossoms\n1 Eidolon of Countless Battles\n1 Ellivere of the Wild Court\n1 Enchantress's Presence\n1 Envoy of the Ancestors\n1 Ethereal Armor\n1 Fertile Ground\n13 Forest\n1 Frantic Strength\n1 Generous Gift\n1 Gilded Lotus\n1 Glittering Frost\n1 Grasp of Fate\n1 Gylwain, Casting Director\n1 Hall of Heliod's Generosity\n1 Heliod's Pilgrim\n1 Hidden Grotto\n1 Horrid Vigor\n1 Idyllic Tutor\n1 Jukai Naturalist\n1 Kenrith's Transformation\n1 Kor Spiritdancer\n1 Krosan Verge\n1 Light-Paws, Emperor's Voice\n1 Luminous Broodmoth\n1 Mantle of the Ancients\n1 Overgrowth\n1 Overprotect\n1 Pacifism\n14 Plains\n1 Rancor\n1 Retether\n1 Rogue's Passage\n1 Sage's Reverie\n1 Sanctum Weaver\n1 Selesnya Guildgate\n1 Setessan Champion\n1 Shalai, Voice of Plenty\n1 Snake Umbra\n1 Sol Ring\n1 Solemnity\n1 Songbirds' Blessing\n1 Starfield Mystic\n1 Swords to Plowshares\n1 Tanglespan Lookout\n1 Timber Paladin\n1 Timely Ward\n1 Tithe Takern1 Transcendent Envoy\n1 Twinblade Blessing\n1 Umbra Mystic\n1 Unfinished Business\n1 Utopia Sprawl\n1 Wild Growth\n1 Winds of Rath\n1 Yenna, Redtooth Regent\n1 Sythis, Harvest's Hand"
func _bulk_callback(cache) -> void:
cache.setup()
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var cache = _caching.new()
@ -25,9 +26,8 @@ func _ready() -> void:
cache.get_card_data_from_name("1996 World Champion")
var deck = deck_input.new(cards)
add_child(deck)
# var deck = deck_input.new(cards)
# add_child(deck)
pass # Replace with function body.