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