My handling of mouse input has a scaring tendency to get messy and devolve in to a large mess of scattered spaghetti in assorted scripts and whenever a new case pops up it becomes more and more difficult to deal with. It’s fine most of the time when I do what amounts to prototyping or testing out small specific concepts but not when I want to tie things together in to resembling games.
I have a rough idea on how to proceed but looking for examples or designs to imitate as to avoid reinventing the wheel but I can’t seem to figure out a good search query netting the results I’m looking for. I only find the api references or questions that are on a too low level. Ie Input.GetMouseButtonDown(1) or how to cast a ray from mousepos and similar.
But what I’m looking for is how do I deal with multiple objects on the screen which say are selectable but then have different behaviours? Some might activate a UI element for upgrade alternatives. Some might be for RTS styled unit selection etc.
My current idea is to base things of a simple Interface with say LeftClick(); RightClick(). Then have all mouse inputs go through a Manager or Handler class for mouse input. The mouse input would then use a ray as normal to see if there is something you try to select and if it hits an object that has a script that implements either of these methods it would then call these functions and they would deal with what happens next.
I would also implement a callback so that it’s perhaps up to the current “holder” of input to say that it’s time for me to be released depending on its logic.
I think I’ll be able to come up with a design that mostly suits my needs but would love to get some input on how others avoid having this specific part devolve in to a whole bunch of “if/else” checking as well as if someone knows of any good examples, resources, blogposts discussing this subject.