Enemy.gd 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. extends KinematicBody2D
  2. export var vida = 2
  3. export var speed = 70
  4. export var type = 1
  5. var enemy_id = 0
  6. var bullet = preload('res://bullet.tscn')
  7. func damage(arg, id):
  8. vida += arg
  9. get_tree().get_nodes_in_group('server')[0]._enemy_dmg(enemy_id)
  10. if vida <= 0:
  11. get_tree().get_nodes_in_group('enemy_authority')[0].death_count(id)
  12. queue_free()
  13. var motion = Vector2()
  14. var target
  15. onready var reaction_time = int(rand_range(20,60))
  16. var dir_buffer = 0
  17. var tick = 20
  18. export var tickrate = 4
  19. var state = 'runaround'
  20. var fliph = false
  21. func _physics_process(delta):
  22. tick += 1
  23. if fmod(tick,tickrate) == 0:
  24. match state:
  25. 'runaround':
  26. type = 0 if motion.x == 0 else 1
  27. fliph = motion.x > 0 and motion.x != 0
  28. if target == null: _ready()
  29. if target == null: return
  30. dir_buffer += 1
  31. if fmod(dir_buffer,reaction_time) == 0:
  32. if is_instance_valid(target): motion.x = (target.global_position - global_position).normalized().x * speed
  33. motion.y += 15
  34. if motion.y > 0 and is_on_floor(): motion.y = 0
  35. move_and_slide(motion*tickrate,Vector2.UP)
  36. 'shooting':
  37. type = 2
  38. if fmod(tick,75) == 0:
  39. var instance = bullet.instance()
  40. instance.motion.x = 200 if fliph else -200
  41. instance.global_position = global_position + Vector2(0,-8)
  42. instance.id = name
  43. instance.dmg = 10
  44. instance.target = 'player'
  45. get_tree().get_nodes_in_group('proj')[0].add_child(instance)
  46. func _on_Area2D_body_entered(body):
  47. print(body)
  48. if body.is_in_group('players'):
  49. var array = get_tree().get_nodes_in_group('players')
  50. array.shuffle()
  51. if array.size() > 0:
  52. target = array[0]
  53. pass # Replace with function body.