Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
WorldState.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <string>
5#include <unordered_map>
6#include <vector>
7
8#include "Orientation.hpp"
9#include "Resources.hpp"
10#include "VisualState.hpp"
11
15struct Player {
16 int id;
17 int x;
18 int y;
20 int level;
21 std::string team;
23 bool incanting = false;
24 bool incantationEnded = false;
25 bool incantationSuccess = false;
26 VisualState visual = {};
27};
28
29inline std::ostream& operator<<(std::ostream& os, const Player& player)
30{
31 os << "Player " << player.id << ":\n"
32 << " From team " << player.team << ", level " << player.level << "\n"
33 << " At " << player.x << ", " << player.y << ", facing " << player.orientation << "\n"
34 << " Inventory: " << player.inventory << "\n"
35 << " Incanting: " << (player.incanting ? "yes" : "no");
36 return os;
37}
38
42struct Egg {
43 int id;
44 int x;
45 int y;
46 std::string team;
47 float rotation = static_cast<float>(rand() % 360);
48 VisualState visual = {};
49};
50
51inline std::ostream& operator<<(std::ostream& os, const Egg& egg)
52{
53 os << "Egg " << egg.id << ":\n"
54 << " From team " << egg.team << "\n"
55 << " At " << egg.x << ", " << egg.y;
56 return os;
57}
58
64 public:
65 int width;
66 int height;
67 std::vector<Resources> tiles;
68 std::unordered_map<int, Player> players;
69 mutable std::unordered_map<int, Player> dyingPlayers;
70 std::unordered_map<int, Egg> eggs;
71 std::vector<std::string> teams;
72
76 Resources& at(int x, int y) { return tiles[y * width + x]; }
77
81 const Resources& at(int x, int y) const { return tiles[y * width + x]; }
82
86 bool playerExists(int id) const { return players.find(id) != players.end(); }
87
91 bool eggExists(int id) const { return eggs.find(id) != eggs.end(); }
92
93 void purgeDyingPlayers() const
94 {
95 for (auto it = dyingPlayers.begin(); it != dyingPlayers.end();)
96 it = it->second.visual.behaviors.empty() ? dyingPlayers.erase(it) : ++it;
97 }
98};
Orientation
std::ostream & operator<<(std::ostream &os, const Player &player)
Visual-only state for an entity, driven by behaviors each frame. Logical state (x,...
Represents the state of the world in the game, including the map size, tile contents,...
Resources & at(int x, int y)
Accesses the resources at a specific tile coordinate (x, y).
std::vector< std::string > teams
std::vector< Resources > tiles
std::unordered_map< int, Player > dyingPlayers
bool playerExists(int id) const
Checks if a player with the given ID exists in the world.
bool eggExists(int id) const
Checks if an egg with the given ID exists in the world.
std::unordered_map< int, Egg > eggs
std::unordered_map< int, Player > players
void purgeDyingPlayers() const
const Resources & at(int x, int y) const
Accesses the resources at a specific tile coordinate (x, y).
int y
int id
int x
Resources inventory
int level
Orientation orientation