feat: start adding new sharing method
This commit is contained in:
parent
72013e48eb
commit
46726bca32
10 changed files with 132 additions and 46 deletions
|
@ -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"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue