I've looked over the rdoc, but I don't see anything that would make using mouse input practical. Is there an easy or clever way to do mouse driven input in Gosu? Like being able to tell if a particular image was clicked or hovered over? I was thinking of maybe having an list of items to check for mouseover and then checking if the mouse is in any of their bounds each time it's clicked, but that seems kind of ugly, especially if any of the things could overlap. It could be doable if the list is somewhat small (menus maybe?) or if mouse x and y are just used to deduce the tile being clicked in a grid based game, but anything else I think might get hairy.
Images have no position, they are only a (part of a) texture in memory. You are right that you'll have to loop all over an array of things that can be clicked, which you'll have to maintain yourself. I don't think it's particularly hairy in Ruby :)
The one thing that can be tricky is to have pixel-perfect collision detection when clicking on things, but bounding boxes are good enough for most uses. When they're not, you might have to keep a copy of the
Image#to_blob result around and use that to do hit-testing.
Unfortunatelly there is no simple method of using a mouse. Though I made a small Mouse class for my game to make it somewhat simpler. Main part of it is these two methods:
if @x >= x and @x <= x+w and @y >= y and @y <= y+h then
dist = Math.sqrt((x - @x) ** 2 + (y - @y) ** 2)
if dist <= r then
As I keep mouse in global variable $mouse, whenever I must check if mouse is over a particular part of menu, I just use code:
do_something if $mouse.over?(x, y, width, height)
Works the way it should most of the times.
That's pretty nice, especially the over_circle.
Nitpick: This is very unlikely to ever be a performance problem, but you can get rid of the
Math.sqrt call and just compare
dist <= r*r instead :)
And you can directly return the result of comparisons without an
@x >= x and @x <= x+w and @y >= y and @y <= y+h
As for the second one, I know that (well, didn't know that I can skip
return), but sometimes I prefer to have things a bit clearer. Old habits die hard :p
Isn't this math thing the same as
Just checked, and it actually is. I completely forgot about that method!
Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill