Intialize and cleanup repo
This commit is contained in:
6
client/scripts/entities/LifeCrystal.gd
Normal file
6
client/scripts/entities/LifeCrystal.gd
Normal file
@ -0,0 +1,6 @@
|
||||
extends Area2D
|
||||
|
||||
const ROTATION_SPEED = 1
|
||||
|
||||
func _process(delta):
|
||||
$Sprite.rotation = $Sprite.rotation + delta * ROTATION_SPEED
|
23
client/scripts/entities/Player.gd
Normal file
23
client/scripts/entities/Player.gd
Normal file
@ -0,0 +1,23 @@
|
||||
extends KinematicBody2D
|
||||
|
||||
export var user : String = ""
|
||||
|
||||
func _ready():
|
||||
$Label.text = user
|
||||
#$Label.text = $"/root/NetworkManager".username
|
||||
|
||||
func _process(delta):
|
||||
var movePos : Vector2 = Vector2(0,0)
|
||||
|
||||
|
||||
if(Input.is_action_just_pressed("ui_up")):
|
||||
movePos.y = movePos.y + 1
|
||||
if(Input.is_action_just_pressed("ui_down")):
|
||||
movePos.y = movePos.y - 1
|
||||
if(Input.is_action_just_pressed("ui_right")):
|
||||
movePos.x = movePos.x + 1
|
||||
if(Input.is_action_just_pressed("ui_left")):
|
||||
movePos.x = movePos.x - 1
|
||||
|
||||
if movePos != Vector2.ZERO:
|
||||
$"/root/NetworkManager".move_player(movePos.x, movePos.y)
|
113
client/scripts/network/NetworkManager.gd
Normal file
113
client/scripts/network/NetworkManager.gd
Normal file
@ -0,0 +1,113 @@
|
||||
extends Node
|
||||
|
||||
const SERVER_HOST : String = "192.168.1.34"
|
||||
const SERVER_PORT : int = 7777
|
||||
|
||||
signal disconnection
|
||||
signal connection_established
|
||||
signal error_occured
|
||||
signal logged_in
|
||||
|
||||
signal world_data_recieved
|
||||
|
||||
var username : String = ""
|
||||
|
||||
var client : GDNetHost = null
|
||||
var peer : GDNetPeer = null
|
||||
var server_address : GDNetAddress = null
|
||||
var connected : bool = false
|
||||
|
||||
var packetQueue = []
|
||||
var error_info = ""
|
||||
var world_data : String = ""
|
||||
|
||||
|
||||
var connection_timer : Timer
|
||||
|
||||
func _init():
|
||||
server_address = GDNetAddress.new()
|
||||
|
||||
server_address.set_host(SERVER_HOST)
|
||||
server_address.set_port(SERVER_PORT)
|
||||
|
||||
client = GDNetHost.new()
|
||||
client.set_max_peers(1)
|
||||
client.set_event_wait(250)
|
||||
client.bind()
|
||||
|
||||
func connect_to_server():
|
||||
peer = client.host_connect(server_address)
|
||||
if not connection_timer:
|
||||
connection_timer = Timer.new()
|
||||
add_child(connection_timer)
|
||||
connection_timer.connect("timeout", self, "timeout_check")
|
||||
connection_timer.start(1)
|
||||
|
||||
func timeout_check():
|
||||
if username == "":
|
||||
display_error("Connection failed!")
|
||||
connection_timer.stop()
|
||||
|
||||
func request_world_map():
|
||||
var request_packet : PoolByteArray = "2|".to_ascii()
|
||||
packetQueue.append(request_packet)
|
||||
|
||||
func connect_as_user(username : String):
|
||||
connect_to_server()
|
||||
var username_packet : PoolByteArray = ("1|" + username).to_ascii()
|
||||
|
||||
packetQueue.append(username_packet)
|
||||
|
||||
func display_error(error = "Unknown error occured!"):
|
||||
error_info = error
|
||||
print("Error: " + error)
|
||||
emit_signal("error_occured")
|
||||
|
||||
func disconnect_from_server():
|
||||
peer.disconnect_later()
|
||||
|
||||
func process_events():
|
||||
if(client.is_event_available()):
|
||||
var event : GDNetEvent = client.get_event()
|
||||
|
||||
var event_type = event.get_event_type()
|
||||
|
||||
if(event_type == GDNetEvent.RECEIVE):
|
||||
var ascii_data : String = str(event.get_packet().get_string_from_ascii())
|
||||
if len(ascii_data) > 0:
|
||||
if ascii_data[0] == '1':
|
||||
if ascii_data.substr(2,2) == "OK":
|
||||
username = ascii_data.substr(4)
|
||||
print("Logged in as: " + username)
|
||||
emit_signal("logged_in")
|
||||
else:
|
||||
display_error("Username not accepted! Reason: " + ascii_data.substr(2))
|
||||
elif ascii_data[0] == '2':
|
||||
world_data = ascii_data.substr(2)
|
||||
emit_signal("world_data_recieved")
|
||||
elif(event_type == GDNetEvent.CONNECT):
|
||||
print("Connected to server with hostname: " + server_address.get_host() + ":" + str(server_address.get_port()))
|
||||
connected = true
|
||||
emit_signal("connection_established")
|
||||
elif(event_type == GDNetEvent.DISCONNECT):
|
||||
print("Disconnected from server")
|
||||
connected = false
|
||||
emit_signal("disconnection")
|
||||
|
||||
func move_player(x,y):
|
||||
var pckt : PoolByteArray = ("3|" + str(x) + "," + str(y)).to_ascii()
|
||||
|
||||
packetQueue.append(pckt)
|
||||
|
||||
func _process(delta):
|
||||
process_events()
|
||||
|
||||
if len(packetQueue) > 0 and connected:
|
||||
peer.send_packet(packetQueue[0], 0, GDNetMessage.RELIABLE)
|
||||
packetQueue.remove(0)
|
||||
|
||||
|
||||
if(Input.is_action_just_pressed("ui_cancel")):
|
||||
print("PACKET")
|
||||
peer.send_packet("TEST".to_ascii(), 0, GDNetMessage.RELIABLE)
|
||||
|
15
client/scripts/singletons/MusicManager.gd
Normal file
15
client/scripts/singletons/MusicManager.gd
Normal file
@ -0,0 +1,15 @@
|
||||
extends AudioStreamPlayer
|
||||
|
||||
var main_player : AudioStreamPlayer
|
||||
var looping : bool = false
|
||||
|
||||
func _ready():
|
||||
main_player = self
|
||||
|
||||
func play_music(song, loop=true):
|
||||
var audio_file = "res://assets/audio/music/" + song + ".ogg"
|
||||
if File.new().file_exists(audio_file):
|
||||
var track = load(audio_file)
|
||||
looping = loop
|
||||
main_player.stream = track
|
||||
#main_player.play()
|
21
client/scripts/systems/LoginManager.gd
Normal file
21
client/scripts/systems/LoginManager.gd
Normal file
@ -0,0 +1,21 @@
|
||||
extends Node
|
||||
|
||||
|
||||
func _ready():
|
||||
$"/root/MusicManager".play_music("wizards")
|
||||
$Button.connect("button_down", self, "_on_button_press")
|
||||
$"/root/NetworkManager".connect("error_occured", self, "_on_error")
|
||||
$"/root/NetworkManager".connect("logged_in", self, "_on_login")
|
||||
|
||||
func _on_error():
|
||||
$ErrorDialog/ErrorLabel.text = $"/root/NetworkManager".error_info
|
||||
$ErrorDialog.popup_centered()
|
||||
|
||||
func _on_button_press():
|
||||
if($"/root/NetworkManager".connected):
|
||||
$"/root/NetworkManager".disconnect_from_server()
|
||||
else:
|
||||
$"/root/NetworkManager".connect_as_user($LineEdit.text)
|
||||
|
||||
func _on_login():
|
||||
get_tree().change_scene("res://scenes/game.scn")
|
49
client/scripts/systems/WorldManager.gd
Normal file
49
client/scripts/systems/WorldManager.gd
Normal file
@ -0,0 +1,49 @@
|
||||
extends Node
|
||||
|
||||
|
||||
func _ready():
|
||||
$Tiles.clear()
|
||||
$"/root/NetworkManager".connect("world_data_recieved", self, "_on_world_update")
|
||||
$"/root/NetworkManager".request_world_map()
|
||||
|
||||
func _on_world_update():
|
||||
var data = $"/root/NetworkManager".world_data.split('\n')
|
||||
for tileUpdate in data:
|
||||
if len(tileUpdate) > 3:
|
||||
if ',' in tileUpdate:
|
||||
var tile_data = tileUpdate.split(',')
|
||||
if ':' in tile_data[2]:
|
||||
var pos : Vector2 = $Tiles.map_to_world(Vector2(int(tile_data[0]), int(tile_data[1])))
|
||||
if 'player:' in tile_data[2]:
|
||||
print(tile_data)
|
||||
var player_name = tile_data[2].substr(len('player:'))
|
||||
print(player_name)
|
||||
if $"/root/NetworkManager".username == player_name:
|
||||
$Player.position = pos
|
||||
else:
|
||||
update_entity(player_name, pos, "player")
|
||||
else:
|
||||
var entity_data = tile_data[2].split(':')
|
||||
update_entity(entity_data[1], pos, entity_data[0])
|
||||
|
||||
else:
|
||||
$Tiles.set_cell(int(tile_data[0]), int(tile_data[1]), int(tile_data[2]))
|
||||
if $Loading != null:
|
||||
$Loading.queue_free()
|
||||
|
||||
func display_error(error):
|
||||
print("Error " + error)
|
||||
|
||||
func update_entity(entity_id : String, pos : Vector2, type : String):
|
||||
var entity : Node2D = get_node_or_null( str(type + "-" + entity_id))
|
||||
if not entity:
|
||||
var entity_location = "res://nodes/entities/" + type + ".tscn"
|
||||
if File.new().file_exists(entity_location):
|
||||
var gobj = load(entity_location)
|
||||
entity = gobj.instance()
|
||||
add_child(entity, true)
|
||||
entity.set_name(str(type + "-" + entity_id))
|
||||
else:
|
||||
display_error("Trying to load entity of type: " + type + ", but failed.")
|
||||
if entity:
|
||||
entity.position = pos
|
Reference in New Issue
Block a user