-
Notifications
You must be signed in to change notification settings - Fork 1
/
game-screen.hh
121 lines (102 loc) · 2.84 KB
/
game-screen.hh
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef _GRAVITY_GAME_SCREEN_HH_
#define _GRAVITY_GAME_SCREEN_HH_
#include "screen.hh"
#include "camera.hh"
#include "timer.hh"
#include "entity.hh"
#include "label-widget.hh"
#include "number-widget.hh"
#include "image-button-widget.hh"
#include "mesh.hh"
#include <box2d/box2d.h>
class GameScreen;
class ContactListener : public b2ContactListener {
protected:
GameScreen *screen;
bool inContact;
public:
ContactListener(GameScreen *screen);
virtual void BeginContact(b2Contact *contact);
virtual void EndContact(b2Contact *contact);
void EnemySunContact(Entity *enemy, Entity *sun);
void EnemyPlanetContact(Entity *enemy, Entity *sun);
void PlanetSunContact(Entity *planet, Entity *sun);
void CollectibleSunContact(Entity *collectible, Entity *sun);
void CollectiblePlanetContact(Entity *collectible, Entity *planet);
};
class ContactFilter : public b2ContactFilter {
public:
bool ShouldCollide(b2Fixture *fixtureA, b2Fixture *fixtureB);
};
class GameScreen : public Screen {
protected:
// state variables
float time;
int score;
int timeRemaining;
bool paused;
Camera camera;
float physicsTimeAccumulator;
float scoreAccumulator;
int lives;
bool spawnPlanet;
vector<Entity*> entities;
// non-state variables
b2World world;
b2Body *draggingBody;
b2Vec2 draggingOffset;
bool stepOnce;
Timer timer;
ContactListener contactListener;
ContactFilter contactFilter;
Entity *sun;
int frameCount;
int fps;
vector<Entity*> toBeRemoved;
Mesh *trailPointMesh;
Background background;
bool mouseDown;
int mouseDownX;
int mouseDownY;
bool discardLeftButtonUp;
NumberWidget *scoreLabel;
LabelWidget *timeLabel;
#ifndef RELEASE_BUILD
LabelWidget *fpsLabel;
#endif
ImageWidget *continueLabel;
ImageWidget *pauseSign;
ImageButtonWidget *endGameButton;
ImageButtonWidget *muteButton;
ImageWidget *gameOverLabel;
ImageWidget *livesLabel;
// methods
void FixCamera();
void FixCamera(Entity *e);
void TimerCallback(float elapsed);
void UpdateTrails();
void AddRandomCollectible();
void AddRandomEnemy();
void SetScore(int score);
void SetTimeRemaining(int time);
b2Vec2 GetRandomPosition();
void SpawnPlanet();
void TogglePause();
void DecreaseLives();
void DiscardPlanet(Entity *planet);
void DrawGrid(Renderer *renderer) const;
void DrawTrail(Renderer *renderer, const Entity *entity) const;
friend class ContactListener;
public:
GameScreen(SDL_Window *window);
virtual ~GameScreen();
virtual void SwitchScreen(const map<string, string> &lastState);
void HandleWidgetEvent(int event_type, Widget *widget);
virtual void HandleEvent(const SDL_Event &e);
virtual void Reset();
virtual void Save(ostream &s) const;
virtual void Load(istream &s);
virtual void Advance(float dt);
virtual void Render(Renderer *renderer);
};
#endif /* _GRAVITY_GAME_SCREEN_HH_ */