From 5c242b216ea40ebd35a8f5c4a38ef0d739eb3845 Mon Sep 17 00:00:00 2001 From: Makosai Date: Fri, 22 Sep 2023 05:01:46 -0400 Subject: [PATCH] Add an early combat test and physics layers. --- assets/characters/enemies/aqua_wizard.tscn | 11 +++++-- assets/characters/npcs/larry.tscn | 33 ++++--------------- project.godot | 10 ++++++ scenes/reia/reia.tscn | 7 ++++ scripts/mob/attackable/attackable.gd | 10 +++--- scripts/mob/attackable/attackable_health.tscn | 33 +++++++++++++++++++ scripts/mob/attackable/enemy/aqua_wizard.gd | 1 + .../mob/attackable/npc}/larry.gd | 0 scripts/mob/attackable/player/player.gd | 27 ++++++++++++--- scripts/utils/physics.gd | 17 ++++++++++ 10 files changed, 110 insertions(+), 39 deletions(-) create mode 100644 scripts/mob/attackable/attackable_health.tscn create mode 100644 scripts/mob/attackable/enemy/aqua_wizard.gd rename {assets/characters/npcs => scripts/mob/attackable/npc}/larry.gd (100%) create mode 100644 scripts/utils/physics.gd diff --git a/assets/characters/enemies/aqua_wizard.tscn b/assets/characters/enemies/aqua_wizard.tscn index a628ae8b..5a47207d 100644 --- a/assets/characters/enemies/aqua_wizard.tscn +++ b/assets/characters/enemies/aqua_wizard.tscn @@ -1,12 +1,16 @@ -[gd_scene load_steps=3 format=3 uid="uid://op561o2k0d4o"] +[gd_scene load_steps=5 format=3 uid="uid://op561o2k0d4o"] +[ext_resource type="Script" path="res://scripts/mob/attackable/enemy/aqua_wizard.gd" id="1_2xua2"] +[ext_resource type="PackedScene" uid="uid://bqpe013mgarni" path="res://scripts/mob/attackable/attackable_health.tscn" id="1_051d3"] [ext_resource type="PackedScene" uid="uid://b3p21o2ljivu2" path="res://assets/characters/npcs/aqua_wizard.glb" id="1_hf2bh"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_jxymx"] radius = 0.573225 height = 2.59712 -[node name="AquaWizard" type="CharacterBody3D"] +[node name="AquaWizard" type="CharacterBody3D" node_paths=PackedStringArray("health_manager")] +script = ExtResource("1_2xua2") +health_manager = NodePath("Visuals/Health") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.29, 0) @@ -14,5 +18,8 @@ shape = SubResource("CapsuleShape3D_jxymx") [node name="Visuals" type="Node3D" parent="."] +[node name="Health" parent="Visuals" instance=ExtResource("1_051d3")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.59677, 0) + [node name="AquaWizard" parent="Visuals" instance=ExtResource("1_hf2bh")] transform = Transform3D(1.5, 0, 0, 0, 1.5, 0, 0, 0, 1.5, 0, 0, 0) diff --git a/assets/characters/npcs/larry.tscn b/assets/characters/npcs/larry.tscn index c1a3f36a..d7b7dbd4 100644 --- a/assets/characters/npcs/larry.tscn +++ b/assets/characters/npcs/larry.tscn @@ -1,20 +1,15 @@ -[gd_scene load_steps=7 format=3 uid="uid://bmicgu6inl8e5"] +[gd_scene load_steps=5 format=3 uid="uid://bmicgu6inl8e5"] -[ext_resource type="Script" path="res://assets/characters/npcs/larry.gd" id="1_7b1nv"] -[ext_resource type="Script" path="res://scripts/mob/attackable/health_manager.gd" id="1_ile63"] +[ext_resource type="Script" path="res://scripts/mob/attackable/npc/larry.gd" id="1_mma82"] [ext_resource type="PackedScene" uid="uid://b3p21o2ljivu2" path="res://assets/characters/npcs/aqua_wizard.glb" id="1_perw3"] -[ext_resource type="PackedScene" uid="uid://bb6pl2i42qjqf" path="res://scenes/ui/status_bars/health_bar.tscn" id="2_46ytx"] +[ext_resource type="PackedScene" uid="uid://bqpe013mgarni" path="res://scripts/mob/attackable/attackable_health.tscn" id="2_8ta6c"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_tslik"] radius = 0.376215 height = 1.77435 -[sub_resource type="ViewportTexture" id="ViewportTexture_vjeyi"] -viewport_path = NodePath("Visuals/Health/SubViewport") - -[node name="Larry" type="CharacterBody3D" node_paths=PackedStringArray("health_manager")] -script = ExtResource("1_7b1nv") -health_manager = NodePath("Visuals/Health") +[node name="Larry" type="CharacterBody3D"] +script = ExtResource("1_mma82") [node name="CollisionShape3D" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.88408, 0) @@ -22,23 +17,7 @@ shape = SubResource("CapsuleShape3D_tslik") [node name="Visuals" type="Node3D" parent="."] -[node name="Health" type="Node3D" parent="Visuals"] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.72083, 0) -script = ExtResource("1_ile63") -metadata/_edit_lock_ = true - -[node name="SubViewport" type="SubViewport" parent="Visuals/Health"] -transparent_bg = true -size = Vector2i(530, 65) - -[node name="HealthBar" parent="Visuals/Health/SubViewport" instance=ExtResource("2_46ytx")] - -[node name="Sprite" type="Sprite3D" parent="Visuals/Health"] -transform = Transform3D(0.15, 0, 0, 0, 0.15, 0, 0, 0, 0.15, 0, 0.0731125, 0) -texture = SubResource("ViewportTexture_vjeyi") -metadata/_edit_lock_ = true - -[node name="HideTimer" type="Timer" parent="Visuals/Health"] +[node name="Health" parent="Visuals" instance=ExtResource("2_8ta6c")] [node name="Larry" parent="Visuals" instance=ExtResource("1_perw3")] metadata/_edit_lock_ = true diff --git a/project.godot b/project.godot index f87d938a..bc970b94 100644 --- a/project.godot +++ b/project.godot @@ -71,6 +71,16 @@ quit={ ] } +[layer_names] + +3d_physics/layer_1="walls" +3d_physics/layer_2="attackable" +3d_physics/layer_3="player" +3d_physics/layer_4="npc" +3d_physics/layer_5="enemy" +3d_physics/layer_6="item" +3d_physics/layer_9="ground" + [rendering] textures/vram_compression/import_etc2_astc=true diff --git a/scenes/reia/reia.tscn b/scenes/reia/reia.tscn index 78b07764..f985f040 100644 --- a/scenes/reia/reia.tscn +++ b/scenes/reia/reia.tscn @@ -28,17 +28,22 @@ light_data = ExtResource("1_6htgl") [node name="MyPlayer" parent="Players" instance=ExtResource("3_m63h2")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.347, -0.754201, 2.465) +collision_layer = 6 +collision_mask = 287 [node name="NPCs" type="Node3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.57933, -0.75015, 1.28274) [node name="Larry" parent="NPCs" instance=ExtResource("2_w4lqh")] +collision_mask = 287 [node name="Enemies" type="Node3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.57933, -0.75015, 1.28274) [node name="AquaWizard" parent="Enemies" instance=ExtResource("3_jgoiv")] transform = Transform3D(-0.351793, 0, 0.936078, 0, 1, 0, -0.936078, 0, -0.351793, -5.80119, 0, 2.76315) +collision_layer = 18 +collision_mask = 287 [node name="Terrain" type="Node" parent="."] @@ -58,6 +63,8 @@ transform = Transform3D(0.766045, 0, 0.642788, 0, 1, 0, -0.642788, 0, 0.766045, [node name="Ground" type="CSGBox3D" parent="Terrain/Groundings"] transform = Transform3D(3.86976, 0, 0, 0, 1.00056, 0, 0, 0, 8.83943, 0, -0.75856, -24.7402) use_collision = true +collision_layer = 256 +collision_mask = 0 size = Vector3(35, 0.002, 15) material = SubResource("StandardMaterial3D_epo51") diff --git a/scripts/mob/attackable/attackable.gd b/scripts/mob/attackable/attackable.gd index 447b5b44..24cff006 100644 --- a/scripts/mob/attackable/attackable.gd +++ b/scripts/mob/attackable/attackable.gd @@ -11,10 +11,10 @@ var armor_defense = 3 @export var health_manager: HealthManager -func _on_hit(attacker: Attackable, attackee: Attackable): - if attackee.health == 0: return - attackee.health -= (attacker.strength + attacker.weapon_damage) - (attackee.defense + attackee.armor_defense) - attackee.health_manager.set_health(attackee.health, attackee.max_health) +func _on_hit(attacker: Attackable): + if health == 0: return + health -= (attacker.strength + attacker.weapon_damage) - (defense + armor_defense) + health_manager.set_health(health, max_health) func _attack(attackable: Attackable): - attackable._on_hit(attackable, self) + attackable._on_hit(self) diff --git a/scripts/mob/attackable/attackable_health.tscn b/scripts/mob/attackable/attackable_health.tscn new file mode 100644 index 00000000..d3d6eb87 --- /dev/null +++ b/scripts/mob/attackable/attackable_health.tscn @@ -0,0 +1,33 @@ +[gd_scene load_steps=4 format=3 uid="uid://bqpe013mgarni"] + +[ext_resource type="Script" path="res://scripts/mob/attackable/health_manager.gd" id="1_tkq40"] +[ext_resource type="PackedScene" uid="uid://bb6pl2i42qjqf" path="res://scenes/ui/status_bars/health_bar.tscn" id="2_xssh5"] + +[sub_resource type="ViewportTexture" id="ViewportTexture_pf4sx"] +viewport_path = NodePath("SubViewport") + +[node name="Health" type="Node3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.72083, 0) +script = ExtResource("1_tkq40") + +[node name="SubViewport" type="SubViewport" parent="."] +transparent_bg = true +size = Vector2i(520, 60) + +[node name="HealthBar" parent="SubViewport" instance=ExtResource("2_xssh5")] +anchors_preset = 15 +anchor_left = 0.0 +anchor_top = 0.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = 0.0 +offset_top = 0.0 +offset_right = 0.0 +offset_bottom = 0.0 +scale = Vector2(0.438, 0.438) + +[node name="Sprite" type="Sprite3D" parent="."] +transform = Transform3D(0.15, 0, 0, 0, 0.15, 0, 0, 0, 0.15, 0, 0.0731125, 0) +texture = SubResource("ViewportTexture_pf4sx") + +[node name="HideTimer" type="Timer" parent="."] diff --git a/scripts/mob/attackable/enemy/aqua_wizard.gd b/scripts/mob/attackable/enemy/aqua_wizard.gd new file mode 100644 index 00000000..9f7944c0 --- /dev/null +++ b/scripts/mob/attackable/enemy/aqua_wizard.gd @@ -0,0 +1 @@ +class_name AquaWizard extends Attackable diff --git a/assets/characters/npcs/larry.gd b/scripts/mob/attackable/npc/larry.gd similarity index 100% rename from assets/characters/npcs/larry.gd rename to scripts/mob/attackable/npc/larry.gd diff --git a/scripts/mob/attackable/player/player.gd b/scripts/mob/attackable/player/player.gd index e66f074d..91e70f7e 100644 --- a/scripts/mob/attackable/player/player.gd +++ b/scripts/mob/attackable/player/player.gd @@ -13,6 +13,10 @@ var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) +func _process(_delta): + if position.y <= -10: # handle falling off the map for now + position.y = 1 + func _input(_event: InputEvent): if Input.is_action_just_pressed("quit"): if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED: @@ -24,10 +28,6 @@ func _input(_event: InputEvent): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) print("click") - if Input.is_action_just_pressed("attack"): - GameUI.instance.status_bars.set_health(72, 100) - print("attack") - func _unhandled_input(event: InputEvent): if event is InputEventMouseButton: print("Mouse Click/Unclick at: ", event.position) @@ -63,6 +63,8 @@ func handle_camera(event: InputEvent): camera.position.z = pos_z_formula func handle_inputs(): + handle_attack() + # Handle jump. if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY @@ -70,7 +72,7 @@ func handle_inputs(): # Get the input direction and handle movement. var input_dir = Input.get_vector("left", "right", "forward", "back") - print("input dir: ", input_dir) + if(input_dir): print("input dir: ", input_dir) var direction = (camera_pivot.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() if direction: velocity.x = direction.x * SPEED @@ -82,3 +84,18 @@ func handle_inputs(): else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) + +func handle_attack(): + if Input.is_action_just_pressed("attack"): + # GameUI.instance.status_bars.set_health(72, 100) + var space = get_world_3d().direct_space_state + var query = PhysicsRayQueryParameters3D.create(camera.global_position, + camera.global_position - camera.global_transform.basis.z * 100, PhysicsUtils.arr_to_collision_mask( + [ PhysicsUtils.ENEMY_MASK ] + )) + var collision = space.intersect_ray(query) + if collision: + print("Attacked:", collision.collider.name) + _attack(collision.collider) + else: + print("Collide with:", "nuthin") diff --git a/scripts/utils/physics.gd b/scripts/utils/physics.gd new file mode 100644 index 00000000..7281748d --- /dev/null +++ b/scripts/utils/physics.gd @@ -0,0 +1,17 @@ +class_name PhysicsUtils + +const WALL_MASK: int = 1 +const ATTACKABLE_MASK: int = 2 +const PLAYER_MASK: int = 3 +const NPC_MASK: int = 4 +static var ENEMY_MASK: int = 5 +const ITEM_MASK: int = 6 + +const GROUND_MASK: int = 9 + +# Converts an array of layer numbers to a collision mask. +static func arr_to_collision_mask(arr: PackedByteArray): + var val = 0 + for num in arr: + val += pow(2, num - 1) + return val