enemy.gd 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. extends RigidBody
  2. # Member variables
  3. const STATE_WALKING = 0
  4. const STATE_DYING = 1
  5. var prev_advance = false
  6. var deaccel = 20.0
  7. var accel = 5
  8. var max_speed = 2
  9. var rot_dir = 4
  10. var rot_speed = 1
  11. var dying = false
  12. func _integrate_forces(state):
  13. var delta = state.get_step()
  14. var lv = state.get_linear_velocity()
  15. var g = state.get_total_gravity()
  16. lv += g * delta # Apply gravity
  17. var up = -g.normalized()
  18. if dying:
  19. state.set_linear_velocity(lv)
  20. return
  21. for i in range(state.get_contact_count()):
  22. var cc = state.get_contact_collider_object(i)
  23. var dp = state.get_contact_local_normal(i)
  24. if cc:
  25. if cc is preload("res://bullet.gd") and not cc.disabled:
  26. set_mode(MODE_RIGID)
  27. dying = true
  28. #lv = s.get_contact_local_normal(i)*400
  29. state.set_angular_velocity(-dp.cross(up).normalized() * 33.0)
  30. get_node("AnimationPlayer").play("impact")
  31. get_node("AnimationPlayer").queue("explode")
  32. set_friction(1)
  33. cc.disabled = true
  34. get_node("sound_hit").play()
  35. return
  36. var col_floor = get_node("Armature/ray_floor").is_colliding()
  37. var col_wall = get_node("Armature/ray_wall").is_colliding()
  38. var advance = not col_wall and col_floor
  39. var dir = get_node("Armature").get_transform().basis[2].normalized()
  40. var deaccel_dir = dir
  41. if advance:
  42. if dir.dot(lv) < max_speed:
  43. lv += dir * accel * delta
  44. deaccel_dir = dir.cross(g).normalized()
  45. else:
  46. if prev_advance:
  47. rot_dir = 1 # randf()*2.0 - 1.0
  48. dir = Basis(up, rot_dir * rot_speed * delta).xform(dir)
  49. get_node("Armature").set_transform(Transform().looking_at(-dir, up))
  50. var dspeed = deaccel_dir.dot(lv)
  51. dspeed -= deaccel * delta
  52. if dspeed < 0:
  53. dspeed = 0
  54. lv = lv - deaccel_dir * deaccel_dir.dot(lv) + deaccel_dir * dspeed
  55. state.set_linear_velocity(lv)
  56. prev_advance = advance
  57. func _die():
  58. queue_free()