I am currently working on a simple SDL project where I need enemies to move in a path before finally settling in their waiting area. Similar to how the enemies in Galaga fly across the screen and then move to their starting positions.
I was thinking about how I could implement this and the main solution I came up with was I could keep time of when I first start moving an enemy. And then after a certain amount of time passes I can have the enemy move in a circle, and then a certain amount of time passes and I can have the enemy move to their final position.
This solution seems very tedious to me and I feel there must be a better way. If anybody has had to do something similar to this before maybe point me in the right direction on some cleaner ways to implement this functionality through a game loop.
Instead of defining this in terms of time, I’d define it in terms of data.
Create definitions in code for basic pathing actions. Fly in a circle, move from point A to point B, patrol back and forth between A and B, etc. Then create an enemy moveset that invokes these actions. For example:
"pointA": [10, 10],
"pointB": [20, 10],
"center": [15, 15],
"point": [25, 25]
Set up something along those lines, and code to interpret and execute it. It offers several advantages, not least the ability to debug more easily without time spent in the debugger throwing your timing off.
Thanks for your response. A couple of follow up questions. I have never done something like this before, when you say “create definitions in code” what would that mean if I am working in c++.
Like defining some sort of static structure that stores this information, and then pointing to it in my enemy class? or should I create these definitions using classes and inheritance somehow? Thanks again!
I like that too much!
Where did you see techniques like that?
May you give me some books names about the subject?
I don’t have any books, just a few decades of experience working with modding and then building games. NPC AI coding is done basically like a higher-level version of implementing a programming language: you define low-level primitive operations, and then you compose them together to build more complex operations.
You want stuff to read, look up some articles on Behavior Trees. It’s a very interesting technique where you can build these primitive AI operations into complex interactions with the world in a surprisingly easy and robust manner.