22 hours ago, Luhan M. said:
I tried to implement the command pattern explained in this book : http://gameprogrammingpatterns.com/command.html. I'm looking up for someone which could revise my code and see if I did implement it the right way, or even if I did deviate a lot from how it should be implemented.
Here's the code : https://github.com/Luhanmacedo/Command-Pattern
Questions:
-
I didn't understand if the base class (Command) could be modified to introduce methods as I did to facilitate implementation details in the derived classes.
-
What would be a good way to map the buttons according to the configuration the player wants to use, such as X for Jump and Y to shoot.
P.S. : If it's not possible to review the whole code, tips in some parts are welcome as well.
I was just looking quickly at the link, are you talking about just remapping 4 buttons on a game pad while still being able to operate the following actions (example):
A = Jump
B = Shoot
X = Potion
Y = Melee Attack
There are several ways to do this.
Assume you have an event manager that checks for those four buttons:
If A is Pressed -> aButton pressed is now True (will execute code outside of events)
If B is Pressed -> bButton pressed is now True (will execute code outside of events)
If X is Pressed -> xButton pressed is now True (will execute code outside of events)
If Y is Pressed -> yButton pressed is now True (will execute code outside of events)
And your action ids are:
Jump = 1
Shoot = 2
Potion = 3
Melee Attack = 4
You would simply map IDs to each Button.
So by default A = Jump which means A will return ID 1. If we set Shoot for A, A will return the ID 2. You can just make a object for Button, and make a function that sets the ID and gets the ID.
In our logic loop we would see if an action is pending.
if (aButton.isPressed()) {
player1.doAction(aButton.getId());
aButton.togglePressed(); // Just means aButton bool pressed = !pressed
}
In your player code for doAction() you would just reference the method based on the ID
void Player::doAction(int id) {
if (id == 1) {
jump();
}
else if (id == 2) {
shoot();
}
ect….
There are different approaches you can use to solve the same problem. I find a lot of books very "dry" and they don't explain in layman's terms well enough.
Is this what you were looking for?