gamedev
world.h
Go to the documentation of this file.
1 
8 #ifndef WORLD_H
9 #define WORLD_H
10 
11 #include <common.h> // For HLINE, vec2, OpenGL utilities, etc.
12 #include <entities.h>
13 
14 #define GEN_INC 10 // Defines at what interval y values should be calculated for the array 'line'.
15  // As explained in World(), the last few lines in the array 'line' are incorrectly calculated
16  // or not calculated at all, so GEN_INC is also used to decrease 'lineCount' in functions like draw()
17  // and detect().
18 
19 #define DAY_CYCLE 3000
20 
27 typedef enum {
31 
38 typedef enum {
39  SUNNY = 0,
40  DARK,
42 } WEATHER;
43 
50 struct line_t {
51  float y;
52  bool gs;
53  float gh[2];
54  unsigned char color;
55 } __attribute__ ((packed));
56 
61 class World {
62 protected:
63 
70  struct line_t *line;
71 
78  int x_start;
79 
88  void singleDetect(Entity *e);
89 
99  void deleteEntities(void);
100 
108  unsigned int lineCount;
109 
115 
116  Texturec *bgTex;
117 
118  Mix_Music *bgmObj;
119  char *bgm;
120 
121 public:
122 
123  /*
124  * These pointers keep track of worlds that are adjacent to this one. Used in ui.cpp
125  * for world jumping.
126  */
127 
128  World *toLeft,
129  *toRight,
130  *behind,
131  *infront;
132 
133  /*
134  * Entity arrays.
135  */
136 
137  std::vector<NPC *> npc;
138  std::vector<Structures *> build;
139  std::vector<Mob *> mob;
140  std::vector<Entity *> entity;
141  std::vector<Object *> object;
142 
143  void addStructure(_TYPE t,float x,float y,World *outside,World *inside);
144  void addMob(int t,float x,float y);
145  void addMob(int t,float x,float y,void (*hey)(Mob *));
146  void addNPC(float x,float y);
147  void addObject(ITEM_ID, bool, const char *, float, float);
148 
149  void update(Player *p,unsigned int delta);
150 
151  /*
152  * Constructor and deconstructor, these do what you would expect.
153  */
154 
155  World(void);
156  virtual ~World(void); // Frees the 'line' array.
157 
158  /*
159  * Generate a world of width `width`. This function is virtual so that other world
160  * classes that are based on this one can generate themselves their own way.
161  */
162 
163  virtual void generate(unsigned int width);
164  void generateFunc(unsigned int width,float(*func)(float));
165 
166  /*
167  * Adds images to using for the background.
168  */
169 
170  void setBackground(WORLD_BG_TYPE bgt);
171 
172  /*
173  * Start/stop background music.
174  */
175 
176  void setBGM(const char *path);
177  void bgmPlay(void);
178  void bgmStop(void);
179 
180  /*
181  * Looks for the furthest back layer in this world and adds a new layer of width `width` behind it.
182  */
183 
184  void addLayer(unsigned int width);
185 
186  /*
187  * Draw the world and entities based on the player's coordinates. Virtual for the same
188  * reason generate() is.
189  */
190 
191  virtual void draw(Player *p);
192 
193 
194  /*
195  * Detect the player and any entities in the current world.
196  */
197 
198  void detect(Player *p);
199 
200  /*
201  * These functions return the pointer to the world in the direction that is requested if it
202  * exists and the player is in a condition that it can make the switch, otherwise they
203  * return the current world.
204  */
205 
206  World *goWorldLeft(Player *p);
207  World *goWorldRight(Player *p);
208  World *goWorldBack(Player *p);
209  World *goWorldFront(Player *p);
210 
211  /*
212  * Called to enter/exit a structure.
213  */
214 
215  World *goInsideStructure(Player *p);
216 
217  /*
218  * These functions add features to the world.
219  */
220 
221  void addHole(unsigned int start,unsigned int end);
222 
223  /*
224  * Get's the world's width.
225  */
226 
227  int getTheWidth(void);
228 
229  void save(FILE *);
230  void load(FILE *);
231 };
232 
233 /*
234  * Gets a good base y value for background rendering.
235 */
236 
237 float worldGetYBase(World *w);
238 
239 /*
240  * IndoorWorld - Indoor settings stored in a World class ;)
241  */
242 
243 class IndoorWorld : public World {
244 public:
245  World *outside;
246  IndoorWorld(void);
247  ~IndoorWorld(void);
248 
249  void generate(unsigned int width); // Generates a flat world of width 'width'
250  void draw(Player *p); // Draws the world (ignores layers)
251 };
252 
253 class Arena : public World {
254 private:
255  vec2 pxy;
256  vec2 door;
257  World *exit;
258 public:
259  Arena(World *leave,Player *p);
260  ~Arena(void);
261  World *exitArena(Player *p);
262 };
263 
264 extern int worldShade;
265 
266 #endif // WORLD_H
Definition: world.h:50
Definition: world.h:39
Definition: world.h:29
Definition: world.h:61
Definition: entities.h:127
Definition: common.h:46
WEATHER
Definition: world.h:38
void deleteEntities(void)
Definition: world.cpp:87
Definition: Texture.h:12
Definition: world.h:41
float y
Definition: world.h:51
struct line_t * line
Definition: world.h:70
void singleDetect(Entity *e)
Definition: world.cpp:660
Definition: world.h:253
Definition: entities.h:37
unsigned char color
Definition: world.h:54
float y
Definition: world.h:88
float gh[2]
Definition: world.h:53
vec2 * star
Definition: world.h:114
int x_start
Definition: world.h:78
bool gs
Definition: world.h:52
unsigned int lineCount
Definition: world.h:108
Definition: entities.h:94
WORLD_BG_TYPE
Definition: world.h:27
Definition: world.h:40
Definition: world.h:243
Definition: world.h:28