cubio.gd 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. extends KinematicBody
  2. # Member variables
  3. var g = -9.8
  4. var vel = Vector3()
  5. const MAX_SPEED = 5
  6. const JUMP_SPEED = 7
  7. const ACCEL= 2
  8. const DEACCEL= 4
  9. const MAX_SLOPE_ANGLE = 30
  10. func _fixed_process(delta):
  11. var dir = Vector3() # Where does the player intend to walk to
  12. var cam_xform = get_node("target/camera").get_global_transform()
  13. if (Input.is_action_pressed("move_forward")):
  14. dir += -cam_xform.basis[2]
  15. if (Input.is_action_pressed("move_backwards")):
  16. dir += cam_xform.basis[2]
  17. if (Input.is_action_pressed("move_left")):
  18. dir += -cam_xform.basis[0]
  19. if (Input.is_action_pressed("move_right")):
  20. dir += cam_xform.basis[0]
  21. dir.y = 0
  22. dir = dir.normalized()
  23. vel.y += delta*g
  24. var hvel = vel
  25. hvel.y = 0
  26. var target = dir*MAX_SPEED
  27. var accel
  28. if (dir.dot(hvel) > 0):
  29. accel = ACCEL
  30. else:
  31. accel = DEACCEL
  32. hvel = hvel.linear_interpolate(target, accel*delta)
  33. vel.x = hvel.x
  34. vel.z = hvel.z
  35. var motion = move(vel*delta)
  36. var on_floor = false
  37. var original_vel = vel
  38. var floor_velocity = Vector3()
  39. var attempts = 4
  40. while(is_colliding() and attempts):
  41. var n = get_collision_normal()
  42. if (rad2deg(acos(n.dot(Vector3(0, 1, 0)))) < MAX_SLOPE_ANGLE):
  43. # If angle to the "up" vectors is < angle tolerance,
  44. # char is on floor
  45. floor_velocity = get_collider_velocity()
  46. on_floor = true
  47. motion = n.slide(motion)
  48. vel = n.slide(vel)
  49. if (original_vel.dot(vel) > 0):
  50. # Do not allow to slide towads the opposite direction we were coming from
  51. motion=move(motion)
  52. if (motion.length() < 0.001):
  53. break
  54. attempts -= 1
  55. if (on_floor and floor_velocity != Vector3()):
  56. move(floor_velocity*delta)
  57. if (on_floor and Input.is_action_pressed("jump")):
  58. vel.y = JUMP_SPEED
  59. var crid = get_node("../elevator1").get_rid()
  60. func _ready():
  61. set_fixed_process(true)
  62. func _on_tcube_body_enter(body):
  63. get_node("../ty").show()