Reputation: 3512
I am developing a little/mid RTS game with SpriteKit. I wonder if my multithreaded approach is OK.
Generally speaking I have user controlled units and enemy units. Both units has a basic AI, enemies obviously has some more AI
Lets say if unit is standing still and enemy unit is approached to its attacking range I want a unit to Automagically attack the enemy. And ofcourse same for the enemy.
I choosed not to add a logic and distance measurements to the update method which is expensive.
I decided to make 2 threads/queues/pools whatever with own update method. 1 thread for enemies and 2 for units respectively.
Question: Is it ok/fine/bad/acceptable approach ? Do I get benefits from it or contrary?
Upvotes: 1
Views: 223
Reputation: 59536
You say
I choosed not to add a logic and distance measurements to the update method which is expensive.
So I assume you are write expensive computations involving distance measurements inside a closure you want to execution on a separate thread.
Time t0
A unit is near the enemy. You start an asynch thread with complex code to determine whether the unit should attack the enemy.
Time t1
Then (on the main thread) the user moves the unit far away from the enemy.
Time t2
Finally the thread started at t0
completes. The decision is to attack the enemy because at time t0
the unit was close to the enemy. But now the unit is no longer near the enemy
so you see a wrong behaviour on the screen.
If this is an acceptable behaviour for your game then I don't see further problems.
If your logic about whether a unit/enemy should attack an enemy/unity is exclusively based on the distance between the 2 objects you could use the physics engine provided by SpriteKit to check for collisions.
Perimeter
You can simply create a circular physics body without mass centered on each unit/enemy. Let's call this Perimeter
.
You also set the bit mask values so that you only receives notification when a unit perimeter and an enemy perimeter collided. So no notification when 2 unit or 2 enemy perimeters collides.
Now the physics engine will notifier you each time a unit is close enough an enemy. no need for multithreading and very easy code.
Upvotes: 1