Skip to content

Commit

Permalink
Merge branch 'ld-clientserver-com'
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanyLeonic committed Mar 3, 2024
2 parents 444eaf1 + 43969fa commit 3fccbf2
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 102 deletions.
10 changes: 4 additions & 6 deletions game-source/WebSockets.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extends Node
class_name WebSocketClient

@export var base_url: String = "ws://localhost:8000/ws"
@export var handshake_headers : PackedStringArray
@export var supported_protocols : PackedStringArray
@export var tls_trusted_certificate : X509Certificate
Expand Down Expand Up @@ -29,7 +30,7 @@ func connect_to_url(url) -> int:


func send(message) -> int:
print(socket.get_ready_state())
print("Yooo " + str(socket.get_ready_state()))
if typeof(message) == TYPE_STRING:
return socket.send_text(message)
return socket.send(var_to_bytes(message))
Expand All @@ -45,6 +46,7 @@ func get_message() -> Variant:


func close(code := 1000, reason := "") -> void:
print("Socket closed!")
socket.close(code, reason)
last_state = socket.get_ready_state()

Expand All @@ -70,10 +72,6 @@ func poll() -> void:
connection_closed.emit()
while socket.get_ready_state() == socket.STATE_OPEN and socket.get_available_packet_count():
message_received.emit(get_message())


func _ready():
connect_to_url("ws://localhost:8000/ws")


func _process(delta):
poll()
Binary file modified game-source/assets/Illustration95.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed game-source/assets/box.png
Binary file not shown.
Binary file removed game-source/assets/lil_guy.png
Binary file not shown.
34 changes: 0 additions & 34 deletions game-source/assets/lil_guy.png.import

This file was deleted.

Binary file removed game-source/assets/main_menu.png
Binary file not shown.
34 changes: 0 additions & 34 deletions game-source/assets/main_menu.png.import

This file was deleted.

Binary file added game-source/assets/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

importer="texture"
type="CompressedTexture2D"
uid="uid://c6jm42o02gsfr"
path="res://.godot/imported/box.png-52cc4e41fddadef836f9ad4dbe7936fa.ctex"
uid="uid://cn58l1sya667a"
path="res://.godot/imported/player.png-be2216fcaabb5c62aa2466cd9a5726a8.ctex"
metadata={
"vram_texture": false
}

[deps]

source_file="res://assets/box.png"
dest_files=["res://.godot/imported/box.png-52cc4e41fddadef836f9ad4dbe7936fa.ctex"]
source_file="res://assets/player.png"
dest_files=["res://.godot/imported/player.png-be2216fcaabb5c62aa2466cd9a5726a8.ctex"]

[params]

Expand Down
5 changes: 5 additions & 0 deletions game-source/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ up={
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
Use={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":102,"echo":false,"script":null)
]
}

[rendering]

Expand Down
35 changes: 35 additions & 0 deletions game-source/scenes/box/box.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extends CharacterBody2D


@export var fetch_url: String = "https://via.placeholder.com/100"

func _ready():
position = position.snapped(Vector2.ONE * 100)
position += Vector2.ONE * 100 / 2
# Create an HTTP request node and connect its completion signal.
var http_request = HTTPRequest.new()
add_child(http_request)
http_request.request_completed.connect(self._http_request_completed)

# Perform the HTTP request. The URL below returns a PNG image as of writing.
var error = http_request.request(fetch_url)
if error != OK:
push_error("An error occurred in the HTTP request.")

# Called when the HTTP request is completed.
func _http_request_completed(result, response_code, headers, body):
if result != HTTPRequest.RESULT_SUCCESS:
push_error("Image couldn't be downloaded. Try a different image.")

var image = Image.new()
var error = image.load_png_from_buffer(body)
if error != OK:
push_error("Couldn't load the image.")

var texture = ImageTexture.create_from_image(image)

# Display the image in a TextureRect node.
var texture_rect = TextureRect.new()
add_child(texture_rect)
texture_rect.texture = texture
texture_rect.position = Vector2(0.5,0.5)*100
15 changes: 8 additions & 7 deletions game-source/scenes/box/box.tscn
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[gd_scene load_steps=2 format=3 uid="uid://cv6rvnsd8jc4u"]
[gd_scene load_steps=3 format=3 uid="uid://cv6rvnsd8jc4u"]

[ext_resource type="Texture2D" uid="uid://c6jm42o02gsfr" path="res://assets/box.png" id="1_blfj8"]
[ext_resource type="Script" path="res://scenes/box/box.gd" id="1_so5sa"]

[sub_resource type="RectangleShape2D" id="RectangleShape2D_pwdsl"]
size = Vector2(100, 100)

[node name="CharacterBody2D" type="CharacterBody2D"]
script = ExtResource("1_so5sa")

[node name="Box" type="Sprite2D" parent="."]
position = Vector2(33, 52)
texture = ExtResource("1_blfj8")

[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
visible = false
position = Vector2(36, 52)
polygon = PackedVector2Array(16, -124.8, 16, -122.9, 29, -111.9, 29, -109.4, 40.2, -85, 41.1, -85, 51, -69.6, 51, -59, 48.7, -59, 47, -57.9, 47, -57, 49.1, -24, 47.8, -24, 45.8, -8, 45, -8, 45, -4, 43, -4, 36, 5, 33.2, 5, 12, 7.2, 12, 5.89999, -13, 4.10001, -13, 4.89999, -80, 8.10001, -80, 6.10001, -93, -1.89999, -93, -4.60001, -95.4, -11, -98.1, -11, -102.1, -17, -105.2, -17, -115, -19.4, -115, -21.3, -122, -31, -123.6, -31, -149, -46.9, -149, -54, -150, -91, -150, -128, -148.6, -128, -137.6, -135, -127.2, -135, -120, -135.8, -120, -137.2)
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_pwdsl")
4 changes: 0 additions & 4 deletions game-source/scenes/main/main.gd
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
extends Node2D
@export var on = true
@export var tile_size = 100

func _process(delta):
queue_redraw()

func _draw():
if !on:
pass

var camera: CharacterBody2D = get_tree().current_scene.find_child('PlayerCharacter')

var size = get_viewport_rect().size / 2
Expand Down
12 changes: 11 additions & 1 deletion game-source/scenes/main/main.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
[gd_scene load_steps=3 format=3 uid="uid://2t2nbl4hrw8b"]
[gd_scene load_steps=5 format=3 uid="uid://2t2nbl4hrw8b"]

[ext_resource type="PackedScene" uid="uid://xqoiyxrn10js" path="res://scenes/player_character/player_character.tscn" id="1_1c02b"]
[ext_resource type="Script" path="res://scenes/main/main.gd" id="1_47q2m"]
[ext_resource type="PackedScene" uid="uid://cv6rvnsd8jc4u" path="res://scenes/box/box.tscn" id="3_2p8op"]
[ext_resource type="Script" path="res://WebSockets.gd" id="3_uxet7"]

[node name="GameLevel" type="Node2D"]
script = ExtResource("1_47q2m")

[node name="PlayerCharacter" parent="." instance=ExtResource("1_1c02b")]
position = Vector2(577, 325)
scale = Vector2(2.9, 2.9)

[node name="WebSocketClient" type="Node" parent="."]
script = ExtResource("3_uxet7")

[node name="CharacterBody2D" parent="." instance=ExtResource("3_2p8op")]
position = Vector2(256, 168)

[node name="OtherObjects" type="Node2D" parent="."]
Loading

0 comments on commit 3fccbf2

Please sign in to comment.