From 1b375d109db5824646218e77acdeeae0a1d86556 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 1 Mar 2022 22:00:10 +0100 Subject: [PATCH] Reworked entity body setup. Now instead of Entity -> body -> (Sprite, Nameplate etc). It will worki like Body -> (Entity, sprite, Nameplate etc). This fixes y sorting. Nameplates are a bit broken right now, that will be fixed later. --- game/Node.tscn | 26 ++++++++++ game/player/Body.gd | 52 ++++++++++++++----- .../Body_2d_old.tscn => player/Body.tscn} | 5 +- game/player/DisplayPlayer.tscn | 28 +++------- game/player/GUI.gd | 10 +++- game/player/NamePlate.gd | 2 +- game/player/Player.tscn | 4 +- game/player/bs_entity_spawner.gd | 25 ++++++++- game/player/bs_entity_spawner.tscn | 4 +- .../terra_2d_world/Terrain2DWorldBlockyGD.gd | 2 +- .../sprites/entities/Human/species_human.tres | 2 +- game/ui/player/player_ui/player_ui.gd | 9 ++-- game/ui/player/player_ui/player_ui.tscn | 1 + 13 files changed, 117 insertions(+), 53 deletions(-) create mode 100644 game/Node.tscn rename game/{sprites/entities/Human/Body_2d_old.tscn => player/Body.tscn} (60%) diff --git a/game/Node.tscn b/game/Node.tscn new file mode 100644 index 0000000..8fb8ebb --- /dev/null +++ b/game/Node.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://sprites/characters/CharacterSideModel.tscn" type="PackedScene" id=1] + +[node name="Node" type="Node"] + +[node name="Node2D" type="Node2D" parent="."] + +[node name="Camera2D" type="Camera2D" parent="Node2D"] + +[node name="YSort" type="YSort" parent="Node2D"] + +[node name="Node" type="Node" parent="Node2D/YSort"] + +[node name="CharacterSideModel" parent="Node2D/YSort/Node" instance=ExtResource( 1 )] +position = Vector2( -55.7296, -15.6844 ) + +[node name="Node2" type="Node" parent="Node2D/YSort"] + +[node name="CharacterSideModel2" parent="Node2D/YSort/Node2" instance=ExtResource( 1 )] +position = Vector2( -57.0644, -30.7013 ) + +[node name="Node3" type="Node" parent="Node2D/YSort"] + +[node name="CharacterSideModel3" parent="Node2D/YSort/Node3" instance=ExtResource( 1 )] +position = Vector2( -40.7126, -17.6866 ) diff --git a/game/player/Body.gd b/game/player/Body.gd index 273e78b..5a8c29b 100644 --- a/game/player/Body.gd +++ b/game/player/Body.gd @@ -24,9 +24,9 @@ extends KinematicBody2D export(float) var MOUSE_SENSITIVITY : float = 0.05 -export(String) var world_path : String = "../.." -export(NodePath) var model_path : NodePath = "Rotation_Helper/Model" -export(NodePath) var character_skeleton_path : NodePath = "Character" +export(String) var world_path : String = ".." +#export(NodePath) var model_path : NodePath = "Rotation_Helper/Model" +#export(NodePath) var character_skeleton_path : NodePath = "Character" const BASE_SPEED = 60.0 @@ -91,20 +91,34 @@ func _enter_tree() -> void: world = get_node(world_path) as Node2D camera = get_node_or_null("Camera") as Camera2D - character_skeleton = get_node(character_skeleton_path) - entity = get_node("..") - entity.set_character_skeleton(character_skeleton) + #character_skeleton = get_node(character_skeleton_path) + #entity = get_node("..") + #entity.set_character_skeleton(character_skeleton) # entity.connect("notification_ccast", self, "on_notification_ccast") entity.connect("diesd", self, "on_diesd") entity.connect("onc_entity_controller_changed", self, "on_c_controlled_changed") - owner = entity + entity.connect("centity_data_changed", self, "on_centity_data_changed") on_c_controlled_changed() transform = entity.get_transform_2d(true) - + entity.set_body(self) set_physics_process(true) +func on_centity_data_changed(entd : EntityData): + if entd.entity_species_data: + var sk : CharacterSkeleton2D = entd.entity_species_data.get_model_data(0).body.instance() + + if character_skeleton: + character_skeleton.queue_free() + + character_skeleton = sk + entity.set_character_skeleton(character_skeleton) + + if character_skeleton: + add_child(character_skeleton) + + func _process(delta : float) -> void: if entity.ai_state == EntityEnums.AI_STATE_OFF: return @@ -135,6 +149,9 @@ func _physics_process(delta : float) -> void: if not _on: return + if !world: + return + if world.initial_generation: return @@ -184,6 +201,9 @@ func process_input(delta: float) -> void: var input_length : float = input_dir.length_squared() + if !character_skeleton: + return + if input_length > 0.1: #handle_graphic_facing(abs(dir.dot(Vector2(0, 1))) > 0.9) character_skeleton.update_facing(input_dir) @@ -398,9 +418,11 @@ func target(position : Vector2): if results: for result in results: - if result.collider and result.collider.owner is Entity: - entity.target_crequest_change((result.collider.owner as Node).get_path()) - return + if result.collider: + + if result.collider.has_method("get_entity"): + entity.target_crequest_change((result.collider.get_entity() as Node).get_path()) + return entity.target_crequest_change(NodePath()) else: @@ -413,8 +435,8 @@ func cmouseover(event): if results: for result in results: - if result.collider and result.collider.owner is Entity: - var mo : Entity = result.collider.owner as Entity + if result.collider and result.collider.has_method("get_entity") : + var mo : Entity = result.collider.get_entity() as Entity if last_mouse_over != null and last_mouse_over != mo: if is_instance_valid(last_mouse_over): @@ -501,7 +523,7 @@ func on_c_controlled_changed(): set_process_unhandled_input(false) var nameplatescn : PackedScene = ResourceLoader.load("res://ui/2d/world/nameplates/NamePlate.tscn") _nameplate = nameplatescn.instance() - get_parent().add_child(_nameplate) + add_child(_nameplate) func on_diesd(entity): if dead: @@ -525,3 +547,5 @@ remote func cset_position(pposition : Vector2) -> void: pposition = pposition +func get_entity() -> Entity: + return entity diff --git a/game/sprites/entities/Human/Body_2d_old.tscn b/game/player/Body.tscn similarity index 60% rename from game/sprites/entities/Human/Body_2d_old.tscn rename to game/player/Body.tscn index 39ebb4f..3fec441 100644 --- a/game/sprites/entities/Human/Body_2d_old.tscn +++ b/game/player/Body.tscn @@ -1,7 +1,6 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=3 format=2] [ext_resource path="res://player/Body.gd" type="Script" id=1] -[ext_resource path="res://sprites/characters/Character.tscn" type="PackedScene" id=2] [sub_resource type="CircleShape2D" id=1] radius = 8.0 @@ -9,7 +8,5 @@ radius = 8.0 [node name="Body" type="KinematicBody2D"] script = ExtResource( 1 ) -[node name="Character" parent="." instance=ExtResource( 2 )] - [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource( 1 ) diff --git a/game/player/DisplayPlayer.tscn b/game/player/DisplayPlayer.tscn index a99ddf4..445b7ea 100644 --- a/game/player/DisplayPlayer.tscn +++ b/game/player/DisplayPlayer.tscn @@ -1,26 +1,12 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://player/DisplayPlayer.gd" type="Script" id=1] -[node name="DisplayPlayer" type="Entity" groups=[ -"players", -]] -body_path = NodePath("KinematicBody2D") -character_skeleton_path = NodePath("KinematicBody2D/Rotation_Helper/Model") +[sub_resource type="EntityResourceHealth" id=1] + +[sub_resource type="EntityResourceSpeed" id=2] + +[node name="DisplayPlayer" type="Entity" groups=["players"]] sseed = 9240987 -cseed = 9240987 +sresources = [ SubResource( 1 ), SubResource( 2 ) ] script = ExtResource( 1 ) - -[node name="KinematicBody2D" type="KinematicBody2D" parent="."] - -[node name="Rotation_Helper" type="Spatial" parent="KinematicBody2D"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.960532, 0 ) -__meta__ = { -"_editor_description_": "" -} - -[node name="Model" type="Spatial" parent="KinematicBody2D/Rotation_Helper"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.875205, 0 ) -__meta__ = { -"_editor_description_": "" -} diff --git a/game/player/GUI.gd b/game/player/GUI.gd index 9d4a993..d4d6ed8 100644 --- a/game/player/GUI.gd +++ b/game/player/GUI.gd @@ -20,12 +20,18 @@ 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 +# var player : Entity = get_node(player_path) as Entity + var n : Node = get_node(player_path) + + if !n: + return + + var player : Entity = n.entity if (player == null): print("Player is null!") diff --git a/game/player/NamePlate.gd b/game/player/NamePlate.gd index 799c20d..5d91581 100644 --- a/game/player/NamePlate.gd +++ b/game/player/NamePlate.gd @@ -53,7 +53,7 @@ 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 + entity = get_node("..").entity entity.connect("centity_resource_added", self, "on_centity_resource_added") name_label.text = entity.centity_name diff --git a/game/player/Player.tscn b/game/player/Player.tscn index e00d396..5be9946 100644 --- a/game/player/Player.tscn +++ b/game/player/Player.tscn @@ -6,8 +6,6 @@ [sub_resource type="EntityResourceSpeed" id=2] -[node name="Player" type="Entity" groups=[ -"players", -]] +[node name="Player" type="Entity" groups=["players"]] sresources = [ SubResource( 1 ), SubResource( 2 ) ] script = ExtResource( 2 ) diff --git a/game/player/bs_entity_spawner.gd b/game/player/bs_entity_spawner.gd index 157a9b0..9a76bd7 100644 --- a/game/player/bs_entity_spawner.gd +++ b/game/player/bs_entity_spawner.gd @@ -24,6 +24,7 @@ extends ESSEntitySpawner # SOFTWARE. export(PackedScene) var player_display_scene : PackedScene +export(PackedScene) var player_body_scene : PackedScene export(String) var spawn_parent_path : String = "/root/Main" export(int) var default_level_override : int = 0 @@ -242,6 +243,21 @@ func _request_entity_spawn(createinfo : EntityCreateInfo): if createinfo.entity_player_type == EntityEnums.ENTITY_PLAYER_TYPE_DISPLAY: entity_node = player_display_scene.instance() + + if (createinfo.parent_path == ""): + if _spawn_parent == null: + _spawn_parent = get_tree().root.get_node(spawn_parent_path) + + if _spawn_parent.current_scene != null: + var spawn_parent = _spawn_parent.current_scene + + spawn_parent.add_child(entity_node) + else: + get_tree().root.get_node(createinfo.parent_path).add_child(entity_node) + + entity_node.setup(createinfo) + createinfo.created_entity = entity_node + return else: if not createinfo.networked: if createinfo.entity_controller == EntityEnums.ENITIY_CONTROLLER_PLAYER: @@ -255,6 +271,11 @@ func _request_entity_spawn(createinfo : EntityCreateInfo): print("EntityManager: entity node is null") return null + var body : Node2D = player_body_scene.instance() + body.add_child(entity_node) + body.entity = entity_node + + entity_node.set_transform_2d(createinfo.transform2d) if (createinfo.parent_path == ""): @@ -264,9 +285,9 @@ func _request_entity_spawn(createinfo : EntityCreateInfo): if _spawn_parent.current_scene != null: var spawn_parent = _spawn_parent.current_scene - spawn_parent.add_child(entity_node) + spawn_parent.add_child(body) else: - get_tree().root.get_node(createinfo.parent_path).add_child(entity_node) + get_tree().root.get_node(createinfo.parent_path).add_child(body) entity_node.setup(createinfo) diff --git a/game/player/bs_entity_spawner.tscn b/game/player/bs_entity_spawner.tscn index 2c1ee59..e59d176 100644 --- a/game/player/bs_entity_spawner.tscn +++ b/game/player/bs_entity_spawner.tscn @@ -1,8 +1,10 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://player/bs_entity_spawner.gd" type="Script" id=1] [ext_resource path="res://player/DisplayPlayer.tscn" type="PackedScene" id=2] +[ext_resource path="res://player/Body.tscn" type="PackedScene" id=3] [node name="ESSEntitySpawner" type="ESSEntitySpawner"] script = ExtResource( 1 ) player_display_scene = ExtResource( 2 ) +player_body_scene = ExtResource( 3 ) diff --git a/game/scenes/terra_2d_world/Terrain2DWorldBlockyGD.gd b/game/scenes/terra_2d_world/Terrain2DWorldBlockyGD.gd index 896c3c2..e833f96 100644 --- a/game/scenes/terra_2d_world/Terrain2DWorldBlockyGD.gd +++ b/game/scenes/terra_2d_world/Terrain2DWorldBlockyGD.gd @@ -104,7 +104,7 @@ func generate() -> void: func save() -> void: if _player == null or _player_file_name == "": return - + ESS.entity_spawner.save_player(_player, _player_file_name) func _generation_finished(): diff --git a/game/sprites/entities/Human/species_human.tres b/game/sprites/entities/Human/species_human.tres index 359583d..fa9caf2 100644 --- a/game/sprites/entities/Human/species_human.tres +++ b/game/sprites/entities/Human/species_human.tres @@ -1,6 +1,6 @@ [gd_resource type="EntitySpeciesData" load_steps=3 format=2] -[ext_resource path="res://sprites/entities/Human/Body_2d_old.tscn" type="PackedScene" id=1] +[ext_resource path="res://sprites/characters/Character.tscn" type="PackedScene" id=1] [sub_resource type="SpeciesModelData" id=1] bone_structure = 4 diff --git a/game/ui/player/player_ui/player_ui.gd b/game/ui/player/player_ui/player_ui.gd index bbd8093..5e20556 100644 --- a/game/ui/player/player_ui/player_ui.gd +++ b/game/ui/player/player_ui/player_ui.gd @@ -38,14 +38,17 @@ func _ready(): if player_path != null: var player = get_node(player_path) - + + if !player: + return + for c in windows.get_children(): if c.has_method("set_player"): - c.set_player(player) + c.set_player(player.entity) for c in gui_base.get_children(): if c.has_method("set_player"): - c.set_player(player) + c.set_player(player.entity) func initialize(): gui_base = get_node(gui_base_path) diff --git a/game/ui/player/player_ui/player_ui.tscn b/game/ui/player/player_ui/player_ui.tscn index 6779553..29fd134 100644 --- a/game/ui/player/player_ui/player_ui.tscn +++ b/game/ui/player/player_ui/player_ui.tscn @@ -12,6 +12,7 @@ [node name="GUILayer" type="CanvasLayer"] script = ExtResource( 6 ) +player_path = ".." gui_base_path = NodePath("GUI") buttons_path = NodePath("GUI/Buttons") windows_path = NodePath("GUI/Windows")