In the continuing efforts to make a fun game that tests my (very limited and basic) skills as a C++ programmer, as well as to further the bounds of SFML, I am expanding the Water Drum Game. You should check out my latest commit to see how I put some wobble into the water drum bobble!

wiggle_drum

The overall code is fairly simple, but comes in two main parts. Here is some relevant code from the WaterDrum.cpp:

void WaterDrum::update(bool stillActive, float elapsedTime, float sResolution) {

if (stillActive) {

m_Motion = m_Motion + elapsedTime;
float motionCount = m_Motion;

// Update the sprite if struck.
if (motionCount == 1.75) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_still.png”));
} else if (motionCount <= .25) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_1.png”));
} else if (motionCount <= .5) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_2.png”));
} else if (motionCount <= .75) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_3.png”));
} else if (motionCount <= 1) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_4.png”));
} else if (motionCount <= 1.25) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_5.png”));
} else if (motionCount <= 1.5) {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_6.png”));
} else {
m_Sprite = Sprite(TextureHolder::GetTexture(
“graphics/waterdrum_still.png”));
}
} else {
m_Sprite.setTexture(TextureHolder::GetTexture(
“graphics/waterdrum_still.png”));

}

if (m_Motion >= 1.75)
{
m_Motion = 0;
m_Active = false;
}

// Apperantly we have to update the scale when we change the sprite.
m_Sprite.setScale((sResolution*.2)/500,(sResolution*.2)/500);
m_Sprite.setPosition(m_Position);

 

}

And here is the part where we call it in the DrumGame.cpp:

for (int i = 0; i < numDrums; i++) {
waterDrums[i].update(waterDrums[i].isActive(), dtAsSeconds, resolution.x);
}

As long as the window is open, on every pass of code, it will run this for loop to check and update every water drum. It passes three variables:

  1. isActive: This was set to true for each drum when it was “struck” or clicked on. After it is done “bobbling” it will be set to false.
  2. dtAsSeconds: This is a value of a fraction of a second that it took to run the code between frames. This is really important for making a realistic “flowing” graphic.
  3. resolution.x: This value is used to send the screen size x variable, so calculations can be made for scaling the art of each sequence of drum.

Once those variables are received, there are two options for the code. If it is active, then it will calculate the “bobble” by adding the time elapsed together. At designated times (which I could arbitrarily set to anything I want) the different phases of the bobble happen by replacing the graphic with another. I have decided that after being struck the water drum should bobble for 1.75 seconds total. During that time, 6 different art frames are used for the drum to create the bobble.

Note: I could have changed this to be movement of position as well, having a separate graphic for the outer and inner bowls of the drum. It could have been random as well as including different scaling to adjust the size of it. I felt this was easier, since our textureHolder class is keeping the drums in memory, we only have to load them once, and then “swap” them into these useful spots as required, but either would have worked, I think.

Once the time is up, the game sets the visual back to the still view and sets the motion time back to zero, as well as the active flag back to false. Now it is ready to start the process over.

Notice that the code for striking the drum starts this process over, not adding time in an accumulative fashion. This prevents a “forever” bobble of 15 seconds if you were to strike the drum 10 times in two seconds. It only bobbles from the last strike, so it will only bobble a maximum of 1.75 seconds after the last strike.

Either way, it is now semi-playable in a free play mode, so you are free to check it out on GitLab!

Linux – keep it simple.

Leave a Reply

Your email address will not be published. Required fields are marked *