PGE Engine
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
scene_level.h
1 /*
2  * Platformer Game Engine by Wohlstand, a free platform for game making
3  * Copyright (c) 2015 Vitaly Novichkov <admin@wohlnet.ru>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef SCENE_LEVEL_H
20 #define SCENE_LEVEL_H
21 
22 #include "scene.h"
23 #include <graphics/graphics.h>
24 #include <graphics/window.h>
25 
26 #include <common_features/pge_texture.h>
27 #include <common_features/episode_state.h>
28 #include <common_features/event_queue.h>
29 #include <common_features/point.h>
30 
31 #include <common_features/RTree/RTree.h>
32 
33 #include <gui/pge_menubox.h>
34 
35 #include "level/lvl_player.h"
36 #include "level/lvl_player_def.h"
37 
38 #include "level/lvl_block.h"
39 #include "level/lvl_bgo.h"
40 #include "level/lvl_npc.h"
41 
42 #include "level/lvl_physenv.h"
43 
44 #include "level/lvl_warp.h"
45 #include "level/lvl_section.h"
46 #include "level/lvl_backgrnd.h"
47 
48 #include "level/lvl_layer_engine.h"
49 #include "level/lvl_event_engine.h"
50 
51 #include <controls/controller.h>
52 
53 #include <data_configs/custom_data.h>
54 
55 #include <script/lua_level_engine.h>
56 
58 #include <QString>
59 #include <QList>
60 #include <QVector>
61 
62 #include <SDL2/SDL_opengl.h>
63 #include <SDL2/SDL_timer.h>
64 
66 {
67  LVL_Block *block;
68  int id;
69  int type;
70 };
71 
72 
73 class LevelScene : public Scene
74 {
75  friend class LVL_EventEngine;
76 public:
77  LevelScene();
78  ~LevelScene();
79 
80  bool init();
81  bool init_items();
82  static int init_thread(void *self);
83  SDL_Thread * initializer_thread;
84 private:
85  bool isInit;
86  bool isInitFinished;
87  bool isInitFailed;
88 public:
89 
90  //Init 1
91  bool loadFile(QString filePath);
92  bool loadFileIP();
93 
94  //Init 2
95  bool setEntrance(int entr);
96 private:
97  bool isWarpEntrance;
98  PGE_Point cameraStart;
99  bool cameraStartDirected;
100  int cameraStartDirection;
101 
102  LevelDoor startWarp;
103  int NewPlayerID;
104 
105 public:
106  PlayerPoint getStartLocation(int playerID);
107 
108  QHash<int, LVL_PlayerDef > player_defs;
109 
110  bool loadConfigs();
111 
112  void onKeyboardPressedSDL(SDL_Keycode sdl_key, Uint16 modifier);
113  void onMousePressed(SDL_MouseButtonEvent &mbevent);
114  void onMouseMoved(SDL_MouseMotionEvent &mvevent);
115  void onMouseReleased(SDL_MouseButtonEvent &mvevent);
116 
117  LuaEngine* getLuaEngine();
118 
119  void update();
120  void processEvents();
121  void render();
122  int exec();
123 
124  void tickAnimations(float ticks);
125 
126  QString getLastError();
127 
128 
129  int findNearestSection(long x, long y);
130 
131  bool isExit();
132 
133  //Dummy textures
134  PGE_Texture TextureBuffer[3];
135 
136  int exitType();
137 
138  //Flags
139  bool isTimeStopped;
140 
141  /****************Level Running State*****************/
142  bool isLevelContinues;
143 
144  void setExiting(int delay, int reason);
145 
146  QString toAnotherLevel();
147  QString warpToLevelFile;
148  long lastWarpID;
149 
150  int toAnotherEntrance();
151  int warpToArrayID;
152 
153  PGE_Point toWorldXY();
154  bool warpToWorld;
155  PGE_Point warpToWorldXY;
156 
157  float exitLevelDelay;
158  int exitLevelCode;
159  /****************Level Running State*****************/
160 
161  int numberOfPlayers;
162 
163  Controller* player1Controller;
164  Controller* player2Controller;
165 
166  //Garbage collecting
167  void collectGarbageNPCs();
168  void collectGarbagePlayers();
169 
170  /**************Z-Layers**************/
171  static double zCounter;
172 
173  static const double Z_backImage; //Background
174 
175  //Background-2
176  static const double Z_BGOBack2; // backround BGO
177 
178  static const double Z_blockSizable; // sizable blocks
179 
180  //Background-1
181  static const double Z_BGOBack1; // backround BGO
182 
183  static const double Z_npcBack; // background NPC
184  static const double Z_Block; // standart block
185  static const double Z_npcStd; // standart NPC
186  static const double Z_Player; //player Point
187 
188  //Foreground-1
189  static const double Z_BGOFore1; // foreground BGO
190  static const double Z_BlockFore; //LavaBlock
191  static const double Z_npcFore; // foreground NPC
192  //Foreground-2
193  static const double Z_BGOFore2; // foreground BGO
194 
195  static const double Z_sys_PhysEnv;
196  static const double Z_sys_door;
197  static const double Z_sys_interspace1; // interSection space layer
198  static const double Z_sys_sctBorder; // section Border
199  /**************Z-Layers**************/
200 
201 
202  /**************LoadScreen**************/
203  int loaderSpeed;
204  bool IsLoaderWorks;
205  void drawLoader();
206  void setLoaderAnimation(int speed);
207  void stopLoaderAnimation();
208  void destroyLoaderTexture();
209  static unsigned int nextLoadAniFrame(unsigned int x, void *p);
210  void loaderTick();
211  bool doLoaderStep;
212  void loaderStep();
213  SDL_TimerID loader_timer_id;
214  /**************LoadScreen**************/
215 
216  LevelData *levelData();
217 
218  QQueue<transformTask_block > block_transforms;
219 
220  QHash<int, QList<LVL_Block* > > switch_blocks;
221  void toggleSwitch(int switch_id);
222 
223  QVector<LVL_Npc* > active_npcs;
224  QVector<LVL_Npc* > dead_npcs;
225  QVector<LVL_Player* > dead_players;
226 
227  QVector<LVL_Block* > fading_blocks;
228 
229  /*********************Item placing**********************/
230  /*********************Initial*placing*******************/
231  void placeBlock(LevelBlock blockData);
232  void placeBGO(LevelBGO bgoData);
233  void placeNPC(LevelNPC npcData);
234  void addPlayer(PlayerPoint playerData, bool byWarp=false, int warpType=0, int warpDirect=0);
235  /*******************************************************/
236 
237  /*********************Dynamical*spawn*******************/
238  enum NpcSpawnType
239  {
240  GENERATOR_APPEAR=0,
241  GENERATOR_WARP,
242  GENERATOR_PROJECTILE
243  };
244  enum NpcSpawnDirection
245  {
246  SPAWN_LEFT=2,
247  SPAWN_RIGHT=4,
248  SPAWN_UP=1,
249  SPAWN_DOWN=3,
250  };
251  LVL_Block *spawnBlock(LevelBlock blockData);
252  LVL_Bgo *spawnBGO(LevelBGO bgoData);
253  LVL_Npc *spawnNPC(LevelNPC npcData, NpcSpawnType sp_type, NpcSpawnDirection sp_dir, bool reSpawnable=false);
254  /*******************************************************/
255 
256  /********************Interprocess Stuff*****************/
257  bool placingMode;
258  int placingMode_item_type;
259  LevelBlock placingMode_block;
260  LevelBGO placingMode_bgo;
261  LevelNPC placingMode_npc;
262  PGE_PointF placingMode_renderAt;
263  PGE_PointF placingMode_renderOffset;
264  PGE_PointF placingMode_drawSize;
265  /********************Rect***********************/
266  bool placingMode_sizableBlock;
267  bool placingMode_rect_draw;
268  PGE_PointF placingMode_drawBegin;
269  PGE_PointF placingMode_drawEnd;
270  /********************Rect***********************/
271  /********************Placing element's texture***********************/
272  bool placingMode_animated;
273  int placingMode_animatorID;
274  PGE_Texture placingMode_texture;
275  /********************Placing element's texture***********************/
276 
277  void process_InterprocessCommands();
278 
279  void drawPlacingItem();
280  void placeItemByMouse(int x, int y);
281  /*******************************************************/
282 
283  /*********************Item placing**end*****************/
284 
285  void destroyBlock(LVL_Block *&_block);
286  void setGameState(EpisodeState *_gameState);
287 
288  EventQueue<LevelScene > system_events;
289  LVL_Section *getSection(int sct);
290  EpisodeState *getGameState();
291 
292  bool isVizibleOnScreen(PGE_RectF &rect);
293  bool isVizibleOnScreen(double x, double y, double w, double h);
294 
295 private:
296  LevelData data;
297 
298  EpisodeState *gameState;
299  QString errorMsg;
300 
301  bool frameSkip;
302 
303  typedef QList<PGE_LevelCamera> LVL_CameraList;
304  typedef QList<LVL_Section> LVL_SectionsList;
305 
306  LVL_CameraList cameras;
307  LVL_SectionsList sections;
308 
309 public:
310  typedef QVector<LVL_Player* > LVL_PlayersArray;
311  typedef QVector<LVL_Block* > LVL_BlocksArray;
312  typedef QVector<LVL_Bgo* > LVL_BgosArray;
313  typedef QVector<LVL_Npc* > LVL_NpcsArray;
314  typedef QVector<LVL_Warp* > LVL_WarpsArray;
315  typedef QVector<LVL_PhysEnv* > LVL_PhysEnvsArray;
316 
317  LVL_LayerEngine layers;
318  LVL_EventEngine events;
319 
320  LVL_PlayersArray players;
321  LVL_BlocksArray blocks;
322  LVL_BgosArray bgos;
323  LVL_NpcsArray npcs;
324  LVL_WarpsArray warps;
325  LVL_PhysEnvsArray physenvs;
326 
327 private:
328  /*****************Pause Menu*******************/
329  enum PauseMenuItems_Menu1
330  {
331  PAUSE_Continue=0,
332  PAUSE_SaveCont,
333  PAUSE_SaveQuit,
334  PAUSE_Exit
335  };
336  enum PauseMenuItems_Menu2
337  {
338  PAUSE_2_Continue=0,
339  PAUSE_2_Exit
340  };
341  int _pauseMenuID;
342  bool isPauseMenu;
343  PGE_MenuBox _pauseMenu;
344  bool _pauseMenu_opened;
345  void initPauseMenu1();
346  void initPauseMenu2();
347  void processPauseMenu();
348  /*****************Pause Menu**end**************/
349 
350  bool debug_player_jumping;
351  bool debug_player_onground;
352  int debug_player_foots;
353  int debug_render_delay;
354  int debug_phys_delay;
355  int debug_event_delay;
356 
357 public:
358  float globalGravity;
359  void processPhysics(float ticks);
360 
361 public:
362  void registerElement(PGE_Phys_Object* item);
363  void unregisterElement(PGE_Phys_Object* item);
364  typedef double RPoint[2];
365  void queryItems(PGE_RectF &zone, QVector<PGE_Phys_Object* > *resultList);
366  void queryItems(double x, double y, QVector<PGE_Phys_Object* > *resultList);
367 
368  LVL_PlayersArray& getPlayers();
369  LVL_NpcsArray& getNpcs();
370  LVL_NpcsArray& getActiveNpcs();
371 
372 private:
374  IndexTree tree;
375 
376  QList<PGE_Texture > textures_bank;
377 
378  LuaLevelEngine luaEngine;
379 };
380 
381 
382 #endif // SCENE_LEVEL_H
Definition: scene_level.h:65
Definition: scene_level.h:73
Definition: pge_texture.h:32
bool loadConfigs()
Definition: lvl_scene_init.cpp:190
Definition: lua_level_engine.h:10
Level specific NPC entry structure. Defines preferences of each NPC.
Definition: lvl_filedata.h:195
Level specific Warp entry structure. Defines preferences of each Warp entry.
Definition: lvl_filedata.h:231
Definition: pge_menubox.h:24
bool loadFileIP()
Load data via interprocessing.
Definition: lvl_scene_files_io.cpp:46
bool init_items()
Definition: lvl_scene_init.cpp:236
Definition: pointf.h:23
The Controller class provides proxy interface between controllable objects array and physical control...
Definition: controller.h:32
Definition: scene.h:36
Level specific Background object entry structure. Defines preferences of each Background object...
Definition: lvl_filedata.h:138
void onKeyboardPressedSDL(SDL_Keycode sdl_key, Uint16 modifier)
Triggering when pressed any key on keyboard.
Definition: scene_level.cpp:534
This class should have basic functions for interacting with lua To run the lua engine you have to con...
Definition: lua_engine.h:27
Definition: point.h:23
Definition: lvl_section.h:34
Level data structure. Contains all available settings and element lists on the level.
Definition: lvl_filedata.h:396
Definition: lvl_bgo.h:29
Definition: lvl_block.h:31
Definition: episode_state.h:47
Definition: lvl_layer_engine.h:25
Definition: rectf.h:26
Level specific Block entry structure. Defines preferences of each block.
Definition: lvl_filedata.h:95
Definition: lvl_npc.h:21
PGE File Library.
Definition: lvl_event_engine.h:39
The PGE_Phys_Object class.
Definition: lvl_base_object.h:51
Level specific Player spawn point entry definition structure.
Definition: lvl_filedata.h:76