diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-13 12:14:58 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-13 12:14:58 -0500 |
| commit | ed933607765a6e010689aaaf85184053ff6e8a2b (patch) | |
| tree | 9c59ddfa9c7ddb221788485ad96cad116f8b0c28 | |
| parent | e9602acecd1c5f63c6cf72a270a1bd09eba1e7e1 (diff) | |
| download | tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.tar.gz tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.tar.bz2 tanetane-ed933607765a6e010689aaaf85184053ff6e8a2b.zip | |
Added non-looping animations
Lucas can get electrocuted now.
| -rw-r--r-- | res/maps/map2.tmx | 8 | ||||
| -rw-r--r-- | res/scripts/common.lua | 13 | ||||
| -rw-r--r-- | res/scripts/map2.lua | 14 | ||||
| -rw-r--r-- | res/sprites/lucas_anim.txt | 3 | ||||
| -rw-r--r-- | src/animation_system.cpp | 29 | ||||
| -rw-r--r-- | src/renderer.cpp | 2 | ||||
| -rw-r--r-- | src/script_system.cpp | 3 | ||||
| -rw-r--r-- | src/sprite.h | 8 |
8 files changed, 67 insertions, 13 deletions
| diff --git a/res/maps/map2.tmx b/res/maps/map2.tmx index ee9931c..6316ef5 100644 --- a/res/maps/map2.tmx +++ b/res/maps/map2.tmx | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="64" height="64" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="5"> | 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="64" height="64" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="6"> |
| 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="180" columns="10"> | 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="180" columns="10"> |
| 4 | <image source="map2_tiles.png" width="160" height="288"/> | 4 | <image source="map2_tiles.png" width="160" height="288"/> |
| 5 | <tile id="61"> | 5 | <tile id="61"> |
| @@ -651,6 +651,12 @@ | |||
| 651 | </properties> | 651 | </properties> |
| 652 | <point/> | 652 | <point/> |
| 653 | </object> | 653 | </object> |
| 654 | <object id="5" name="mailbox_lightning" type="tileSprite" x="519" y="422.667"> | ||
| 655 | <properties> | ||
| 656 | <property name="interactionScript" value="mailbox_lightning"/> | ||
| 657 | </properties> | ||
| 658 | <point/> | ||
| 659 | </object> | ||
| 654 | </objectgroup> | 660 | </objectgroup> |
| 655 | <layer id="1" name="Layer 0" width="64" height="64"> | 661 | <layer id="1" name="Layer 0" width="64" height="64"> |
| 656 | <data encoding="csv"> | 662 | <data encoding="csv"> |
| diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 3c412c6..3db1b26 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua | |||
| @@ -59,11 +59,24 @@ function HideCutsceneBars() | |||
| 59 | playerSprite.controllable = true | 59 | playerSprite.controllable = true |
| 60 | end | 60 | end |
| 61 | 61 | ||
| 62 | function SetDirection(spriteName, dir) | ||
| 63 | local spriteId = getSpriteByAlias(spriteName) | ||
| 64 | animation():setSpriteDirection(spriteId, dir) | ||
| 65 | end | ||
| 66 | |||
| 62 | function SetAnimation(spriteName, animName) | 67 | function SetAnimation(spriteName, animName) |
| 63 | local spriteId = getSpriteByAlias(spriteName) | 68 | local spriteId = getSpriteByAlias(spriteName) |
| 64 | animation():setSpriteAnimation(spriteId, animName) | 69 | animation():setSpriteAnimation(spriteId, animName) |
| 65 | end | 70 | end |
| 66 | 71 | ||
| 72 | function WaitForAnimation(spriteName) | ||
| 73 | local spriteId = getSpriteByAlias(spriteName) | ||
| 74 | local sprite = getSprite(spriteId) | ||
| 75 | repeat | ||
| 76 | coroutine.yield() | ||
| 77 | until sprite.animFinished | ||
| 78 | end | ||
| 79 | |||
| 67 | function PlaySound(filename) | 80 | function PlaySound(filename) |
| 68 | mixer():playSound("../res/sfx/" .. filename) | 81 | mixer():playSound("../res/sfx/" .. filename) |
| 69 | end | 82 | end |
| diff --git a/res/scripts/map2.lua b/res/scripts/map2.lua index 1b55072..3bc9eff 100644 --- a/res/scripts/map2.lua +++ b/res/scripts/map2.lua | |||
| @@ -23,3 +23,17 @@ function map2.mailbox1() | |||
| 23 | 23 | ||
| 24 | HideCutsceneBars() | 24 | HideCutsceneBars() |
| 25 | end | 25 | end |
| 26 | |||
| 27 | function map2.mailbox_lightning() | ||
| 28 | StartCutscene() | ||
| 29 | DisplayMessage("* ...?", "", SpeakerType.NONE) | ||
| 30 | WaitForEndOfMessage() | ||
| 31 | |||
| 32 | SetDirection("lucas", Direction.DOWN) | ||
| 33 | SetAnimation("lucas", "electrocute!") | ||
| 34 | WaitForAnimation("lucas") | ||
| 35 | |||
| 36 | DisplayMessage("* It was lightning.\n\fAh.", "", SpeakerType.NONE) | ||
| 37 | WaitForEndOfMessage() | ||
| 38 | HideCutsceneBars() | ||
| 39 | end | ||
| diff --git a/res/sprites/lucas_anim.txt b/res/sprites/lucas_anim.txt index 455c321..12a373e 100644 --- a/res/sprites/lucas_anim.txt +++ b/res/sprites/lucas_anim.txt | |||
| @@ -31,4 +31,5 @@ run[up_left]: 70,78,86,94 | |||
| 31 | run[up]: 71,79,87,95 | 31 | run[up]: 71,79,87,95 |
| 32 | run[up_right]: 72,80,88,96 | 32 | run[up_right]: 72,80,88,96 |
| 33 | run[right]: 73,81,89,97 | 33 | run[right]: 73,81,89,97 |
| 34 | run[down_right]: 74,82,90,98 \ No newline at end of file | 34 | run[down_right]: 74,82,90,98 |
| 35 | electrocute![down]: 116,117,118,116,117,118,116,117,118,119,120,121,122 \ No newline at end of file | ||
| diff --git a/src/animation_system.cpp b/src/animation_system.cpp index 997b7f7..3f3f22a 100644 --- a/src/animation_system.cpp +++ b/src/animation_system.cpp | |||
| @@ -65,21 +65,26 @@ void AnimationSystem::initSprite(int spriteId, std::string_view filename) { | |||
| 65 | std::string animLine; | 65 | std::string animLine; |
| 66 | std::getline(datafile, animLine); // blank | 66 | std::getline(datafile, animLine); // blank |
| 67 | while (std::getline(datafile, animLine)) { | 67 | while (std::getline(datafile, animLine)) { |
| 68 | std::regex re(R"(([a-z]+)\[([a-z_]+)\]: ([0-9,]+))"); | 68 | std::regex re(R"(([a-z!]+)\[([a-z_]+)\]: ([0-9,]+))"); |
| 69 | std::smatch m; | 69 | std::smatch m; |
| 70 | std::regex_match(animLine, m, re); | 70 | std::regex_match(animLine, m, re); |
| 71 | 71 | ||
| 72 | std::vector<int> frames; | 72 | std::string animName = m[1]; |
| 73 | Animation anim; | ||
| 73 | auto framestrs = splitStr<std::list<std::string>>(m[3], ","); | 74 | auto framestrs = splitStr<std::list<std::string>>(m[3], ","); |
| 74 | for (const std::string& f : framestrs) { | 75 | for (const std::string& f : framestrs) { |
| 75 | frames.push_back(std::stoi(f)); | 76 | anim.frameIndices.push_back(std::stoi(f)); |
| 77 | } | ||
| 78 | |||
| 79 | if (animName.back() == '!') { | ||
| 80 | anim.looping = false; | ||
| 76 | } | 81 | } |
| 77 | 82 | ||
| 78 | int animId = sprite.animations.size(); | 83 | int animId = sprite.animations.size(); |
| 79 | sprite.animations.push_back(std::move(frames)); | 84 | sprite.animations.push_back(std::move(anim)); |
| 80 | 85 | ||
| 81 | Direction dir = directionFromString(std::string(m[2])); | 86 | Direction dir = directionFromString(std::string(m[2])); |
| 82 | sprite.nameDirToAnim[m[1]][dir] = animId; | 87 | sprite.nameDirToAnim[animName][dir] = animId; |
| 83 | } | 88 | } |
| 84 | 89 | ||
| 85 | updateAnimation(spriteId); | 90 | updateAnimation(spriteId); |
| @@ -89,10 +94,17 @@ void AnimationSystem::tick(double dt) { | |||
| 89 | animTimer_.accumulate(dt); | 94 | animTimer_.accumulate(dt); |
| 90 | while (animTimer_.step()) { | 95 | while (animTimer_.step()) { |
| 91 | for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { | 96 | for (Sprite& sprite : game_.getSprites() | game_.spriteView()) { |
| 92 | if (sprite.isAnimated) { | 97 | if (sprite.isAnimated && !sprite.animFinished) { |
| 93 | sprite.animationFrame++; | 98 | sprite.animationFrame++; |
| 94 | if (sprite.animationFrame >= sprite.animations[sprite.animationId].size()) { | 99 | if (sprite.animations[sprite.animationId].looping) { |
| 95 | sprite.animationFrame = 0; | 100 | if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size()) { |
| 101 | sprite.animationFrame = 0; | ||
| 102 | } | ||
| 103 | } else { | ||
| 104 | if (sprite.animationFrame >= sprite.animations[sprite.animationId].frameIndices.size() - 1) { | ||
| 105 | sprite.animationFrame = sprite.animations[sprite.animationId].frameIndices.size() - 1; | ||
| 106 | sprite.animFinished = true; | ||
| 107 | } | ||
| 96 | } | 108 | } |
| 97 | } | 109 | } |
| 98 | } | 110 | } |
| @@ -119,4 +131,5 @@ void AnimationSystem::updateAnimation(int spriteId) { | |||
| 119 | Sprite& sprite = game_.getSprite(spriteId); | 131 | Sprite& sprite = game_.getSprite(spriteId); |
| 120 | sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir]; | 132 | sprite.animationId = sprite.nameDirToAnim[sprite.animationName][sprite.dir]; |
| 121 | sprite.animationFrame = 0; | 133 | sprite.animationFrame = 0; |
| 134 | sprite.animFinished = false; | ||
| 122 | } | 135 | } |
| diff --git a/src/renderer.cpp b/src/renderer.cpp index f07931b..a7169e9 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
| @@ -130,7 +130,7 @@ void Renderer::render(Game& game) { | |||
| 130 | SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest); | 130 | SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest); |
| 131 | } | 131 | } |
| 132 | 132 | ||
| 133 | const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).at(sprite.animationFrame)); | 133 | const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame)); |
| 134 | const SDL_Rect& src = frame.srcRect; | 134 | const SDL_Rect& src = frame.srcRect; |
| 135 | SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; | 135 | SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; |
| 136 | SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); | 136 | SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); |
| diff --git a/src/script_system.cpp b/src/script_system.cpp index 5b9b987..3f89290 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp | |||
| @@ -16,7 +16,8 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { | |||
| 16 | "dir", &Sprite::dir, | 16 | "dir", &Sprite::dir, |
| 17 | "followers", &Sprite::followers, | 17 | "followers", &Sprite::followers, |
| 18 | "characterState", &Sprite::characterState, | 18 | "characterState", &Sprite::characterState, |
| 19 | "controllable", &Sprite::controllable); | 19 | "controllable", &Sprite::controllable, |
| 20 | "animFinished", &Sprite::animFinished); | ||
| 20 | 21 | ||
| 21 | engine_.new_usertype<MessageSystem>( | 22 | engine_.new_usertype<MessageSystem>( |
| 22 | "message", | 23 | "message", |
| diff --git a/src/sprite.h b/src/sprite.h index 84a7b03..fcf7e1d 100644 --- a/src/sprite.h +++ b/src/sprite.h | |||
| @@ -16,6 +16,11 @@ struct SpriteFrame { | |||
| 16 | vec2i size; | 16 | vec2i size; |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | struct Animation { | ||
| 20 | bool looping = true; | ||
| 21 | std::vector<int> frameIndices; | ||
| 22 | }; | ||
| 23 | |||
| 19 | enum class CharacterState { | 24 | enum class CharacterState { |
| 20 | Still, | 25 | Still, |
| 21 | Walking, | 26 | Walking, |
| @@ -48,8 +53,9 @@ public: | |||
| 48 | int animationId = 0; | 53 | int animationId = 0; |
| 49 | int animationFrame = 0; | 54 | int animationFrame = 0; |
| 50 | std::vector<SpriteFrame> frames; | 55 | std::vector<SpriteFrame> frames; |
| 51 | std::vector<std::vector<int>> animations; | 56 | std::vector<Animation> animations; |
| 52 | std::map<std::string, std::map<Direction, int>> nameDirToAnim; | 57 | std::map<std::string, std::map<Direction, int>> nameDirToAnim; |
| 58 | bool animFinished = false; | ||
| 53 | bool hasShadow = false; | 59 | bool hasShadow = false; |
| 54 | 60 | ||
| 55 | // Character | 61 | // Character |
