monsterfangen/entities/player/player.gd

119 lines
3.5 KiB
GDScript

extends CharacterBody3D
@onready var animated_mesh = $GobotSkin
var in_battle: bool = false
var enemy: MonsterData = null
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
func _ready() -> void:
Utils.set_player(self)
func on_save_game(saved_data : Array[SavedData]):
var data = SavedData.new()
data.position = global_position
data.scene_path = scene_file_path
saved_data.append(data)
func on_before_load_game():
get_parent().remove_child(self)
queue_free()
func on_load_game(saved_data: SavedData):
global_position = saved_data.position
# Update the global reference to the player
Utils.set_player(self)
func _physics_process(delta: float) -> void:
if in_battle == true:
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 := 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 := 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 := (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:
$GobotSkin.rotation.y = atan2(direction.x,direction.z)
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()
# what happens when a battle starts?
# the player is positioned (mostly somewhere else than the current position)
# ui changes
# music changes
func start_battle(p_enemy: Node3D) -> void:
enemy = p_enemy.data
var camera: Camera3D = $CameraRoot/SpringArm3D/Camera3D
# local z axis of the camera
var local_z = camera.global_transform.basis.z
var new_player_position = camera.global_transform.origin + local_z * 3
var monster_position = camera.global_transform.origin + local_z * 2
position = Vector3(new_player_position.x, position.y, new_player_position.z)
in_battle = true
velocity = Vector3.ZERO
animated_mesh.idle()
UI.show_battle_ui(true)
UI.show_ingame_controls(false)
# Spawn player monster
var monster_data : MonsterData = SaveManager.current_save.party[0]
var monster = Monster.new()
monster.data = monster_data
monster.idle = true
print("%s (%s), Lv. %s, %s XP" % [monster.data.name, monster.data.nickname, monster.data.level, monster.data.xp])
monster.position = Vector3(monster_position.x, position.y + 1, monster_position.z)
add_child(monster)
camera.look_at(monster.position, Vector3.UP, false)
# How to attack and finish the battle?
await get_tree().create_timer(5.0).timeout
print("Battle over")
remove_child(monster)
monster.queue_free()
in_battle = false