enemy.gd 1.8 KB

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