- Home /
TopDown Mouse Click Handler
Hello Everybody, I'm currently developing a 2D tile-based strategy game and i'm having bad troubles trying to implement some code to be able to handle different actions based on which mouse button is pressed and what "entity" is being pressed.
To be more specific this is how i want it to work:
mouseButton(0) -> perform a "selection" (be it "select friendly character" / "select enemy char" / "select chest" / "select trap on the ground" )
mouseButton(1) -> perform an "action" (be it "movement" / "heal nearby friendly char" / "attack enemy" / "open chest" / "open door" / "defuse trap" )
Entities are:
Friendly Character
Enemy Character
Ground Tile
DoorTile
ChestTile
TrapTile
Practical example: to "select a friendly character" -> "move it" -> "attack enemy if in range" i would press button(0) on a friendly character -> then press button(1) on a ground tile -> then press button(1) on an enemy character to attack it
I know i could write a kilometer long switch-case to handle all of this but it would not be elegant, and more importantly, if in the future i want to add new actions it would be a mess to do so. i read online i could implement in some way a FiniteStateMachine that handles everything I'm quite familiar with the theory about FSMs but i dont really know how to implement one. Here is how i would like to make my FSM
From any state, if you click mouseButton(0) on an enity -> select that entity selection shows on a GUI-Panel the stats of what is selected if a player is selected it is also shown his reacheable tiles, attack range, etc..
Only Player-Controlled-Characters (allied) can perform "actions" if the target is in range These actions are (for now): Move to a particular tile / Attack an enemy / If an Healer is selected he is able to Heal Allies / If a Rogue is selected, he is able to Defuse Traps / open closed Doors or Chests
A nice final touch would be to check whether the currently selected character has finished his actions for this round and automatically select the next allied character that still has some actions available.
What i am asking for is some hindsights on how to implement that (or similar) FSM every time i try to do so i just return to write endless switch-cases and i know that is not the right way to do so.
Sorry for my bad english, Thank you for your time.
Your answer
Follow this Question
Related Questions
How can I detect the mouse is moving through code? 3 Answers
Is there a built-in function to determine if any GUI item has been clicked? 2 Answers
Can you access non-standard mouse buttons? 2 Answers
How to disable mouse click detection in game when clicking exit menu button? 1 Answer
does getmousebuttondown counts as physicaly clicking (for mobile games)? 1 Answer