player_math_25d.gd 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Handles Player-specific behavior like moving. We calculate such things with CharacterBody3D.
  2. extends CharacterBody3D
  3. class_name PlayerMath25D # No icon necessary
  4. var vertical_speed := 0.0
  5. var isometric_controls := true
  6. @onready var _parent_node25d: Node25D = get_parent()
  7. func _process(delta):
  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"):
  16. transform = Transform3D(Basis(), Vector3.UP * 10)
  17. vertical_speed = 0
  18. else:
  19. _horizontal_movement(delta)
  20. _vertical_movement(delta)
  21. # Checks WASD and Shift for horizontal movement via move_and_slide.
  22. func _horizontal_movement(delta):
  23. var localX = Vector3.RIGHT
  24. var localZ = Vector3.BACK
  25. if isometric_controls and is_equal_approx(Node25D.SCALE * 0.86602540378, _parent_node25d.get_basis()[0].x):
  26. localX = Vector3(0.70710678118, 0, -0.70710678118)
  27. localZ = Vector3(0.70710678118, 0, 0.70710678118)
  28. # Gather player input and add directional movement to a Vector3 variable.
  29. var movement_vec2 = Input.get_vector(&"move_left", &"move_right", &"move_forward", &"move_back")
  30. var move_dir = localX * movement_vec2.x + localZ * movement_vec2.y
  31. velocity = move_dir * delta * 600
  32. if Input.is_action_pressed(&"movement_modifier"):
  33. velocity /= 2
  34. move_and_slide()
  35. # Checks Jump and applies gravity and vertical speed via move_and_collide.
  36. func _vertical_movement(delta):
  37. var localY = Vector3.UP
  38. if Input.is_action_just_pressed(&"jump"):
  39. vertical_speed = 1.25
  40. vertical_speed -= delta * 5 # Gravity
  41. var k = move_and_collide(localY * vertical_speed)
  42. if k != null:
  43. vertical_speed = 0