Concurrent player actions in a c++ game

I'm quite a beginner in C++ asking for advice from other C++ people! As a way to practice coding, I have been working on a simple c++ game that involves a player that the user can control and monster(s) on screen that the player can attack.

Here's a simplified interface of the player class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Player{
    //update the player according to its current state then change its state
    //according to the value passed to the function
    void update(PlayerControlAction action = PLAYER_ACTION_NONE);
    //draw the player onto a "ScreenLayer" that will be drawn on the screen
    void draw(ScreenLayer *player_layer);

    Vector position_;
    Vector size_;
    Direction direction_;
    int level_;
    int hp_;
    int max_hp_;
    int exp_;
    int max_exp_;
    int strength_;
    int speed_;
    
    PlayerAttackInfo player_attack_info_;
    PlayerStateInfo player_state_info_;
    Sprite* sprite_;
};


My current problem is figuring out a practical way to make the player able to persist certain actions while the user can freely control the player. That can be something like continuing a player attack animation while the user controls the player to move.

The update() function works like this pseudocode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Player::update(PlayerControlAction action){
    //first update the player based on its current state
    switch(player_state_info_.player_state_){
    case PLAYER_STATE_STANDING:
        //do nothing
        break;
    case PLAYER_STATE_MOVING:
        //change the player's position
        
        //the player will stop moving unless the action is to keep moving
        player_state_info_.player_state_ = PLAYER_STATE_STANDING;
        break;
    //etc...
    }

    //then change the state based on the action
    switch(action){
    case PLAYER_ACTION_NONE:
        //do nothing
        break;
    case PLAYER_ACTION_ATTACK:
        //attack whatever is in front of the player
        break;
    //etc...
    }
}


As shown, or at least I tried to show concisely, is that the player behaves based on its assigned state variable. The draw() function works similarly with the switch statement working according to the player state. However, I am trying to figure out a way to make the draw function work more independently from the update function since they both depend on the player state variable.

I am a little hesitant to start adding member variables to the player class that indicate which specific actions are in progress and when they are finished since more variables might have to be added when some new action is defined. Is there a better solution for checking what tasks/actions should be updated?
Is the main question here how to decouple updates and draws?

If you do that, how do you want to handle a draw when the update is partially complete?


as to your last question, I advise a container of some sort (maybe a queue or priority queue) in the class to handle actions, and let actions be its own class, with an in-progress/done boolean and whatever else is needed?? You need a way to empty/clear/reset this queue of course.
Last edited on
Topic archived. No new replies allowed.