I'm starting work on a fairly simple RTS, more so to advance my knowledge in Love and less so to make an amazing game. My question is- what is the best way to determine what a user has clicked on? My game is on a grid, so that helps simplify this problem. I've got one approaches which I feel might work;
1) Store every active entity (anything the player can interact with) in an X by Y matrix. When the player clicks, determine what tile was clicked. Check the matrix to see if theres any object in that location. Object move fluidly between tiles, which poses a small problem here.
2) I think I'm going to have another list that contains any entities that are in motion. The game will just have to check that entire list if it doesn't find anything in the first matrix.
3) This then leaves the environmental objects to be checked. Luckily, this one won't change much, as depleting a resource takes some time.
Overall, I think this should be a good strategy. Any thoughts on other ways to optimize this?
Best way to check what a user has clicked
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Party member
- Posts: 235
- Joined: Sat Dec 15, 2012 6:54 am
Re: Best way to check what a user has clicked
Your method seems like it would work, but, without making it much more complicated, won't it restrict your game to 1 entity per tile? This seems like it would be pretty bad for a standard RTS game where you potentially have several units trying to move around stationary units. Also, snapping mouse clicks to a grid may end up coming across as inaccurate (and even frustrating) to the player, especially since one wouldn't typically expect this in an RTS.
I think it'd be better to just store all of your active clickable units in a table, and then run through that table each time the mouse is clicked (just do a basic "is point in rectangle" test to see if the mouse is over a particular entity). If you have the possibility of multiple units over top of each other (e.g. if they're air units), then you could just add another layer where, while doing the first check, you add each entity which is being clicked to a second "clicked" table. You'd then sort that table by something like "z depth," and then have it set the first entry as the clicked/selected one.
This would be a pretty easy and simple way to do it, though it may cause some slowdown if you will have a lot of clickable units at once. You may be able to speed it up by only checking units which are currently in the "camera view" (assuming your game will have a scrolling camera/viewpoint)... In most cases though, I don't think the minor speed loss will be too huge of an issue. If you look at something like my Raycaster example ( not spamming at all , but if you're curious: viewtopic.php?f=5&t=12069 ), that runs through a table that would probably be much larger than one for an RTS. If your resolution is 1024x768, my raycaster sorts a table of 1024 entries + any "things" (e.g. enemies, pick-ups, etc...), creates a quad for each of those, textures the quad, draws the quad, and performs any other processing each frame (on top of all the raycasting stuff). In comparison, your average RTS will have 200 max clickable units at once (if that - a Starcraft game typically had less), not including buildings of course. And, this "is clicked?" loop would only happen when you click your mouse.
All that being said: there may be a better way to do this, so please don't take my word as an end-all, be-all. I am fairly new to Lua and LOVE
I think it'd be better to just store all of your active clickable units in a table, and then run through that table each time the mouse is clicked (just do a basic "is point in rectangle" test to see if the mouse is over a particular entity). If you have the possibility of multiple units over top of each other (e.g. if they're air units), then you could just add another layer where, while doing the first check, you add each entity which is being clicked to a second "clicked" table. You'd then sort that table by something like "z depth," and then have it set the first entry as the clicked/selected one.
This would be a pretty easy and simple way to do it, though it may cause some slowdown if you will have a lot of clickable units at once. You may be able to speed it up by only checking units which are currently in the "camera view" (assuming your game will have a scrolling camera/viewpoint)... In most cases though, I don't think the minor speed loss will be too huge of an issue. If you look at something like my Raycaster example ( not spamming at all , but if you're curious: viewtopic.php?f=5&t=12069 ), that runs through a table that would probably be much larger than one for an RTS. If your resolution is 1024x768, my raycaster sorts a table of 1024 entries + any "things" (e.g. enemies, pick-ups, etc...), creates a quad for each of those, textures the quad, draws the quad, and performs any other processing each frame (on top of all the raycasting stuff). In comparison, your average RTS will have 200 max clickable units at once (if that - a Starcraft game typically had less), not including buildings of course. And, this "is clicked?" loop would only happen when you click your mouse.
All that being said: there may be a better way to do this, so please don't take my word as an end-all, be-all. I am fairly new to Lua and LOVE
Who is online
Users browsing this forum: Google [Bot] and 4 guests