JNR
laTypedObject.cpp
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 
30 #include "stdafx.h"
31 #include "laParser.h"
32 #include "Core.h"
33 #include "Core.h"
34 #include "Core-Level-JR.h"
35 #include "RPG\RPG.h"
36 
37 class laAnimatedModel_Element: public laLoadableObj
38 {
39 public:
40  laAnimatedModel* pModel;
41  M_BOOL bSolid;
42 
43  laAnimatedModel_Element(laAnimatedModel *ptr){
44  pModel = ptr;
45  }
46 
47  virtual void load(class laFileParser *fp)
48  {
49  char strModelFile[128], label[128];
50  laPoint3 pos, scale;
51  M_BOOL bSnap, bInvertAnimation;
52 
53  fp->readText(strModelFile, M_FALSE);
54  pModel->load(strModelFile);
55 
56  fp->readObj(&pos, M_FALSE);
57  fp->readObj(&scale, M_FALSE);
58  fp->readBool(&bSnap, M_FALSE);
59  fp->readBool(&bSolid /*_arSolidFlag + _nModelCnt*/, M_FALSE);
60  fp->readBool(&bInvertAnimation, M_FALSE);
61 
62  pos*=M_UNIT;
63 
64  pModel->edScale(scale);
65  pModel->edTranslate(pos);
66  if(bSnap) pModel->edSnap();
67  if(bInvertAnimation) pModel->edInvertAnimation();
68  }
69 };
70 
71 /*laTypedObject::~laTypedObject()
72 {
73 }*/
74 
75 // Free memory
76 void laTypedObject::discard()
77 {
78  ASSERT(_pdata, "Object is nil");
79 
80  switch(_nType)
81  {
82  case M_TYPE_TEXT:
83  delete [] _pdata;
84  return;
85 
86  default:
87  delete _pdata;
88  }
89 
90  _pdata = NULL;
91 }
92 
93 void laTypedObject::_type(char* strType)
94 {
95  _nType = M_TYPE_INVALID;
96 
97  if( !strcmp(strType, "comment") ) _nType = M_TYPE_COMMENT;
98  else if( !strcmp(strType, "bool") ) _nType = M_TYPE_BOOL;
99  else if( !strcmp(strType, "int") ) _nType = M_TYPE_INT;
100  else if( !strcmp(strType, "double") ) _nType = M_TYPE_DOUBLE;
101  else if( !strcmp(strType, "point3") ) _nType = M_TYPE_POINT3;
102  else if( !strcmp(strType, "color") ) _nType = M_TYPE_COLOR;
103  //else if( !strcmp(strType, "trap") ) _nType = M_TYPE_TRAP;
104  else if( !strcmp(strType, "text") ) _nType = M_TYPE_TEXT;
105  else if( !strcmp(strType, "model") ) _nType = M_TYPE_MODEL;
106  else if( !strcmp(strType, "fx") ) _nType = M_TYPE_FX;
107  else if( !strcmp(strType, "attack") ) _nType = M_TYPE_ATTACK;
108  else if( !strcmp(strType, "include") ) _nType = M_TYPE_INCLUDE;
109  else
110  {
111  _nType = M_TYPE_PLIST;
112  }
113 
114  ASSERT(_nType != M_TYPE_INVALID, "Unknown type", strType);
115 }
116 
117 void laTypedObject::load(class laFileParser *fp)
118 {
119  //Read type if not already specified
120  if(_nType==M_TYPE_INVALID)
121  {
122  fp->readSectionSeparator(_strType);
123  _type(_strType);
124  }
125 
126  //Read data of the requested type
127  switch(_nType)
128  {
129  case M_TYPE_COMMENT:
130  {
131  char comment[256];
132  fp->readText(comment, M_FALSE);
133  }
134  return;
135  case M_TYPE_BOOL:
136  _pdata = new M_BOOL;
137  fp->readBool( (M_BOOL*)_pdata, M_FALSE);
138  return;
139  case M_TYPE_INT:
140  _pdata = new int;
141  fp->readInt( (int*)_pdata, M_FALSE);
142  return;
143  case M_TYPE_DOUBLE:
144  _pdata = new double;
145  fp->readDouble( (double*)_pdata, M_FALSE);
146  return;
147  case M_TYPE_POINT3:
148  _pdata = new laPoint3;
149  fp->readObj( (laPoint3*)_pdata, M_FALSE);
150  *((laPoint3*)_pdata) *= M_UNIT;
151  return;
152  case M_TYPE_COLOR:
153  _pdata = new laColor;
154  fp->readObj( (laColor*)_pdata, M_FALSE);
155  return;
156  /*case M_TYPE_TRAP:
157  _pdata = new rpgTrap;
158  fp->readObj( (rpgTrap*)_pdata, M_FALSE);
159  return;*/
160  case M_TYPE_INCLUDE:
161  case M_TYPE_TEXT:
162  _pdata = new char[128];
163  fp->readText( (char*)_pdata, M_FALSE);
164  return;
165  case M_TYPE_MODEL:
166  {
167  laAnimatedModel_Element me( new laAnimatedModel() );
168  fp->readObj( &me, M_FALSE);
169  me.pModel->bIsSolid = me.bSolid;
170 
171  _pdata = me.pModel;
172  }
173  return;
174  case M_TYPE_FX:
175  _pdata = new fxParticleSystem();
176  fp->readObj( (laLoadableObj*)_pdata, M_FALSE);
177  return;
178  case M_TYPE_ATTACK:
179  _pdata = new rpgAttack();
180  fp->readObj( (laLoadableObj*)_pdata, M_FALSE);
181  return;
182 
183  case M_TYPE_PLIST:
184  _pdata = new laPropertyList();
185  ((laPropertyList*)_pdata)->_bReadName = M_FALSE; // tweak
186  strcpy( ((laPropertyList*)_pdata)->_strName, _strType);
187  fp->readObj( (laLoadableObj*)_pdata, M_FALSE);
188  return;
189  }
190 
191  ASSERT(0, "Invalid Type", _nType); //shouldn't reach this, but still;
192 }
193 
194 M_BOOL* laTypedObject::getBool() {
195  ASSERT(type()==M_TYPE_BOOL, "Incompatible Type");
196  return ((M_BOOL*)_pdata);
197 }
198 
199 int* laTypedObject::getInt() {
200  ASSERT(type()==M_TYPE_INT, "Incompatible Type");
201  return ((int*)_pdata);
202 }
203 double* laTypedObject::getDouble() {
204  ASSERT(type()==M_TYPE_DOUBLE, "Incompatible Type");
205  return ((double*)_pdata);
206 }
207 
208 laPoint3* laTypedObject::getPoint() {
209  ASSERT(type()==M_TYPE_POINT3, "Incompatible Type");
210  return ((laPoint3*)_pdata);
211 }
212 
213 laColor* laTypedObject::getColor() {
214  ASSERT(type()==M_TYPE_COLOR, "Incompatible Type");
215  return ((laColor*)_pdata);
216 }
217 
218 /*rpgTrap* laTypedObject::trap() {
219  ASSERT(type()==M_TYPE_TRAP, "Incompatible Type");
220  return ((rpgTrap*)_pdata);
221 }*/
222 
223 char* laTypedObject::getText() {
224  ASSERT( (type()==M_TYPE_TEXT) || (type()==M_TYPE_INCLUDE), "Incompatible Type");
225  return (char*)(_pdata);
226 }
227 
228 class laAnimatedModel* laTypedObject::getModel() {
229  ASSERT(type()==M_TYPE_MODEL, "Incompatible Type");
230  return (class laAnimatedModel*) (_pdata);
231 }
232 
233 class fxParticleSystem* laTypedObject::getFx() {
234  ASSERT(type()==M_TYPE_FX, "Incompatible Type");
235  return (class fxParticleSystem*) (_pdata);
236 }
237 
238 class rpgAttack* laTypedObject::attack() {
239  ASSERT(type()==M_TYPE_ATTACK, "Incompatible Type");
240  return (class rpgAttack*) (_pdata);
241 }
242 
243 class laPropertyList* laTypedObject::getPList()
244 {
245  ASSERT(type()==M_TYPE_PLIST, "Incompatible Type");
246  return (class laPropertyList*) (_pdata);
247 }
#define M_UNIT
Unit of 1 meter.
Animated 3D Model.
Interface for loadable objects.
Definition: laLoadableObj.h:43
Attack RPG Properties.
Definition: rpgAttack.h:55
File Parser.
Definition: laFileParser.h:41