add: basic virus implementation

This commit is contained in:
2025-10-04 15:32:50 +02:00
parent f4e4fe23ee
commit 338137be09
2 changed files with 57 additions and 11 deletions

View File

@@ -1,16 +1,52 @@
extends Node2D extends Node2D
@export var speed = 400 # How fast the player will move (pixels/sec). @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(): func _ready():
# Initalize with random abilities from available abilities self.spawning_state = SpawningState.Spawning
self.action_state = ActionState.Waiting
func _process(delta): func _process(delta):
# Grow in size if self.spawning_state != SpawningState.Spawned:
return # do nothing if not yet spawned
# RayCast towards player (every N frames), moving towards it if close enough if len(self.targets) == 0:
# Or should we always move towards the player? return # do nothing if no set targets
# Use abilities if: # --- determine nearest target
## Raycast hit var nearest_target = self.targets[0]
## Regular intervals (if close enough to player)? 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

View File

@@ -1,6 +1,9 @@
[gd_scene format=3 uid="uid://bsv3h2lpv7h77"] [gd_scene load_steps=2 format=3 uid="uid://bsv3h2lpv7h77"]
[ext_resource type="Script" uid="uid://bwhxwf77egesx" path="res://virus.gd" id="1_5nuwd"]
[node name="Virus" type="Node2D"] [node name="Virus" type="Node2D"]
script = ExtResource("1_5nuwd")
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
@@ -9,3 +12,10 @@
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] [node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
[node name="RayCast2D" type="RayCast2D" parent="."] [node name="RayCast2D" type="RayCast2D" parent="."]
[node name="SpawningTime" type="Timer" parent="."]
editor_description = "How long to mature from Spawning to Spawned"
one_shot = true
autostart = true
[connection signal="timeout" from="SpawningTime" to="." method="_on_mature"]