When I started working on this game, I took some old path movement code and adjusted it to work with the game. I considered it a quick(ish) implementation and hoped it would be fine. The code took a path and and every update it would pass in the time that had past and it would calculate where the object needed to be. This created a consistent linear movement which is what I wanted.
Unfortunately the code had two main problems. The enemies are only single file and the movement didn’t feel as smooth as I wanted; especially when it was running at 4x speed. Now that I am in a polishing mind set, for the past few days I started reworking this code.
My first though was to see what options SpriteKit and SKActions had for me, and to my surprised it has a follow path action. This action work much better then what I had created. The main difficulty that I have, is it has no dynamic speed control. Well, that is not completely true, there are methods for controlling speed and duration of an action, but getting this to work for just one action yet alone for the many that I will have in the future made controlling and calculating the speed for actions far to difficult for me.
I felt like I was doing thing that hardest way possible, so I started to review documentation and blog posts to see if there was better way. I discover that there is a speed property on SKNode. This property will affect the speed on itself and all its children node and their corresponding SKActions. Meaning I could just set the scene (SKScene is a child of SKNode) speed to 2.0 and all animation and actions will move at that speed. I wished I had notice this properties earlier, but I guess I just need to learn to read the manual more closely.
I wish that, that was the end of this tale, but there is still a problem. If I slow down the game, the enemies will spawn really close together, if I speed up the game they will be further apart. The enemies still all move at the same speed it is just a problem when they spawn. I think I know what the problem is though.
When I spawn an enemy I am using the time I get from SKScene.update method which is in system time. In my code I calculate the different in system time from the last update and I pass that delta down to the update method of my game objects. My thinking is that I need to create what I am calling simulation time. Simulation time is the time that the game thinks has past, not the what the wall clock says.
My thinking with simulated time is it will add a fix duration for each frame. This duration will be modified by the current select game speed. Then when I need to do a time base calculation I can know that simulation time will match what the user is seeing on the screen and everything will be nice and smooth.
I have a bit more research to do, because I don’t think this idea of simulated time is anything new. Hopefully I am not to far off on a workable solution.