Skip to content

Initial implementation of load and save game; #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
699 changes: 586 additions & 113 deletions CMake/conan.cmake

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ if (MSVC)
add_compile_options(
# treat warnings as errors
/WX
/wd4996
)

add_link_options(
Expand Down
Binary file added notes/newart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions notes/strawberry-lake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Manufactring
# Distribution
# Laundering
# Legal


# Marijuana

* pots
* soil
* lights
- different types yield different results
* fans

# Cocaine

# Meth

# Heroin

# Crack

# Mushrooms

#
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(SOURCE_FILES
Background.cpp
DelayedSound.cpp
Engine.cpp
GameState.cpp
GameWorld.cpp
Intersection.cpp
Item.cpp
Expand Down Expand Up @@ -47,8 +48,9 @@ set(HEADER_FILES
Engine.h
Item.h
ItemFactory.h
GameWorld.h
GameState.h
GameTypes.h
GameWorld.h
Intersection.h
Path.hpp
PathFactory.h
Expand Down
43 changes: 43 additions & 0 deletions src/GameState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "TTUtils.h"
#include "GameState.h"

namespace tt
{

void from_json(const nl::json& j, GameState& gs)
{
j["player"].get_to(gs.playerstate);
j["location"].get_to(gs.location);
}

void to_json(nl::json& j, const GameState& gs)
{
j["player"] = gs.playerstate;
j["location"] = gs.location;
}

void from_json(const nl::json& j, PlayerLocation& pl)
{
j["map"].get_to(pl.map);
j["cords"].get_to(pl.cords);
}

void to_json(nl::json& j, const PlayerLocation& pl)
{
j["map"] = pl.map;
j["cords"] = pl.cords;
}

void from_json(const nl::json& j, PlayerState& ps)
{
j["health"].get_to(ps.health);
j["cash"].get_to(ps.cash);
}

void to_json(nl::json& j, const PlayerState& ps)
{
j["health"] = ps.health;
j["cash"] = ps.cash;
}

} // namespace tt
47 changes: 47 additions & 0 deletions src/GameState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma

#include <string>

#include <SFML/System/Vector2.hpp>

#include <nlohmann/json.hpp>

#include "Player.h"
#include "TTUtils.h"

namespace tt
{

struct GameState;
using GameStatePtr = std::shared_ptr<GameState>;

void from_json(const nl::json& j, GameState& pl);
void to_json(nl::json& j, const GameState& b);

struct PlayerLocation;
void from_json(const nl::json& j, PlayerLocation& pl);
void to_json(nl::json& j, const PlayerLocation& b);

struct PlayerState;
void from_json(const nl::json& j, PlayerState& pl);
void to_json(nl::json& j, const PlayerState& b);

struct PlayerLocation
{
std::string map;
sf::Vector2f cords;
};

struct PlayerState
{
std::uint32_t health = 0;
float cash = 0.f;
};

struct GameState
{
PlayerState playerstate;
PlayerLocation location;
};

} // namespace tt
9 changes: 9 additions & 0 deletions src/Item.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ struct ItemCallbacks
struct ItemInstanceInfo
{
std::string objid; // the id of the object this is an instance of
std::optional<std::uint32_t> index; // index of the object in an "instances" array

// a null x,y means that the coordinate was not specified,
// and a value of -1 means it should be picked randomly
Expand All @@ -112,6 +113,14 @@ struct ItemInstanceInfo

ItemCallbacks callbacks;

ItemInstanceInfo() = default;
ItemInstanceInfo(std::uint32_t idx)
: index{idx}
{
// nothing to do
}


// will apply the default values if they are set in the `defaults` object
// and *not* set in this object.
// NOTE: We cannot use a constructor since these objects are constructed
Expand Down
10 changes: 9 additions & 1 deletion src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
namespace tt
{

void from_json(const nl::json & j, Player & i)
void from_json(const nl::json& j, Player& p)
{
j["health"].get_to(p._health);
j["cash"].get_to(p._cash);
}

void to_json(nl::json& j, const Player& p)
{
j["health"] = p.health();
j["cash"] = p.balance();
}

namespace
Expand Down
4 changes: 4 additions & 0 deletions src/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ namespace tt

class Player;
using PlayerPtr = std::shared_ptr<Player>;

void from_json(const nl::json& j, Player& i);
void to_json(nl::json& j, const Player& p);

class Player : public Item
{
public:
static constexpr auto CLASS_NAME = "Player";
static const struct luaL_Reg LuaMethods[];

friend void from_json(const nl::json& j, Player& i);

using Item::Item;

sf::Vector2f getGlobalCenter() const;
Expand Down
8 changes: 5 additions & 3 deletions src/Scenes/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,12 +724,14 @@ void Scene::createItems()
ItemInstanceInfo groupinfo = data.get<ItemInstanceInfo>();
groupinfo.objid = itemid; // we don't want to require the objid to be set in json

for (const auto& instance : data["instances"])
for (const auto& instance : (data["instances"] | boost::adaptors::indexed()))
{
auto instanceinfo = instance.get<ItemInstanceInfo>();
auto instanceinfo = instance.value().get<ItemInstanceInfo>();
instanceinfo.index = static_cast<std::uint32_t>(instance.index());

instanceinfo.applyDefaults(groupinfo);

auto groupcallbacks = instance.get<ItemCallbacks>();
auto groupcallbacks = instance.value().get<ItemCallbacks>();
auto item = _itemFactory.createItem(instanceinfo);
if (item) placeItem(item);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Screens/GameScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../GameWorld.h"
#include "../Settings.h"
#include "../AudioService.h"
#include "../GameState.h"

#include "Screen.h"

Expand Down Expand Up @@ -221,6 +222,7 @@ class GameScreen final : public Screen

sf::Clock _gameClock;
std::shared_ptr<GameWorld> _gameCalendar;
tt::GameState _gameState;

std::shared_ptr<Hud> _hud;
};
Expand Down
Loading