Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The game will be slow #3

Open
hemnf opened this issue Feb 6, 2017 · 5 comments
Open

The game will be slow #3

hemnf opened this issue Feb 6, 2017 · 5 comments

Comments

@hemnf
Copy link

hemnf commented Feb 6, 2017

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
Copy link
Owner

JakeLin commented Feb 6, 2017

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
Copy link
Author

hemnf commented Feb 6, 2017

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
Copy link
Owner

JakeLin commented Feb 7, 2017

@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
Copy link

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

@VNystad
Copy link

VNystad commented Dec 9, 2018

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][ ][ ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants