From f924cfea72e224d83d37f12376fa043d547aa901 Mon Sep 17 00:00:00 2001 From: Mab-Nig Date: Sun, 7 Jan 2024 23:26:55 +0700 Subject: [PATCH] Fix player trapped when spawn. --- include/entity/Field.hpp | 14 ++-- include/map_generator/FieldProperties.hpp | 4 +- include/map_generator/MapGenerator.hpp | 7 +- include/map_renderer/MapRenderer.hpp | 8 +-- src/entity/Field.cpp | 46 ++++++++----- src/map_generator/FieldProperties.cpp | 34 +++------- src/map_generator/MapGenerator.cpp | 25 +++++--- src/map_renderer/MapRenderer.cpp | 78 ++++++++++++++--------- 8 files changed, 120 insertions(+), 96 deletions(-) diff --git a/include/entity/Field.hpp b/include/entity/Field.hpp index 57e101d..ff22cf9 100644 --- a/include/entity/Field.hpp +++ b/include/entity/Field.hpp @@ -3,10 +3,7 @@ #include "SFML/System/Vector2.hpp" -#include "SFML/System/Vector2.hpp" - -#include -#include +#include #include #include #include @@ -14,14 +11,17 @@ #include #include #include -#include +#include +#include -class Field : public Lane { +class Field : public Lane +{ public: typedef std::vector> Greens; public: - Field(TextureManager *textures, bool isReverse, const Greens& greenSlots, bool isLoad = false); + Field(TextureManager *textures, bool isReverse, const Greens &greenSlots, + bool isLoad = false); Field(TextureManager *textures, bool isReverse, bool isLoad = false); void add(std::unique_ptr green, unsigned int index); diff --git a/include/map_generator/FieldProperties.hpp b/include/map_generator/FieldProperties.hpp index 868ca0c..f65ca6c 100644 --- a/include/map_generator/FieldProperties.hpp +++ b/include/map_generator/FieldProperties.hpp @@ -15,7 +15,8 @@ class FieldProperties : public LaneProperties typedef std::vector> Greens; FieldProperties(unsigned int map_width, unsigned int level, - LaneProperties const *prev_lane, bool initializing_p); + LaneProperties const *prev_lane, bool initializing_p, + bool spawn_lane_p); virtual Lane::Type getType() const override; Greens const &getGreens() const; @@ -29,6 +30,7 @@ class FieldProperties : public LaneProperties LaneProperties const *m_prev_lane; bool const m_initialing_p; + bool const m_spawn_lane_p; Greens m_greens; }; diff --git a/include/map_generator/MapGenerator.hpp b/include/map_generator/MapGenerator.hpp index ae9f29e..99682e6 100644 --- a/include/map_generator/MapGenerator.hpp +++ b/include/map_generator/MapGenerator.hpp @@ -14,7 +14,7 @@ class MapGenerator : public sf::NonCopyable MapGenerator(unsigned int map_width, unsigned int map_max_height, unsigned int level); - void moveView(bool initializing_p); + void moveView(bool initializing_p, bool spawn_lane_p); LaneProperties const &getPrevLane() const; LaneProperties const &getCurrLane() const; @@ -28,12 +28,13 @@ class MapGenerator : public sf::NonCopyable void updateContext(bool initializing_p); std::unique_ptr - generateLaneProperties(bool initializing_p) const; + generateLaneProperties(bool initializing_p, bool spawn_lane_p) const; Lane::Type generateLaneType(bool initializing_p) const; std::unique_ptr - createLanePropertiesWithType(Lane::Type type, bool initializing_p) const; + createLanePropertiesWithType(Lane::Type type, bool initializing_p, + bool spawn_lane_p) const; unsigned int getRealLevel() const; diff --git a/include/map_renderer/MapRenderer.hpp b/include/map_renderer/MapRenderer.hpp index cd9162b..52257c4 100644 --- a/include/map_renderer/MapRenderer.hpp +++ b/include/map_renderer/MapRenderer.hpp @@ -27,17 +27,17 @@ class MapRenderer : public sf::NonCopyable unsigned int map_width, unsigned int map_max_height, unsigned int level, bool isLoad = false); - Lane* createNewLane(); + Lane *createNewLane(); void popLane(); LaneList const &getLanes() const; - void loadLanes(std::ifstream& outf); - void saveLanes(std::ofstream& inf); + void loadLanes(std::ifstream &outf); + void saveLanes(std::ofstream &inf); int getLevel() const; private: void initialize(); - void pushLane(bool initializing_p); + void pushLane(bool initializing_p, bool spawn_lane_p); Lane *convertPropertiesToLane(LaneProperties const &properties) const; diff --git a/src/entity/Field.cpp b/src/entity/Field.cpp index 567296e..ec726c8 100644 --- a/src/entity/Field.cpp +++ b/src/entity/Field.cpp @@ -2,13 +2,15 @@ #include #include -#include #include +#include -Field::Field(TextureManager *textures, bool isReverse, const Greens& greenSlots, bool isLoad) - : Lane(textures->get(TextureID::Field), textures, isReverse), greenSlots(greenSlots) +Field::Field(TextureManager *textures, bool isReverse, const Greens &greenSlots, + bool isLoad) + : Lane(textures->get(TextureID::Field), textures, isReverse), + greenSlots(greenSlots) { - AppConfig& config = AppConfig::getInstance(); + AppConfig &config = AppConfig::getInstance(); sf::Vector2f cellSize = config.get(ConfigKey::CellSize); float laneLength = cellSize.x * greenSlots.size(); setSize(sf::Vector2f(laneLength, cellSize.y)); @@ -24,7 +26,9 @@ Field::Field(TextureManager *textures, bool isReverse, const Greens& greenSlots, } Field::Field(TextureManager *textures, bool isReverse, bool isLoad) - : Field(textures, isReverse, {}, isLoad) {} + : Field(textures, isReverse, {}, isLoad) +{ +} // buildLane is for initialization (it is called in constructor) // add is for later processes @@ -39,12 +43,15 @@ void Field::add(std::unique_ptr green, unsigned int index) bool Field::spawnPlayer(ViewGroup::Ptr player) { std::vector markSlots(seqZone->getNumZone(), false); - for (const auto& slot : greenSlots) { + for (const auto &slot : greenSlots) + { markSlots[slot.first] = true; } std::vector emptySlots; - for (int i = 0; i < markSlots.size(); i++) { - if (!markSlots[i]) { + for (int i = 0; i < markSlots.size(); i++) + { + if (!markSlots[i]) + { emptySlots.push_back(i); } } @@ -57,8 +64,10 @@ void Field::updateCurrent(sf::Time dt) // currently update nothing because bushes don't move } -void Field::buildLane() { - for (auto const &[index, green_type] : greenSlots) { +void Field::buildLane() +{ + for (auto const &[index, green_type] : greenSlots) + { auto green = std::make_unique(green_type, *laneTextures); add(std::move(green), index); } @@ -81,17 +90,21 @@ void Field::saveLaneData(std::ofstream &outf) if (outf.is_open()) { int castedType = static_cast(type); - outf.write(reinterpret_cast(&castedType), sizeof(castedType)); - outf.write(reinterpret_cast(&isReverse), sizeof(isReverse)); - + outf.write(reinterpret_cast(&castedType), + sizeof(castedType)); + outf.write(reinterpret_cast(&isReverse), + sizeof(isReverse)); + int numGreen = greenSlots.size(); outf.write(reinterpret_cast(&numGreen), sizeof(numGreen)); - for (const auto &green : greenSlots) { + for (const auto &green : greenSlots) + { int index = green.first; int greenType = static_cast(green.second); outf.write(reinterpret_cast(&index), sizeof(index)); - outf.write(reinterpret_cast(&greenType), sizeof(greenType)); + outf.write(reinterpret_cast(&greenType), + sizeof(greenType)); } } else @@ -106,7 +119,8 @@ void Field::loadLaneData(std::ifstream &inf) { int numGreen; inf.read(reinterpret_cast(&numGreen), sizeof(numGreen)); - for (int i = 0; i < numGreen; i++) { + for (int i = 0; i < numGreen; i++) + { int index; int greenType; inf.read(reinterpret_cast(&index), sizeof(index)); diff --git a/src/map_generator/FieldProperties.cpp b/src/map_generator/FieldProperties.cpp index 77cb5f9..a07c21d 100644 --- a/src/map_generator/FieldProperties.cpp +++ b/src/map_generator/FieldProperties.cpp @@ -2,16 +2,17 @@ #include "Field.hpp" #include "LaneProperties.hpp" -#include +#include #include FieldProperties::FieldProperties(unsigned int map_width, unsigned int level, LaneProperties const *prev_lane, - bool initializing_p) + bool initializing_p, bool spawn_lane_p) : LaneProperties(map_width, level), m_prev_lane(prev_lane), - m_initialing_p(initializing_p) + m_initialing_p(initializing_p), + m_spawn_lane_p(spawn_lane_p) { } @@ -27,29 +28,14 @@ FieldProperties::Greens const &FieldProperties::getGreens() const void FieldProperties::generate() { - // unsigned int green_cnt = MyRandom::random_range(0, m_width / 2); - - // unsigned int field_slot = generateFieldSlot(); - - // unsigned int lbound = 0; - // unsigned int rbound = m_width - green_cnt - (field_slot < m_width); - // for (int i = 1; i <= green_cnt; ++i) - // { - // unsigned int green_type = MyRandom::random_range( - // 0, static_cast(Green::Type::Count) - 1); - // unsigned int green_slot = MyRandom::random_range(lbound, rbound); - // m_greens.emplace_back(green_slot, static_cast(green_type)); - - // lbound = green_slot + 1; - // ++rbound; - // } - // for (auto &[index, green] : m_greens) - // { - // index += (index >= field_slot); - // } + if (m_spawn_lane_p) + { + return; + } unsigned int green_cnt = MyRandom::random_range(0, m_width / 2); - std::vector green_slots = MyRandom::sample(green_cnt, m_width); + std::vector green_slots = + MyRandom::sample(green_cnt, m_width); std::sort(green_slots.begin(), green_slots.end()); for (auto slot : green_slots) { diff --git a/src/map_generator/MapGenerator.cpp b/src/map_generator/MapGenerator.cpp index 959c110..c213f10 100644 --- a/src/map_generator/MapGenerator.cpp +++ b/src/map_generator/MapGenerator.cpp @@ -6,8 +6,8 @@ #include "RailwayProperties.hpp" #include "RiverProperties.hpp" #include "RoadProperties.hpp" -#include +#include #include MapGenerator::MapGenerator(unsigned int map_width, unsigned int map_max_height, @@ -21,10 +21,10 @@ MapGenerator::MapGenerator(unsigned int map_width, unsigned int map_max_height, { } -void MapGenerator::moveView(bool initializing_p) +void MapGenerator::moveView(bool initializing_p, bool spawn_lane_p) { m_prev_lane = std::move(m_curr_lane); - m_curr_lane = generateLaneProperties(initializing_p); + m_curr_lane = generateLaneProperties(initializing_p, spawn_lane_p); updateContext(initializing_p); } @@ -39,11 +39,13 @@ LaneProperties const &MapGenerator::getCurrLane() const } std::unique_ptr -MapGenerator::generateLaneProperties(bool initializing_p) const +MapGenerator::generateLaneProperties(bool initializing_p, + bool spawn_lane_p) const { Lane::Type type = generateLaneType(initializing_p); std::unique_ptr lane_properties = - MapGenerator::createLanePropertiesWithType(type, initializing_p); + MapGenerator::createLanePropertiesWithType(type, initializing_p, + spawn_lane_p); lane_properties->create(); return lane_properties; } @@ -77,16 +79,17 @@ Lane::Type MapGenerator::generateLaneType(bool initializing_p) const } std::unique_ptr -MapGenerator::createLanePropertiesWithType(Lane::Type type, - bool initializing_p) const +MapGenerator::createLanePropertiesWithType(Lane::Type type, bool initializing_p, + bool spawn_lane_p) const { unsigned int real_level = getRealLevel(); switch (type) { case Lane::Type::Field: { - return std::make_unique( - m_width, real_level, m_prev_lane.get(), initializing_p); + return std::make_unique(m_width, real_level, + m_prev_lane.get(), + initializing_p, spawn_lane_p); } case Lane::Type::Railway: @@ -119,7 +122,9 @@ unsigned int MapGenerator::getRealLevel() const { GameActivity::GameLevel real_level = static_cast(m_level); - if (static_cast(m_level) == GameActivity::GameLevel::Endless) { + if (static_cast(m_level) + == GameActivity::GameLevel::Endless) + { real_level = (m_level_lanes_cnts[0] < ENDLESS_LEVEL_LANES_CNT[0] ? GameActivity::GameLevel::Easy : (m_level_lanes_cnts[1] < ENDLESS_LEVEL_LANES_CNT[1] diff --git a/src/map_renderer/MapRenderer.cpp b/src/map_renderer/MapRenderer.cpp index bd91b60..7da4b18 100644 --- a/src/map_renderer/MapRenderer.cpp +++ b/src/map_renderer/MapRenderer.cpp @@ -25,14 +25,16 @@ MapRenderer::MapRenderer(TextureManager &textures, ViewGroup &foreground, m_max_height(m_sizes.y), m_level(level) { - if (!isLoad) + if (!isLoad) + { initialize(); + } } -Lane* MapRenderer::createNewLane() +Lane *MapRenderer::createNewLane() { // popLane(); - pushLane(0); + pushLane(0, 0); return m_lanes.back(); } @@ -47,36 +49,42 @@ void MapRenderer::initialize() std::make_unique(m_width, m_max_height, m_level); for (int i = 0; i < m_max_height; ++i) { - pushLane(1); + pushLane(1, (i == 3)); } } -void MapRenderer::pushLane(bool initializing_p) +void MapRenderer::pushLane(bool initializing_p, bool spawn_lane_p) { - m_map_generator->moveView(initializing_p); - Lane* newLane = convertPropertiesToLane(m_map_generator->getCurrLane()); - if (!initializing_p) { - for (auto lane : m_lanes) { + m_map_generator->moveView(initializing_p, spawn_lane_p); + Lane *newLane = convertPropertiesToLane(m_map_generator->getCurrLane()); + if (!initializing_p) + { + for (auto lane : m_lanes) + { lane->move(sf::Vector2f(0.f, newLane->getSize().y)); } } m_lanes.push_back(newLane); } -void MapRenderer::saveLanes(std::ofstream& outf) { +void MapRenderer::saveLanes(std::ofstream &outf) +{ outf.write(reinterpret_cast(&m_level), sizeof(m_level)); int laneSize = m_lanes.size(); outf.write(reinterpret_cast(&laneSize), sizeof(laneSize)); - if (m_lanes.size() > 0) { - for (auto it = m_lanes.begin(); it != m_lanes.end(); ++it) { + if (m_lanes.size() > 0) + { + for (auto it = m_lanes.begin(); it != m_lanes.end(); ++it) + { Lane *lane = *it; lane->saveLaneData(outf); } } } -void MapRenderer::loadLanes(std::ifstream& inf) { +void MapRenderer::loadLanes(std::ifstream &inf) +{ inf.read(reinterpret_cast(&m_level), sizeof(m_level)); m_map_generator = @@ -89,24 +97,29 @@ void MapRenderer::loadLanes(std::ifstream& inf) { int laneType; bool laneIsReverse; inf.read(reinterpret_cast(&laneType), sizeof(laneType)); - inf.read(reinterpret_cast(&laneIsReverse), sizeof(laneIsReverse)); + inf.read(reinterpret_cast(&laneIsReverse), + sizeof(laneIsReverse)); // std::unique_ptr lane; - Lane* lane; + Lane *lane; switch (static_cast(laneType)) { case Lane::Type::Road: float animalVelocity, vehicleVelocity; bool hasAnimal, hasVehicle; - inf.read(reinterpret_cast(&animalVelocity), sizeof(animalVelocity)); - inf.read(reinterpret_cast(&vehicleVelocity), sizeof(vehicleVelocity)); + inf.read(reinterpret_cast(&animalVelocity), + sizeof(animalVelocity)); + inf.read(reinterpret_cast(&vehicleVelocity), + sizeof(vehicleVelocity)); inf.read(reinterpret_cast(&hasAnimal), sizeof(hasAnimal)); inf.read(reinterpret_cast(&hasVehicle), sizeof(hasVehicle)); - lane = new Road(&m_textures, laneIsReverse, animalVelocity, vehicleVelocity, hasAnimal, hasVehicle, true); + lane = new Road(&m_textures, laneIsReverse, animalVelocity, + vehicleVelocity, hasAnimal, hasVehicle, true); lane->loadLaneData(inf); break; case Lane::Type::River: float logVelocity; - inf.read(reinterpret_cast(&logVelocity), sizeof(logVelocity)); + inf.read(reinterpret_cast(&logVelocity), + sizeof(logVelocity)); lane = new River(&m_textures, laneIsReverse, logVelocity, true); lane->loadLaneData(inf); break; @@ -116,10 +129,13 @@ void MapRenderer::loadLanes(std::ifstream& inf) { break; case Lane::Type::Railway: float trainInterval, trainDelay, trainOffSet; - inf.read(reinterpret_cast(&trainInterval), sizeof(trainInterval)); - inf.read(reinterpret_cast(&trainDelay), sizeof(trainDelay)); - inf.read(reinterpret_cast(&trainOffSet), sizeof(trainOffSet)); - lane = new Railway(&m_textures, laneIsReverse, trainInterval, trainDelay, trainOffSet, true); + inf.read(reinterpret_cast(&trainInterval), + sizeof(trainInterval)); + inf.read(reinterpret_cast(&trainDelay), sizeof(trainDelay)); + inf.read(reinterpret_cast(&trainOffSet), + sizeof(trainOffSet)); + lane = new Railway(&m_textures, laneIsReverse, trainInterval, + trainDelay, trainOffSet, true); lane->loadLaneData(inf); break; default: @@ -169,18 +185,18 @@ MapRenderer::convertPropertiesToLane(FieldProperties const &properties) const Railway * MapRenderer::convertPropertiesToLane(RailwayProperties const &properties) const { - return new Railway(&m_textures, properties.isReverse(), - properties.getTrainInterval(), properties.getTrainDelay(), properties.getTrainOffSet()); + return new Railway(&m_textures, properties.isReverse(), + properties.getTrainInterval(), + properties.getTrainDelay(), properties.getTrainOffSet()); } Road * MapRenderer::convertPropertiesToLane(RoadProperties const &properties) const { - auto road = new Road( - &m_textures, properties.isReverse(), - properties.getVehicleVelocity(), properties.getAnimalVelocity(), - properties.getHasVehicle(), properties.getHasAnimal() - ); + auto road = new Road(&m_textures, properties.isReverse(), + properties.getVehicleVelocity(), + properties.getAnimalVelocity(), + properties.getHasVehicle(), properties.getHasAnimal()); return road; } @@ -196,4 +212,4 @@ MapRenderer::convertPropertiesToLane(RiverProperties const &properties) const int MapRenderer::getLevel() const { return m_level; -} \ No newline at end of file +}