extends Node2D @export var speed = 400 # How fast the virus will move (pixels/sec). @export var attack_range = 200 # virus vision radius (pixels) # a virus is instantiated as Spawning and transitions (matures) into Spawned # after a timer timeout, after which it is activated and ready to do its thing. enum SpawningState {Spawning, Spawned} # when idle, a virus Waits. if it has already performed some action, it may # become Blocked for a while. otherwise it might be Running. like a process. enum ActionState {Blocked, Waiting, Running} var spawning_state var action_state var targets # who the virus will attack func _ready(): self.spawning_state = SpawningState.Spawning self.action_state = ActionState.Waiting func _process(delta): if self.spawning_state != SpawningState.Spawned: return # do nothing if not yet spawned if len(self.targets) == 0: return # do nothing if no set targets # --- determine nearest target var nearest_target = self.targets[0] var shortest_distance = 0 for i in range(1, len(self.targets)): var target = self.targets[i] var distance = self.position.distance_to(target) if distance < shortest_distance: shortest_distance = distance nearest_target = target # --- attack the nearest target, if within range if self.action_state != ActionState.Blocked: if shortest_distance < self.attack_range: self.attack(nearest_target) # --- move towards nearest target self.position += \ self.position.direction_to(nearest_target) * self.speed * delta func attack(target): # do attack, block until timer self.action_state = ActionState.Blocked func _on_mature(): spawning_state = SpawningState.Spawned