I am stuck in between a decision.
I need to choose between code repetition or program performance.
Currently, I have 100 asteroids on screen and I am planning on having 5 different types of enemy ships (all capable of colliding into asteroids).
My question is should I write the collision code in the asteroid.lua file and place the enterFrame and collision listener on them (Meaning 100 Collision Listeners) or should I place it for each individual ship. (Meaning a block code would be repeated around 6 times throughout the project, but only a max of about 15 to 20 Collision Listeners).
Also, this is the code in question:
if event and event.other and event.phase == "began" then if event.other.id == "bullet" or event.other.type == "ship" then event.other.health = event.other.health - self.damage self.health = self.health - event.other.damage if self.health \<= 0 then explosion.new(self.parent, self.x, self.y, 1, 1, math.random(-10, 10), math.random(-10, 10)) display.remove(self) end end end
So, what should I do?
I am expecting to potentially add more asteroids than 100, maybe 250. I am thinking I’d rather repeat the code, than have 250 listeners all at once. But at the same time, they will only be triggered on collision…
What do you guys think?
PS: I am aware I would have to make modifications to the code if I go for the repetition option.