enemy.gd 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. extends RigidBody2D
  2. # Member variables
  3. const STATE_WALKING = 0
  4. const STATE_DYING = 1
  5. var state = STATE_WALKING
  6. var direction = -1
  7. var anim = ""
  8. var rc_left = null
  9. var rc_right = null
  10. var WALK_SPEED = 50
  11. var bullet_class = preload("res://bullet.gd")
  12. func _die():
  13. queue_free()
  14. func _pre_explode():
  15. # Stay there
  16. clear_shapes()
  17. set_mode(MODE_STATIC)
  18. get_node("sound").play("explode")
  19. func _integrate_forces(s):
  20. var lv = s.get_linear_velocity()
  21. var new_anim = anim
  22. if (state == STATE_DYING):
  23. new_anim = "explode"
  24. elif (state == STATE_WALKING):
  25. new_anim = "walk"
  26. var wall_side = 0.0
  27. for i in range(s.get_contact_count()):
  28. var cc = s.get_contact_collider_object(i)
  29. var dp = s.get_contact_local_normal(i)
  30. if (cc):
  31. if (cc extends bullet_class and not cc.disabled):
  32. set_mode(MODE_RIGID)
  33. state = STATE_DYING
  34. #lv = s.get_contact_local_normal(i)*400
  35. s.set_angular_velocity(sign(dp.x)*33.0)
  36. set_friction(1)
  37. cc.disable()
  38. get_node("sound").play("hit")
  39. break
  40. if (dp.x > 0.9):
  41. wall_side = 1.0
  42. elif (dp.x < -0.9):
  43. wall_side = -1.0
  44. if (wall_side != 0 and wall_side != direction):
  45. direction = -direction
  46. get_node("sprite").set_scale(Vector2(-direction, 1))
  47. if (direction < 0 and not rc_left.is_colliding() and rc_right.is_colliding()):
  48. direction = -direction
  49. get_node("sprite").set_scale(Vector2(-direction, 1))
  50. elif (direction > 0 and not rc_right.is_colliding() and rc_left.is_colliding()):
  51. direction = -direction
  52. get_node("sprite").set_scale(Vector2(-direction, 1))
  53. lv.x = direction*WALK_SPEED
  54. if(anim != new_anim):
  55. anim = new_anim
  56. get_node("anim").play(anim)
  57. s.set_linear_velocity(lv)
  58. func _ready():
  59. rc_left = get_node("raycast_left")
  60. rc_right = get_node("raycast_right")