feat: start adding new sharing method
This commit is contained in:
parent
72013e48eb
commit
46726bca32
10 changed files with 132 additions and 46 deletions
|
@ -7,5 +7,6 @@
|
|||
|
||||
[resource]
|
||||
script = ExtResource("4_sxi2r")
|
||||
allow_null = false
|
||||
outfits = Array[Texture2D]([ExtResource("1_3e6qx"), ExtResource("2_lvc22"), ExtResource("3_8ji77")])
|
||||
metadata/_custom_type_script = "uid://rw26nny160xh"
|
||||
|
|
|
@ -196,6 +196,7 @@ offset_bottom = 276.0
|
|||
texture = ExtResource("12_qppok")
|
||||
|
||||
[node name="LineEdit" type="LineEdit" parent="CanvasLayer/NameLabel"]
|
||||
layout_mode = 0
|
||||
offset_left = 10.0
|
||||
offset_top = 8.0
|
||||
offset_right = 138.0
|
||||
|
|
34
scenes/dragon_sharing.gd
Normal file
34
scenes/dragon_sharing.gd
Normal file
|
@ -0,0 +1,34 @@
|
|||
extends HTTPRequest
|
||||
class_name DragonSharing
|
||||
|
||||
|
||||
signal on_dragon_received(dragon: DragonProperties)
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
request_completed.connect(_on_request_completed)
|
||||
|
||||
|
||||
func _on_request_completed(result: int, response_code: int, headers: PackedStringArray, body: PackedByteArray):
|
||||
if response_code != 200:
|
||||
print("HTTP request returned error: ", response_code)
|
||||
return
|
||||
if body.size() != 0:
|
||||
var json = JSON.parse_string(body.get_string_from_utf8())
|
||||
var properties = DragonProperties.new(json['name'], json['origin'], json['color'], json['hat'], json['shirt'], json['decor'])
|
||||
print(properties)
|
||||
on_dragon_received.emit(properties)
|
||||
|
||||
|
||||
func send(origin: String, name: String, color: int, shirt: int, hat: int, decor: int) -> void:
|
||||
var url: String = 'http://torreta.gerardgascon.com/add/%s/%s/%s/%s/%s/%s/' % [origin, name, color, shirt, hat, decor]
|
||||
var err = request(url)
|
||||
if err != OK:
|
||||
print("HTTP request failed: ", err)
|
||||
|
||||
|
||||
func receive(origin: String) -> void:
|
||||
var url: String = 'http://torreta.gerardgascon.com/get/%s/' % origin
|
||||
var err = request(url)
|
||||
if err != OK:
|
||||
print("HTTP request failed: ", err)
|
1
scenes/dragon_sharing.gd.uid
Normal file
1
scenes/dragon_sharing.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://bg58e3dobon4j
|
|
@ -25,6 +25,8 @@ var rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
|||
@export var min_dragon_instantiation_time: float
|
||||
@export var max_dragon_instantiation_time: float
|
||||
|
||||
@onready var dragon_sharing: DragonSharing = $HTTPRequest
|
||||
|
||||
|
||||
func _ready():
|
||||
_library = DragonLibrary.new(hat_outfits, shirt_outfits, shoes_outfits)
|
||||
|
@ -43,32 +45,41 @@ func _ready():
|
|||
func _queue_dragon_instantiation():
|
||||
await get_tree().create_timer(rng.randf_range(min_dragon_instantiation_time, max_dragon_instantiation_time)).timeout
|
||||
_instantiate_random_dragon()
|
||||
|
||||
|
||||
func _instantiate_random_dragon():
|
||||
for spot in dragon_spots:
|
||||
if not _filled_spots.has(spot):
|
||||
var dragon: DragonEntity = _pick_random_dragon(spot.position)
|
||||
if dragon != null:
|
||||
_filled_spots[spot] = dragon.id
|
||||
break
|
||||
_queue_dragon_instantiation()
|
||||
|
||||
|
||||
func _pick_random_dragon(pos: Vector2) -> DragonEntity:
|
||||
for dragon in _library.dragons:
|
||||
var id: int = dragon['id']
|
||||
if _instantiated_dragons.has(id) or _dragon_entities.has(id):
|
||||
continue
|
||||
var dragon_name: String = dragon['name']
|
||||
var hat: Texture2D = hat_outfits.get_texture(dragon['hat'])
|
||||
var shirt: Texture2D = shirt_outfits.get_texture(dragon['shirt'])
|
||||
var shoes: Texture2D = shoes_outfits.get_texture(dragon['shoes'])
|
||||
return _instantiate_dragon_ingame(pos, hat, shirt, shoes, dragon_name, id)
|
||||
|
||||
func _instantiate_random_dragon():
|
||||
var spot: Node2D = _get_free_spot()
|
||||
if spot == null:
|
||||
return
|
||||
|
||||
for i in range(5):
|
||||
dragon_sharing.receive("potato")
|
||||
var dragon: DragonProperties = await dragon_sharing.on_dragon_received
|
||||
for d in _library.dragons:
|
||||
# TODO: Search for dragon and if is in library or in instantiated dragons continue next iteration
|
||||
pass
|
||||
var dragon_entity: DragonEntity = _generate_entity(spot.position, dragon)
|
||||
_filled_spots[spot] = dragon_entity.id
|
||||
return
|
||||
|
||||
|
||||
func _get_free_spot() -> Node2D:
|
||||
for spot in dragon_spots:
|
||||
if not _filled_spots.has(spot):
|
||||
return spot
|
||||
return null
|
||||
|
||||
|
||||
func _generate_entity(pos: Vector2, properties: DragonProperties) -> DragonEntity:
|
||||
var id: int = rng.randi()
|
||||
var hat: Texture2D = hat_outfits.get_texture(properties.hat)
|
||||
var shirt: Texture2D = shirt_outfits.get_texture(properties.shirt)
|
||||
var shoes: Texture2D = shoes_outfits.get_texture(properties.decor)
|
||||
var dragon_name: String = properties.name
|
||||
return _instantiate_dragon_ingame(pos, hat, shirt, shoes, dragon_name, id)
|
||||
|
||||
|
||||
func move_window_to_bottom_right():
|
||||
var display_index: int = DisplayServer.window_get_current_screen()
|
||||
|
||||
|
@ -94,14 +105,14 @@ func add_dragon(hat: Texture2D, shirt: Texture2D, shoes: Texture2D, dragon_name:
|
|||
|
||||
func _load_game():
|
||||
_save_load.load()
|
||||
var dragons: Array = _save_load.get_dragons()
|
||||
var dragons: Array[DragonProperties] = _save_load.get_dragons()
|
||||
_library.add_dragons(dragons)
|
||||
for d in dragons:
|
||||
if d['pos'] != Vector2i(0, 0):
|
||||
var hat: Texture2D = hat_outfits.get_texture(d['hat'])
|
||||
var shirt: Texture2D = shirt_outfits.get_texture(d['shirt'])
|
||||
var shoes: Texture2D = shoes_outfits.get_texture(d['shoes'])
|
||||
_pick_dragon(d['id'], d['pos'], hat, shirt, shoes, false)
|
||||
for d: DragonProperties in dragons:
|
||||
if d.position != Vector2i(0, 0):
|
||||
var hat: Texture2D = hat_outfits.get_texture(d.hat)
|
||||
var shirt: Texture2D = shirt_outfits.get_texture(d.shirt)
|
||||
var shoes: Texture2D = shoes_outfits.get_texture(d.decor)
|
||||
_pick_dragon(rng.randi(), d.position, hat, shirt, shoes, false)
|
||||
print(_save_load.contents_to_save)
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
[gd_scene load_steps=17 format=3 uid="uid://ctytpqaed0yqx"]
|
||||
[gd_scene load_steps=18 format=3 uid="uid://ctytpqaed0yqx"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://3kyt3shje5r1" path="res://scenes/main.gd" id="1_sugp2"]
|
||||
[ext_resource type="PackedScene" uid="uid://c7nfcgjxqeg7l" path="res://scenes/window/dragon_popup.tscn" id="2_jyhfs"]
|
||||
|
@ -16,6 +16,7 @@
|
|||
[ext_resource type="Texture2D" uid="uid://bc2lykc3gwykh" path="res://assets/sprites/environment/castle/DP_castle_back.png" id="15_muem4"]
|
||||
[ext_resource type="Texture2D" uid="uid://o1e8lge2vuqu" path="res://assets/sprites/environment/castle/DP_castle_mid.png" id="16_dp3eg"]
|
||||
[ext_resource type="Texture2D" uid="uid://b1w6y1qvetsaw" path="res://assets/sprites/environment/castle/DP_castle_front.png" id="17_0ld40"]
|
||||
[ext_resource type="Script" uid="uid://bg58e3dobon4j" path="res://scenes/dragon_sharing.gd" id="17_muem4"]
|
||||
|
||||
[node name="Base" type="Node2D" node_paths=PackedStringArray("dragon_spots")]
|
||||
script = ExtResource("1_sugp2")
|
||||
|
@ -111,6 +112,9 @@ position = Vector2(142, 118)
|
|||
[node name="DragonSpot3" type="Node2D" parent="."]
|
||||
position = Vector2(207, 252)
|
||||
|
||||
[node name="HTTPRequest" type="HTTPRequest" parent="."]
|
||||
script = ExtResource("17_muem4")
|
||||
|
||||
[connection signal="tree_entered" from="." to="." method="_on_tree_entered"]
|
||||
[connection signal="tree_exiting" from="." to="." method="_on_tree_exiting"]
|
||||
[connection signal="button_up" from="CanvasLayer/Create" to="CanvasLayer/Create" method="_on_button_up"]
|
||||
|
|
|
@ -2,7 +2,7 @@ extends Node
|
|||
class_name DragonLibrary
|
||||
|
||||
|
||||
var dragons: Array = []
|
||||
var dragons: Array[DragonProperties] = []
|
||||
|
||||
var hat_outfits: DragonOutfit
|
||||
var shirt_outfits: DragonOutfit
|
||||
|
@ -15,21 +15,13 @@ func _init(hat_outfits: DragonOutfit, shirt_outfits: DragonOutfit, shoes_outfits
|
|||
self.shoes_outfits = shoes_outfits
|
||||
|
||||
|
||||
func add_dragon(name: String, hat: Texture2D, shirt: Texture2D, shoes: Texture2D) -> int:
|
||||
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 id: int = RandomNumberGenerator.new().randi()
|
||||
_push_dragon(id, name, hat_index, shirt_index, shoes_index)
|
||||
return id
|
||||
func add_dragon(properties: DragonProperties) -> int:
|
||||
if properties.id == 0:
|
||||
properties.id = randi()
|
||||
dragons.push_back(properties)
|
||||
return properties.id
|
||||
|
||||
|
||||
func add_dragons(dragons: Array) -> void:
|
||||
func add_dragons(dragons: Array[DragonProperties]) -> void:
|
||||
for d in dragons:
|
||||
_push_dragon(d['id'], d['name'], d['hat'], d['shirt'], d['shoes'])
|
||||
|
||||
|
||||
func _push_dragon(id: int, name: String, hat: int, shirt: int, shoes: int):
|
||||
dragons.push_back(
|
||||
{'id': id, 'name': name, 'hat': hat, 'shirt': shirt, 'shoes': shoes}
|
||||
)
|
||||
dragons.push_back(d)
|
||||
|
|
24
src/dragon_properties.gd
Normal file
24
src/dragon_properties.gd
Normal file
|
@ -0,0 +1,24 @@
|
|||
class_name DragonProperties
|
||||
|
||||
|
||||
var name: String
|
||||
var origin: String
|
||||
var color: int
|
||||
var hat: int
|
||||
var shirt: int
|
||||
var decor: int
|
||||
var position: Vector2i
|
||||
var id: int
|
||||
|
||||
|
||||
func _init(name: String, origin: String, color: int, hat: int, shirt: int, decor: int) -> void:
|
||||
self.name = name
|
||||
self.origin = origin
|
||||
self.color = color
|
||||
self.hat = hat
|
||||
self.shirt = shirt
|
||||
self.decor = decor
|
||||
|
||||
|
||||
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]
|
1
src/dragon_properties.gd.uid
Normal file
1
src/dragon_properties.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://jyglnbx4cxwm
|
|
@ -44,8 +44,25 @@ func add_dragon(id: int, name: String, hat: int, shirt: int, shoes: int, pos: Ve
|
|||
contents_to_save['dragons'].push_back({'id': id, 'name': name, 'hat': hat, 'shirt': shirt, 'shoes': shoes, 'pos': pos})
|
||||
|
||||
|
||||
func get_dragons() -> Array:
|
||||
return contents_to_save['dragons']
|
||||
func get_dragon(dragon: Dictionary) -> DragonProperties:
|
||||
var name: String = dragon['name']
|
||||
var origin: String = dragon['origin']
|
||||
var hat: int = dragon['hat']
|
||||
var shirt: int = dragon['shirt']
|
||||
var decor: int = dragon['decor']
|
||||
var color: int = dragon['color']
|
||||
var position: Vector2i = dragon['pos']
|
||||
var properties = DragonProperties.new(name, origin, color, hat, shirt, decor)
|
||||
properties.position = position
|
||||
properties.id = randi()
|
||||
return properties
|
||||
|
||||
|
||||
func get_dragons() -> Array[DragonProperties]:
|
||||
var dragons: Array[DragonProperties] = []
|
||||
for dragon in contents_to_save['dragons']:
|
||||
dragons.push_back(get_dragon(dragon))
|
||||
return dragons
|
||||
|
||||
|
||||
func add_item(id: int, position: Vector2i) -> void:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue