walk animation

This commit is contained in:
ohsqueezy 2023-11-14 18:38:37 -05:00
parent 25fd91e67c
commit fd6ef890cf
8 changed files with 88 additions and 9 deletions

View File

@ -127,7 +127,12 @@
"min speed": -0.4115226,
"increment mod": 0.125,
"decrement mod": 0.1,
"animation frames": ["resource/cake-frames/cake1.png"]
"animation frames": [
"resource/cake/cake1.png",
"resource/cake/cake2.png",
"resource/cake/cake3.png",
"resource/cake/cake4.png"
]
},
{
"name": "BEEF CAKE",
@ -137,7 +142,12 @@
"min speed": -0.4115226,
"increment mod": 0.125,
"decrement mod": 0.1,
"animation frames": ["resource/beef/cake1.png"]
"animation frames": [
"resource/beef/cake1.png",
"resource/beef/cake2.png",
"resource/beef/cake3.png",
"resource/beef/cake4.png"
]
},
{
"name": "BUFFALO BEEF CAKE",
@ -147,7 +157,12 @@
"min speed": -0.3125,
"increment mod": 0.08,
"decrement mod": 0.125,
"animation frames": ["resource/buffalo/cake1.png"]
"animation frames": [
"resource/buffalo/cake1.png",
"resource/buffalo/cake2.png",
"resource/buffalo/cake3.png",
"resource/buffalo/cake4.png"
]
}
],
"hitbox": 0.7

2
lib/sb

@ -1 +1 @@
Subproject commit 110b876648f7e529caddde3475b1f5543897f6de
Subproject commit 0a42df9a0029164448d600ec8270268382c4a438

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 212 B

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

Before

Width:  |  Height:  |  Size: 220 B

After

Width:  |  Height:  |  Size: 220 B

View File

@ -184,7 +184,7 @@
{
"curve": [
[83.000000, 231.000000], [83.000000, 37.000000], [55.000000, 90.000000], [220.000000, 226.000000], [499.000000, -77.333333],
[78.000000, 231.000000], [83.000000, 37.000000], [55.000000, 90.000000], [220.000000, 226.000000], [499.000000, -77.333333],
[782.000000, 148.333333], [783.000000, 229.000000], [779.333333, 454.666667], [320.666667, 423.333333], [220.000000, 294.000000],
[82.666667, 358.333333], [68.333333, 478.666667], [83.000000, 275.000000]
],

View File

@ -1,7 +1,11 @@
#include "Character.hpp"
Character::Character(const Configuration& configuration, std::map<std::string, sb::audio::Chunk>& audio) :
configuration(configuration), audio(audio) {}
configuration(configuration), audio(audio)
{
walk.frame_length(0.1f);
walk.play();
}
void Character::profile(const std::string& name)
{
@ -10,7 +14,10 @@ void Character::profile(const std::string& name)
/* Reload the texture */
_sprite.clear_textures();
_sprite.texture(profile()["animation frames"][0].get<std::string>());
for (const std::string& path : profile().at("animation frames"))
{
_sprite.texture(path);
}
_sprite.scale(size);
}
@ -158,6 +165,50 @@ void Character::update(const Curve& curve, const sb::Timer& timer, bool muted)
}
}
/* Set the walk frame animation based on speed */
if (walk.update(timer.stamp()))
{
if (_resting)
{
_sprite.texture_index(0);
}
else
{
if (speed > 0.0f)
{
if (speed == max_speed)
{
walk.frame_length(0.1f);
}
else if (speed > max_speed / 2)
{
walk.frame_length(0.07f);
}
else
{
walk.frame_length(0.04f);
}
_sprite.texture_increment(1);
}
else
{
if (speed == min_speed)
{
walk.frame_length(0.1f);
}
else if (speed < max_speed / 2)
{
walk.frame_length(0.07f);
}
else
{
walk.frame_length(0.04f);
}
_sprite.texture_increment(-1);
}
}
}
/* Move along unwrapped curve vertices */
float distance_remaining = std::abs(speed), distance = 0.0f;
glm::vec3 next_point, step;
@ -204,10 +255,20 @@ void Character::update(const Curve& curve, const sb::Timer& timer, bool muted)
}
}
void Character::draw(const Curve& curve, GLuint transformation_uniform, const glm::mat4 view, const glm::mat4 projection, GLuint texture_flag_uniform)
void Character::draw(const Curve& curve, GLuint transformation_uniform, const glm::mat4& view, const glm::mat4& projection, GLuint texture_flag_uniform)
{
/* Position along the curve */
glm::vec2 translation = sb::wrap_point(glm::vec3{_box.center(), 0.0f}, {-curve.aspect, -1.0f, -1.0f}, {curve.aspect, 1.0f, 1.0f});
_sprite.translate(glm::vec3{translation, 0.0f});
/* Reflect if walking left */
if (curve[next_point_index].x < position.x)
{
glm::mat4 reflection = glm::mat4{1.0f};
reflection[0][0] = -1.0f;
_sprite.transform(reflection);
}
_sprite.draw(transformation_uniform, view, projection, texture_flag_uniform);
/* Draw hitbox if requested */

View File

@ -36,6 +36,9 @@ private:
int next_point_index {0};
Box _box {{0.0f, 0.0f}, 2.0f * this->size};
bool _resting = true;
/* Walking forward, backward, and not walking animation timer */
sb::Animation walk;
/*!
* A JSON object containing the fields: name, speed increment, speed decrement, max speed, increment mod, decrement mod, and animation frames.
@ -158,5 +161,5 @@ public:
* @param projection projection transformation matrix
* @param texture_flag_uniform uniform location in the shader program of the boolean that turns texture drawing on or off
*/
void draw(const Curve& curve, GLuint transformation_uniform, const glm::mat4 view, const glm::mat4 projection, GLuint texture_flag_uniform);
void draw(const Curve& curve, GLuint transformation_uniform, const glm::mat4& view, const glm::mat4& projection, GLuint texture_flag_uniform);
};