Skip to content

Commit

Permalink
add a flip-action option for sprite actions
Browse files Browse the repository at this point in the history
replace ispy's UP actions with flip-action of DOWN
  • Loading branch information
Narre committed Jul 23, 2023
1 parent 47f349f commit 62be76b
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 59 deletions.
Binary file removed data/images/objects/ispy/i-hiding-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-idle-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-shaking-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-shaking2-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-shaking3-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-showing1-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-showing2-up.png
Binary file not shown.
Binary file removed data/images/objects/ispy/i-showing3-up.png
Binary file not shown.
49 changes: 4 additions & 45 deletions data/images/objects/ispy/ispy.sprite
Original file line number Diff line number Diff line change
Expand Up @@ -144,63 +144,22 @@
(action
(name "idle-up")
(hitbox 0 0 34 28)
(fps 14)
(images
"i-idle-up.png"
)
(flip-action "idle-down")
)
(action
(name "alert-up")
(hitbox 0 0 34 28)
(fps 14)
(images
"i-idle-up.png"
"i-idle-up.png"
"i-idle-up.png"
"i-idle-up.png"
"i-shaking-up.png"
"i-shaking2-up.png"
"i-shaking-up.png"
"i-shaking2-up.png"
"i-shaking-up.png"
"i-shaking2-up.png"
"i-shaking-up.png"
"i-shaking2-up.png"
"i-shaking3-up.png"
"i-showing2-up.png"
"i-showing1-up.png"
"i-hiding-up.png"
)
(flip-action "alert-down")
)
(action
(name "hiding-up")
(hitbox 0 0 34 28)
(fps 14)
(images
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
"i-hiding-up.png"
)
(flip-action "hiding-down")
)
(action
(name "showing-up")
(hitbox 0 0 34 28)
(fps 14)
(images
"i-hiding-up.png"
"i-showing1-up.png"
"i-showing2-up.png"
"i-showing3-up.png"
"i-idle-up.png"
"i-idle-up.png"
)
(flip-action "showing-down")
)
)

91 changes: 77 additions & 14 deletions src/sprite/sprite_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ SpriteData::SpriteData(const ReaderMapping& mapping) :
name()
{
auto iter = mapping.get_iter();
while (iter.next()) {
while (iter.next())
{
if (iter.get_key() == "name") {
iter.get(name);
} else if (iter.get_key() == "action") {
Expand Down Expand Up @@ -102,14 +103,15 @@ SpriteData::parse_action(const ReaderMapping& mapping)
{
auto action = std::make_unique<Action>();

if (!mapping.get("name", action->name)) {
if (!mapping.get("name", action->name))
{
if (!actions.empty())
throw std::runtime_error(
"If there are more than one action, they need names!");
throw std::runtime_error("If there are more than one action, they need names!");
}

std::vector<float> hitbox;
if (mapping.get("hitbox", hitbox)) {
if (mapping.get("hitbox", hitbox))
{
switch (hitbox.size())
{
case 4:
Expand Down Expand Up @@ -145,18 +147,24 @@ SpriteData::parse_action(const ReaderMapping& mapping)
}

std::string mirror_action;
std::string flip_action;
std::string clone_action;
if (mapping.get("mirror-action", mirror_action)) {
if (mapping.get("mirror-action", mirror_action))
{
const auto act_tmp = get_action(mirror_action);
if (act_tmp == nullptr) {
if (act_tmp == nullptr)
{
std::ostringstream msg;
msg << "Could not mirror action. Action not found: \"" << mirror_action << "\"\n"
<< "Mirror actions must be defined after the real one!";
throw std::runtime_error(msg.str());
} else {
}
else
{
float max_w = 0;
float max_h = 0;
for (const auto& surf : act_tmp->surfaces) {
for (const auto& surf : act_tmp->surfaces)
{
auto surface = surf->clone(HORIZONTAL_FLIP);
max_w = std::max(max_w, static_cast<float>(surface->get_width()));
max_h = std::max(max_h, static_cast<float>(surface->get_height()));
Expand All @@ -182,18 +190,70 @@ SpriteData::parse_action(const ReaderMapping& mapping)
action->fps = act_tmp->fps;
}

if (action->family_name == "::" + action->name)
{
action->family_name = act_tmp->family_name;
}
}
}
else if (mapping.get("flip-action", flip_action))
{
const auto act_tmp = get_action(flip_action);
if (act_tmp == nullptr)
{
std::ostringstream msg;
msg << "Could not flip action. Action not found: \"" << flip_action << "\"\n"
<< "Flip actions must be defined after the real one!";
throw std::runtime_error(msg.str());
}
else
{
float max_w = 0;
float max_h = 0;
for (const auto& surf : act_tmp->surfaces)
{
auto surface = surf->clone(VERTICAL_FLIP);
max_w = std::max(max_w, static_cast<float>(surface->get_width()));
max_h = std::max(max_h, static_cast<float>(surface->get_height()));
action->surfaces.push_back(surface);
}

if (action->hitbox_w < 1 && action->hitbox_h < 1)
{
action->hitbox_w = act_tmp->hitbox_w;
action->hitbox_h = act_tmp->hitbox_h;
action->x_offset = act_tmp->x_offset;
action->y_offset = act_tmp->y_offset;
}

if (!action->has_custom_loops && act_tmp->has_custom_loops)
{
action->has_custom_loops = act_tmp->has_custom_loops;
action->loops = act_tmp->loops;
}

if (action->fps == 0)
{
action->fps = act_tmp->fps;
}

if (action->family_name == "::" + action->name) {
action->family_name = act_tmp->family_name;
}
}
} else if (mapping.get("clone-action", clone_action)) {
}
else if (mapping.get("clone-action", clone_action))
{
const auto* act_tmp = get_action(clone_action);
if (act_tmp == nullptr) {
if (act_tmp == nullptr)
{
std::ostringstream msg;
msg << "Could not clone action. Action not found: \"" << clone_action << "\"\n"
<< "Clone actions must be defined after the real one!";
throw std::runtime_error(msg.str());
} else {
}
else
{
// copy everything except the name (Semphris: and the family name)
const std::string oldname = action->name;
const std::string oldfam = action->family_name;
Expand All @@ -205,14 +265,17 @@ SpriteData::parse_action(const ReaderMapping& mapping)
action->family_name = act_tmp->family_name;
}
}
} else { // Load images
}
else
{ // Load images
std::optional<ReaderCollection> surfaces_collection;
std::vector<std::string> images;
if (mapping.get("images", images))
{
float max_w = 0;
float max_h = 0;
for (const auto& image : images) {
for (const auto& image : images)
{
auto surface = Surface::from_file(FileSystem::join(mapping.get_doc().get_directory(), image));
max_w = std::max(max_w, static_cast<float>(surface->get_width()));
max_h = std::max(max_h, static_cast<float>(surface->get_height()));
Expand Down

0 comments on commit 62be76b

Please sign in to comment.