summary refs log tree commit diff stats
path: root/src/character_system.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-06 11:40:48 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-06 11:40:48 -0500
commit478bc11eec70e6127161ff360cd77d6893a81c42 (patch)
tree0b9dee46b3f2111acce17475f309487fc8759cb4 /src/character_system.cpp
parent64ad64ae11a9497f36258e1c83d23f7964bbf885 (diff)
downloadtanetane-478bc11eec70e6127161ff360cd77d6893a81c42.tar.gz
tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.tar.bz2
tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.zip
Moved some collision stuff into the TransformSystem
Diffstat (limited to 'src/character_system.cpp')
-rw-r--r--src/character_system.cpp57
1 files changed, 8 insertions, 49 deletions
diff --git a/src/character_system.cpp b/src/character_system.cpp index 4d2d338..3df4f9a 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp
@@ -67,58 +67,15 @@ void CharacterSystem::tick(double dt) {
67 pLoc += (unitVecInDirection(sprite.dir) * speed); 67 pLoc += (unitVecInDirection(sprite.dir) * speed);
68 68
69 // Check collision. 69 // Check collision.
70 const Map& map = game_.getMap(); 70 CollisionResult collision = game_.getSystem<TransformSystem>().checkCollision(spriteId, pLoc, sprite.dir);
71 bool blocked = false; 71 bool blocked = collision.horiz.blocked || collision.vert.blocked;
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 }
90
91 if (dirHasDir(sprite.dir, Direction::left) &&
92 newColPosUL.x() < oldColPosUL.x()) {
93 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
94 if (map.isBlocked(newColPosUL.x(), y)) {
95 blocked = true;
96 pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
97 break;
98 }
99 }
100 }
101 72
102 if (dirHasDir(sprite.dir, Direction::down) && 73 if (collision.horiz.blocked) {
103 newColPosDR.y() > oldColPosDR.y()) { 74 pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
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 } 75 }
112 76
113 if (dirHasDir(sprite.dir, Direction::up) && 77 if (collision.vert.blocked) {
114 newColPosUL.y() < oldColPosUL.y()) { 78 pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1;
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 } 79 }
123 80
124 if (blocked && sprite.characterState == CharacterState::Running) { 81 if (blocked && sprite.characterState == CharacterState::Running) {
@@ -131,6 +88,8 @@ void CharacterSystem::tick(double dt) {
131 game_.getSystem<TransformSystem>().moveSprite(spriteId, pLoc); 88 game_.getSystem<TransformSystem>().moveSprite(spriteId, pLoc);
132 89
133 if (sprite.characterState == CharacterState::Running) { 90 if (sprite.characterState == CharacterState::Running) {
91 const Map& map = game_.getMap();
92
134 vec2i newMapTileLoc = pLoc / map.getTileSize(); 93 vec2i newMapTileLoc = pLoc / map.getTileSize();
135 StepType newTileStep = map.getStepType(newMapTileLoc.x(), newMapTileLoc.y()); 94 StepType newTileStep = map.getStepType(newMapTileLoc.x(), newMapTileLoc.y());
136 if (sprite.stepType != newTileStep) { 95 if (sprite.stepType != newTileStep) {