From 0bd9fe0b8795e6247922f9496bfea8381d745173 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 13 Jul 2020 01:38:56 +0200 Subject: [PATCH] Now the world is in a viewport, so it can be enderet at a different resolution that the UI. Temporarily disabled nameplates for now. --- game/player/Body.gd | 21 ++++- game/player/GUI.gd | 19 ++-- game/player/NamePlate.gd | 17 ++-- game/project.godot | 2 +- game/scenes/GameViewport.gd | 93 +++++++++++++++++++ game/scenes/Main.tscn | 23 ++++- game/scenes/MainScene.gd | 12 ++- game/scripts/networking/SpawnPoint.gd | 8 +- game/ui/nameplates/NamePlate.tscn | 14 +-- .../ui/nameplates/name_plate_dynamicfont.tres | 1 - game/ui/player_ui/player_ui.gd | 3 + game/ui/player_ui/player_ui.tscn | 4 +- game/ui/touch_pad/analog.gd | 21 ++++- 13 files changed, 196 insertions(+), 42 deletions(-) create mode 100644 game/scenes/GameViewport.gd diff --git a/game/player/Body.gd b/game/player/Body.gd index 56401c8..aabe0a8 100644 --- a/game/player/Body.gd +++ b/game/player/Body.gd @@ -85,6 +85,8 @@ var character_skeleton : CharacterSkeleton2D var visibility_update_timer : float = randi() +var gui : Node = null + func _enter_tree() -> void: world = get_node(world_path) as Node camera = get_node_or_null("Camera") as Camera2D @@ -102,6 +104,10 @@ func _enter_tree() -> void: transform = entity.get_transform_2d(true) set_physics_process(true) + +func _exit_tree(): + if gui != null and not gui.is_queued_for_deletion(): + gui.queue_free() func _process(delta : float) -> void: if entity.ai_state == EntityEnums.AI_STATE_OFF: @@ -478,7 +484,10 @@ func on_c_controlled_changed(val): var uiscn : PackedScene = ResourceLoader.load("res://ui/player_ui/player_ui.tscn") var ui = uiscn.instance() - add_child(ui) +# add_child(ui) + get_node("/root/Main/GUI").add_child(ui) + ui.set_player(entity) + gui = ui set_process_input(true) set_process_unhandled_input(true) @@ -489,9 +498,13 @@ func on_c_controlled_changed(val): set_process_input(false) set_process_unhandled_input(false) - var nameplatescn : PackedScene = ResourceLoader.load("res://ui/nameplates/NamePlate.tscn") - var nameplate = nameplatescn.instance() - get_parent().add_child(nameplate) +# var nameplatescn : PackedScene = ResourceLoader.load("res://ui/nameplates/NamePlate.tscn") +# var nameplate = nameplatescn.instance() +# +# get_node("/root/Main/GUI").add_child(nameplate) +## get_parent().add_child(nameplate) +# nameplate.set_player(entity) +# gui = nameplate diff --git a/game/player/GUI.gd b/game/player/GUI.gd index 9d4a993..24161c5 100644 --- a/game/player/GUI.gd +++ b/game/player/GUI.gd @@ -20,19 +20,16 @@ extends Control # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -export (String) var player_path : String = "../../.." +#export (String) var player_path : String = "../../.." export (Array, NodePath) var child_controls : Array -func _ready() -> void: - if player_path != null: - var player : Entity = get_node(player_path) as Entity - - if (player == null): - print("Player is null!") - return +func set_player(player : Entity) -> void: + if (player == null): + print("Player is null!") + return - for child_path in child_controls: - var child = get_node(child_path) + for child_path in child_controls: + var child = get_node(child_path) - child.set_player(player) + child.set_player(player) diff --git a/game/player/NamePlate.gd b/game/player/NamePlate.gd index 799c20d..e495358 100644 --- a/game/player/NamePlate.gd +++ b/game/player/NamePlate.gd @@ -53,7 +53,16 @@ func _enter_tree(): health_bar = get_node(health_bar_path) as TextureProgress health_bar_label = get_node(health_bar_label_path) as Label - entity = get_node("..") as Entity + modulate = normal_color + set_scale(normal_scale) + + target_scale = normal_scale + interpolating = false + + set_process(false) + +func set_player(e : Entity): + entity = e entity.connect("centity_resource_added", self, "on_centity_resource_added") name_label.text = entity.centity_name @@ -64,12 +73,6 @@ func _enter_tree(): entity.connect("notification_ctargeted", self, "notification_ctargeted") entity.connect("notification_cuntargeted", self, "notification_cuntargeted") - modulate = normal_color - set_scale(normal_scale) - - target_scale = normal_scale - interpolating = false - set_process(true) func _process(delta): diff --git a/game/project.godot b/game/project.godot index b5efae8..6b4b57c 100644 --- a/game/project.godot +++ b/game/project.godot @@ -371,7 +371,7 @@ gdscript/warnings/integer_division=false [display] window/dpi/allow_hidpi=true -window/handheld/orientation="sensor_landscape" +window/handheld/orientation="sensor" window/stretch/mode="2d" window/stretch/aspect="expand" mouse_cursor/sensitivity=0.9 diff --git a/game/scenes/GameViewport.gd b/game/scenes/GameViewport.gd new file mode 100644 index 0000000..063a4d7 --- /dev/null +++ b/game/scenes/GameViewport.gd @@ -0,0 +1,93 @@ +extends Container + +export(Vector2) var base_resolution : Vector2 = Vector2(600, 600) +var _scale : Vector2 = Vector2(1, 1) + +func _init(): + set_process_input(true); + set_process_unhandled_input(true); + +func _notification(p_what): + if (p_what == NOTIFICATION_RESIZED): + + for c in get_children(): + + if not c is Viewport: + continue + + var ar : float = 0 + var rc : Vector2 = Vector2() + +# if get_size().x > get_size().y: + ar = get_size().y / get_size().x + rc = Vector2(base_resolution.x, base_resolution.y * ar) +# else : +# ar = get_size().x / get_size().y +# rc = Vector2(base_resolution.x * ar, base_resolution.y) + + c.set_size(rc) + + _scale = get_size() / rc + + + if p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_VISIBILITY_CHANGED: + for c in get_children(): + + if not c is Viewport: + continue + + if is_visible_in_tree(): + c.set_update_mode(Viewport.UPDATE_ALWAYS) + else: + c.set_update_mode(Viewport.UPDATE_DISABLED) + + c.set_handle_input_locally(false); #do not handle input locally here + + if p_what == NOTIFICATION_DRAW: + for c in get_children(): + + if not c is Viewport: + continue + + draw_texture_rect(c.get_texture(), Rect2(Vector2(), get_size() * Vector2(1, -1)), false) + +func _input(p_event): + + if (Engine.is_editor_hint()): + return; + + var xform : Transform2D = get_global_transform() + + var scale_xf : Transform2D + scale_xf = scale_xf.scaled(_scale) + xform *= scale_xf + + var ev : InputEvent = p_event.xformed_by(xform.affine_inverse()) + + for c in get_children(): + + if not c is Viewport or c.is_input_disabled(): + continue + + c.input(ev) + +func _unhandled_input(p_event): + + if Engine.is_editor_hint(): + return + + var xform : Transform2D = get_global_transform() + + var scale_xf : Transform2D + scale_xf = scale_xf.scaled(_scale) + xform *= scale_xf + + var ev : InputEvent = p_event.xformed_by(xform.affine_inverse()) + + for c in get_children(): + + if not c is Viewport or c.is_input_disabled(): + continue + + c.unhandled_input(ev) + diff --git a/game/scenes/Main.tscn b/game/scenes/Main.tscn index 43dcdb4..9ed9bac 100644 --- a/game/scenes/Main.tscn +++ b/game/scenes/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=8 format=2] [ext_resource path="res://scenes/MainScene.gd" type="Script" id=1] [ext_resource path="res://scenes/World.tscn" type="PackedScene" id=2] @@ -6,6 +6,7 @@ [ext_resource path="res://ui/theme/ui_theme.tres" type="Theme" id=4] [ext_resource path="res://ui/debug/DebugInfo.tscn" type="PackedScene" id=5] [ext_resource path="res://debug/FreeLookCam.tscn" type="PackedScene" id=6] +[ext_resource path="res://scenes/GameViewport.gd" type="Script" id=7] [node name="Main" type="Node"] script = ExtResource( 1 ) @@ -13,6 +14,8 @@ menu_scene = ExtResource( 3 ) world_scene = ExtResource( 2 ) debug_camera_scene = ExtResource( 6 ) loading_screen_path = NodePath("LoadingScreen/PanelContainer") +gui_path = NodePath("GUI") +viewport_path = NodePath("GameViewport/Viewport") [node name="LoadingScreen" type="CanvasLayer" parent="."] layer = 100 @@ -94,3 +97,21 @@ LOADING " [node name="DebugInfo" parent="." instance=ExtResource( 5 )] + +[node name="GameViewport" type="Container" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +mouse_filter = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +script = ExtResource( 7 ) + +[node name="Viewport" type="Viewport" parent="GameViewport"] +size = Vector2( 400, 200 ) +handle_input_locally = false +disable_3d = true +usage = 0 +render_target_update_mode = 3 +audio_listener_enable_2d = true + +[node name="GUI" type="Node" parent="."] diff --git a/game/scenes/MainScene.gd b/game/scenes/MainScene.gd index 0d6491f..164ac6e 100644 --- a/game/scenes/MainScene.gd +++ b/game/scenes/MainScene.gd @@ -26,6 +26,11 @@ export(PackedScene) var menu_scene : PackedScene export(PackedScene) var world_scene : PackedScene export(PackedScene) var debug_camera_scene : PackedScene export(NodePath) var loading_screen_path : NodePath +export(NodePath) var gui_path : NodePath +export(NodePath) var viewport_path : NodePath + +var gui : Node +var viewport : Node enum StartSceneTypes { NONE, MENU, WORLD @@ -40,6 +45,8 @@ var _modules : Array func _ready() -> void: _loading_screen = get_node(loading_screen_path) + gui = get_node(gui_path) + viewport = get_node(viewport_path) ProfileManager.load() ESS.load_entity_spawner() @@ -116,7 +123,7 @@ func switch_scene(scene : int) -> void: elif scene == StartSceneTypes.WORLD: var gs : Node = world_scene.instance() - add_child(gs) + viewport.add_child(gs) gs.owner = self current_scene = gs @@ -149,6 +156,9 @@ func switch_scene(scene : int) -> void: if current_scene.needs_loading_screen(): show_loading_screen() +func add_entity(entity : Entity) -> void: + pass + func load_character(file_name : String) -> void: current_character_file_name = file_name diff --git a/game/scripts/networking/SpawnPoint.gd b/game/scripts/networking/SpawnPoint.gd index 06f4eeb..3663cdb 100644 --- a/game/scripts/networking/SpawnPoint.gd +++ b/game/scripts/networking/SpawnPoint.gd @@ -41,7 +41,7 @@ var connect_button : Button var naturalist_button : Button var player : Entity -var terrarin : VoxelWorld +#var terrarin : VoxelWorld var spawned : bool = false @@ -61,7 +61,7 @@ func _ready(): naturalist_button = get_node(naturalist_button_path) naturalist_button.connect("pressed", self, "_on_client_naturalist_button_clicked") - terrarin = get_node(terrarin_path) as VoxelWorld +# terrarin = get_node(terrarin_path) as VoxelWorld Server.connect("cplayer_master_created", self, "_cplayer_master_created") @@ -90,8 +90,8 @@ func spawn(): # ESS.entity_spawner.spawn_mob(1, 50, Vector3(54, 6, 22)) # ESS.entity_spawner.spawn_mob(1, 50, Vector3(76, 6, 54)) -func set_terrarin_player(): - terrarin.set_player(player.get_body() as Spatial) +#func set_terrarin_player(): +# terrarin.set_player(player.get_body() as Spatial) func _on_host_button_clicked(): get_tree().connect("network_peer_connected", self, "_network_peer_connected") diff --git a/game/ui/nameplates/NamePlate.tscn b/game/ui/nameplates/NamePlate.tscn index 43518ef..989b77f 100644 --- a/game/ui/nameplates/NamePlate.tscn +++ b/game/ui/nameplates/NamePlate.tscn @@ -5,7 +5,7 @@ [ext_resource path="res://player/NamePlate.gd" type="Script" id=3] [node name="NamePlate" type="VBoxContainer"] -margin_right = 62.0 +margin_right = 60.0 margin_bottom = 17.0 rect_scale = Vector2( 0.75, 0.75 ) mouse_filter = 2 @@ -21,7 +21,7 @@ normal_color = Color( 0.74902, 0.74902, 0.74902, 1 ) mouseover_scale = Vector2( 0.75, 0.75 ) [node name="Name" type="Label" parent="."] -margin_right = 62.0 +margin_right = 60.0 margin_bottom = 15.0 custom_constants/line_spacing = 0 text = "Asda" @@ -30,14 +30,14 @@ valign = 1 [node name="MarginContainer" type="MarginContainer" parent="."] margin_top = 15.0 -margin_right = 62.0 +margin_right = 60.0 margin_bottom = 17.0 size_flags_horizontal = 15 size_flags_vertical = 15 size_flags_stretch_ratio = 2.3 [node name="TextureProgress" type="TextureProgress" parent="MarginContainer"] -margin_right = 62.0 +margin_right = 60.0 margin_bottom = 2.0 size_flags_horizontal = 3 size_flags_vertical = 3 @@ -54,15 +54,15 @@ stretch_margin_bottom = 1 [node name="CenterContainer" type="CenterContainer" parent="MarginContainer"] visible = false -margin_right = 62.0 +margin_right = 60.0 margin_bottom = 2.0 size_flags_horizontal = 3 size_flags_vertical = 3 [node name="Label" type="Label" parent="MarginContainer/CenterContainer"] visible = false -margin_left = 22.0 +margin_left = 21.0 margin_top = -7.0 -margin_right = 40.0 +margin_right = 39.0 margin_bottom = 8.0 text = "50%" diff --git a/game/ui/nameplates/name_plate_dynamicfont.tres b/game/ui/nameplates/name_plate_dynamicfont.tres index b9a53e2..306f2ca 100644 --- a/game/ui/nameplates/name_plate_dynamicfont.tres +++ b/game/ui/nameplates/name_plate_dynamicfont.tres @@ -2,7 +2,6 @@ [ext_resource path="res://fonts/VT323-Regular.ttf" type="DynamicFontData" id=1] - [resource] size = 14 font_data = ExtResource( 1 ) diff --git a/game/ui/player_ui/player_ui.gd b/game/ui/player_ui/player_ui.gd index 1d2c524..f4a11c0 100644 --- a/game/ui/player_ui/player_ui.gd +++ b/game/ui/player_ui/player_ui.gd @@ -29,3 +29,6 @@ func _ready(): func _on_Player_onc_open_loot_winow_request() -> void: if loot_window != null: loot_window.show() + +func set_player(player : Entity) -> void: + $GUI.set_player(player) diff --git a/game/ui/player_ui/player_ui.tscn b/game/ui/player_ui/player_ui.tscn index 700a97b..45ad5f5 100644 --- a/game/ui/player_ui/player_ui.tscn +++ b/game/ui/player_ui/player_ui.tscn @@ -40,7 +40,7 @@ script = ExtResource( 1 ) __meta__ = { "_edit_lock_": true } -child_controls = [ NodePath("Unitframes"), NodePath("Actionbars"), NodePath("Windows/SpellBookWindow"), NodePath("Buttons"), NodePath("Castbar"), NodePath("AuraFrame"), NodePath("Windows/Inventory"), NodePath("Windows/LootWindow"), NodePath("Windows/TalentWindow"), NodePath("Windows/CraftingWindow") ] +child_controls = [ NodePath("Unitframes"), NodePath("Actionbars"), NodePath("Windows/SpellBookWindow"), NodePath("Buttons"), NodePath("Castbar"), NodePath("AuraFrame"), NodePath("Windows/Inventory"), NodePath("Windows/LootWindow"), NodePath("Windows/TalentWindow"), NodePath("Windows/CraftingWindow"), NodePath("TouchTargetControls/HBoxContainer/TargetPad/Analog"), NodePath("TouchMovementControls/HBoxContainer/VBoxContainer/TouchPad/Analog") ] [node name="TouchTargetControls" type="MarginContainer" parent="GUI"] visible = false @@ -86,6 +86,7 @@ __meta__ = { [node name="Analog" parent="GUI/TouchTargetControls/HBoxContainer/TargetPad" instance=ExtResource( 13 )] position = Vector2( 40, 30 ) +listenerNodePath = "." padname = "TargetPad" [node name="TouchMovementControls" type="MarginContainer" parent="GUI"] @@ -131,6 +132,7 @@ size_flags_vertical = 15 [node name="Analog" parent="GUI/TouchMovementControls/HBoxContainer/VBoxContainer/TouchPad" instance=ExtResource( 13 )] position = Vector2( 107.368, 94.2101 ) +listenerNodePath = "." padname = "TouchPad" [node name="Control" type="Control" parent="GUI/TouchMovementControls/HBoxContainer"] diff --git a/game/ui/touch_pad/analog.gd b/game/ui/touch_pad/analog.gd index 2d8f5f1..3dd0183 100644 --- a/game/ui/touch_pad/analog.gd +++ b/game/ui/touch_pad/analog.gd @@ -20,6 +20,8 @@ var ballPos = Vector2() var squaredHalfSizeLength = 0 var currentPointerIDX = INACTIVE_IDX; +var entity + func _ready(): set_process_input(true) bg = get_node("bg") @@ -29,16 +31,17 @@ func _ready(): halfSize = bg.texture.get_size()/2 squaredHalfSizeLength = halfSize.x * halfSize.y - if (listenerNodePath != "" && listenerNodePath!=null): - listenerNode = get_node(listenerNodePath) - elif listenerNodePath=="": - listenerNode = null +# if (listenerNodePath != "" && listenerNodePath!=null): +# listenerNode = get_node(listenerNodePath) +# elif listenerNodePath=="": +# listenerNode = null # isDynamicallyShowing = isDynamicallyShowing and parent extends Control if isDynamicallyShowing: modulate.a = 0 # hide() + func get_force(): return currentForce @@ -154,3 +157,13 @@ func isReleased(event): return !event.is_pressed() elif event is InputEventMouseButton: return !event.is_pressed() + +func set_player(p): + entity = p + listenerNode = p.get_body() + + if listenerNode == null: + call_deferred("get_body") + +func get_body(): + listenerNode = entity.get_body()