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

@ -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