enemy.tres.gd 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. match state:
  23. 'runaround':
  24. type = 0 if motion.x == 0 else 1
  25. fliph = motion.x > 0 and motion.x != 0
  26. if target == null: _ready()
  27. if target == null: return
  28. dir_buffer += 1
  29. if fmod(dir_buffer,reaction_time) == 0:
  30. if is_instance_valid(target): motion.x = (target.global_position - global_position).normalized().x * speed
  31. motion.y += 15
  32. if motion.y > 0 and is_on_floor(): motion.y = 0
  33. move_and_slide(motion*tickrate,Vector2.UP)
  34. 'shooting':
  35. type = 2
  36. tick += 1
  37. if fmod(tick,75) == 0:
  38. var instance = bullet.instance()
  39. instance.motion.x = 200 if fliph else -200
  40. instance.global_position = global_position + Vector2(0,-8)
  41. instance.id = name
  42. instance.dmg = 10
  43. instance.target = 'player'
  44. get_tree().get_nodes_in_group('proj')[0].add_child(instance)
  45. func _on_Area2D_body_entered(body):
  46. # print(body)
  47. if body.is_in_group('players'):
  48. var array = get_tree().get_nodes_in_group('players')
  49. array.shuffle()
  50. if array.size() > 0:
  51. target = array[0]
  52. pass # Replace with function body.