Skip to content

Commit

Permalink
Fix player trapped when spawn.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mab-Nig committed Jan 7, 2024
1 parent 62164f0 commit f924cfe
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 96 deletions.
14 changes: 7 additions & 7 deletions include/entity/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@

#include "SFML/System/Vector2.hpp"

#include "SFML/System/Vector2.hpp"

#include <fstream>
#include <iostream>
#include <FieldProperties.hpp>
#include <Green.hpp>
#include <Lane.hpp>
#include <ResourceID.hpp>
#include <ResourceManager.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <FieldProperties.hpp>
#include <fstream>
#include <iostream>

class Field : public Lane {
class Field : public Lane
{
public:
typedef std::vector<std::pair<unsigned int, Green::Type>> 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> green, unsigned int index);
Expand Down
4 changes: 3 additions & 1 deletion include/map_generator/FieldProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class FieldProperties : public LaneProperties
typedef std::vector<std::pair<unsigned int, Green::Type>> 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;
Expand All @@ -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;
};

Expand Down
7 changes: 4 additions & 3 deletions include/map_generator/MapGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,12 +28,13 @@ class MapGenerator : public sf::NonCopyable
void updateContext(bool initializing_p);

std::unique_ptr<LaneProperties>
generateLaneProperties(bool initializing_p) const;
generateLaneProperties(bool initializing_p, bool spawn_lane_p) const;

Lane::Type generateLaneType(bool initializing_p) const;

std::unique_ptr<LaneProperties>
createLanePropertiesWithType(Lane::Type type, bool initializing_p) const;
createLanePropertiesWithType(Lane::Type type, bool initializing_p,
bool spawn_lane_p) const;

unsigned int getRealLevel() const;

Expand Down
8 changes: 4 additions & 4 deletions include/map_renderer/MapRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
46 changes: 30 additions & 16 deletions src/entity/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include <AppConfig.hpp>
#include <Field.hpp>
#include <RectangleView.hpp>
#include <MyRandom.hpp>
#include <RectangleView.hpp>

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<sf::Vector2f>(ConfigKey::CellSize);
float laneLength = cellSize.x * greenSlots.size();
setSize(sf::Vector2f(laneLength, cellSize.y));
Expand All @@ -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
Expand All @@ -39,12 +43,15 @@ void Field::add(std::unique_ptr<Green> green, unsigned int index)
bool Field::spawnPlayer(ViewGroup::Ptr player)
{
std::vector<bool> markSlots(seqZone->getNumZone(), false);
for (const auto& slot : greenSlots) {
for (const auto &slot : greenSlots)
{
markSlots[slot.first] = true;
}
std::vector<int> 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);
}
}
Expand All @@ -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>(green_type, *laneTextures);
add(std::move(green), index);
}
Expand All @@ -81,17 +90,21 @@ void Field::saveLaneData(std::ofstream &outf)
if (outf.is_open())
{
int castedType = static_cast<int>(type);
outf.write(reinterpret_cast<const char *>(&castedType), sizeof(castedType));
outf.write(reinterpret_cast<const char *>(&isReverse), sizeof(isReverse));

outf.write(reinterpret_cast<const char *>(&castedType),
sizeof(castedType));
outf.write(reinterpret_cast<const char *>(&isReverse),
sizeof(isReverse));

int numGreen = greenSlots.size();
outf.write(reinterpret_cast<const char *>(&numGreen), sizeof(numGreen));

for (const auto &green : greenSlots) {
for (const auto &green : greenSlots)
{
int index = green.first;
int greenType = static_cast<int>(green.second);
outf.write(reinterpret_cast<const char *>(&index), sizeof(index));
outf.write(reinterpret_cast<const char *>(&greenType), sizeof(greenType));
outf.write(reinterpret_cast<const char *>(&greenType),
sizeof(greenType));
}
}
else
Expand All @@ -106,7 +119,8 @@ void Field::loadLaneData(std::ifstream &inf)
{
int numGreen;
inf.read(reinterpret_cast<char *>(&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<char *>(&index), sizeof(index));
Expand Down
34 changes: 10 additions & 24 deletions src/map_generator/FieldProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

#include "Field.hpp"
#include "LaneProperties.hpp"
#include <MyRandom.hpp>

#include <MyRandom.hpp>
#include <climits>

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)
{
}

Expand All @@ -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<unsigned int>(Green::Type::Count) - 1);
// unsigned int green_slot = MyRandom::random_range(lbound, rbound);
// m_greens.emplace_back(green_slot, static_cast<Green::Type>(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<unsigned int> green_slots = MyRandom::sample(green_cnt, m_width);
std::vector<unsigned int> green_slots =
MyRandom::sample(green_cnt, m_width);
std::sort(green_slots.begin(), green_slots.end());
for (auto slot : green_slots)
{
Expand Down
25 changes: 15 additions & 10 deletions src/map_generator/MapGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include "RailwayProperties.hpp"
#include "RiverProperties.hpp"
#include "RoadProperties.hpp"
#include <MyRandom.hpp>

#include <MyRandom.hpp>
#include <memory>

MapGenerator::MapGenerator(unsigned int map_width, unsigned int map_max_height,
Expand All @@ -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);
}

Expand All @@ -39,11 +39,13 @@ LaneProperties const &MapGenerator::getCurrLane() const
}

std::unique_ptr<LaneProperties>
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<LaneProperties> lane_properties =
MapGenerator::createLanePropertiesWithType(type, initializing_p);
MapGenerator::createLanePropertiesWithType(type, initializing_p,
spawn_lane_p);
lane_properties->create();
return lane_properties;
}
Expand Down Expand Up @@ -77,16 +79,17 @@ Lane::Type MapGenerator::generateLaneType(bool initializing_p) const
}

std::unique_ptr<LaneProperties>
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<FieldProperties>(
m_width, real_level, m_prev_lane.get(), initializing_p);
return std::make_unique<FieldProperties>(m_width, real_level,
m_prev_lane.get(),
initializing_p, spawn_lane_p);
}

case Lane::Type::Railway:
Expand Down Expand Up @@ -119,7 +122,9 @@ unsigned int MapGenerator::getRealLevel() const
{
GameActivity::GameLevel real_level =
static_cast<GameActivity::GameLevel>(m_level);
if (static_cast<GameActivity::GameLevel>(m_level) == GameActivity::GameLevel::Endless) {
if (static_cast<GameActivity::GameLevel>(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]
Expand Down
Loading

0 comments on commit f924cfe

Please sign in to comment.