player_math_25d.gd 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Handles Player-specific behavior like moving. We calculate such things with CharacterBody3D.
  2. class_name PlayerMath25D # No icon necessary
  3. extends CharacterBody3D
  4. var vertical_speed := 0.0
  5. var isometric_controls := true
  6. @onready var _parent_node25d: Node25D = get_parent()
  7. func _physics_process(delta: float) -> void:
  8. if Input.is_action_pressed(&"exit"):
  9. get_tree().quit()
  10. if Input.is_action_just_pressed(&"view_cube_demo"):
  11. get_tree().change_scene_to_file("res://assets/cube/cube.tscn")
  12. return
  13. if Input.is_action_just_pressed(&"toggle_isometric_controls"):
  14. isometric_controls = not isometric_controls
  15. if Input.is_action_just_pressed(&"reset_position") or position.y <= -100:
  16. # Reset player position if the player fell down into the void.
  17. transform = Transform3D(Basis(), Vector3.UP * 0.5)
  18. vertical_speed = 0
  19. else:
  20. _horizontal_movement(delta)
  21. _vertical_movement(delta)
  22. # Checks WASD and Shift for horizontal movement via move_and_slide.
  23. func _horizontal_movement(_delta: float) -> void:
  24. var local_x := Vector3.RIGHT
  25. var local_z := Vector3.BACK
  26. if isometric_controls and is_equal_approx(Node25D.SCALE * 0.86602540378, _parent_node25d.get_basis()[0].x):
  27. local_x = Vector3(0.70710678118, 0, -0.70710678118)
  28. local_z = Vector3(0.70710678118, 0, 0.70710678118)
  29. # Gather player input and add directional movement to a Vector3 variable.
  30. var movement_vec2 := Input.get_vector(&"move_left", &"move_right", &"move_forward", &"move_back")
  31. var move_dir: Vector3 = local_x * movement_vec2.x + local_z * movement_vec2.y
  32. velocity = move_dir * 10
  33. if Input.is_action_pressed(&"movement_modifier"):
  34. velocity /= 2
  35. move_and_slide()
  36. # Checks Jump and applies gravity and vertical speed via move_and_collide.
  37. func _vertical_movement(delta: float) -> void:
  38. if Input.is_action_just_pressed(&"jump"):
  39. vertical_speed = 60
  40. vertical_speed -= delta * 240 # Gravity
  41. var k := move_and_collide(Vector3.UP * vertical_speed * delta)
  42. if k != null:
  43. vertical_speed = 0