vanburendolphin

PlayerController

Jul 28th, 2025
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 5.64 KB | Gaming | 0 0
  1. extends CharacterBody3D
  2. class_name Player
  3.  
  4. # Ссылки на компоненты
  5. @onready var health_system: HealthSystem = $HealthSystem
  6. @onready var stamina_system: StaminaSystem = $StaminaSystem  
  7. @onready var movement_system: MovementSystem = $MovementSystem
  8. @onready var camera_pivot: Node3D = $CameraPivot
  9. @onready var camera: Camera3D = $CameraPivot/Camera3D
  10. @onready var mesh: MeshInstance3D = $MeshInstance3D
  11.  
  12. # Настройки камеры
  13. @export var camera_distance: float = 10.0
  14. @export var camera_height: float = 8.0
  15. @export var camera_angle: float = -30.0  # Для изометрии
  16.  
  17. # Переменные для ввода
  18. var movement_input: Vector2 = Vector2.ZERO
  19. var look_direction: Vector2 = Vector2.ZERO
  20. var mouse_position: Vector2 = Vector2.ZERO
  21.  
  22. # Сигналы для других систем
  23. signal health_changed(new_health: float, max_health: float)
  24. signal stamina_changed(movement_stamina: float, dash_stamina: float)
  25. signal player_died
  26. signal player_damaged(damage: float)
  27.  
  28. func _ready():
  29.     # Настройка камеры для изометрической проекции
  30.     setup_camera()
  31.    
  32.     # Подключение сигналов систем
  33.     health_system.health_changed.connect(_on_health_changed)
  34.     health_system.died.connect(_on_player_died)
  35.     stamina_system.stamina_changed.connect(_on_stamina_changed)
  36.    
  37.     # Добавление в группу игроков
  38.     add_to_group("player")
  39.  
  40. func setup_camera():
  41.     # Камера зафиксирована в изометрической позиции и НЕ поворачивается с персонажем
  42.     camera_pivot.position = Vector3.ZERO
  43.     camera.position = Vector3(0, camera_height, camera_distance)
  44.     camera.rotation_degrees = Vector3(camera_angle, 0, 0)
  45.     camera.projection = Camera3D.PROJECTION_ORTHOGONAL
  46.     camera.size = 20  # Размер ортогональной проекции
  47.  
  48. func _input(event):
  49.     # Получение позиции мыши для направления взгляда
  50.     if event is InputEventMouseMotion:
  51.         mouse_position = event.position
  52.  
  53. func _process(delta):
  54.     handle_input()
  55.     update_look_direction()
  56.  
  57. func _physics_process(delta):
  58.     # Передача ввода в систему движения
  59.     movement_system.process_movement(movement_input, delta)
  60.    
  61.     # Применение результата движения к CharacterBody3D
  62.     velocity = movement_system.get_velocity()
  63.     move_and_slide()
  64.  
  65. func handle_input():
  66.     # Получение ввода движения
  67.     movement_input = Vector2(
  68.         Input.get_axis("move_left", "move_right"),
  69.         Input.get_axis("move_up", "move_down")
  70.     )
  71.    
  72.     # Проверка действий
  73.     if Input.is_action_just_pressed("jump"):
  74.         movement_system.try_jump()
  75.    
  76.     if Input.is_action_just_pressed("sprint"):
  77.         movement_system.toggle_sprint()
  78.    
  79.     if Input.is_action_just_pressed("roll"):
  80.         var roll_direction = movement_input if movement_input.length() > 0 else look_direction
  81.         movement_system.try_roll(roll_direction)
  82.    
  83.     if Input.is_action_just_pressed("dash"):
  84.         var dash_direction = movement_input if movement_input.length() > 0 else look_direction
  85.         movement_system.try_dash(dash_direction)
  86.  
  87. func update_look_direction():
  88.     # Простой расчёт направления взгляда от центра экрана к курсору
  89.     var viewport = get_viewport()
  90.     var screen_center = viewport.get_visible_rect().size / 2
  91.     var mouse_pos = viewport.get_mouse_position()
  92.    
  93.     # Вычисляем направление от центра к курсору
  94.     var screen_direction = (mouse_pos - screen_center).normalized()
  95.    
  96.     # Преобразуем в игровое направление (учитывая изометрию)
  97.     look_direction = Vector2(screen_direction.x, -screen_direction.y)
  98.    
  99.     # Поворачиваем ТОЛЬКО меш персонажа, НЕ камеру и НЕ весь CharacterBody3D
  100.     if look_direction.length() > 0.1:
  101.         var target_rotation = atan2(-look_direction.x, -look_direction.y)
  102.         mesh.rotation.y = lerp_angle(mesh.rotation.y, target_rotation, 8.0 * get_process_delta_time())
  103.  
  104. # Методы для получения урона и восстановления
  105. func take_damage(damage: float, source: Node = null):
  106.     health_system.take_damage(damage)
  107.     player_damaged.emit(damage)
  108.  
  109. func heal(amount: float):
  110.     health_system.heal(amount)
  111.  
  112. func restore_stamina(amount: float, stamina_type: StaminaSystem.StaminaType):
  113.     stamina_system.restore_stamina(amount, stamina_type)
  114.  
  115. # Обработчики сигналов
  116. func _on_health_changed(new_health: float, max_health: float):
  117.     health_changed.emit(new_health, max_health)
  118.  
  119. func _on_player_died():
  120.     # Логика смерти игрока
  121.     movement_system.set_can_move(false)
  122.     player_died.emit()
  123.  
  124. func _on_stamina_changed(movement_stamina: float, dash_stamina: float):
  125.     stamina_changed.emit(movement_stamina, dash_stamina)
  126.  
  127. # Геттеры для других систем
  128. func get_health_percentage() -> float:
  129.     return health_system.get_health_percentage()
  130.  
  131. func get_stamina_percentage(stamina_type: StaminaSystem.StaminaType) -> float:
  132.     return stamina_system.get_stamina_percentage(stamina_type)
  133.  
  134. func is_alive() -> bool:
  135.     return health_system.is_alive()
  136.  
  137. func can_sprint() -> bool:
  138.     return stamina_system.can_use_stamina(movement_system.sprint_stamina_cost, StaminaSystem.StaminaType.MOVEMENT)
  139.  
  140. func can_roll() -> bool:
  141.     return stamina_system.can_use_stamina(movement_system.roll_stamina_cost, StaminaSystem.StaminaType.MOVEMENT)
  142.  
  143. func can_dash() -> bool:
  144.     return stamina_system.can_use_stamina(movement_system.dash_stamina_cost, StaminaSystem.StaminaType.DASH)
  145.  
Advertisement
Add Comment
Please, Sign In to add comment