JNR
laLevel.h
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 #ifndef M_LEVEL_H
30 #define M_LEVEL_H
31 
33 
34 
47 
49 {
50 protected:
51 
52  // Source file and status
53  //
54  char _strFileName[256];
55  char _strName[128];
56 
57  M_BOOL _bValid;
58  M_BOOL _bCompleted;
59 
60  // Tileset
61  //
62  class laTileset* _pTileSet;
63 
64  // Intro / outro
65  //
66  //class stageIntro* _pIntro;
67  M_BOOL _bIntroPresent;
68  M_BOOL _bIntroOver;
69 
70  char _strNextLevel[64];
71 
72  // List of segments
73  //
74  laSegment _cbFirstSeg;
75  unsigned _nSegCnt;
76 
77  // Dynamic objects
78  //
79  class laObject* _arObjects[M_MAXOBJCNT];
80  unsigned _nObjCnt;
81 
82  // Viewport & Zoom
83  //
84  laPoint3 _ptViewPosition;
85  static double _dZoom;
86 
87  // Precomputed terrain curvitrue
88  //
89  double *_arCurviture_ZOffsets;
90  double *_arCurviture_Angles;
91  double *_arCurviture_Scales;
92 
93  void _curviture_precompute();
94 
95  // Noise/defect texture to be overlayed on the terrain
96  //
97  //double _dSkyX; // bg animation tex pos
98  laTexture* _pNoiseTex;
99 
100  // Internal sub-routines
101  //
102  void _save_profile(char* strName);
103 
104  void _load_segments(laFileParser &fp, unsigned len);
105  void _load_objects(laFileParser &fp);
106 
107  inline void _setup_scene(laRenderer *r);
108  inline void _scroll_view(laRenderer *r) {
109  r->transTranslate( -1 * _ptViewPosition );
110  }
111 
112  inline void _draw_pass_1(laRenderer *r);
113  inline void _draw_pass_1a(laRenderer *r);
114  inline void _draw_pass_2(laRenderer *r);
115 
116 public:
117 
118  //Constructor and destructor
119  //
120  laLevel(void);
121  ~laLevel(void);
122 
123  // Level control methods
124  //
125  void load(char* strFile, M_BOOL bReload=M_FALSE); //< load from level file
126  void create(unsigned nSegCnt, laTileset *pTS); //< manually create
127  void discard(); //< discard and free memory
128 
129  void reload();
130  void reset();
131 
132  // Level completion and saving
133  //
134  void forceCompleted() {
135  if(!_bCompleted)
136  {
137  _bCompleted = M_TRUE;
138  _save_profile( laSystemIntegrator::getSettings()->load_player_name );
139  }
140  };
141 
142  void forceSave() {
143  _save_profile( laSystemIntegrator::getSettings()->load_player_name );
144  };
145 
146  inline M_BOOL isCompleted() { return _bCompleted; };
147 
148  //Get methods
149  //
150  inline char* fileName() { return _strFileName; }
151  inline char* name() { return _strName; }
152  inline laTileset* getTS() { ASSERT(_pTileSet, "Nil tileset"); return _pTileSet; }
153 
154  // Access segments
155  //
156  inline unsigned segmentCount() const { return _nSegCnt; };
157 
158  inline unsigned segmentIndex(double x) const {
159  int ix = (int) floor( x/(M_UNIT*M_SEGW) );
160  return (unsigned) M_CLAMP(0, (int)segmentCount()-1, ix);
161  };
162 
163  inline laSegment* segmentAtIndex(int ix) {
164  //ASSERT(ix < _nSegCnt, "Requested segment %u is outside of level boundary (%u).", ix, _nSegCnt-1);
165  ix = M_CLAMP(0, (int)segmentCount()-1, ix);
166  laSegment *p = &_cbFirstSeg;
167 
168  for(int i=0; i<ix; i++) {
169  p = p->next();
170  ASSERT(p, "Nil segmnent, wrong segment count?");
171  }
172  return p;
173  };
174 
175  inline laSegment* segmentAtPixel(double dx) {
176  return segmentAtIndex( segmentIndex(dx) );
177  };
178 
179  // Access tiles
180  //
181 
182  inline laTile* laLevel::getTile(laPoint3 pos) {
183  unsigned index = segmentIndex(pos.x());
184  laSegment* ps = segmentAtIndex(index);
185 
186  pos.x( M_CLAMP(0, segmentCount()*M_SEGW*M_UNIT - M_UNIT, pos.x()) );
187  return ps->get( pos - laPoint3(index*M_SEGW*M_UNIT, 0) );
188  }
189 
190  inline laTile* laLevel::getTile( int ix, int iy )
191  {
192  int nSegIndex = M_CLAMP(0, (int)_nSegCnt-1, ix/M_SEGW);
193 
194  laSegment* ps = segmentAtIndex(nSegIndex);
195  return ps->get( ix - nSegIndex*M_SEGW, iy);
196  }
197 
198  // Access dynamic objects
199  //
200  inline unsigned getObjCnt() const{ return _nObjCnt; }
201 
202  inline class laPlayer* getPlayer() const{
203  ASSERT(_nObjCnt>0, "Nil object list");
204  return ((laPlayer*)_arObjects[0]);
205  }
206 
207  inline class laObject* getObject(unsigned i) const{
208  ASSERT(i<_nObjCnt, "Nil object list");
209  return (_arObjects[i]);
210  }
211 
212  //void addObject( class laObject *po );
213 
214  //Navigation (viewport control)
215  //
216  inline void scroll(laPoint3 ptViewPos) { _ptViewPosition = ptViewPos; }
217  inline void zoom(double zoom) { _dZoom = zoom; }
218  inline double zoom() { return _dZoom; }
219 
220  //Drawing methods
221  //
222  void drawInterface(laRenderer *r, laPoint3 ptBasePos);
223  void drawSky(laRenderer *r);
224  //void draw(laRenderer *r, unsigned index, unsigned nleft=0, unsigned nright=0);
225  void draw(laRenderer *r, laSegment* pfirst, unsigned nSegments);
226 
227  inline void cameraSetup(laRenderer *r){
228  //_setup_scene(r);
229  _scroll_view(r);
230  }
231 
232  //Animation methods
233  //
234  //void animate(laTimer &t, unsigned index, unsigned nleft=0, unsigned nright=0);
235  void animate(laTimer &t, laSegment* pfirst, unsigned nSegments);
236 
237  // Curviture and offset of the terrain elements
238  //
239  inline double terrainZOffset(long int nX) const{
240  return _arCurviture_ZOffsets[ M_CLAMP(0, (int)_nSegCnt*M_SEGW-1, nX) ];
241  };
242 
243  inline double terrainAngle(long int nX) const{
244  return _arCurviture_Angles[ M_CLAMP(0, (int)_nSegCnt*M_SEGW-1, nX) ];
245  };
246 
247  inline double terrainScale(long int nX) const{
248  return _arCurviture_Scales[ M_CLAMP(0, (int)_nSegCnt*M_SEGW-1, nX) ];
249  };
250 
251  inline double terrainZOffset_atPixel( double x ) const{
252  double div = x/M_UNIT, dw = ceil(div)-div;
253  return terrainZOffset( floor(div) )*dw + terrainZOffset( ceil(div) )*(1-dw);
254  }
255 
256  inline double terrainAngle_atPixel( double x ) const{
257  double div = x/M_UNIT, dw = ceil(div)-div;
258  return M_R2D( terrainAngle(floor(div) )*dw + terrainAngle( ceil(div) )*(1-dw) );
259  }
260 
261  // Intro outro
262  //
263  M_BOOL isIntroPresent() { return _bIntroPresent; }
264  M_BOOL isIntroOver() { return _bIntroOver; }
265  void introOver(M_BOOL bOver = M_TRUE) { _bIntroOver = bOver; }
266 
267  char* nextLevel() { return strcmp(_strNextLevel, "none") ? _strNextLevel : NULL; }
268 
269  // Global percepts
270  //
271  aiPercepts _perceptsGlobal;
272  inline aiPercepts *getGlobalPercepts() { return &_perceptsGlobal; }
273 
274  // Misc
275  //
276  laTexture* getNoiseTex() { return _pNoiseTex; };
277 };
279 #endif //#ifndef M_LEVEL_H
Level Terrain Building-block.
Definition: laTile.h:42
#define M_R2D(r)
Convert radians to degrees.
Playable Character.
Definition: laPlayer.h:46
#define M_UNIT
Unit of 1 meter.
#define M_MAXOBJCNT
Count limitations.
: JR Level
Definition: laLevel.h:48
Named class factory.
Terrain Semgnet.
Definition: laSegment.h:48
2D Texture
Definition: laTexture.h:45
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
Base Class for Level Objects.
Definition: laObject.h:43
Tileset Class.
Definition: laTileset.h:46
File Parser.
Definition: laFileParser.h:41
Progressive Task.
virtual std::string name()=0
Abstract naming method.