From 1c462ef3780b33468ed93dde3ab6178765807ffe Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 5 Mar 2021 20:50:51 -0500 Subject: Added MirrorSystem This is really just for letting one sprite mirror another's movement and animation. I tried doing it in the BehaviourSystem, but you get stuttering if you do it earlier in the loop than the CharacterSystem, so I ended up having to make a new system just for this thing that will not happen very often. --- CMakeLists.txt | 1 + src/direction.h | 13 +++++++++++++ src/game.cpp | 5 +++++ src/main.cpp | 2 ++ src/map.cpp | 8 ++++++++ src/map.h | 3 +++ src/mirror_system.cpp | 29 +++++++++++++++++++++++++++++ src/mirror_system.h | 22 ++++++++++++++++++++++ src/sprite.h | 10 ++++++++++ src/system.h | 1 + 10 files changed, 94 insertions(+) create mode 100644 src/mirror_system.cpp create mode 100644 src/mirror_system.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f42260e..5427efc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,7 @@ add_executable(tanetane src/script_system.cpp src/effect_system.cpp src/behaviour_system.cpp + src/mirror_system.cpp ) set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) diff --git a/src/direction.h b/src/direction.h index 432f7a9..325bcaf 100644 --- a/src/direction.h +++ b/src/direction.h @@ -65,6 +65,19 @@ inline Direction oppositeDirection(Direction value) { } } +inline Direction directionMirroredVertically(Direction value) { + switch (value) { + case Direction::up: return Direction::down; + case Direction::up_right: return Direction::down_right; + case Direction::right: return Direction::right; + case Direction::down_right: return Direction::up_right; + case Direction::down: return Direction::up; + case Direction::down_left: return Direction::up_left; + case Direction::left: return Direction::left; + case Direction::up_left: return Direction::down_left; + } +} + inline Direction directionFacingPoint(vec2i point) { double theta = atan2(-point.y(), point.x()); theta /= M_PI; diff --git a/src/game.cpp b/src/game.cpp index 7e3c88f..bedd934 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -76,6 +76,11 @@ void Game::loadMap(std::string filename) { if (!p.enclosureZone.empty()) { getSprite(spriteId).enclosureZone = p.enclosureZone; } + if (p.mirrorType != MirrorType::None) { + getSprite(spriteId).mirrorType = p.mirrorType; + getSprite(spriteId).mirrorAxis = p.mirrorAxis; + getSprite(spriteId).mirroredSpriteId = getSpriteByAlias(p.spriteToMirror); + } } for (const Trigger& t : map_->getTriggers()) { diff --git a/src/main.cpp b/src/main.cpp index d0220fc..772fff8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ #include "script_system.h" #include "effect_system.h" #include "behaviour_system.h" +#include "mirror_system.h" void loop(Renderer& renderer, std::mt19937& rng) { Game game(renderer, rng); @@ -22,6 +23,7 @@ void loop(Renderer& renderer, std::mt19937& rng) { game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); + game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); game.emplaceSystem(); diff --git a/src/map.cpp b/src/map.cpp index 8d0ada6..32203e5 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -111,6 +111,14 @@ Map::Map(std::string_view name) : name_(name) { p.enclosureZone = property.getStringValue(); } else if (property.getName() == "movementSpeed") { p.movementSpeed = property.getIntValue(); + } else if (property.getName() == "mirror") { + if (property.getStringValue() == "vertical") { + p.mirrorType = MirrorType::Vertical; + } + } else if (property.getName() == "mirrorAxis") { + p.mirrorAxis = property.getIntValue(); + } else if (property.getName() == "mirrorSprite") { + p.spriteToMirror = property.getStringValue(); } } diff --git a/src/map.h b/src/map.h index 1167290..eb926c7 100644 --- a/src/map.h +++ b/src/map.h @@ -32,6 +32,9 @@ struct Prototype { int movementSpeed = -1; std::string enclosureZone; bool masked = false; + MirrorType mirrorType = MirrorType::None; + int mirrorAxis = 0; + std::string spriteToMirror; }; struct Trigger { diff --git a/src/mirror_system.cpp b/src/mirror_system.cpp new file mode 100644 index 0000000..b506e59 --- /dev/null +++ b/src/mirror_system.cpp @@ -0,0 +1,29 @@ +#include "mirror_system.h" +#include "game.h" +#include "transform_system.h" +#include "animation_system.h" +#include "vector.h" +#include "sprite.h" + +void MirrorSystem::tick(double dt) { + for (int spriteId : game_.getSprites()) { + Sprite& sprite = game_.getSprite(spriteId); + if (!sprite.paused) { + switch (sprite.mirrorType) { + case MirrorType::None: break; + case MirrorType::Vertical: { + Sprite& mirroredSprite = game_.getSprite(sprite.mirroredSpriteId); + + vec2i mirroredLoc = mirroredSprite.loc; + mirroredLoc.y() = 2 * sprite.mirrorAxis - mirroredLoc.y(); + + game_.getSystem().moveSprite(spriteId, mirroredLoc); + game_.getSystem().setSpriteAnimation(spriteId, mirroredSprite.animationName); + game_.getSystem().setSpriteDirection(spriteId, directionMirroredVertically(mirroredSprite.dir)); + + break; + } + } + } + } +} diff --git a/src/mirror_system.h b/src/mirror_system.h new file mode 100644 index 0000000..5549dc4 --- /dev/null +++ b/src/mirror_system.h @@ -0,0 +1,22 @@ +#ifndef MIRROR_SYSTEM_H_0BA755A8 +#define MIRROR_SYSTEM_H_0BA755A8 + +#include "system.h" + +class Game; + +class MirrorSystem : public System { +public: + + static constexpr SystemKey Key = SystemKey::Mirror; + + explicit MirrorSystem(Game& game) : game_(game) {} + + void tick(double dt) override; + +private: + + Game& game_; +}; + +#endif /* end of include guard: MIRROR_SYSTEM_H_0BA755A8 */ diff --git a/src/sprite.h b/src/sprite.h index 6538c1d..2ab306d 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -48,6 +48,11 @@ enum class BehaviourType { Path }; +enum class MirrorType { + None, + Vertical +}; + struct Movement { vec2i pos; Direction dir; @@ -123,6 +128,11 @@ public: vec2i pathfindingDestination; bool cardinalDirectionsOnly = false; std::deque path; + + // Mirror + MirrorType mirrorType = MirrorType::None; + int mirroredSpriteId = -1; + int mirrorAxis = 0; }; #endif /* end of include guard: SPRITE_H_70503825 */ diff --git a/src/system.h b/src/system.h index c0b0637..a129b3b 100644 --- a/src/system.h +++ b/src/system.h @@ -7,6 +7,7 @@ enum class SystemKey { Input, Behaviour, Character, + Mirror, Animation, Camera, Message, -- cgit 1.4.1