Huhu, ich muss mal wieder mit einem grundlegendem Thema nerven, für dass ich auch mit Google keine Antwort gefunden habe
Ich habe folgende Klassen:
GameState
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#ifndef GAMESTATE_HPP
#define GAMESTATE_HPP
class GameState {
public:
//Executed at the beginning of each state
virtual void init()=0;
//Executed when a state is left (popped off the state stack)
virtual void cleanup()=0;
//Pushing and popping states causes pausing/resuming
virtual void pause()=0;
virtual void resume()=0;
//Recieves input and performs actions as response
virtual void getEvents()=0;
//Executed each frame
virtual void update()=0;
//Executed each frame, having a separate Display-function for each state allows drawing of different things based on the current state
virtual void render()=0;
};
#endif
|
GameStateManager
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#ifndef GAMESTATEMANAGER_HPP
#define GAMESTATEMANAGER_HPP
#include "GameState.hpp"
#include <vector>
class GameStateManager {
public:
//Will remove the current state from the stack and add a new state to the end of the stack
void changeState(GameState* state);
//Will pause the current state and add a new state to the end of the stack
void pushState(GameState* state);
//Will remove the last state on the stack and set the current state to the previous state on the stack
void popState();
void clear();
//Will update the current state, handle it's events and render it
void update();
private:
std::vector<GameState*> states;
};
#endif
|
main-Funktion
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
int main()
{
sf::RenderWindow app(sf::VideoMode(1280, 720, 32), "SFML Graphics");
app.setVerticalSyncEnabled(true);
GameStateManager stateManager;
Game* game = new Game(&app);
stateManager.pushState(game);
while (app.isOpen()) {
stateManager.update();
//float Framerate = 1.f / elapsedTime.asSeconds();
//int index = static_cast<int>((player.getPosition().x / 32) + static_cast<int>(player.getPosition().y / 32) * mapWidth);
//std::cout << "FPS: " << Framerate << " || Playerpos: " << index << " || realx: " << player.getPosition().x << " || realy: " << player.getPosition().y << std::endl;
}
delete game;
return EXIT_SUCCESS;
}
|
Die Klasse Game behandelt das Hauptspiel (Handelt sich momentan um ein Tilemap-RPG). Sie enthält die Funktionen für das Rendern, Kollisionen, den Spieler, etc.
Nun möchte ich, wie in einem RPG üblich, ein Inventar implementieren und dafür einen neuen State erstellen (InventoryState).
Wenn ich nun im Hauptspiel auf "I" drücke, soll der GameState Manager diesen State aufnehmen und damit weiterarbeiten.
Die Frage ist jetzt, wie genau ich das machen soll, im Internet habe ich dazu nichts gefunden, egtl. nur den Grundaufbau eines Managers, wie genau man damit arbeiten soll und auf Events, die States erstellen, reagieren soll, wurde nicht erwähnt. Vielleicht hat jemand hier Erfahrung damit und kann mir helfen, weitere Klassen/Funktionen kann ich auf Nachfrage gerne posten.
So far,
Nimelrian