feat: pulling dragons from server working

This commit is contained in:
Gerard Gascón 2025-04-12 00:21:07 +02:00
parent 50bc814367
commit b080ab7bf4
11 changed files with 46 additions and 193 deletions

View file

@ -1,10 +1,7 @@
[gd_scene load_steps=6 format=3 uid="uid://tubxrqxjic6r"] [gd_scene load_steps=3 format=3 uid="uid://tubxrqxjic6r"]
[ext_resource type="PackedScene" uid="uid://bs45hvb2mpw4k" path="res://scenes/dragon_list/dragon_properties.tscn" id="1_4vct3"] [ext_resource type="PackedScene" uid="uid://bs45hvb2mpw4k" path="res://scenes/dragon_list/dragon_properties.tscn" id="1_4vct3"]
[ext_resource type="Script" uid="uid://dm8d0ikf1n8qa" path="res://scenes/dragon_list/dragon_list_view.gd" id="1_q7g8i"] [ext_resource type="Script" uid="uid://dm8d0ikf1n8qa" path="res://scenes/dragon_list/dragon_list_view.gd" id="1_q7g8i"]
[ext_resource type="Resource" uid="uid://cnhwn6kfcbjl5" path="res://assets/outfits/hats.tres" id="3_xxwat"]
[ext_resource type="Resource" uid="uid://etegcak2sphs" path="res://assets/outfits/shirts.tres" id="4_7mhmu"]
[ext_resource type="Resource" uid="uid://ba5684xylts3f" path="res://assets/outfits/shoes.tres" id="5_h4iwm"]
[node name="DragonList" type="Window" node_paths=PackedStringArray("dragon_list_view")] [node name="DragonList" type="Window" node_paths=PackedStringArray("dragon_list_view")]
size = Vector2i(450, 700) size = Vector2i(450, 700)
@ -12,9 +9,6 @@ always_on_top = true
script = ExtResource("1_q7g8i") script = ExtResource("1_q7g8i")
dragon_list_entry = ExtResource("1_4vct3") dragon_list_entry = ExtResource("1_4vct3")
dragon_list_view = NodePath("CanvasLayer/ScrollContainer/VBoxContainer") dragon_list_view = NodePath("CanvasLayer/ScrollContainer/VBoxContainer")
hat_outfits = ExtResource("3_xxwat")
shirt_outfits = ExtResource("4_7mhmu")
shoes_outfits = ExtResource("5_h4iwm")
[node name="CanvasLayer" type="CanvasLayer" parent="."] [node name="CanvasLayer" type="CanvasLayer" parent="."]

View file

@ -3,15 +3,13 @@ class_name DragonListEntry
@export var texture_rect: TextureRect @export var texture_rect: TextureRect
@export var name_label: Label @export var name_label: Label
@export var code_label: Label @export var origin_label: Label
var code_generator: CodeGenerator
@export var hat_outfits: DragonOutfit @export var hat_outfits: DragonOutfit
@export var shirt_outfits: DragonOutfit @export var shirt_outfits: DragonOutfit
@export var shoes_outfits: DragonOutfit @export var shoes_outfits: DragonOutfit
func set_properties(name: String, hat: Texture2D, shirt: Texture2D, shoes: Texture2D) -> void: func set_properties(properties: DragonProperties) -> void:
name_label.text = name name_label.text = properties.name
code_label.text = CodeGenerator.new(hat_outfits, shirt_outfits, shoes_outfits).encrypt(hat, shirt, shoes, name) origin_label.text = properties.origin

View file

@ -4,25 +4,18 @@ class_name DragonList
@export var dragon_list_entry: PackedScene @export var dragon_list_entry: PackedScene
@export var dragon_list_view: VBoxContainer @export var dragon_list_view: VBoxContainer
@export var hat_outfits: DragonOutfit
@export var shirt_outfits: DragonOutfit
@export var shoes_outfits: DragonOutfit
var _library: DragonLibrary var _library: DragonLibrary
func set_library(library: DragonLibrary): func set_library(library: DragonLibrary):
_library = library _library = library
for d in _library.dragons: for d in _library.dragons:
var hat: Texture2D = hat_outfits.get_texture(d['hat']) append_dragon(d)
var shirt: Texture2D = shirt_outfits.get_texture(d['shirt'])
var shoes: Texture2D = shoes_outfits.get_texture(d['shoes'])
append_dragon(d['name'], hat, shirt, shoes)
#
func append_dragon(name: String, hat: Texture2D, shirt: Texture2D, shoes: Texture2D):
func append_dragon(properties: DragonProperties):
var dragon: DragonListEntry = dragon_list_entry.instantiate() var dragon: DragonListEntry = dragon_list_entry.instantiate()
dragon.set_properties(name, hat, shirt, shoes) dragon.set_properties(properties)
dragon_list_view.add_child(dragon) dragon_list_view.add_child(dragon)

View file

@ -7,14 +7,14 @@
[ext_resource type="Resource" uid="uid://etegcak2sphs" path="res://assets/outfits/shirts.tres" id="3_84ie0"] [ext_resource type="Resource" uid="uid://etegcak2sphs" path="res://assets/outfits/shirts.tres" id="3_84ie0"]
[ext_resource type="Resource" uid="uid://ba5684xylts3f" path="res://assets/outfits/shoes.tres" id="4_xvxt0"] [ext_resource type="Resource" uid="uid://ba5684xylts3f" path="res://assets/outfits/shoes.tres" id="4_xvxt0"]
[node name="DragonProperties" type="Control" node_paths=PackedStringArray("texture_rect", "name_label", "code_label")] [node name="DragonProperties" type="Control" node_paths=PackedStringArray("texture_rect", "name_label", "origin_label")]
custom_minimum_size = Vector2(0, 128) custom_minimum_size = Vector2(0, 128)
layout_mode = 3 layout_mode = 3
anchors_preset = 0 anchors_preset = 0
script = ExtResource("1_s7he7") script = ExtResource("1_s7he7")
texture_rect = NodePath("TextureRect") texture_rect = NodePath("TextureRect")
name_label = NodePath("Name") name_label = NodePath("Name")
code_label = NodePath("Code") origin_label = NodePath("Origin")
hat_outfits = ExtResource("2_y33em") hat_outfits = ExtResource("2_y33em")
shirt_outfits = ExtResource("3_84ie0") shirt_outfits = ExtResource("3_84ie0")
shoes_outfits = ExtResource("4_xvxt0") shoes_outfits = ExtResource("4_xvxt0")
@ -39,7 +39,7 @@ theme_override_fonts/font = ExtResource("2_s7he7")
theme_override_font_sizes/font_size = 36 theme_override_font_sizes/font_size = 36
text = "AAAAAAAA" text = "AAAAAAAA"
[node name="Code" type="Label" parent="."] [node name="Origin" type="Label" parent="."]
layout_mode = 0 layout_mode = 0
offset_left = 132.0 offset_left = 132.0
offset_top = 8.0 offset_top = 8.0

View file

@ -128,6 +128,9 @@ func _pick_dragon(properties: DragonProperties, drag: bool = true):
add_child(dragon) add_child(dragon)
_instantiated_dragons.erase(properties.id) _instantiated_dragons.erase(properties.id)
if not _library.has(properties):
_library.add_dragon(properties)
for spot in _filled_spots: for spot in _filled_spots:
if _filled_spots[spot] == properties.id: if _filled_spots[spot] == properties.id:
_filled_spots.erase(spot) _filled_spots.erase(spot)
@ -148,10 +151,11 @@ func _dragon_place_back(dragon: Dragon):
func _on_close_pressed() -> void: func _on_close_pressed() -> void:
_save_load.clear_dragons() _save_load.clear_dragons()
for d in _library.dragons: for d in _library.dragons:
if _dragon_entities.has(d['id']) and _dragon_entities[d['id']] != null: if _dragon_entities.has(d.id) and _dragon_entities[d.id] != null:
_save_load.add_dragon(d['id'], d['name'], d['hat'], d['shirt'], d['shoes'], _dragon_entities[d['id']].position) _save_load.add_dragon(d)
else: else:
_save_load.add_dragon(d['id'], d['name'], d['hat'], d['shirt'], d['shoes'], Vector2i(0, 0)) d.position = Vector2i(0, 0)
_save_load.add_dragon(d)
_save_load.save() _save_load.save()
await get_tree().process_frame await get_tree().process_frame
await get_tree().process_frame await get_tree().process_frame

View file

@ -63,6 +63,8 @@ func _process(delta: float) -> void:
State.FLYING: State.FLYING:
_move_to_target(delta) _move_to_target(delta)
properties.position = position
func dress(): func dress():
dragon.dress(properties) dragon.dress(properties)

View file

@ -1,147 +0,0 @@
class_name CodeGenerator
var hat_outfits: DragonOutfit
var shirt_outfits: DragonOutfit
var shoes_outfits: DragonOutfit
var key: int = 123
var alphabet: Dictionary = {
'A': '00',
'B': '01',
'C': '02',
'D': '03',
'E': '04',
'F': '05',
'G': '06',
'H': '07',
'I': '08',
'J': '09',
'K': '10',
'L': '11',
'M': '12',
'N': '13',
'O': '14',
'P': '15',
'Q': '16',
'R': '17',
'S': '18',
'T': '19',
'U': '20',
'V': '21',
'W': '22',
'X': '23',
'Y': '24',
'Z': '25',
' ': '26'
}
var alphabet_inverse: Dictionary = {
'00': 'A',
'01': 'B',
'02': 'C',
'03': 'D',
'04': 'E',
'05': 'F',
'06': 'G',
'07': 'H',
'08': 'I',
'09': 'J',
'10': 'K',
'11': 'L',
'12': 'M',
'13': 'N',
'14': 'O',
'15': 'P',
'16': 'Q',
'17': 'R',
'18': 'S',
'19': 'T',
'20': 'U',
'21': 'V',
'22': 'W',
'23': 'X',
'24': 'Y',
'25': 'Z',
'26': ' '
}
func _init(hat: DragonOutfit, shirt: DragonOutfit, shoes: DragonOutfit) -> void:
hat_outfits = hat
shirt_outfits = shirt
shoes_outfits = shoes
func encrypt(hat: Texture2D, shirt: Texture2D, shoes: Texture2D, name: String) -> String:
var hat_index: int = hat_outfits.get_index(hat)
var shirt_index: int = shirt_outfits.get_index(shirt)
var shoes_index: int = shoes_outfits.get_index(shoes)
var code: String = generate_code(hat_index, shirt_index, shoes_index, name)
return xor_encrypt_decrypt(code)
func descrypt(input: String) -> Dictionary:
var decrypted_code: String = xor_encrypt_decrypt(input)
if len(decrypted_code) % 2 != 0 or len(decrypted_code) <= 6:
return {'hat': null, 'shirt': null, 'shoes': null, 'name': null}
return read_code(decrypted_code)
func generate_code(int1: int, int2: int, int3: int, input_string: String) -> String:
input_string = input_string.substr(0, 8)
var binary_data: String = ""
binary_data += _int_to_bin(int1)
binary_data += _int_to_bin(int2)
binary_data += _int_to_bin(int3)
for i in range(input_string.length()):
binary_data += _char_to_bin(input_string[i])
return binary_data
func _int_to_bin(value: int) -> String:
return "%02X" % value
func _bin_to_int(value: String) -> int:
return value.hex_to_int()
func _char_to_bin(character: String) -> String:
return alphabet[character]
func _bin_to_char(character: String) -> String:
if not alphabet_inverse.has(character):
return ""
return alphabet_inverse[character]
func xor_encrypt_decrypt(data: String) -> String:
var result: String = ""
for i in range(data.length()):
var c: int = data[i].unicode_at(0)
var encrypted_char: int = c ^ key
result += char(encrypted_char)
return result
func read_code(code: String) -> Dictionary:
var hat: Texture2D = hat_outfits.get_texture(_bin_to_int(code.substr(0, 2)))
var shirt: Texture2D = shirt_outfits.get_texture(_bin_to_int(code.substr(2, 2)))
var shoes: Texture2D = shoes_outfits.get_texture(_bin_to_int(code.substr(4, 2)))
var name: String = ""
for i in range(code.substr(6).length()):
if i % 2 != 0:
continue
if _bin_to_char(code.substr(6 + i, 2)) == "":
return {'hat': null, 'shirt': null, 'shoes': null, 'name': null}
name += _bin_to_char(code.substr(6 + i, 2))
return {'hat': hat, 'shirt': shirt, 'shoes': shoes, 'name': name}

View file

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

View file

@ -5,6 +5,13 @@ class_name DragonLibrary
var dragons: Array[DragonProperties] = [] var dragons: Array[DragonProperties] = []
func has(properties: DragonProperties) -> bool:
for dragon in dragons:
if properties.equals(dragon):
return true
return false
func add_dragon(properties: DragonProperties) -> int: func add_dragon(properties: DragonProperties) -> int:
if properties.id == 0: if properties.id == 0:
properties.id = randi() properties.id = randi()
@ -12,6 +19,6 @@ func add_dragon(properties: DragonProperties) -> int:
return properties.id return properties.id
func add_dragons(dragons: Array[DragonProperties]) -> void: func add_dragons(new_dragons: Array[DragonProperties]) -> void:
for d in dragons: for d in new_dragons:
dragons.push_back(d) dragons.push_back(d)

View file

@ -1,6 +1,6 @@
extends RefCounted
class_name DragonProperties class_name DragonProperties
var name: String var name: String
var origin: String var origin: String
var color: int var color: int
@ -22,3 +22,7 @@ func _init(name: String, origin: String, color: int, hat: int, shirt: int, decor
func _to_string() -> String: func _to_string() -> String:
return '%s: [origin: %s, color: %d, hat: %d, shirt: %d, decor: %d]' % [self.name, self.origin, self.color, self.hat, self.shirt, self.decor] return '%s: [origin: %s, color: %d, hat: %d, shirt: %d, decor: %d]' % [self.name, self.origin, self.color, self.hat, self.shirt, self.decor]
func equals(other: DragonProperties):
return name == other.name and origin == other.origin and color == other.color and hat == other.hat and shirt == other.shirt and decor == other.decor

View file

@ -1,11 +1,10 @@
class_name SaveLoad class_name SaveLoad
const save_location = "user://save.data" const save_location = "user://save.data"
var contents_to_save: Dictionary = { var contents_to_save: Dictionary = {
} }
func save() -> void: func save() -> void:
@ -40,19 +39,19 @@ func clear_dragons() -> void:
contents_to_save['dragons'] = [] contents_to_save['dragons'] = []
func add_dragon(id: int, name: String, hat: int, shirt: int, shoes: int, pos: Vector2i) -> void: func add_dragon(dragon: DragonProperties) -> void:
contents_to_save['dragons'].push_back({'id': id, 'name': name, 'hat': hat, 'shirt': shirt, 'shoes': shoes, 'pos': pos}) contents_to_save['dragons'].push_back({'name': dragon.name, 'origin': dragon.origin, 'color': dragon.color, 'hat': dragon.hat, 'shirt': dragon.shirt, 'decor': dragon.decor, 'pos': dragon.position})
func get_dragon(dragon: Dictionary) -> DragonProperties: func get_dragon(dragon: Dictionary) -> DragonProperties:
var name: String = dragon['name'] var name: String = dragon['name']
var origin: String = dragon['origin'] var origin: String = dragon['origin']
var hat: int = dragon['hat'] var hat: int = dragon['hat']
var shirt: int = dragon['shirt'] var shirt: int = dragon['shirt']
var decor: int = dragon['decor'] var decor: int = dragon['decor']
var color: int = dragon['color'] var color: int = dragon['color']
var position: Vector2i = dragon['pos'] var position: Vector2i = dragon['pos']
var properties = DragonProperties.new(name, origin, color, hat, shirt, decor) var properties = DragonProperties.new(name, origin, color, hat, shirt, decor)
properties.position = position properties.position = position
properties.id = randi() properties.id = randi()
return properties return properties