diff options
Diffstat (limited to 'src/party.cpp')
| -rw-r--r-- | src/party.cpp | 231 |
1 files changed, 0 insertions, 231 deletions
| diff --git a/src/party.cpp b/src/party.cpp deleted file mode 100644 index 8b0c34e..0000000 --- a/src/party.cpp +++ /dev/null | |||
| @@ -1,231 +0,0 @@ | |||
| 1 | #include "party.h" | ||
| 2 | #include "consts.h" | ||
| 3 | #include "mixer.h" | ||
| 4 | #include "transform_system.h" | ||
| 5 | #include "animation_system.h" | ||
| 6 | |||
| 7 | void Party::addMember(Game& game, int spriteId) { | ||
| 8 | int index = members_.size(); | ||
| 9 | |||
| 10 | PartyMember newMember; | ||
| 11 | newMember.spriteId = spriteId; | ||
| 12 | |||
| 13 | if (index > 0) { | ||
| 14 | const Sprite& sprite = game.getSprite(spriteId); | ||
| 15 | |||
| 16 | newMember.movement = std::deque<Movement>(PARTY_FRAME_DELAY * index, {.pos = sprite.loc, .dir = sprite.dir}); | ||
| 17 | } | ||
| 18 | |||
| 19 | members_.push_back(std::move(newMember)); | ||
| 20 | |||
| 21 | game.getSystem<AnimationSystem>().setSpriteAnimation(spriteId, "still"); | ||
| 22 | } | ||
| 23 | |||
| 24 | void Party::move(Game& game, Mixer& mixer, const Input& keystate) { | ||
| 25 | if (members_.empty()) { | ||
| 26 | return; | ||
| 27 | } | ||
| 28 | |||
| 29 | Direction dir = Direction::left; | ||
| 30 | |||
| 31 | if (!keystate.up && !keystate.down && !keystate.left && !keystate.right) { | ||
| 32 | if (state_ == State::Running) { | ||
| 33 | dir = lastDir_; | ||
| 34 | } else { | ||
| 35 | if (state_ == State::Normal) { | ||
| 36 | for (int i = 0; i < members_.size(); i++) { | ||
| 37 | game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "still"); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | return; | ||
| 42 | } | ||
| 43 | } else { | ||
| 44 | if (keystate.up) | ||
| 45 | { | ||
| 46 | dir = Direction::up; | ||
| 47 | } else if (keystate.down) | ||
| 48 | { | ||
| 49 | dir = Direction::down; | ||
| 50 | } | ||
| 51 | |||
| 52 | if (keystate.left) | ||
| 53 | { | ||
| 54 | if (dir == Direction::up) { | ||
| 55 | dir = Direction::up_left; | ||
| 56 | } else if (dir == Direction::down) { | ||
| 57 | dir = Direction::down_left; | ||
| 58 | } else { | ||
| 59 | dir = Direction::left; | ||
| 60 | } | ||
| 61 | } else if (keystate.right) | ||
| 62 | { | ||
| 63 | if (dir == Direction::up) { | ||
| 64 | dir = Direction::up_right; | ||
| 65 | } else if (dir == Direction::down) { | ||
| 66 | dir = Direction::down_right; | ||
| 67 | } else { | ||
| 68 | dir = Direction::right; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | lastDir_ = dir; | ||
| 74 | |||
| 75 | const Sprite& p1 = game.getSprite(members_[0].spriteId); | ||
| 76 | vec2i pLoc = p1.loc; | ||
| 77 | |||
| 78 | if (state_ == State::Crouching) { | ||
| 79 | for (int i = 0; i < members_.size(); i++) { | ||
| 80 | game.getSystem<AnimationSystem>().setSpriteDirection(members_[i].spriteId, dir); | ||
| 81 | } | ||
| 82 | |||
| 83 | return; | ||
| 84 | } else { | ||
| 85 | game.getSystem<AnimationSystem>().setSpriteDirection(members_[0].spriteId, dir); | ||
| 86 | } | ||
| 87 | |||
| 88 | int speed = MOVEMENT_SPEED; | ||
| 89 | if (state_ == State::Running) { | ||
| 90 | speed *= 2; | ||
| 91 | } else { | ||
| 92 | for (int i = 0; i < members_.size(); i++) { | ||
| 93 | game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "walk"); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | pLoc += (unitVecInDirection(dir) * speed); | ||
| 98 | |||
| 99 | // Check collision. | ||
| 100 | const Map& map = game.getMap(); | ||
| 101 | bool blocked = false; | ||
| 102 | |||
| 103 | const vec2i UL_COL_BOX = { 8, 8 }; | ||
| 104 | const vec2i DR_COL_BOX = { 4, 0 }; | ||
| 105 | vec2i oldColPosUL = (p1.loc - UL_COL_BOX) / map.getTileSize(); | ||
| 106 | vec2i newColPosUL = (pLoc - UL_COL_BOX) / map.getTileSize(); | ||
| 107 | vec2i oldColPosDR = (p1.loc + DR_COL_BOX) / map.getTileSize(); | ||
| 108 | vec2i newColPosDR = (pLoc + DR_COL_BOX) / map.getTileSize(); | ||
| 109 | |||
| 110 | if (dirHasDir(dir, Direction::right) && | ||
| 111 | newColPosDR.x() > oldColPosDR.x()) { | ||
| 112 | for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { | ||
| 113 | if (map.isBlocked(newColPosDR.x(), y)) { | ||
| 114 | blocked = true; | ||
| 115 | pLoc.x() = p1.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; | ||
| 116 | break; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if (dirHasDir(dir, Direction::left) && | ||
| 122 | newColPosUL.x() < oldColPosUL.x()) { | ||
| 123 | for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { | ||
| 124 | if (map.isBlocked(newColPosUL.x(), y)) { | ||
| 125 | blocked = true; | ||
| 126 | pLoc.x() = p1.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | if (dirHasDir(dir, Direction::down) && | ||
| 133 | newColPosDR.y() > oldColPosDR.y()) { | ||
| 134 | for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { | ||
| 135 | if (map.isBlocked(x, newColPosDR.y())) { | ||
| 136 | blocked = true; | ||
| 137 | pLoc.y() = p1.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; | ||
| 138 | break; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | if (dirHasDir(dir, Direction::up) && | ||
| 144 | newColPosUL.y() < oldColPosUL.y()) { | ||
| 145 | for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { | ||
| 146 | if (map.isBlocked(x, newColPosUL.y())) { | ||
| 147 | blocked = true; | ||
| 148 | pLoc.y() = p1.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; | ||
| 149 | break; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | if (blocked && state_ == State::Running) { | ||
| 155 | stopRunning(game); | ||
| 156 | mixer.playSound("../res/bump.wav"); | ||
| 157 | } | ||
| 158 | |||
| 159 | // Move everything | ||
| 160 | if (pLoc != p1.loc) { | ||
| 161 | game.getSystem<TransformSystem>().moveSprite(members_[0].spriteId, pLoc); | ||
| 162 | |||
| 163 | for (int i = 1; i < members_.size(); i++) { | ||
| 164 | const Sprite& pNext = game.getSprite(members_[i].spriteId); | ||
| 165 | const Movement& posdir = members_[i].movement.front(); | ||
| 166 | game.getSystem<TransformSystem>().moveSprite(members_[i].spriteId, posdir.pos); | ||
| 167 | game.getSystem<AnimationSystem>().setSpriteDirection(members_[i].spriteId, posdir.dir); | ||
| 168 | |||
| 169 | members_[i].movement.pop_front(); | ||
| 170 | members_[i].movement.push_back({.pos = pLoc, .dir = dir}); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | void Party::beginCrouch(Game& game) { | ||
| 176 | if (state_ == State::Running) { | ||
| 177 | stopRunning(game); | ||
| 178 | } else { | ||
| 179 | state_ = State::Crouching; | ||
| 180 | |||
| 181 | for (int i = 0; i < members_.size(); i++) { | ||
| 182 | game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "crouch"); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | void Party::endCrouch(Game& game) { | ||
| 188 | if (state_ == State::Crouching) { | ||
| 189 | state_ = State::Running; | ||
| 190 | |||
| 191 | for (int i = 0; i < members_.size(); i++) { | ||
| 192 | game.getSystem<AnimationSystem>().setSpriteAnimation(members_[i].spriteId, "run"); | ||
| 193 | |||
| 194 | // Halve the movement buffer for the followers. | ||
| 195 | if (i > 0) { | ||
| 196 | std::deque<Movement> newMove; | ||
| 197 | |||
| 198 | while (!members_[i].movement.empty()) { | ||
| 199 | newMove.push_back(members_[i].movement.front()); | ||
| 200 | members_[i].movement.pop_front(); | ||
| 201 | members_[i].movement.pop_front(); | ||
| 202 | } | ||
| 203 | |||
| 204 | members_[i].movement = std::move(newMove); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | void Party::stopRunning(Game& game) { | ||
| 211 | state_ = State::Normal; | ||
| 212 | |||
| 213 | // Double the movement buffer for the followers. | ||
| 214 | for (int i = 1; i < members_.size(); i++) { | ||
| 215 | std::deque<Movement> newMove; | ||
| 216 | vec2i lastPos = game.getSprite(members_[i].spriteId).loc; | ||
| 217 | |||
| 218 | while (!members_[i].movement.empty()) { | ||
| 219 | Movement m1 = members_[i].movement.front(); | ||
| 220 | Movement m2 = m1; | ||
| 221 | m1.pos = (m1.pos + lastPos) / 2; | ||
| 222 | lastPos = m2.pos; | ||
| 223 | |||
| 224 | newMove.push_back(m1); | ||
| 225 | newMove.push_back(m2); | ||
| 226 | members_[i].movement.pop_front(); | ||
| 227 | } | ||
| 228 | |||
| 229 | members_[i].movement = std::move(newMove); | ||
| 230 | } | ||
| 231 | } | ||
