summary refs log tree commit diff stats
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
parent64ad64ae11a9497f36258e1c83d23f7964bbf885 (diff)
downloadtanetane-478bc11eec70e6127161ff360cd77d6893a81c42.tar.gz
tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.tar.bz2
tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.zip
Moved some collision stuff into the TransformSystem
-rw-r--r--src/character_system.cpp57
-rw-r--r--src/transform_system.cpp67
-rw-r--r--src/transform_system.h13
3 files changed, 88 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) {
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 0e602e0..ad7947f 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp
@@ -1,5 +1,6 @@
1#include "transform_system.h" 1#include "transform_system.h"
2#include "game.h" 2#include "game.h"
3#include "map.h"
3 4
4void TransformSystem::initSprite(int spriteId, vec2i loc) { 5void TransformSystem::initSprite(int spriteId, vec2i loc) {
5 Sprite& sprite = game_.getSprite(spriteId); 6 Sprite& sprite = game_.getSprite(spriteId);
@@ -18,3 +19,69 @@ void TransformSystem::moveSprite(int spriteId, vec2i newLoc) {
18 spritesByY_.emplace(newLoc.y(), spriteId); 19 spritesByY_.emplace(newLoc.y(), spriteId);
19 } 20 }
20} 21}
22
23CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Direction dir) {
24 CollisionResult result;
25
26 Sprite& sprite = game_.getSprite(spriteId);
27
28 const Map& map = game_.getMap();
29 bool blocked = false;
30
31 const vec2i UL_COL_BOX = { 8, 8 };
32 const vec2i DR_COL_BOX = { 4, 0 };
33 vec2i oldColPosUL = (sprite.loc - UL_COL_BOX) / map.getTileSize();
34 vec2i newColPosUL = (newLoc - UL_COL_BOX) / map.getTileSize();
35 vec2i oldColPosDR = (sprite.loc + DR_COL_BOX) / map.getTileSize();
36 vec2i newColPosDR = (newLoc + DR_COL_BOX) / map.getTileSize();
37
38 if (dirHasDir(sprite.dir, Direction::right) &&
39 newColPosDR.x() > oldColPosDR.x()) {
40 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
41 if (map.isBlocked(newColPosDR.x(), y)) {
42 result.horiz.blocked = true;
43 result.horiz.dir = Direction::right;
44
45 break;
46 }
47 }
48 }
49
50 if (dirHasDir(sprite.dir, Direction::left) &&
51 newColPosUL.x() < oldColPosUL.x()) {
52 for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) {
53 if (map.isBlocked(newColPosUL.x(), y)) {
54 result.horiz.blocked = true;
55 result.horiz.dir = Direction::left;
56
57 break;
58 }
59 }
60 }
61
62 if (dirHasDir(sprite.dir, Direction::down) &&
63 newColPosDR.y() > oldColPosDR.y()) {
64 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
65 if (map.isBlocked(x, newColPosDR.y())) {
66 result.vert.blocked = true;
67 result.vert.dir = Direction::down;
68
69 break;
70 }
71 }
72 }
73
74 if (dirHasDir(sprite.dir, Direction::up) &&
75 newColPosUL.y() < oldColPosUL.y()) {
76 for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) {
77 if (map.isBlocked(x, newColPosUL.y())) {
78 result.vert.blocked = true;
79 result.vert.dir = Direction::up;
80
81 break;
82 }
83 }
84 }
85
86 return result;
87}
diff --git a/src/transform_system.h b/src/transform_system.h index 290d518..eb1a95b 100644 --- a/src/transform_system.h +++ b/src/transform_system.h
@@ -4,11 +4,22 @@
4#include <range/v3/all.hpp> 4#include <range/v3/all.hpp>
5#include <set> 5#include <set>
6#include <tuple> 6#include <tuple>
7#include "direction.h"
7#include "system.h" 8#include "system.h"
8#include "vector.h" 9#include "vector.h"
9 10
10class Game; 11class Game;
11 12
13struct AxisResult {
14 Direction dir;
15 bool blocked = false;
16};
17
18struct CollisionResult {
19 AxisResult horiz;
20 AxisResult vert;
21};
22
12class TransformSystem : public System { 23class TransformSystem : public System {
13public: 24public:
14 25
@@ -26,6 +37,8 @@ public:
26 }); 37 });
27 } 38 }
28 39
40 CollisionResult checkCollision(int spriteId, vec2i newLoc, Direction dir);
41
29private: 42private:
30 43
31 Game& game_; 44 Game& game_;