diff --git a/client/nodes/entities/player.tscn b/client/nodes/entities/player.tscn index 94b4467..240ff1e 100644 --- a/client/nodes/entities/player.tscn +++ b/client/nodes/entities/player.tscn @@ -33,3 +33,6 @@ valign = 1 __meta__ = { "_edit_use_anchors_": false } + +[node name="Camera" type="Camera2D" parent="."] +zoom = Vector2( 0.5, 0.5 ) diff --git a/client/project.godot b/client/project.godot index e36439a..555412c 100644 --- a/client/project.godot +++ b/client/project.godot @@ -23,9 +23,38 @@ config/icon="res://icon.png" MusicManager="*res://nodes/MusicManager.tscn" NetworkManager="*res://nodes/NetworkManager.tscn" +ImportantEntities="*res://scripts/singletons/ImportantEntities.gd" [input] +ui_left={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"unicode":0,"echo":false,"script":null) + ] +} +ui_right={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":15,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"unicode":0,"echo":false,"script":null) + ] +} +ui_up={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"unicode":0,"echo":false,"script":null) + ] +} +ui_down={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"unicode":0,"echo":false,"script":null) + ] +} send_chat_message={ "deadzone": 0.5, "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777221,"unicode":0,"echo":false,"script":null) diff --git a/client/scenes/game.scn b/client/scenes/game.scn index e050f0f..b77bccd 100644 Binary files a/client/scenes/game.scn and b/client/scenes/game.scn differ diff --git a/client/scripts/entities/Player.gd b/client/scripts/entities/Player.gd index 280df2e..884d390 100644 --- a/client/scripts/entities/Player.gd +++ b/client/scripts/entities/Player.gd @@ -1,27 +1,43 @@ extends KinematicBody2D export var user : String = "" +var main : bool = false +var input_locks = 0 + +func set_main(): + main = true + $Label.hide() + $Camera.show() + $Camera.current = true + + $"/root/ImportantEntities".main_player = self func _ready(): + $Camera.hide() set_username("") func set_username(username): - print(username) user = username $Label.text = user func _process(delta): - var movePos : Vector2 = Vector2(0,0) + if main and input_locks <= 0: + 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(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) - if movePos != Vector2.ZERO: - $"/root/NetworkManager".move_player(movePos.x, movePos.y) +func lock_input(): + input_locks = input_locks + 1 +func unlock_input(): + input_locks = input_locks - 1 diff --git a/client/scripts/singletons/ImportantEntities.gd b/client/scripts/singletons/ImportantEntities.gd new file mode 100644 index 0000000..11b6f86 --- /dev/null +++ b/client/scripts/singletons/ImportantEntities.gd @@ -0,0 +1,3 @@ +extends Node + +var main_player : KinematicBody2D = null diff --git a/client/scripts/systems/WorldManager.gd b/client/scripts/systems/WorldManager.gd index 16605a0..85fc6b6 100644 --- a/client/scripts/systems/WorldManager.gd +++ b/client/scripts/systems/WorldManager.gd @@ -17,28 +17,19 @@ func _on_world_update(): 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]) + 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: + if get_node_or_null("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)) + 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): @@ -46,6 +37,9 @@ func update_entity(entity_id : String, pos : Vector2, type : String): entity = gobj.instance() add_child(entity, true) entity.set_name(str(type + "-" + entity_id)) + if type == "player": + if entity_id == $"/root/NetworkManager".username: + entity.set_main() else: display_error("Trying to load entity of type: " + type + ", but failed.") if entity: diff --git a/client/scripts/systems/chatbox.gd b/client/scripts/systems/chatbox.gd new file mode 100644 index 0000000..b699109 --- /dev/null +++ b/client/scripts/systems/chatbox.gd @@ -0,0 +1,37 @@ +extends Control + +const MESSAGE_TIMEOUT : int = 17 + +var locking = false + +func _ready(): + $"/root/NetworkManager".connect("chat_message_recieved", self, "_on_new_message") + +func _on_new_message(message): + print(message) + var message_block : Label = Label.new() + message_block.text = message + var timer = Timer.new() + timer.connect("timeout", message_block, "queue_free") + message_block.add_child(timer) + $Messages.add_child(message_block) + timer.start(MESSAGE_TIMEOUT) + +func _process(delta): + + if $LineEdit.has_focus() != locking: + locking = $LineEdit.has_focus() + if locking: + $"/root/ImportantEntities".main_player.lock_input() + else: + $"/root/ImportantEntities".main_player.unlock_input() + + + if(Input.is_action_just_pressed("send_chat_message")): + if $LineEdit.has_focus(): + if len($LineEdit.text) > 0: + $"/root/NetworkManager".send_chat_message($LineEdit.text) + $LineEdit.text = "" + $LineEdit.release_focus() + else: + $LineEdit.grab_focus() diff --git a/server/gameserver.hpp b/server/gameserver.hpp index e0e20cb..4c4f273 100644 --- a/server/gameserver.hpp +++ b/server/gameserver.hpp @@ -76,7 +76,7 @@ void Authenticate(ENetEvent* event) usernames[peer_id] = username; //Spawn entity - std::string spawn_data = gamemap.spawn_entity(usernames[peer_id], "player", 127, 127); + std::string spawn_data = gamemap.spawn_entity(usernames[peer_id], "player", 128, 129); //Tell peers about new player data = ("2|" + spawn_data).c_str();