JNR
laObject.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_OBJECT_INSTANCE_H
30 #define M_OBJECT_INSTANCE_H
31 
33 
34 
42 
43 class laObject: public laLoadableObj, public aiController, public laNamedClass
44 {
45  //Can be constructed from class name
46  CLASS_NAME(laObject);
47 
48 private:
49  // Level and tileset pointers
50  //
51  class laLevel* _pLevel;
52  class laSegment* _pSegment;
53  laTileset* _pTileset;
54 
55  // ID and pointer to the tileset element
56  // to be used by this dynamic object
57  //
58  class laElement* _pTSObject;
59  unsigned _nTSIndex;
60  char _strTSName[32];
61 
62 protected:
63  // Object pivot
64  //
65  // The position (pivot) of dynamic objects is assumed to be
66  // at the centre of the bottom edge of the bounding box.
67  //
68  laPivot _ptPivot;
69 
70  // Display properties
71  //
72  laColor _color;
73  laOutline _outline;
74  M_BOOL _bLightOn;
75 
76  // Pointer to a nested object (or NULL)
77  //
78  //unsigned _nID;
79  //laObject* _pNestedObject;
80  unsigned _nNext_ID;
81  std::map<unsigned, laObject*> _mNestedObjects;
82 
83  // Asoicated AI
84  //
85  aiAgent _ai;
86 
87  // False-shadow texture
88  //
89  static laTexture _texShadow;
90 
91  // Internal sub-routines
92  //
93  inline void _set_style(laRenderer *r);
94  inline void _offset(laRenderer *r, laPoint3 ptBasePos);
95  inline void _rotation(laRenderer *r);
96  inline void _offset_and_rotation(laRenderer *r, laPoint3 ptBasePos);
97  inline void _draw_shadow_fx(laRenderer *r);
98 
99 
100  inline void _nested_draw_fx(laRenderer *r, laPoint3 ptBasePos);
101  inline void _nested_draw_interface(laRenderer *r, laPoint3 ptBasePos);
102  inline void _nested_animate(laTimer &t);
103 
104  inline void _nested_draw(laRenderer *r, laPoint3 ptBasePos);
105  inline void _nested_destroy();
106 
107  // Redefinable internals
108  //
109  virtual void _drawFx_notrans(laRenderer *r, laPoint3 ptBasePos);
110  virtual void _drawFx_positioned(laRenderer *r);
111  virtual void _drawFx_positioned_rotated(laRenderer *r);
112 
113  virtual void _draw_notrans(laRenderer *r, laPoint3 ptBasePos);
114  virtual void _draw_positioned(laRenderer *r);
115  virtual void _draw_positioned_rotated(laRenderer *r);
116 
117  virtual void _execute_ai(laTimer &t);
118 
119 public:
120  // Messages (e.g. related to HP and XP changes, changes of state, character greetings)
121  //
123 
124  // Constructor/destructor
125  //
126  laObject();
127  ~laObject(void);
128 
129  // Associated TS element
130  //
131  void setLevel(class laLevel* plvl) { _pLevel = plvl; };
132  void setSegment(class laSegment* pseg) { _pSegment = pseg; };
133  void setTS(laTileset* pTileset){ _pTileset = pTileset; }
134  void setObject(unsigned nTSObject, class laElement* pTSObject) {_nTSIndex = nTSObject; _pTSObject = pTSObject;};
135 
136 
137  inline laLevel* getLevelObject() { return _pLevel; };
138  inline laSegment* getSegmentObject() { return _pSegment; };
139  inline laTileset* getTS() { return _pTileset; }
140  inline laElement* getObject() { return _pTSObject; };
141  inline unsigned getObjectIndex() { return _nTSIndex; }
142 
143  // Nested object(s)
144  //
145  unsigned nestedAdd(laObject* p){
146  //std::map<unsigned, laObject*>::iterator i;
147  //i = _mNestedObjects.find( _nNext_ID );
148  //ASSERT( i == _mNestedObjects.end(), "Object already present" );
149 
150  _mNestedObjects[_nNext_ID] = p;
151  return _nNext_ID++;
152 
153  /*if(_pNestedObject)
154  {
155  p->_nID = rand();
156  _pNestedObject->nestedAdd(p);
157  }
158  else
159  _pNestedObject = p;*/
160  }
161 
162  void nestedRemove(unsigned ID, M_BOOL bFreeMem = M_TRUE) {
163 
164  std::map<unsigned, laObject*>::iterator i;
165  i = _mNestedObjects.find( ID );
166  ASSERT( i != _mNestedObjects.end(), "Object not found" );
167 
168  if(bFreeMem) delete ( (*i).second );
169  _mNestedObjects.erase(i);
170 
171  /*ASSERT(_pNestedObject, "Nested object already nil");
172 
173  if(_pNestedObject->nestedGet())
174  _pNestedObject->nestedGet()->nestedRemove();
175  else
176  {
177  if(bFreeMem) delete _pNestedObject;
178  _pNestedObject = NULL;
179  }*/
180  }
181 
182  inline laObject* nestedGet(unsigned ID) {
183  std::map<unsigned, laObject*>::iterator i;
184  i = _mNestedObjects.find( ID );
185  ASSERT( i != _mNestedObjects.end(), "Object not found" );
186 
187  return ( (*i).second );
188  }
189 
190  //void setNestedObject(laObject* p) { _pNestedObject = p; };
191 
192  // Load the object from file
193  //
194  virtual void load(class laFileParser *fp);
195  virtual void save(FILE* f) {};
196 
197  // Dynamicly create a new object
198  //
199  virtual void create(laPoint3 pos);
200 
201  // Respawn. Revert object to its original state
202  //
203  virtual void respawn();
204 
205  // Dispaly style
206  //
207  void setOutline( laOutline o) { _outline = o; }
208  void setColor( laColor c ) { _color = c; }
209  void setLight( M_BOOL on) { _bLightOn = on;}
210 
211  // Draw the object
212  //
213  virtual void drawGeometry(laRenderer *r, laPoint3 ptBasePos);
214  virtual void drawFx(laRenderer *r, laPoint3 ptBasePos);
215  virtual void drawInterface(laRenderer *r, laPoint3 ptBasePos);
216 
217  // Animate the object
218  //
219  virtual void animate(laTimer &t);
220 
221  // Positiioning of the object
222  //
223  void setPosition(laPoint3 pos) {
224  _ptPivot.x( pos.x() );
225  _ptPivot.y( pos.y() );
226  _ptPivot.z( pos.z() );
227  }
228 
229  laPoint3 getPosition() { return _ptPivot; }
230  laPivot* getPivot() { return &_ptPivot; }
231 
232  // Build a bounding box
233  //
234  // Note:
235  // This method assumes the pivot of the object is placet
236  // at the centre of the lower edge of the bounding box;
237  //
238  laRect2 boundingRect()
239  {
240  laPoint2 pivot(_ptPivot.x() - _ptPivot.size.x()/2, _ptPivot.y()-_ptPivot.size.y());
241  return laRect2(pivot, _ptPivot.size);
242  };
243 
244 };
246 #endif //#ifndef M_OBJECT_INSTANCE_H
Base Class for Tileset Elements.
Definition: laElement.h:48
2D Point
Definition: laPoint_novec.h:41
Abstract base class for AI agents.
Definition: aiAgent.h:59
Interface for loadable objects.
Definition: laLoadableObj.h:43
: JR Level
Definition: laLevel.h:48
Object outline properties.
Definition: laRenderer.h:66
Named class.
Definition: laNamedClass.h:45
Animated Text Effect.
Definition: fxMessages.h:64
Terrain Semgnet.
Definition: laSegment.h:48
2D rectangle (used by the GUI and also as a simple tool for proximity testing)
Definition: laRect2.h:39
2D Texture
Definition: laTexture.h:45
Dynamic Object Pivot.
Definition: laPivot.h:44
Class aiController is an abstract class for behaviours which can be performed by objects.
Definition: aiController.h:41
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