This is a very minor update from a gameplay perspective. I did a lot of work today on optimizing some of the gnarlier parts of the game, specifically the way I handle the toroidal world. Previously, I had 9 full copies of each asteroid arranged in a 3x3 grid with one screen separation. As the middle asteroid left the screen, another one would automatically enter the screen on the opposite side.
This works, and gives me the full wrap-around physics, visuals, and particle effects that I wanted. But holy hell is it expensive from a computation standpoint. For a standard asteroid, this meant:
9 Sprite2Ds for the asteroid itself
9 AnimatedSprite2Ds for the explosion
9 Area2Ds with a corresponding 9 CollisionShape2Ds
27 GPUParticles2D for explosion effects
9 AudioStreamPlayer2Ds for the explosion sound (replicated because the whole explosion scene is replicated, even though only one is actually used)
9 Sprite2Ds for the revenge reticule
For a total of 70 nodes per asteroid. And later stages can have 100+ asteroids on them, so that's 700+ nodes. And while I have an asteroid pre-cache system, even adding that many asteroids to a scene at once (if, say, one were to hit a bunch of large and medium asteroids with the Faraday Gun) brings the engine to its knees just from the scene tree operations.
The solution is threefold. First, if I can have one Sprite2D draw itself 9 times, then I don't actually need 9 Sprite2Ds. Same with the AnimatedSprite2Ds. That's 16 fewer nodes per asteroid right there. I can collapse all of the CollisionShape2Ds under a single Area2D to save another 8 nodes. I haven't done it yet, but I believe I can write a custom particle shader to emulate the grid so a single particle emitter can emit particles from each virtual screen.
Second, the asteroids don't need to be removed from the scene once they're destroyed. They can just be hidden, moved off-screen, and have their collision and processing turned off. SceneTree operations in Godot are expensive (and are not thread-safe for the main tree), so minimizing these is a key optimization.
Third, it's not necessary to play an explosion sound for every asteroid that explodes on a frame. Just the first five or so will suffice, and any additional just get compressed out anyway so the player can't tell the difference. I already have a maximum particle system mechanism to limit the number of particle systems turned on by explosions, so I can easily do the same thing with sounds.
For this update, I wrote a WraparoundSprite2D and WraparoundAnimatedSprite2D class that is a drop-in replacement for the Sprite2D and AnimatedSprite2D classes respectively. All it does extra is implement a custom _Draw() method to draw the other 8 sprites as copies of the main one. I've replaced the ship, all the asteroids, the explosion, and the lightning from the Faraday Gun with these new classes, and performance on the Steam Deck is noticeably improved.
Particle effects (ship thruster, explosions) currently only play on the main screen, so that's probably going to be tomorrow's project.
