JakeLin / SaveTheDot

A game developed using UIViewPropertyAnimator
MIT License
804 stars 84 forks source link

The game will be slow #3

Open hemnf opened 7 years ago

hemnf commented 7 years ago

Hi, Thank you for your code and I really like it. I have one issue, when the game reached more than 3 minutes, it will be very slow and the user can not move the dot. Thanks

JakeLin commented 7 years ago

haha, this is a hidden feature since you are the best player in the world 😁 Which devices are you using? will it always happen after 3 minutes?

hemnf commented 7 years ago

Dear JakeLin, Thank you for your reply, I am using iPhone, 5, 6, and 6s. Always when you reach 120 second you will face this. When I checked the Time profiling I found the 95% of CPU usage will come from this function: func checkCollision() { enemyViews.forEach { guard let playerFrame = playerView.layer.presentation()?.frame, let enemyFrame = $0.layer.presentation()?.frame, playerFrame.intersects(enemyFrame) else { return } gameOver() } }

especially with this line
let enemyFrame = $0.layer.presentation()?.frame,

I think it is not about hidden feature, if you reach more than 150 seconds the CPU usage will be the highest and the game will be jammed.

Kind regards

JakeLin commented 7 years ago

@hemnf it was a joke 😅, we call checkCollision too many times in the run loop, we can optimise that. Could you like to give it a go?

imbaggaarm commented 6 years ago

I think we need a maximum number of enemies, ex: 60.

VNystad commented 5 years ago

After a quick scan on your code, it seems that you iterate over all enemies to check if there is a collision. A possible workaround would be to save all positions of every enemy in a 2D list and update it on each tick with new positions. Then only checking the current position of the player in the list for collision, sudo code: if (enemyPositionMap[playerPos.y, playerPos.x] != 0) gameover

Not sure if this is doable but if it is, this solution will cost a little memory tho for the fixed list size, but could save some cpu usage: [ ][ ][ ][e][ ][ ][ ][ ] [ ][ ][ ][ ][ ][e][ ][ ] [ ][ ][ ][P][e][ ][ ][ ] [ ][e][ ][e][ ][ ][ ][ ] [ ][ ][ ][e][ ][e][ ][ ]