Hey everyone, I am getting back into game dev as a way to exercise my programming muscles because all the work I do for pay is easy and kind of boring.
I want to make a simple RTS with only a handful of units, a builder, tower, and resource building. I am struggling to find a good way to set up my systems. For example let’s say I want to have a “movement” component that all mobile units have, or a “resource container” component that lets units or structures hold and transfer resources between one another.
I am actually revisiting this project after like a year so maybe I should spend some time playing around in the project so I can ask my question better. Right now my question is just “why is this bad and what can I do that is better?”. But here is some example code I had that felt very spaghetti.
I have a handful of Behavior classes which take in the agent doing the action, plus some other variables. Here is the poorly-named “seek” behavior:
class_name SeekBehavior
extends Behavior
var path: Array[Vector3]
var position: Vector3
func _init(agent: Node3D, position: Vector3) -> void:
self.agent = agent
self.position = position
self.path = Pathfinding.generate_path(agent.position, position)
func apply(delta: float) -> void:
if path.size() == 0:
self.is_finished = true
return
var waypoint = path[0];
waypoint.y = agent.position.y
if agent.position.distance_to(waypoint) > 0.01:
agent.rc.consume_power()
agent.position += agent.position.direction_to(waypoint).normalized() * agent.speed * Resources.efficiency
else:
path.pop_front()
agent.look_at(waypoint, Vector3.UP)
Each unit has a queue of behaviors that it will do until each one is finished. In practice it feels very hacky though because rather than some central system telling each unit what to do, each unit has to keep track of its own internal queue of behavior objects, each one has some opaque state.
Anyways, sorry this question is not formatted better, I should have a better mental model of what I actually want later in the week.