Rehti MMORPG  1.0.0
Rehti MMORPG is a free and open source MMORPG game.
GameWorld.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <optional>
5 #include <vector>
6 
7 #include "../entity/Npc.hpp"
8 #include "../entity/PlayerCharacter.hpp"
9 #include "../object/Object.hpp"
10 #include "Map.hpp"
11 
15 class GameWorld
16 {
17 
18 public:
19  GameWorld();
20 
21  ~GameWorld() = default;
22 
23  std::vector<std::shared_ptr<PlayerCharacter>>& getPlayers();
24 
25  std::shared_ptr<PlayerCharacter> getPlayer(unsigned int playerId);
26 
27  Map& getMap();
28 
39  void addPlayer(std::string playerName, unsigned int playerId, int baseDamage, int baseAccuracy, SpawnCoordinateBounds spawnCoordinateBounds, Coordinates location);
40 
47  bool removePlayer(unsigned int playerId);
48 
54  void addNpc(Npc npc);
55 
61  std::vector<std::shared_ptr<Npc>>& getNpcs();
62 
69  std::shared_ptr<Npc> getNpc(unsigned int npcId);
70 
77  std::shared_ptr<Entity> getEntity(unsigned int entityId);
78 
84  std::map<std::string, std::shared_ptr<Object>>& getObjects();
85 
91  std::map<Coordinates, std::vector<std::shared_ptr<Item>>>& getItems();
92 
99  void addItem(Coordinates location, std::shared_ptr<Item>);
100 
106  std::shared_ptr<Item> removeItem(Coordinates location, int itemId);
107 
111  void updateGameWorld();
112 
116  void initWorld();
117 
118  std::mutex& getPlayersMutex();
119 
120  std::mutex& getItemsMutex();
121 
122 private:
123  std::vector<std::shared_ptr<PlayerCharacter>> playersM;
124  std::mutex playersMutexM;
125 
126  std::vector<std::shared_ptr<Npc>> npcsM;
127 
128  std::map<std::string, std::shared_ptr<Object>> objectsM;
129 
130  std::map<Coordinates, std::vector<std::shared_ptr<Item>>> itemsM;
131  std::mutex itemsMutexM;
132 
134 };
GameWorld class. It contains all the player, npc, object and item instances.
Definition: GameWorld.hpp:16
~GameWorld()=default
void addNpc(Npc npc)
Adds npc to the game world.
Definition: GameWorld.cpp:60
void addPlayer(std::string playerName, unsigned int playerId, int baseDamage, int baseAccuracy, SpawnCoordinateBounds spawnCoordinateBounds, Coordinates location)
Adds player to the game world.
Definition: GameWorld.cpp:16
bool removePlayer(unsigned int playerId)
Removes player from the game world.
Definition: GameWorld.cpp:21
std::vector< std::shared_ptr< PlayerCharacter > > playersM
Definition: GameWorld.hpp:123
std::vector< std::shared_ptr< Npc > > & getNpcs()
Get all npcs.
Definition: GameWorld.cpp:65
std::mutex & getPlayersMutex()
Definition: GameWorld.cpp:229
std::mutex itemsMutexM
Definition: GameWorld.hpp:131
std::shared_ptr< PlayerCharacter > getPlayer(unsigned int playerId)
Definition: GameWorld.cpp:41
std::shared_ptr< Entity > getEntity(unsigned int entityId)
Get entity by id.
Definition: GameWorld.cpp:210
GameWorld()
Definition: GameWorld.cpp:14
void addItem(Coordinates location, std::shared_ptr< Item >)
Add item to the game world.
Definition: GameWorld.cpp:93
std::mutex & getItemsMutex()
Definition: GameWorld.cpp:234
std::map< std::string, std::shared_ptr< Object > > objectsM
Definition: GameWorld.hpp:128
void initWorld()
Initializes the game world. Loads assets and map. Adds npcs and objects to the world.
Definition: GameWorld.cpp:137
std::map< Coordinates, std::vector< std::shared_ptr< Item > > > itemsM
Definition: GameWorld.hpp:130
std::map< std::string, std::shared_ptr< Object > > & getObjects()
Get all objects.
Definition: GameWorld.cpp:83
std::vector< std::shared_ptr< PlayerCharacter > > & getPlayers()
Definition: GameWorld.cpp:36
std::shared_ptr< Npc > getNpc(unsigned int npcId)
Get npc by id.
Definition: GameWorld.cpp:70
Map mapM
Definition: GameWorld.hpp:133
std::map< Coordinates, std::vector< std::shared_ptr< Item > > > & getItems()
Get all items.
Definition: GameWorld.cpp:88
Map & getMap()
Definition: GameWorld.cpp:55
std::shared_ptr< Item > removeItem(Coordinates location, int itemId)
Remove item from the game world.
Definition: GameWorld.cpp:106
void updateGameWorld()
Updates the game world. Updates all the npcs and players.
Definition: GameWorld.cpp:124
std::mutex playersMutexM
Definition: GameWorld.hpp:124
std::vector< std::shared_ptr< Npc > > npcsM
Definition: GameWorld.hpp:126
Static class representing the map of the game world. Map contains information about which tiles are a...
Definition: Map.hpp:17
Base NPC class. It represents a peaceful NPC that does not attack the player under any circumstances.
Definition: Npc.hpp:11
Struct for coordinates.
Definition: Coordinates.hpp:7
Contains the spawn coordinate bounds for a character.
Definition: CharacterReader.hpp:10