player.gd 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. extends KinematicBody
  2. # Member variables
  3. const ANIM_FLOOR = 0
  4. const ANIM_AIR_UP = 1
  5. const ANIM_AIR_DOWN = 2
  6. const SHOOT_TIME = 1.5
  7. const SHOOT_SCALE = 2
  8. const CHAR_SCALE = Vector3(0.3, 0.3, 0.3)
  9. var facing_dir = Vector3(1, 0, 0)
  10. var movement_dir = Vector3()
  11. var jumping = false
  12. var turn_speed = 40
  13. var keep_jump_inertia = true
  14. var air_idle_deaccel = false
  15. var accel = 19.0
  16. var deaccel = 14.0
  17. var sharp_turn_threshold = 140
  18. var max_speed = 3.1
  19. var prev_shoot = false
  20. var linear_velocity=Vector3()
  21. var shoot_blend = 0
  22. func adjust_facing(p_facing, p_target, p_step, p_adjust_rate, current_gn):
  23. var n = p_target # Normal
  24. var t = n.cross(current_gn).normalized()
  25. var x = n.dot(p_facing)
  26. var y = t.dot(p_facing)
  27. var ang = atan2(y,x)
  28. if abs(ang) < 0.001: # Too small
  29. return p_facing
  30. var s = sign(ang)
  31. ang = ang * s
  32. var turn = ang * p_adjust_rate * p_step
  33. var a
  34. if ang < turn:
  35. a = ang
  36. else:
  37. a = turn
  38. ang = (ang - a) * s
  39. return (n * cos(ang) + t * sin(ang)) * p_facing.length()
  40. func _physics_process(delta):
  41. var lv = linear_velocity
  42. var g = Vector3(0, -9.8, 0)
  43. # var d = 1.0 - delta*state.get_total_density()
  44. # if (d < 0):
  45. # d = 0
  46. lv += g * delta # Apply gravity
  47. var anim = ANIM_FLOOR
  48. var up = -g.normalized() # (up is against gravity)
  49. var vv = up.dot(lv) # Vertical velocity
  50. var hv = lv - up * vv # Horizontal velocity
  51. var hdir = hv.normalized() # Horizontal direction
  52. var hspeed = hv.length() # Horizontal speed
  53. var dir = Vector3() # Where does the player intend to walk to
  54. var cam_xform = get_node("target/camera").get_global_transform()
  55. if Input.is_action_pressed("move_forward"):
  56. dir += -cam_xform.basis[2]
  57. if Input.is_action_pressed("move_backwards"):
  58. dir += cam_xform.basis[2]
  59. if Input.is_action_pressed("move_left"):
  60. dir += -cam_xform.basis[0]
  61. if Input.is_action_pressed("move_right"):
  62. dir += cam_xform.basis[0]
  63. var jump_attempt = Input.is_action_pressed("jump")
  64. var shoot_attempt = Input.is_action_pressed("shoot")
  65. var target_dir = (dir - up * dir.dot(up)).normalized()
  66. if is_on_floor():
  67. var sharp_turn = hspeed > 0.1 and rad2deg(acos(target_dir.dot(hdir))) > sharp_turn_threshold
  68. if dir.length() > 0.1 and !sharp_turn:
  69. if hspeed > 0.001:
  70. #linear_dir = linear_h_velocity/linear_vel
  71. #if (linear_vel > brake_velocity_limit and linear_dir.dot(ctarget_dir) < -cos(Math::deg2rad(brake_angular_limit)))
  72. # brake = true
  73. #else
  74. hdir = adjust_facing(hdir, target_dir, delta, 1.0 / hspeed * turn_speed, up)
  75. facing_dir = hdir
  76. else:
  77. hdir = target_dir
  78. if hspeed < max_speed:
  79. hspeed += accel * delta
  80. else:
  81. hspeed -= deaccel * delta
  82. if hspeed < 0:
  83. hspeed = 0
  84. hv = hdir * hspeed
  85. var mesh_xform = get_node("Armature").get_transform()
  86. var facing_mesh = -mesh_xform.basis[0].normalized()
  87. facing_mesh = (facing_mesh - up * facing_mesh.dot(up)).normalized()
  88. if hspeed>0:
  89. facing_mesh = adjust_facing(facing_mesh, target_dir, delta, 1.0/hspeed*turn_speed, up)
  90. var m3 = Basis(-facing_mesh, up, -facing_mesh.cross(up).normalized()).scaled(CHAR_SCALE)
  91. get_node("Armature").set_transform(Transform(m3, mesh_xform.origin))
  92. if not jumping and jump_attempt:
  93. vv = 7.0
  94. jumping = true
  95. get_node("sound_jump").play()
  96. else:
  97. if vv > 0:
  98. anim = ANIM_AIR_UP
  99. else:
  100. anim = ANIM_AIR_DOWN
  101. var hs
  102. if dir.length() > 0.1:
  103. hv += target_dir * (accel * 0.2) * delta
  104. if (hv.length() > max_speed):
  105. hv = hv.normalized()*max_speed
  106. else:
  107. if air_idle_deaccel:
  108. hspeed = hspeed - (deaccel * 0.2) * delta
  109. if hspeed < 0:
  110. hspeed = 0
  111. hv = hdir * hspeed
  112. if jumping and vv < 0:
  113. jumping = false
  114. lv = hv + up*vv
  115. if is_on_floor():
  116. movement_dir = lv
  117. linear_velocity = move_and_slide(lv,-g.normalized())
  118. if shoot_blend > 0:
  119. shoot_blend -= delta * SHOOT_SCALE
  120. if (shoot_blend < 0):
  121. shoot_blend = 0
  122. if shoot_attempt and not prev_shoot:
  123. shoot_blend = SHOOT_TIME
  124. var bullet = preload("res://bullet.scn").instance()
  125. bullet.set_transform(get_node("Armature/bullet").get_global_transform().orthonormalized())
  126. get_parent().add_child(bullet)
  127. bullet.set_linear_velocity(get_node("Armature/bullet").get_global_transform().basis[2].normalized() * 20)
  128. bullet.add_collision_exception_with(self) # Add it to bullet
  129. get_node("sound_shoot").play()
  130. prev_shoot = shoot_attempt
  131. if is_on_floor():
  132. get_node("AnimationTreePlayer").blend2_node_set_amount("walk", hspeed / max_speed)
  133. get_node("AnimationTreePlayer").transition_node_set_current("state", anim)
  134. get_node("AnimationTreePlayer").blend2_node_set_amount("gun", min(shoot_blend, 1.0))
  135. # state.set_angular_velocity(Vector3())
  136. func _ready():
  137. get_node("AnimationTreePlayer").set_active(true)