diff options
-rw-r--r-- | res/maps/map1.tmx | 7 | ||||
-rw-r--r-- | res/scripts/test_trigger.lua | 6 | ||||
-rw-r--r-- | src/character_system.cpp | 13 | ||||
-rw-r--r-- | src/main.cpp | 11 | ||||
-rw-r--r-- | src/map.cpp | 15 | ||||
-rw-r--r-- | src/map.h | 10 | ||||
-rw-r--r-- | src/renderer.cpp | 10 | ||||
-rw-r--r-- | src/sprite.h | 2 | ||||
-rw-r--r-- | src/transform_system.cpp | 31 | ||||
-rw-r--r-- | src/transform_system.h | 2 |
10 files changed, 88 insertions, 19 deletions
diff --git a/res/maps/map1.tmx b/res/maps/map1.tmx index d830158..9b1d1b1 100644 --- a/res/maps/map1.tmx +++ b/res/maps/map1.tmx | |||
@@ -1,5 +1,5 @@ | |||
1 | <?xml version="1.0" encoding="UTF-8"?> | 1 | <?xml version="1.0" encoding="UTF-8"?> |
2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="48" height="32" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="6"> | 2 | <map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="48" height="32" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="8"> |
3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="240" columns="10"> | 3 | <tileset firstgid="1" name="fromRom" tilewidth="16" tileheight="16" tilecount="240" columns="10"> |
4 | <image source="map1_tiles.png" width="160" height="384"/> | 4 | <image source="map1_tiles.png" width="160" height="384"/> |
5 | <tile id="77"> | 5 | <tile id="77"> |
@@ -774,6 +774,11 @@ | |||
774 | <object id="5" name="spawn" type="warp" x="201.931" y="284.096"> | 774 | <object id="5" name="spawn" type="warp" x="201.931" y="284.096"> |
775 | <point/> | 775 | <point/> |
776 | </object> | 776 | </object> |
777 | <object id="7" name="testTrigger" type="trigger" x="70.3333" y="344.333" width="37.3333" height="67.3333"> | ||
778 | <properties> | ||
779 | <property name="script" value="test_trigger"/> | ||
780 | </properties> | ||
781 | </object> | ||
777 | </objectgroup> | 782 | </objectgroup> |
778 | <layer id="1" name="Layer 0" width="48" height="32"> | 783 | <layer id="1" name="Layer 0" width="48" height="32"> |
779 | <data encoding="csv"> | 784 | <data encoding="csv"> |
diff --git a/res/scripts/test_trigger.lua b/res/scripts/test_trigger.lua new file mode 100644 index 0000000..cd64fb8 --- /dev/null +++ b/res/scripts/test_trigger.lua | |||
@@ -0,0 +1,6 @@ | |||
1 | function test_trigger() | ||
2 | PlaySound("boney_growl.wav") | ||
3 | DisplayMessage("Hi! Welcome to the funky zone.", "", SpeakerType.NONE) | ||
4 | WaitForEndOfMessage() | ||
5 | HideCutsceneBars() | ||
6 | end | ||
diff --git a/src/character_system.cpp b/src/character_system.cpp index fcd69dd..d4765bf 100644 --- a/src/character_system.cpp +++ b/src/character_system.cpp | |||
@@ -5,6 +5,7 @@ | |||
5 | #include "game.h" | 5 | #include "game.h" |
6 | #include "transform_system.h" | 6 | #include "transform_system.h" |
7 | #include "animation_system.h" | 7 | #include "animation_system.h" |
8 | #include "script_system.h" | ||
8 | 9 | ||
9 | void CharacterSystem::initSprite(int spriteId) { | 10 | void CharacterSystem::initSprite(int spriteId) { |
10 | Sprite& sprite = game_.getSprite(spriteId); | 11 | Sprite& sprite = game_.getSprite(spriteId); |
@@ -83,10 +84,22 @@ void CharacterSystem::tick(double dt) { | |||
83 | 84 | ||
84 | if (collision.horiz.blocked) { | 85 | if (collision.horiz.blocked) { |
85 | pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; | 86 | pLoc.x() = sprite.loc.x();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; |
87 | } else if (collision.horiz.colliderSprite != -1) { | ||
88 | Sprite& collider = game_.getSprite(collision.horiz.colliderSprite); | ||
89 | |||
90 | if (collider.walkthroughScript != "") { | ||
91 | game_.getSystem<ScriptSystem>().runScript(collider.walkthroughScript); | ||
92 | } | ||
86 | } | 93 | } |
87 | 94 | ||
88 | if (collision.vert.blocked) { | 95 | if (collision.vert.blocked) { |
89 | pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; | 96 | pLoc.y() = sprite.loc.y();//(newColPosDR * map.getTileSize() - (collisionBox / 2)).x() - 1; |
97 | } else if (collision.vert.colliderSprite != -1) { | ||
98 | Sprite& collider = game_.getSprite(collision.vert.colliderSprite); | ||
99 | |||
100 | if (collider.walkthroughScript != "") { | ||
101 | game_.getSystem<ScriptSystem>().runScript(collider.walkthroughScript); | ||
102 | } | ||
90 | } | 103 | } |
91 | 104 | ||
92 | if (blocked && sprite.characterState == CharacterState::Running) { | 105 | if (blocked && sprite.characterState == CharacterState::Running) { |
diff --git a/src/main.cpp b/src/main.cpp index 62c81f3..a350c8d 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -27,7 +27,7 @@ void loop(Renderer& renderer) { | |||
27 | 27 | ||
28 | int lucasSprite = game.emplaceSprite("lucas"); | 28 | int lucasSprite = game.emplaceSprite("lucas"); |
29 | game.getSystem<TransformSystem>().initSprite(lucasSprite, game.getMap().getWarpPoint("spawn")); | 29 | game.getSystem<TransformSystem>().initSprite(lucasSprite, game.getMap().getWarpPoint("spawn")); |
30 | game.getSystem<TransformSystem>().setUpCollision(lucasSprite, {-8, -8}, {12, 8}); | 30 | game.getSystem<TransformSystem>().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true); |
31 | game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt", renderer); | 31 | game.getSystem<AnimationSystem>().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt", renderer); |
32 | game.getSprite(lucasSprite).controllable = true; | 32 | game.getSprite(lucasSprite).controllable = true; |
33 | game.getSystem<CharacterSystem>().initSprite(lucasSprite); | 33 | game.getSystem<CharacterSystem>().initSprite(lucasSprite); |
@@ -50,11 +50,18 @@ void loop(Renderer& renderer) { | |||
50 | for (const Prototype& p : game.getMap().getPrototypes()) { | 50 | for (const Prototype& p : game.getMap().getPrototypes()) { |
51 | int spriteId = game.emplaceSprite(p.name); | 51 | int spriteId = game.emplaceSprite(p.name); |
52 | game.getSystem<TransformSystem>().initSprite(spriteId, p.pos); | 52 | game.getSystem<TransformSystem>().initSprite(spriteId, p.pos); |
53 | game.getSystem<TransformSystem>().setUpCollision(spriteId, p.collisionOffset, p.collisionSize); | 53 | game.getSystem<TransformSystem>().setUpCollision(spriteId, p.collisionOffset, p.collisionSize, true); |
54 | game.getSystem<AnimationSystem>().initSprite(spriteId, p.animationFilename, renderer); | 54 | game.getSystem<AnimationSystem>().initSprite(spriteId, p.animationFilename, renderer); |
55 | game.getSprite(spriteId).interactionScript = p.interactionScript; | 55 | game.getSprite(spriteId).interactionScript = p.interactionScript; |
56 | } | 56 | } |
57 | 57 | ||
58 | for (const Trigger& t : game.getMap().getTriggers()) { | ||
59 | int spriteId = game.emplaceSprite(t.name); | ||
60 | game.getSystem<TransformSystem>().initSprite(spriteId, t.pos); | ||
61 | game.getSystem<TransformSystem>().setUpCollision(spriteId, {0, 0}, t.size, false); | ||
62 | game.getSprite(spriteId).walkthroughScript = t.script; | ||
63 | } | ||
64 | |||
58 | game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite); | 65 | game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite); |
59 | game.getSystem<CameraSystem>().unlockCamera(); | 66 | game.getSystem<CameraSystem>().unlockCamera(); |
60 | 67 | ||
diff --git a/src/map.cpp b/src/map.cpp index 2214d17..b6f6755 100644 --- a/src/map.cpp +++ b/src/map.cpp | |||
@@ -84,6 +84,21 @@ Map::Map(std::string_view filename, Renderer& renderer) { | |||
84 | point.y() = object.getPosition().y; | 84 | point.y() = object.getPosition().y; |
85 | 85 | ||
86 | warpPoints_[object.getName()] = std::move(point); | 86 | warpPoints_[object.getName()] = std::move(point); |
87 | } else if (object.getType() == "trigger") { | ||
88 | Trigger t; | ||
89 | t.name = object.getName(); | ||
90 | t.pos.x() = object.getPosition().x; | ||
91 | t.pos.y() = object.getPosition().y; | ||
92 | t.size.w() = object.getAABB().width; | ||
93 | t.size.h() = object.getAABB().height; | ||
94 | |||
95 | for (const tmx::Property& property : object.getProperties()) { | ||
96 | if (property.getName() == "script") { | ||
97 | t.script = property.getStringValue(); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | triggers_.push_back(std::move(t)); | ||
87 | } | 102 | } |
88 | } | 103 | } |
89 | } | 104 | } |
diff --git a/src/map.h b/src/map.h index 7bfb15e..4c5d6d4 100644 --- a/src/map.h +++ b/src/map.h | |||
@@ -28,6 +28,13 @@ struct Prototype { | |||
28 | std::string interactionScript; | 28 | std::string interactionScript; |
29 | }; | 29 | }; |
30 | 30 | ||
31 | struct Trigger { | ||
32 | std::string name; | ||
33 | vec2i pos; | ||
34 | vec2i size; | ||
35 | std::string script; | ||
36 | }; | ||
37 | |||
31 | class Map { | 38 | class Map { |
32 | public: | 39 | public: |
33 | 40 | ||
@@ -51,6 +58,8 @@ public: | |||
51 | 58 | ||
52 | const vec2i& getWarpPoint(const std::string& name) const { return warpPoints_.at(name); } | 59 | const vec2i& getWarpPoint(const std::string& name) const { return warpPoints_.at(name); } |
53 | 60 | ||
61 | const std::vector<Trigger>& getTriggers() const { return triggers_; } | ||
62 | |||
54 | private: | 63 | private: |
55 | 64 | ||
56 | vec2i mapSize_; | 65 | vec2i mapSize_; |
@@ -60,6 +69,7 @@ private: | |||
60 | int tilesetColumns_; | 69 | int tilesetColumns_; |
61 | std::vector<Prototype> prototypes_; | 70 | std::vector<Prototype> prototypes_; |
62 | std::map<std::string, vec2i> warpPoints_; | 71 | std::map<std::string, vec2i> warpPoints_; |
72 | std::vector<Trigger> triggers_; | ||
63 | }; | 73 | }; |
64 | 74 | ||
65 | #endif /* end of include guard: MAP_H_D95D6D47 */ | 75 | #endif /* end of include guard: MAP_H_D95D6D47 */ |
diff --git a/src/renderer.cpp b/src/renderer.cpp index 0035ce2..87bbbcd 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -114,10 +114,12 @@ void Renderer::render(Game& game) { | |||
114 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); | 114 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); |
115 | 115 | ||
116 | for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY() | game.spriteView()) { | 116 | for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY() | game.spriteView()) { |
117 | const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).at(sprite.animationFrame)); | 117 | if (sprite.isAnimated) { |
118 | const SDL_Rect& src = frame.srcRect; | 118 | const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).at(sprite.animationFrame)); |
119 | SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; | 119 | const SDL_Rect& src = frame.srcRect; |
120 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.textureId).get(), &src, &dest); | 120 | SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; |
121 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.textureId).get(), &src, &dest); | ||
122 | } | ||
121 | } | 123 | } |
122 | 124 | ||
123 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); | 125 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); |
diff --git a/src/sprite.h b/src/sprite.h index b2cca87..4a65763 100644 --- a/src/sprite.h +++ b/src/sprite.h | |||
@@ -34,9 +34,11 @@ public: | |||
34 | // Transform | 34 | // Transform |
35 | vec2i loc { 0, 0 }; | 35 | vec2i loc { 0, 0 }; |
36 | bool collidable = false; | 36 | bool collidable = false; |
37 | bool solid = false; | ||
37 | vec2i collisionOffset; | 38 | vec2i collisionOffset; |
38 | vec2i collisionSize; | 39 | vec2i collisionSize; |
39 | std::string interactionScript; | 40 | std::string interactionScript; |
41 | std::string walkthroughScript; | ||
40 | 42 | ||
41 | // Animation | 43 | // Animation |
42 | bool isAnimated = false; | 44 | bool isAnimated = false; |
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 3d65acb..2ec133e 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp | |||
@@ -8,11 +8,12 @@ void TransformSystem::initSprite(int spriteId, vec2i loc) { | |||
8 | spritesByY_.emplace(loc.y(), spriteId); | 8 | spritesByY_.emplace(loc.y(), spriteId); |
9 | } | 9 | } |
10 | 10 | ||
11 | void TransformSystem::setUpCollision(int spriteId, vec2i offset, vec2i size) { | 11 | void TransformSystem::setUpCollision(int spriteId, vec2i offset, vec2i size, bool solid) { |
12 | Sprite& sprite = game_.getSprite(spriteId); | 12 | Sprite& sprite = game_.getSprite(spriteId); |
13 | sprite.collidable = true; | 13 | sprite.collidable = true; |
14 | sprite.collisionOffset = offset; | 14 | sprite.collisionOffset = offset; |
15 | sprite.collisionSize = size; | 15 | sprite.collisionSize = size; |
16 | sprite.solid = solid; | ||
16 | 17 | ||
17 | addCollidable(spriteId); | 18 | addCollidable(spriteId); |
18 | } | 19 | } |
@@ -74,9 +75,11 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
74 | it++) { | 75 | it++) { |
75 | if (newColDR.y() >= it->second.lower && | 76 | if (newColDR.y() >= it->second.lower && |
76 | newColUL.y() <= it->second.upper) { | 77 | newColUL.y() <= it->second.upper) { |
77 | result.horiz.blocked = true; | 78 | int colliderSpriteId = std::get<1>(it->first); |
79 | Sprite& collider = game_.getSprite(colliderSpriteId); | ||
80 | result.horiz.blocked = collider.solid; | ||
78 | result.horiz.dir = Direction::right; | 81 | result.horiz.dir = Direction::right; |
79 | result.horiz.colliderSprite = std::get<1>(it->first); | 82 | result.horiz.colliderSprite = colliderSpriteId; |
80 | 83 | ||
81 | break; | 84 | break; |
82 | } | 85 | } |
@@ -104,9 +107,11 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
104 | it++) { | 107 | it++) { |
105 | if (newColDR.y() >= it->second.lower && | 108 | if (newColDR.y() >= it->second.lower && |
106 | newColUL.y() <= it->second.upper) { | 109 | newColUL.y() <= it->second.upper) { |
107 | result.horiz.blocked = true; | 110 | int colliderSpriteId = std::get<1>(it->first); |
111 | Sprite& collider = game_.getSprite(colliderSpriteId); | ||
112 | result.horiz.blocked = collider.solid; | ||
108 | result.horiz.dir = Direction::left; | 113 | result.horiz.dir = Direction::left; |
109 | result.horiz.colliderSprite = std::get<1>(it->first); | 114 | result.horiz.colliderSprite = colliderSpriteId; |
110 | 115 | ||
111 | break; | 116 | break; |
112 | } | 117 | } |
@@ -134,9 +139,11 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
134 | it++) { | 139 | it++) { |
135 | if (newColDR.x() >= it->second.lower && | 140 | if (newColDR.x() >= it->second.lower && |
136 | newColUL.x() <= it->second.upper) { | 141 | newColUL.x() <= it->second.upper) { |
137 | result.vert.blocked = true; | 142 | int colliderSpriteId = std::get<1>(it->first); |
138 | result.vert.dir = Direction::down; | 143 | Sprite& collider = game_.getSprite(colliderSpriteId); |
139 | result.vert.colliderSprite = std::get<1>(it->first); | 144 | result.horiz.blocked = collider.solid; |
145 | result.horiz.dir = Direction::down; | ||
146 | result.horiz.colliderSprite = colliderSpriteId; | ||
140 | 147 | ||
141 | break; | 148 | break; |
142 | } | 149 | } |
@@ -164,9 +171,11 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
164 | it++) { | 171 | it++) { |
165 | if (newColDR.x() >= it->second.lower && | 172 | if (newColDR.x() >= it->second.lower && |
166 | newColUL.x() <= it->second.upper) { | 173 | newColUL.x() <= it->second.upper) { |
167 | result.vert.blocked = true; | 174 | int colliderSpriteId = std::get<1>(it->first); |
168 | result.vert.dir = Direction::up; | 175 | Sprite& collider = game_.getSprite(colliderSpriteId); |
169 | result.vert.colliderSprite = std::get<1>(it->first); | 176 | result.horiz.blocked = collider.solid; |
177 | result.horiz.dir = Direction::up; | ||
178 | result.horiz.colliderSprite = colliderSpriteId; | ||
170 | 179 | ||
171 | break; | 180 | break; |
172 | } | 181 | } |
diff --git a/src/transform_system.h b/src/transform_system.h index a7294ce..10e33db 100644 --- a/src/transform_system.h +++ b/src/transform_system.h | |||
@@ -31,7 +31,7 @@ public: | |||
31 | 31 | ||
32 | void initSprite(int spriteId, vec2i loc); | 32 | void initSprite(int spriteId, vec2i loc); |
33 | 33 | ||
34 | void setUpCollision(int spriteId, vec2i offset, vec2i size); | 34 | void setUpCollision(int spriteId, vec2i offset, vec2i size, bool solid); |
35 | 35 | ||
36 | void moveSprite(int spriteId, vec2i newLoc); | 36 | void moveSprite(int spriteId, vec2i newLoc); |
37 | 37 | ||