Guest User

Untitled

a guest
Jun 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. //Serializes objects into game
  2. Composition* ObjectFactory::Create(const char * filename){
  3.  
  4. if(ObjectSerializer.Open(filename))
  5. {
  6. Composition *Cmp = new Composition();
  7. Composition *CmpCpy = new Composition();
  8.  
  9. char * line = NULL;
  10. char *TexureName = NULL;
  11. Transform * T = NULL;
  12. Body * B = NULL;
  13. Logic *L = NULL;
  14. Sprite *S = NULL;
  15. bool InvisibleMessageFound = false;
  16. bool NoClipingMessage = false;
  17. bool MouseDetected = false;
  18. POINT Point;
  19. SimpleAI * Intel = NULL;
  20. int IdName = 0;
  21. int x = 0;
  22. /*Opens Binary file,
  23. checks for Component Types
  24. Loads found Component data from file into engine */
  25. while( ObjectSerializer.IsAlive() )
  26. {
  27. if(line != NULL)
  28. delete []line;
  29. line = ObjectSerializer.Read_Line();
  30.  
  31. if(line == NULL)
  32. {
  33.  
  34. if(!Cmp->ComponentList.empty())
  35. {
  36. unsigned location = Cmp->ThisObject->GetCmpId();
  37. Cmp->ThisObject = Cmp;
  38. Instance.at(CompositionList).push_back(*Cmp);
  39. Instance.at(CompositionList).back().SetCmpId(UniqueId);
  40. Lookup.at(UniqueId) = Cmp;
  41. Cmp->SetCmpId(UniqueId);
  42. if(B)
  43. {
  44. //B->SetShapeDimensions(Vect2D(T->GetWidth(), T->GetHeight()));
  45. //B->SetPosition(Vect2D(T->GetXPosition(), T->GetYPosition()));
  46. if(NoClipingMessage)
  47. B->SetNoClip(true);
  48. }
  49. if(S && InvisibleMessageFound)
  50. {
  51. S->SetTransparency(true);
  52. }
  53. if(MouseDetected && T)
  54. T->SetPosition(Point.x, Point.y);
  55. ObjectSerializer.CloseFile();
  56.  
  57. ++UniqueId;
  58. return Cmp;
  59. }
  60. }
  61.  
  62. else if(CompareSubString("SPRITE: ",line))
  63. {
  64. char *Head = line + 8;
  65. char *Tail = line;
  66. char *TextureName = NULL;
  67. unsigned Size[2] = {0,0};
  68. unsigned Bool = 0;
  69. int x = 0;
  70. if(*Head)
  71. {
  72. Tail = GetData(Tail);
  73. TextureName = SubStringToString(Head, Tail);
  74. Head = Tail;
  75. }
  76. else
  77. continue;
  78.  
  79. while(*Head)
  80. {
  81. Tail = GetData(Tail);
  82. Size[x++] = SubStringToInt(Head, Tail);
  83. Head = Tail;
  84. if(x > 1)
  85. break;
  86. }
  87. if(*Head < 32 && *Tail < 32)
  88. {
  89. S = new Sprite(TextureName, Size[0], Size[1], 0.0f, 0.0f);
  90. }
  91. else
  92. {
  93. Tail = GetData(Tail);
  94. Bool = SubStringToInt(Head, Tail);
  95. Head = Tail;
  96.  
  97. if(!Bool)
  98. {
  99. float Offset[2] = {0.0f,0.0f};
  100. x = 0;
  101. while(*Head)
  102. {
  103. Tail = GetData(Tail);
  104. Offset[x++] = SubStringToFloat(Head, Tail);
  105. Head = Tail;
  106. if(x > 1)
  107. break;
  108. }
  109. S = new Sprite(TextureName,Size[0], Size[1], Offset[0],Offset[1]);
  110. }
  111. else
  112. {
  113. unsigned TextureAttributes[2] = {0, 0};
  114. x = 0;
  115. while(*Head)
  116. {
  117. Tail = GetData(Tail);
  118. TextureAttributes[x++] = SubStringToInt(Head,Tail);
  119. Head = Tail + 1;
  120. if(x > 1)
  121. break;
  122. }
  123.  
  124. if(*Head == NULL)
  125. {
  126. S = new Sprite(TextureName,Size[0], Size[1],TextureAttributes[0], TextureAttributes[1]);
  127. }
  128. else
  129. {
  130. float Offset[2] = {0.0f, 0.0f};
  131. x = 0;
  132. while(*Head)
  133. {
  134. Tail = GetData(Tail);
  135. Offset[x++] = SubStringToFloat(Head, Tail);
  136. Head = Tail;
  137. if(x > 1)
  138. break;
  139. }
  140. char * Texturename = NULL;
  141.  
  142. S = new Sprite(TextureName,Size[0], Size[1],TextureAttributes[0], TextureAttributes[1], Offset[0],Offset[1]);
  143. }
  144. }
  145. delete TextureName;
  146. AddInstanceToFactory(*S);
  147. Cmp->AddToComponentList(*S);
  148. SpriteComponentLookup.at(UniqueId) = S;
  149. }
  150. }
  151.  
  152. else if(CompareSubString("LOGIC: ",line))
  153. {
  154. char *Head = line + 6;
  155. char *Tail = line;
  156. char *ObjectType[2] = {0,0};
  157. int i = 0;
  158. while(*Head)
  159. {
  160. Tail = GetData(Tail);
  161. ObjectType[i++] = SubStringToString(Head, Tail);
  162. Head = Tail;
  163. if(i > 1)
  164. break;
  165. }
  166. if(CompareSubString("MoneyBags",ObjectType[0]))
  167. {
  168. L = new Logic(_MoneyBags, UniqueId);
  169. //modify primaryplayer and camera
  170. LOGIC->SetPrimaryPlayer(Cmp);
  171. LOGIC->PlayerList.push_back(Cmp);
  172. GRAPHICS->PrimaryCamera.SetTarget(static_cast<Sprite*>(this->SpriteComponentLookup.at(UniqueId)));
  173. }
  174. else if(CompareSubString("Loot",ObjectType[0]))
  175. {
  176. L = new Logic(_Loot, UniqueId);
  177. }
  178. else if(CompareSubString("Smashable1",ObjectType[0]))
  179. {
  180. L = new Logic(_Smashable1, UniqueId);
  181. }
  182. else if(CompareSubString("Smashable2",ObjectType[0]))
  183. {
  184. L = new Logic(_Smashable2, UniqueId);
  185. }
  186. else if(CompareSubString("Smashable3",ObjectType[0]))
  187. {
  188. L = new Logic(_Smashable3, UniqueId);
  189. }
  190. else if(CompareSubString("Ladder",ObjectType[0]))
  191. {
  192. L = new Logic(_Ladder, UniqueId);
  193. }
  194. else if(CompareSubString("DoubleJump",ObjectType[0]))
  195. {
  196. L = new Logic(_DoubleJump, UniqueId);
  197. NoClipingMessage = true;
  198. }
  199. else if(CompareSubString("Finish",ObjectType[0]))
  200. {
  201. L = new Logic(_Finish, UniqueId);
  202. NoClipingMessage = true;
  203. }
  204. else if(CompareSubString("JetpackJack",ObjectType[0]))
  205. {
  206. L = new Logic(_JetpackJack, UniqueId);
  207. }
  208. else if(CompareSubString("Torque",ObjectType[0]))
  209. {
  210. L = new Logic(_Torque, UniqueId);
  211. }
  212. else if(CompareSubString("Switch",ObjectType[0]))
  213. {
  214. L = new Logic(_Switch, UniqueId);
  215. }
  216. else if(CompareSubString("Hazard",ObjectType[0]))
  217. {
  218. L = new Logic(_Hazard, UniqueId);
  219. }
  220. else if(CompareSubString("Glide",ObjectType[0]))
  221. {
  222. L = new Logic(_Glide, UniqueId);
  223. }
  224. else if(CompareSubString("InvisibleMessage",ObjectType[0]))
  225. {
  226. L = new Logic(_InvisibleMessage, UniqueId);
  227. }
  228. else if(CompareSubString("EventFlag",ObjectType[0]))
  229. {
  230. L = new Logic(_EventFlag, UniqueId);
  231. }
  232. else if(CompareSubString("Door",ObjectType[0]))
  233. {
  234. L = new Logic(_Door, UniqueId);
  235. }
  236. else if(CompareSubString("Plate",ObjectType[0]))
  237. {
  238. L = new Logic(_Plate, UniqueId);
  239. }
  240. else if(CompareSubString("MovingPlatform",ObjectType[0]))
  241. {
  242. L = new Logic(_MovingPlatform, UniqueId);
  243. }
  244. else if(CompareSubString("Mouse",ObjectType[0]))
  245. {
  246. L = new Logic(_Mouse, UniqueId);
  247. LocalMouse = dynamic_cast<Mouse*>(L->GetData());
  248. LocalMouse->SetID(UniqueId);
  249. GetCursorPos(&Point);
  250. MouseDetected = true;
  251. NoClipingMessage = true;
  252. }
  253. else if(CompareSubString("MenuButton",ObjectType[0]))
  254. {
  255. if(CompareSubString("ReturnFromPauseMenu",ObjectType[1])){
  256. L = new Logic(_MenuButton, _ReturnFromPauseMenu, UniqueId);
  257. }
  258. else if(CompareSubString("QuitButton",ObjectType[1])){
  259. L = new Logic(_MenuButton, _QuitButton, UniqueId);
  260. }
  261. else if(CompareSubString("KillSwitch",ObjectType[1])){
  262. L = new Logic(_MenuButton, _KillSwitch, UniqueId);
  263. }
  264. else if(CompareSubString("ControlsButton",ObjectType[1])){
  265. L = new Logic(_MenuButton, _ControlsButton, UniqueId);
  266. }
  267. else if(CompareSubString("OptionsButton",ObjectType[1])){
  268. L = new Logic(_MenuButton, _OptionsButton, UniqueId);
  269. }
  270. else if(CompareSubString("CreditsButton",ObjectType[1])){
  271. L = new Logic(_MenuButton, _CreditsButton, UniqueId);
  272. }
  273. else if(CompareSubString("BackButton",ObjectType[1])){
  274. L = new Logic(_MenuButton, _BackButton, UniqueId);
  275.  
  276. }else if(CompareSubString("NextLevelButton",ObjectType[1])){
  277. L = new Logic(_MenuButton, _NextLevelButton, UniqueId);
  278. }
  279. else if(CompareSubString("MainMenuButton",ObjectType[1])){
  280. L = new Logic(_MenuButton, _MainMenuButton, UniqueId);
  281. ShowCursor(true);
  282. }
  283. else if(CompareSubString("RestartButton",ObjectType[1])){
  284. L = new Logic(_MenuButton, _RestartButton, UniqueId);
  285. }
  286.  
  287. else{
  288. L = new Logic(_MenuButton, _QuitButton, UniqueId);
  289. }
  290.  
  291. NoClipingMessage = true;
  292. AddInstanceToFactory(*L);
  293. Cmp->AddToComponentList(*L);
  294. LogicComponentLookup.at(UniqueId) = L;
  295. continue;
  296. }
  297. else if(CompareSubString("MenuToggle",ObjectType[0]))
  298. {
  299. L = new Logic(_MenuToggle, UniqueId);
  300. AddInstanceToFactory(*L);
  301. Cmp->AddToComponentList(*L);
  302. LogicComponentLookup.at(UniqueId) = L;
  303. continue;
  304. }
  305. if(L){
  306. AddInstanceToFactory(*L);
  307. Cmp->AddToComponentList(*L);
  308. LogicComponentLookup.at(UniqueId) = L;
  309. }
  310. }
  311. else if(CompareSubString("AI: ",line))
  312. {
  313. char *Head = line + 4;
  314. char *Tail = line;
  315. int x = 0;
  316. while(*Head)
  317. {
  318. Tail = GetData(Tail);
  319. x = SubStringToInt(Head, Tail);
  320. Head = Tail;
  321. break;
  322. }
  323. Intel = new SimpleAI(x);
  324. AddInstanceToFactory(*Intel);
  325. Cmp->AddToComponentList(*Intel);
  326. FACTORY->AIComponentLookup.at(UniqueId) = Intel;
  327. }
  328. else if(CompareSubString("SOUND: ",line))
  329. {
  330. char *Head = line + 6;
  331. char *Tail = line;
  332. int x = 0;
  333. while(*Head)
  334. {
  335. Tail = GetData(Tail);
  336. x = SubStringToInt(Head, Tail);
  337. Head = Tail;
  338. break;
  339. }
  340. }
  341. else if(CompareSubString("BODY: ",line))
  342. {
  343. char *Head = line + 5;
  344. char *Tail = line;
  345. int data[9] = {0,0,0,0,0,0,0,0,0};
  346. int x = 0;
  347. while(*Head)
  348. {
  349. Tail = GetData(Tail);
  350. if(*Head < ' ')
  351. break;
  352. data[x++] = SubStringToInt(Head, Tail);
  353. Head = Tail;
  354.  
  355. if(x > 7)
  356. break; //garbage data
  357. }
  358. B = new Body;
  359. B->SetVelocity(Vect2D(static_cast<float>(data[0]),static_cast<float>(data[1])));
  360. B->SetMass(static_cast<float>(data[2]));
  361. if(data[3])
  362. B->RemainStatic(false);
  363. else
  364. B->RemainStatic(true);
  365.  
  366. B->SetShape(static_cast<enum ShapeType>(data[4]));
  367. B->pShape->SetDimensions(Vect2D(static_cast<float>(data[5]),static_cast<float>(data[6])));
  368. AddInstanceToFactory(*B);
  369. Cmp->AddToComponentList(*B);
  370. BodyComponentLookup.at(UniqueId) = B;
  371.  
  372. }
  373. else if(CompareSubString("TRANSFORM: ",line))
  374. {
  375. char *Head = line + 11;
  376. char *Tail = line;
  377. float data[5] = {0.0f,0.0f,0.0f,0.0f,0.0f};
  378. int x = 0;
  379. while(*Head)
  380. {
  381. Tail = GetData(Tail);
  382. data[x++] = SubStringToFloat(Head, Tail);
  383. Head = Tail;
  384. if(x > 4)
  385. break; //garbage data
  386. }
  387. T = new Transform();
  388. T->SetPosition(data[0],data[1]);
  389. T->SetRotationAngle(data[2]);
  390. T->SetWidth(data[3]);
  391. T->SetHeight(data[4]);
  392. AddInstanceToFactory(*T);
  393. Cmp->AddToComponentList(*T);
  394. TransformComponentLookup.at(UniqueId) = T;
  395.  
  396. }
  397.  
  398. }
  399.  
  400. }
  401.  
  402. ObjectSerializer.CloseFile();
  403. return NULL;
  404. }
Add Comment
Please, Sign In to add comment