As a hobbyist I've never really learned about ECS, so this is a good opportunity to look into it a bit more.
There's no rudeness, but I appreciate the consideration. Keep writing like you normally do.P.S. I apologize for any kind of rude words if any --- it is just my reaction in this particular case and context.
states like "RUN_SAD", "RUN_HAPPY", etc.
OR
a std::map<std::pair<EState, EMood>, std::vector<animation>>.
I think all of those are essentially the same in terms of "information entropy". That is, "RUN_SAD" is no different than [run][sad], where you're using an underscore to establish the hierarchy in one or using nested tables in the other. You have to use 'something', so any of those is good.Another idea is to have a property string inside the animation component which is a general key inside its animation pool table to get the right animation:
(code)
One thing though is that there's a dependency between those keys: in your design the mood key 'must' come after the action key, and all animations 'must' have a mood, direction and action.
So maybe something like a tag system (not like ECS tag, but like an orderless categorization) to identify animations could be an alternative, because then you don't have to worry about the ordering of the keys or the presence / lack of them.
So searching for the animation under (sad, run) or (run, sad) would yield the same data.
Code: Select all
-- Animation tag order map.
animation_tags = {
-- Action tags.
walk = 1,
fly = 1,
run = 1,
jump = 1,
-- Mood tags.
happy = 2,
sad = 2,
-- Direction tags.
left = 3,
right = 3,
up = 3,
down = 3,
-- Some other type of tag.
--bla = 4,
--blu = 4,
--(...)
}
local sorted_tags_helper = {}
function make_animation_key(a, b, c, d)
-- Alternative: use a FOR loop with select() for a cleaner & unlimited number
-- of parameter tags, but it's not as fast as this unrolled way because of the extra
-- function calls.
sorted_tags_helper[1], sorted_tags_helper[2],
sorted_tags_helper[3],
sorted_tags_helper[4] = '', '', '', ''
local tag_index = animation_tags[a]
if tag_index then sorted_tags_helper[tag_index] = a end
tag_index = animation_tags[b]
if tag_index then sorted_tags_helper[tag_index] = b end
tag_index = animation_tags[c]
if tag_index then sorted_tags_helper[tag_index] = c end
tag_index = animation_tags[d]
if tag_index then sorted_tags_helper[tag_index] = d end
return (sorted_tags_helper[1] .. sorted_tags_helper[2] ..
sorted_tags_helper[3] .. sorted_tags_helper[4])
end
-- Player
animation {
-- WALK / SAD / LEFT.
[make_animation_key('walk', 'sad', 'left')] = {
asset = 'assets/actors/player/walk_sad_left.png',
frames = {4, 5, 6, 7},
--(...)
}
-- FLY / LEFT.
[make_animation_key('fly', 'left')] = {
asset = 'assets/actors/player/fly_all_left.png',
frames = {1, 2, 3, 4},
loop = true,
--(...)
}
}
Code: Select all
> print(make_animation_key('sad', 'up', 'walk'))
walksadup
If you're stuck somewhere, then like Dan Harmon said (in the context of screenwriting): "prove you're a bad writer".