Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Serializes objects into game
- Composition* ObjectFactory::Create(const char * filename){
- if(ObjectSerializer.Open(filename))
- {
- Composition *Cmp = new Composition();
- Composition *CmpCpy = new Composition();
- char * line = NULL;
- char *TexureName = NULL;
- Transform * T = NULL;
- Body * B = NULL;
- Logic *L = NULL;
- Sprite *S = NULL;
- bool InvisibleMessageFound = false;
- bool NoClipingMessage = false;
- bool MouseDetected = false;
- POINT Point;
- SimpleAI * Intel = NULL;
- int IdName = 0;
- int x = 0;
- /*Opens Binary file,
- checks for Component Types
- Loads found Component data from file into engine */
- while( ObjectSerializer.IsAlive() )
- {
- if(line != NULL)
- delete []line;
- line = ObjectSerializer.Read_Line();
- if(line == NULL)
- {
- if(!Cmp->ComponentList.empty())
- {
- unsigned location = Cmp->ThisObject->GetCmpId();
- Cmp->ThisObject = Cmp;
- Instance.at(CompositionList).push_back(*Cmp);
- Instance.at(CompositionList).back().SetCmpId(UniqueId);
- Lookup.at(UniqueId) = Cmp;
- Cmp->SetCmpId(UniqueId);
- if(B)
- {
- //B->SetShapeDimensions(Vect2D(T->GetWidth(), T->GetHeight()));
- //B->SetPosition(Vect2D(T->GetXPosition(), T->GetYPosition()));
- if(NoClipingMessage)
- B->SetNoClip(true);
- }
- if(S && InvisibleMessageFound)
- {
- S->SetTransparency(true);
- }
- if(MouseDetected && T)
- T->SetPosition(Point.x, Point.y);
- ObjectSerializer.CloseFile();
- ++UniqueId;
- return Cmp;
- }
- }
- else if(CompareSubString("SPRITE: ",line))
- {
- char *Head = line + 8;
- char *Tail = line;
- char *TextureName = NULL;
- unsigned Size[2] = {0,0};
- unsigned Bool = 0;
- int x = 0;
- if(*Head)
- {
- Tail = GetData(Tail);
- TextureName = SubStringToString(Head, Tail);
- Head = Tail;
- }
- else
- continue;
- while(*Head)
- {
- Tail = GetData(Tail);
- Size[x++] = SubStringToInt(Head, Tail);
- Head = Tail;
- if(x > 1)
- break;
- }
- if(*Head < 32 && *Tail < 32)
- {
- S = new Sprite(TextureName, Size[0], Size[1], 0.0f, 0.0f);
- }
- else
- {
- Tail = GetData(Tail);
- Bool = SubStringToInt(Head, Tail);
- Head = Tail;
- if(!Bool)
- {
- float Offset[2] = {0.0f,0.0f};
- x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- Offset[x++] = SubStringToFloat(Head, Tail);
- Head = Tail;
- if(x > 1)
- break;
- }
- S = new Sprite(TextureName,Size[0], Size[1], Offset[0],Offset[1]);
- }
- else
- {
- unsigned TextureAttributes[2] = {0, 0};
- x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- TextureAttributes[x++] = SubStringToInt(Head,Tail);
- Head = Tail + 1;
- if(x > 1)
- break;
- }
- if(*Head == NULL)
- {
- S = new Sprite(TextureName,Size[0], Size[1],TextureAttributes[0], TextureAttributes[1]);
- }
- else
- {
- float Offset[2] = {0.0f, 0.0f};
- x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- Offset[x++] = SubStringToFloat(Head, Tail);
- Head = Tail;
- if(x > 1)
- break;
- }
- char * Texturename = NULL;
- S = new Sprite(TextureName,Size[0], Size[1],TextureAttributes[0], TextureAttributes[1], Offset[0],Offset[1]);
- }
- }
- delete TextureName;
- AddInstanceToFactory(*S);
- Cmp->AddToComponentList(*S);
- SpriteComponentLookup.at(UniqueId) = S;
- }
- }
- else if(CompareSubString("LOGIC: ",line))
- {
- char *Head = line + 6;
- char *Tail = line;
- char *ObjectType[2] = {0,0};
- int i = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- ObjectType[i++] = SubStringToString(Head, Tail);
- Head = Tail;
- if(i > 1)
- break;
- }
- if(CompareSubString("MoneyBags",ObjectType[0]))
- {
- L = new Logic(_MoneyBags, UniqueId);
- //modify primaryplayer and camera
- LOGIC->SetPrimaryPlayer(Cmp);
- LOGIC->PlayerList.push_back(Cmp);
- GRAPHICS->PrimaryCamera.SetTarget(static_cast<Sprite*>(this->SpriteComponentLookup.at(UniqueId)));
- }
- else if(CompareSubString("Loot",ObjectType[0]))
- {
- L = new Logic(_Loot, UniqueId);
- }
- else if(CompareSubString("Smashable1",ObjectType[0]))
- {
- L = new Logic(_Smashable1, UniqueId);
- }
- else if(CompareSubString("Smashable2",ObjectType[0]))
- {
- L = new Logic(_Smashable2, UniqueId);
- }
- else if(CompareSubString("Smashable3",ObjectType[0]))
- {
- L = new Logic(_Smashable3, UniqueId);
- }
- else if(CompareSubString("Ladder",ObjectType[0]))
- {
- L = new Logic(_Ladder, UniqueId);
- }
- else if(CompareSubString("DoubleJump",ObjectType[0]))
- {
- L = new Logic(_DoubleJump, UniqueId);
- NoClipingMessage = true;
- }
- else if(CompareSubString("Finish",ObjectType[0]))
- {
- L = new Logic(_Finish, UniqueId);
- NoClipingMessage = true;
- }
- else if(CompareSubString("JetpackJack",ObjectType[0]))
- {
- L = new Logic(_JetpackJack, UniqueId);
- }
- else if(CompareSubString("Torque",ObjectType[0]))
- {
- L = new Logic(_Torque, UniqueId);
- }
- else if(CompareSubString("Switch",ObjectType[0]))
- {
- L = new Logic(_Switch, UniqueId);
- }
- else if(CompareSubString("Hazard",ObjectType[0]))
- {
- L = new Logic(_Hazard, UniqueId);
- }
- else if(CompareSubString("Glide",ObjectType[0]))
- {
- L = new Logic(_Glide, UniqueId);
- }
- else if(CompareSubString("InvisibleMessage",ObjectType[0]))
- {
- L = new Logic(_InvisibleMessage, UniqueId);
- }
- else if(CompareSubString("EventFlag",ObjectType[0]))
- {
- L = new Logic(_EventFlag, UniqueId);
- }
- else if(CompareSubString("Door",ObjectType[0]))
- {
- L = new Logic(_Door, UniqueId);
- }
- else if(CompareSubString("Plate",ObjectType[0]))
- {
- L = new Logic(_Plate, UniqueId);
- }
- else if(CompareSubString("MovingPlatform",ObjectType[0]))
- {
- L = new Logic(_MovingPlatform, UniqueId);
- }
- else if(CompareSubString("Mouse",ObjectType[0]))
- {
- L = new Logic(_Mouse, UniqueId);
- LocalMouse = dynamic_cast<Mouse*>(L->GetData());
- LocalMouse->SetID(UniqueId);
- GetCursorPos(&Point);
- MouseDetected = true;
- NoClipingMessage = true;
- }
- else if(CompareSubString("MenuButton",ObjectType[0]))
- {
- if(CompareSubString("ReturnFromPauseMenu",ObjectType[1])){
- L = new Logic(_MenuButton, _ReturnFromPauseMenu, UniqueId);
- }
- else if(CompareSubString("QuitButton",ObjectType[1])){
- L = new Logic(_MenuButton, _QuitButton, UniqueId);
- }
- else if(CompareSubString("KillSwitch",ObjectType[1])){
- L = new Logic(_MenuButton, _KillSwitch, UniqueId);
- }
- else if(CompareSubString("ControlsButton",ObjectType[1])){
- L = new Logic(_MenuButton, _ControlsButton, UniqueId);
- }
- else if(CompareSubString("OptionsButton",ObjectType[1])){
- L = new Logic(_MenuButton, _OptionsButton, UniqueId);
- }
- else if(CompareSubString("CreditsButton",ObjectType[1])){
- L = new Logic(_MenuButton, _CreditsButton, UniqueId);
- }
- else if(CompareSubString("BackButton",ObjectType[1])){
- L = new Logic(_MenuButton, _BackButton, UniqueId);
- }else if(CompareSubString("NextLevelButton",ObjectType[1])){
- L = new Logic(_MenuButton, _NextLevelButton, UniqueId);
- }
- else if(CompareSubString("MainMenuButton",ObjectType[1])){
- L = new Logic(_MenuButton, _MainMenuButton, UniqueId);
- ShowCursor(true);
- }
- else if(CompareSubString("RestartButton",ObjectType[1])){
- L = new Logic(_MenuButton, _RestartButton, UniqueId);
- }
- else{
- L = new Logic(_MenuButton, _QuitButton, UniqueId);
- }
- NoClipingMessage = true;
- AddInstanceToFactory(*L);
- Cmp->AddToComponentList(*L);
- LogicComponentLookup.at(UniqueId) = L;
- continue;
- }
- else if(CompareSubString("MenuToggle",ObjectType[0]))
- {
- L = new Logic(_MenuToggle, UniqueId);
- AddInstanceToFactory(*L);
- Cmp->AddToComponentList(*L);
- LogicComponentLookup.at(UniqueId) = L;
- continue;
- }
- if(L){
- AddInstanceToFactory(*L);
- Cmp->AddToComponentList(*L);
- LogicComponentLookup.at(UniqueId) = L;
- }
- }
- else if(CompareSubString("AI: ",line))
- {
- char *Head = line + 4;
- char *Tail = line;
- int x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- x = SubStringToInt(Head, Tail);
- Head = Tail;
- break;
- }
- Intel = new SimpleAI(x);
- AddInstanceToFactory(*Intel);
- Cmp->AddToComponentList(*Intel);
- FACTORY->AIComponentLookup.at(UniqueId) = Intel;
- }
- else if(CompareSubString("SOUND: ",line))
- {
- char *Head = line + 6;
- char *Tail = line;
- int x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- x = SubStringToInt(Head, Tail);
- Head = Tail;
- break;
- }
- }
- else if(CompareSubString("BODY: ",line))
- {
- char *Head = line + 5;
- char *Tail = line;
- int data[9] = {0,0,0,0,0,0,0,0,0};
- int x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- if(*Head < ' ')
- break;
- data[x++] = SubStringToInt(Head, Tail);
- Head = Tail;
- if(x > 7)
- break; //garbage data
- }
- B = new Body;
- B->SetVelocity(Vect2D(static_cast<float>(data[0]),static_cast<float>(data[1])));
- B->SetMass(static_cast<float>(data[2]));
- if(data[3])
- B->RemainStatic(false);
- else
- B->RemainStatic(true);
- B->SetShape(static_cast<enum ShapeType>(data[4]));
- B->pShape->SetDimensions(Vect2D(static_cast<float>(data[5]),static_cast<float>(data[6])));
- AddInstanceToFactory(*B);
- Cmp->AddToComponentList(*B);
- BodyComponentLookup.at(UniqueId) = B;
- }
- else if(CompareSubString("TRANSFORM: ",line))
- {
- char *Head = line + 11;
- char *Tail = line;
- float data[5] = {0.0f,0.0f,0.0f,0.0f,0.0f};
- int x = 0;
- while(*Head)
- {
- Tail = GetData(Tail);
- data[x++] = SubStringToFloat(Head, Tail);
- Head = Tail;
- if(x > 4)
- break; //garbage data
- }
- T = new Transform();
- T->SetPosition(data[0],data[1]);
- T->SetRotationAngle(data[2]);
- T->SetWidth(data[3]);
- T->SetHeight(data[4]);
- AddInstanceToFactory(*T);
- Cmp->AddToComponentList(*T);
- TransformComponentLookup.at(UniqueId) = T;
- }
- }
- }
- ObjectSerializer.CloseFile();
- return NULL;
- }
Add Comment
Please, Sign In to add comment