extends CharacterBody3D @onready var animated_mesh: Node3D = %GobotSkin @onready var camera: Camera3D = %Camera3D ## State @export var battle_state: Enums.BattleState = Enums.BattleState.NOT_IN_BATTLE var in_battle: bool = false var player_monster: Monster = null var enemy: Monster = null ## Movement 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 # HACK: This should rather be connected from a global SignalManager or SignalBus 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 if battle_state != Enums.BattleState.NOT_IN_BATTLE: return animated_mesh.idle() # Add the gravity. if not is_on_floor(): velocity += get_gravity() * delta animated_mesh.fall() # Handle jump. if Input.is_action_just_pressed("ui_accept"): print("jump") animated_mesh.run() if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY # Camera rotation var camera_input_dir : Vector2 = Input.get_vector("joystick_right_left", "joystick_right_right", "joystick_right_left", "joystick_right_right") $CameraRoot.rotation.y += camera_input_dir.x # Get the input direction and handle the movement / deceleration. # As good practice, you should replace UI actions with custom gameplay actions. var input_dir : Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") if input_dir.length() > 0 and is_on_floor(): animated_mesh.run() if input_dir.length() > 0 and not is_on_floor(): animated_mesh.fall() var direction : Vector3 = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() # Rotate the direction with the camera, so that the cameras forward direction # is the joysticks and the players forward direction direction = direction.rotated(Vector3.UP, $CameraRoot.rotation.y) # Move character to the direction if direction.length() != 0: 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 velocity.z = direction.z * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) move_and_slide() if velocity.length() < 0.05: animated_mesh.idle() 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) func on_before_load_game() -> void: get_parent().remove_child(self) queue_free() 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: Enums.BattleState.STARTED: # Start with disabled UI UI.battle_ui.disable() UI.battle_ui.set_message("A wild %s appeared!" % enemy.data.name) await get_tree().create_timer(3.0).timeout battle_state = Enums.BattleState.PLAYER_TURN handle_battle() Enums.BattleState.PLAYER_TURN: UI.battle_ui.set_message("What will you do?") UI.battle_ui.enable() UI.battle_ui.update() Enums.BattleState.ENEMY_TURN: UI.battle_ui.disable() # If the enemy has no health left, we win if enemy.data.current_health <= 0: battle_state = Enums.BattleState.WIN handle_battle() return UI.battle_ui.set_message("What will the enemy do?") await get_tree().create_timer(2.0).timeout # 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() battle_state = Enums.BattleState.PLAYER_TURN handle_battle() Enums.BattleState.CATCH: print("TODO: Catch") 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() UI.ingame_menu.update() UI.show_battle_ui(false) UI.show_ingame_controls(true) var gained_xp: int = player_monster.data.calculate_gained_xp(enemy.data) player_monster.data.add_xp(gained_xp) print("You gained %s xp!" % str(gained_xp)) Enums.BattleState.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 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() # Set the current enemy enemy = p_enemy # Get the loccal z axis of the camera to position the player 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 * 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) # Create the player's monster player_monster = Monster.new() # Get the data from the party var monster_data : MonsterData = SaveManager.current_save.party[0] # And add it to the monster player_monster.data = monster_data player_monster.idle = true 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 handle_battle() 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 # 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 battle_state = Enums.BattleState.ENEMY_TURN handle_battle()