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