diff options
-rw-r--r-- | src/game.h | 4 | ||||
-rw-r--r-- | src/party.cpp | 6 | ||||
-rw-r--r-- | src/sprite.cpp | 19 | ||||
-rw-r--r-- | src/sprite.h | 7 |
4 files changed, 14 insertions, 22 deletions
diff --git a/src/game.h b/src/game.h index a8626fa..274ef2e 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -40,8 +40,8 @@ public: | |||
40 | 40 | ||
41 | void moveSprite(int id, vec2i newLoc); | 41 | void moveSprite(int id, vec2i newLoc); |
42 | 42 | ||
43 | void setSpriteWalking(int id, bool walking) { | 43 | void setSpriteState(int id, std::string state) { |
44 | sprites_[id].setWalking(walking); | 44 | sprites_[id].setState(std::move(state)); |
45 | } | 45 | } |
46 | 46 | ||
47 | void setSpriteDirection(int id, Direction dir) { | 47 | void setSpriteDirection(int id, Direction dir) { |
diff --git a/src/party.cpp b/src/party.cpp index 371514b..28492a9 100644 --- a/src/party.cpp +++ b/src/party.cpp | |||
@@ -17,6 +17,8 @@ void Party::addMember(Game& game, int spriteId) { | |||
17 | } | 17 | } |
18 | 18 | ||
19 | members_.push_back(std::move(newMember)); | 19 | members_.push_back(std::move(newMember)); |
20 | |||
21 | game.setSpriteState(spriteId, "still"); | ||
20 | } | 22 | } |
21 | 23 | ||
22 | void Party::move(Game& game, const Input& keystate) { | 24 | void Party::move(Game& game, const Input& keystate) { |
@@ -64,7 +66,7 @@ void Party::move(Game& game, const Input& keystate) { | |||
64 | 66 | ||
65 | if (keystate.up || keystate.down || keystate.left || keystate.right) { | 67 | if (keystate.up || keystate.down || keystate.left || keystate.right) { |
66 | for (int i = 0; i < members_.size(); i++) { | 68 | for (int i = 0; i < members_.size(); i++) { |
67 | game.setSpriteWalking(members_[i].spriteId, true); | 69 | game.setSpriteState(members_[i].spriteId, "walk"); |
68 | } | 70 | } |
69 | 71 | ||
70 | game.moveSprite(members_[0].spriteId, pLoc); | 72 | game.moveSprite(members_[0].spriteId, pLoc); |
@@ -82,7 +84,7 @@ void Party::move(Game& game, const Input& keystate) { | |||
82 | } | 84 | } |
83 | } else { | 85 | } else { |
84 | for (int i = 0; i < members_.size(); i++) { | 86 | for (int i = 0; i < members_.size(); i++) { |
85 | game.setSpriteWalking(members_[i].spriteId, false); | 87 | game.setSpriteState(members_[i].spriteId, "still"); |
86 | } | 88 | } |
87 | } | 89 | } |
88 | } \ No newline at end of file | 90 | } \ No newline at end of file |
diff --git a/src/sprite.cpp b/src/sprite.cpp index cc196ae..c52807a 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp | |||
@@ -38,12 +38,7 @@ Sprite::Sprite(std::string_view filename, Renderer& renderer) { | |||
38 | animations_.push_back(std::move(frames)); | 38 | animations_.push_back(std::move(frames)); |
39 | 39 | ||
40 | Direction dir = directionFromString(std::string(m[2])); | 40 | Direction dir = directionFromString(std::string(m[2])); |
41 | 41 | stateDirToAnim_[m[1]][dir] = animId; | |
42 | if (m[1] == "still") { | ||
43 | stillAnims_[dir] = animId; | ||
44 | } else { | ||
45 | walkingAnims_[dir] = animId; | ||
46 | } | ||
47 | } | 42 | } |
48 | 43 | ||
49 | updateAnimation(); | 44 | updateAnimation(); |
@@ -56,19 +51,15 @@ void Sprite::setDirection(Direction dir) { | |||
56 | } | 51 | } |
57 | } | 52 | } |
58 | 53 | ||
59 | void Sprite::setWalking(bool walking) { | 54 | void Sprite::setState(std::string state) { |
60 | if (isWalking_ != walking) { | 55 | if (state_ != state) { |
61 | isWalking_ = walking; | 56 | state_ = state; |
62 | updateAnimation(); | 57 | updateAnimation(); |
63 | } | 58 | } |
64 | } | 59 | } |
65 | 60 | ||
66 | void Sprite::updateAnimation() { | 61 | void Sprite::updateAnimation() { |
67 | if (isWalking_) { | 62 | curAnim_ = stateDirToAnim_[state_][curDir_]; |
68 | curAnim_ = walkingAnims_[curDir_]; | ||
69 | } else { | ||
70 | curAnim_ = stillAnims_[curDir_]; | ||
71 | } | ||
72 | curFrame_ = 0; | 63 | curFrame_ = 0; |
73 | } | 64 | } |
74 | 65 | ||
diff --git a/src/sprite.h b/src/sprite.h index 5e8003b..82ea90c 100644 --- a/src/sprite.h +++ b/src/sprite.h | |||
@@ -31,7 +31,7 @@ public: | |||
31 | 31 | ||
32 | Direction getDirection() const { return curDir_; } | 32 | Direction getDirection() const { return curDir_; } |
33 | 33 | ||
34 | void setWalking(bool walking); | 34 | void setState(std::string state); |
35 | 35 | ||
36 | void tickAnim(); | 36 | void tickAnim(); |
37 | 37 | ||
@@ -43,12 +43,11 @@ private: | |||
43 | vec2i loc_ { 0, 0 }; | 43 | vec2i loc_ { 0, 0 }; |
44 | vec2i size_; | 44 | vec2i size_; |
45 | Direction curDir_ = Direction::down; | 45 | Direction curDir_ = Direction::down; |
46 | bool isWalking_ = false; | 46 | std::string state_; |
47 | int curAnim_ = 0; | 47 | int curAnim_ = 0; |
48 | int curFrame_ = 0; | 48 | int curFrame_ = 0; |
49 | std::vector<std::vector<int>> animations_; | 49 | std::vector<std::vector<int>> animations_; |
50 | std::map<Direction, int> stillAnims_; | 50 | std::map<std::string, std::map<Direction, int>> stateDirToAnim_; |
51 | std::map<Direction, int> walkingAnims_; | ||
52 | }; | 51 | }; |
53 | 52 | ||
54 | #endif /* end of include guard: SPRITE_H_70503825 */ | 53 | #endif /* end of include guard: SPRITE_H_70503825 */ |