Player.gd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. extends KinematicBody
  2. #13.5
  3. export var max_speed = 13.5
  4. #10
  5. export var acceleration = 10
  6. export var turn_acceleration = 12
  7. export var max_speed_back = 180
  8. export var acceleration_back = 90
  9. #0.004
  10. export var turn_speed = 3.3
  11. export var gravity = -21
  12. var velocity = Vector3(0, 0, 0)
  13. var direction = Vector3(0, 0, 0)
  14. var current_turn = 0
  15. onready var wall_ray_cast = get_node("CollisionShape/WallRayCast")
  16. onready var floor_ray_cast = get_node("CollisionShape/FloorRayCast")
  17. onready var player_camera = get_node("PlayerCamera")
  18. var camera_angle = 0
  19. var player_control_enabled
  20. var was_on_wall
  21. #used for debugging now
  22. var jump_count = 0
  23. var inputEventMouseMove = null
  24. var is_on_ground
  25. func _ready():
  26. player_control_enabled = true
  27. was_on_wall = false
  28. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  29. func enable_player_control(enable):
  30. player_control_enabled = enable
  31. func _on_game_started():
  32. player_control_enabled = true
  33. func _input(event):
  34. if event is InputEventMouseMotion:
  35. inputEventMouseMove = event
  36. func walk(delta):
  37. var aim = global_transform.basis
  38. direction = Vector3()
  39. #if floor_ray_cast.is_colliding():
  40. # print ("floor collision!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
  41. #else:
  42. # print ("no floor collision")
  43. #if wall_ray_cast.is_colliding():
  44. # print ("wall collision#################################")
  45. if !wall_ray_cast.is_colliding() or floor_ray_cast.is_colliding():
  46. if Input.is_action_pressed("ui_up"):
  47. direction.x = direction.x - aim.y.x
  48. direction.z = direction.z - aim.y.z
  49. elif Input.is_action_pressed("ui_down"):
  50. direction.x = direction.x + aim.y.x
  51. direction.z = direction.z + aim.y.z
  52. if Input.is_action_pressed("ui_left"):
  53. direction.x = direction.x - aim.x.x
  54. direction.z = direction.z - aim.x.z
  55. if Input.is_action_pressed("ui_right"):
  56. direction.x = direction.x + aim.x.x
  57. direction.z = direction.z + aim.x.z
  58. direction.y = 0
  59. direction = direction.normalized()
  60. if (is_on_floor()):
  61. is_on_ground = true
  62. else:
  63. if !floor_ray_cast.is_colliding():
  64. is_on_ground = false
  65. if (is_on_ground and !is_on_floor()):
  66. move_and_collide(Vector3(0, -1, 0))
  67. velocity.y += gravity * delta
  68. var temp_velocity = velocity
  69. temp_velocity.y = 0
  70. var target = direction * max_speed
  71. var accel
  72. if direction.dot(temp_velocity) > 0:
  73. accel = 8
  74. else:
  75. accel = 16
  76. if wall_ray_cast.is_colliding() and !floor_ray_cast.is_colliding() and velocity.y > 0:
  77. accel = 20
  78. temp_velocity = temp_velocity.linear_interpolate(target, accel * delta)
  79. velocity.x = temp_velocity.x
  80. velocity.z = temp_velocity.z
  81. #print (velocity.length())
  82. velocity = move_and_slide(velocity, Vector3(0, 1, 0), 0.9, 4, 0.75)
  83. #velocity = move_and_slide(velocity, Vector3(0, 1, 0))
  84. func turn(delta):
  85. if inputEventMouseMove != null:
  86. var speed_factor = min(abs(inputEventMouseMove.relative.x), 3.6)
  87. var v_speed_factor = min(abs(inputEventMouseMove.relative.y), 3.6)
  88. current_turn = ((inputEventMouseMove.relative.x * -turn_speed) + (inputEventMouseMove.relative.x * -speed_factor * 0.4)) * delta
  89. #print (current_turn)
  90. var change = ((inputEventMouseMove.relative.y * -turn_speed) + (inputEventMouseMove.relative.y * -speed_factor * 0.4)) * delta
  91. if change + camera_angle < 90 and change + camera_angle > -90:
  92. player_camera.rotate_x(deg2rad(change))
  93. camera_angle += change
  94. else:
  95. current_turn = current_turn * 0.88
  96. rotate_y(deg2rad(current_turn))
  97. inputEventMouseMove = null
  98. func jump(delta):
  99. if player_control_enabled:
  100. var jump_height = 22
  101. if is_on_floor() and Input.is_action_pressed("jump"):
  102. velocity.y = jump_height
  103. jump_count = jump_count + 1
  104. get_node("/root/Save").save_node_data(self)
  105. func _physics_process(delta):
  106. walk(delta)
  107. turn(delta)
  108. jump(delta)
  109. func save():
  110. var save_dict = {
  111. jump_count = jump_count,
  112. }
  113. return save_dict