added sounds to ui and game, prepared interaction with character

This commit is contained in:
Luca 2024-10-12 14:30:39 +02:00
parent b5ace17da9
commit 21d7cf417a
42 changed files with 450 additions and 155 deletions

View file

@ -1,5 +1 @@
extends Node3D
func _on_area_3d_body_entered(body: Node3D) -> void:
print("%s entered" % body.name)
pass # Replace with function body.

View file

@ -1,9 +1,10 @@
[gd_scene load_steps=4 format=3 uid="uid://dibg4wpthwdov"]
[gd_scene load_steps=5 format=3 uid="uid://dibg4wpthwdov"]
[ext_resource type="Script" path="res://entities/healing_station/healing_station.gd" id="1_7biqd"]
[ext_resource type="ArrayMesh" uid="uid://dotngcuyvdg1v" path="res://assets/models/environment/kenney_nature-kit/bed.obj" id="2_m00qm"]
[sub_resource type="BoxShape3D" id="BoxShape3D_qkiex"]
size = Vector3(3.6, 2.5, 2.6)
size = Vector3(0.358923, 0.296997, 0.806982)
[sub_resource type="CylinderShape3D" id="CylinderShape3D_3nvn0"]
height = 1.0
@ -12,21 +13,22 @@ radius = 1.1
[node name="HealingStation" type="Node3D"]
script = ExtResource("1_7biqd")
[node name="Mesh" type="Node3D" parent="."]
[node name="Bed" type="MeshInstance3D" parent="."]
mesh = ExtResource("2_m00qm")
[node name="Collision" type="StaticBody3D" parent="."]
collision_layer = 2
collision_mask = 5
[node name="CollisionShape3D" type="CollisionShape3D" parent="Collision"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.25, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0159729, 0.148499, -0.0114014)
shape = SubResource("BoxShape3D_qkiex")
[node name="InteractableArea" type="Area3D" parent="."]
collision_layer = 8
[node name="CollisionShape3D" type="CollisionShape3D" parent="InteractableArea"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.6, 0.5, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0)
shape = SubResource("CylinderShape3D_3nvn0")
[connection signal="body_entered" from="InteractableArea" to="." method="_on_area_3d_body_entered"]

View file

@ -41,7 +41,7 @@ func _physics_process(delta: float) -> void:
var collider: Object = collision.get_collider()
# if the monster collided with the player...
if collider.name == "Player":
if collider.name == "Player" and collider.battle_state == Enums.BattleState.NOT_IN_BATTLE:
velocity = Vector3.ZERO
in_battle = true
collider.start_battle(self)

View file

@ -10,13 +10,17 @@ var player_monster: Monster = null
var enemy: Monster = null
## Movement
const SPEED : float = 5.0
const JUMP_VELOCITY : float = 4.5
const SPEED: float = 5.0
const JUMP_VELOCITY: float = 4.5
const ROTATION_SPEED: float = 12.0
func _ready() -> void:
# Set the global reference to the player
Utils.set_player(self)
# Connect the event from the joysticks click
get_viewport().get_tree().get_root().find_child("JoystickPanel", true, false).connect("on_interact", interact)
func _physics_process(delta: float) -> void:
# If the player is in a battle, handle it via start_battle and then handle_battle
@ -59,7 +63,8 @@ func _physics_process(delta: float) -> void:
# Move character to the direction
if direction.length() != 0:
$GobotSkin.rotation.y = atan2(direction.x,direction.z)
var target_angle: float = Vector3.BACK.signed_angle_to(direction, Vector3.UP)
$GobotSkin.rotation.y = lerp_angle(%GobotSkin.rotation.y, target_angle, ROTATION_SPEED * delta)
if direction:
velocity.x = direction.x * SPEED
@ -77,6 +82,7 @@ func _physics_process(delta: float) -> void:
func on_save_game(saved_data : Array[SavedData]) -> void:
var data : SavedData = SavedData.new()
data.position = global_position
data.rotation = rotation
data.scene_path = scene_file_path
saved_data.append(data)
@ -88,11 +94,16 @@ func on_before_load_game() -> void:
func on_load_game(saved_data: SavedData) -> void:
global_position = saved_data.position
rotation = saved_data.rotation
# Update the global reference to the player
Utils.set_player(self)
func interact() -> void:
print("interact")
func handle_battle() -> void:
match battle_state:
@ -123,6 +134,7 @@ func handle_battle() -> void:
# The enemy can currently only attack
var damage_amount: int = enemy.attack_enemy(player_monster)
UI.battle_ui.set_message("The enemy attacked you for %s damage!" % str(damage_amount))
SoundManager.play_sound_effect("Hit")
await get_tree().create_timer(1.0).timeout
UI.battle_ui.update()
@ -134,11 +146,14 @@ func handle_battle() -> void:
await get_tree().create_timer(3.0).timeout
Enums.BattleState.WIN:
SoundManager.play_sound_effect("Battle Win")
SoundManager.play_background_music("Unexplored Fields")
UI.battle_ui.disable()
UI.battle_ui.set_message("You won!")
await get_tree().create_timer(2.0).timeout
# End the battle and reset some states
# TODO: Maybe add a cooldown of 1.5s after the battle ended
battle_state = Enums.BattleState.NOT_IN_BATTLE
enemy.queue_free()
player_monster.queue_free()
@ -150,17 +165,19 @@ func handle_battle() -> void:
print("You gained %s xp!" % str(gained_xp))
Enums.BattleState.LOSE:
print("LOSE")
SoundManager.play_sound_effect("Lost Battle")
await get_tree().create_timer(3.0).timeout
## This method sets up a battle between the player and an enemy monster
func start_battle(p_enemy: Monster) -> void:
# Don't start a battle if we are already in one
# TODO: Maybe add a cooldown of 1.5s after the battle ended
if battle_state != Enums.BattleState.NOT_IN_BATTLE:
return
# TODO: Add a "surprise!" sound effect and an emoji to the players head
SoundManager.play_background_music("Unexpected Fight")
# Stop the player
velocity = Vector3.ZERO
animated_mesh.idle()
@ -172,12 +189,16 @@ func start_battle(p_enemy: Monster) -> void:
var local_z: Vector3 = camera.global_transform.basis.z
# Get the new player and monster positions
var new_player_position: Vector3 = camera.global_transform.origin + local_z * 3
var monster_position: Vector3 = camera.global_transform.origin + local_z * 3
var new_player_position: Vector3 = camera.global_transform.origin + local_z * 4
var monster_position: Vector3 = camera.global_transform.origin + local_z * 2
# Set the players new position
position = Vector3(new_player_position.x, position.y, new_player_position.z)
# and set the camera to the middle of the two involved monsters
#var new_camera_root_position: Vector3 = (p_enemy.global_position - monster_position) / 2.0 + monster_position
#%CameraRoot.global_position = Vector3(new_camera_root_position.x, %CameraRoot.global_position.y, new_camera_root_position.z)
# Change the UI
UI.show_battle_ui(true)
UI.show_ingame_controls(false)
@ -191,8 +212,9 @@ func start_battle(p_enemy: Monster) -> void:
# And add it to the monster
player_monster.data = monster_data
player_monster.idle = true
player_monster.position = Vector3(monster_position.x, position.y + 1, monster_position.z)
add_child(player_monster)
self.add_child(player_monster)
player_monster.global_position = Vector3(monster_position.x, position.y + 1, monster_position.z)
player_monster.look_at(enemy.position, Vector3.UP, true)
# Get into the battle and switch between the states
battle_state = Enums.BattleState.STARTED
@ -202,9 +224,10 @@ func start_battle(p_enemy: Monster) -> void:
func attack_enemy() -> void:
UI.battle_ui.disable()
UI.battle_ui.set_message("You attacked the enemy!")
SoundManager.play_sound_effect("Hit")
await get_tree().create_timer(1.0).timeout
# this should rather be player_monster.attack_enemy(enemy) for correct values
# FIXME: this must be changed to player_monster.attack_enemy(enemy) for correct values
enemy.take_damage(10)
UI.battle_ui.update()
await get_tree().create_timer(1.0).timeout

View file

@ -22,6 +22,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.85, 0)
shape = SubResource("CapsuleShape3D_xuba7")
[node name="CameraRoot" type="Node3D" parent="."]
unique_name_in_owner = true
[node name="SpringArm3D" type="SpringArm3D" parent="CameraRoot"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.64528, 2.83335)