diff --git a/Platformer-GameDev/Game/Source/Cerdo.cpp b/Platformer-GameDev/Game/Source/Cerdo.cpp index 3f47303..ec83cb1 100644 --- a/Platformer-GameDev/Game/Source/Cerdo.cpp +++ b/Platformer-GameDev/Game/Source/Cerdo.cpp @@ -68,6 +68,12 @@ bool Cerdo::Update(float dt) distance = playerTilePos.DistanceTo(enemyPosition); // calculamos la distancia entre player y enemigo + if (tp) + { + pbody->body->SetTransform({ PIXEL_TO_METERS(position.x), PIXEL_TO_METERS(position.y) }, 0); + tp = false; + } + if (isDead) { app->entityManager->DestroyEntity(this); @@ -154,6 +160,12 @@ bool Cerdo::Update(float dt) } } + if (!tp) + { + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x); + position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y); + } + return true; } diff --git a/Platformer-GameDev/Game/Source/Cerdo_Volador.cpp b/Platformer-GameDev/Game/Source/Cerdo_Volador.cpp index 34d280d..130fef8 100644 --- a/Platformer-GameDev/Game/Source/Cerdo_Volador.cpp +++ b/Platformer-GameDev/Game/Source/Cerdo_Volador.cpp @@ -66,6 +66,12 @@ bool Cerdo_Volador::Update(float dt) distance = sqrt(pow(playerTilePos.x - enemyPosition.x, 2) + pow(playerTilePos.y - enemyPosition.y, 2)); + if (tp) + { + pbody->body->SetTransform({ PIXEL_TO_METERS(position.x), PIXEL_TO_METERS(position.y) }, 0); + tp = false; + } + if (distance < 0) { currentVelocity.x = 0; @@ -125,6 +131,12 @@ bool Cerdo_Volador::Update(float dt) app->map->pathfinding->ClearLastPath(); } + if (!tp) + { + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x); + position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y); + } + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x) - 18; position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y) - 15; @@ -148,6 +160,7 @@ bool Cerdo_Volador::Update(float dt) app->render->DrawTexture(pathTexture, pos.x + 8, pos.y + 8); } } + return true; } diff --git a/Platformer-GameDev/Game/Source/Cerdo_bomba.cpp b/Platformer-GameDev/Game/Source/Cerdo_bomba.cpp index 1c7dfe3..8b59b22 100644 --- a/Platformer-GameDev/Game/Source/Cerdo_bomba.cpp +++ b/Platformer-GameDev/Game/Source/Cerdo_bomba.cpp @@ -60,12 +60,23 @@ bool CerdoPatrullador::Start() { bool CerdoPatrullador::Update(float dt) { + if (tp) + { + pbody->body->SetTransform({ PIXEL_TO_METERS(position.x), PIXEL_TO_METERS(position.y) }, 0); + tp = false; + } + if (!tp) + { + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x); + position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y); + } b2Vec2 vel = b2Vec2(0, -GRAVITY_Y); b2Vec2 currentVelocity = pbody->body->GetLinearVelocity(); currentVelocity.y += 0.5; + if (isFollowingPlayer) { currentAnimation = &watifokIn; @@ -81,7 +92,7 @@ bool CerdoPatrullador::Update(float dt) { OnDeath(); } - + if (posA - 400 <= app->scene->player->position.x && app->scene->player->position.x <= posB + 400 && app->scene->player->position.y < position.y && app->scene->player->position.y >= position.y - 32) { if (isFollowingPlayer == false) @@ -134,6 +145,8 @@ bool CerdoPatrullador::Update(float dt) } } + + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x) - 18; position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y) - 15; @@ -156,6 +169,7 @@ bool CerdoPatrullador::Update(float dt) app->render->DrawTexture(texture, position.x, position.y - 12, ¤tAnimation->GetCurrentFrame(),flip); currentAnimation->Update(); } + return true; } diff --git a/Platformer-GameDev/Game/Source/Cerdo_bomba.h b/Platformer-GameDev/Game/Source/Cerdo_bomba.h index 113fc61..07fff34 100644 --- a/Platformer-GameDev/Game/Source/Cerdo_bomba.h +++ b/Platformer-GameDev/Game/Source/Cerdo_bomba.h @@ -41,7 +41,7 @@ class CerdoPatrullador : public Entity int health = 100; int pigExplosion_FXid; int pigOink_FXid; - bool isDead = false; + bool looksRight = true; Animation* currentAnimation = nullptr; diff --git a/Platformer-GameDev/Game/Source/Dragon.cpp b/Platformer-GameDev/Game/Source/Dragon.cpp index 84d507b..24eaa08 100644 --- a/Platformer-GameDev/Game/Source/Dragon.cpp +++ b/Platformer-GameDev/Game/Source/Dragon.cpp @@ -67,6 +67,12 @@ bool Dragon::Update(float dt) app->physics->world->DestroyBody(pbody->body); } + if (tp) + { + pbody->body->SetTransform({ PIXEL_TO_METERS(position.x), PIXEL_TO_METERS(position.y) }, 0); + tp = false; + } + if (distance < 4) { currentVelocity.x = 0; @@ -120,6 +126,12 @@ bool Dragon::Update(float dt) app->map->pathfinding->ClearLastPath(); } + if (!tp) + { + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x); + position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y); + } + position.x = METERS_TO_PIXELS(pbody->body->GetTransform().p.x) - 18; position.y = METERS_TO_PIXELS(pbody->body->GetTransform().p.y) - 15; @@ -139,6 +151,7 @@ bool Dragon::Update(float dt) app->render->DrawTexture(pathTexture, pos.x + 8, pos.y + 8); } } + return true; } diff --git a/Platformer-GameDev/Game/Source/Entity.h b/Platformer-GameDev/Game/Source/Entity.h index c4e0039..495a256 100644 --- a/Platformer-GameDev/Game/Source/Entity.h +++ b/Platformer-GameDev/Game/Source/Entity.h @@ -92,6 +92,8 @@ class Entity // want our Entity class, maybe it's not renderable... iPoint position; bool renderable = true; + bool isDead = false; + bool tp = false; }; #endif // __ENTITY_H__ \ No newline at end of file diff --git a/Platformer-GameDev/Game/Source/EntityManager.cpp b/Platformer-GameDev/Game/Source/EntityManager.cpp index b76c72d..75d634f 100644 --- a/Platformer-GameDev/Game/Source/EntityManager.cpp +++ b/Platformer-GameDev/Game/Source/EntityManager.cpp @@ -138,6 +138,66 @@ void EntityManager::AddEntity(Entity* entity) if ( entity != nullptr) entities.Add(entity); } +void EntityManager::GetCerdo(List& Cerdolista) const +{ + Cerdolista.Clear(); + + ListItem* entity; + + for (entity = entities.start; entity != NULL; entity = entity->next) + { + if (entity->data->type == EntityType::CERDO) + { + Cerdolista.Add(entity->data); + } + } +} + +void EntityManager::GetCerdoVolador(List& CerdoVoladorlista) const +{ + CerdoVoladorlista.Clear(); + + ListItem* entity; + + for (entity = entities.start; entity != NULL; entity = entity->next) + { + if (entity->data->type == EntityType::CERDO_VOLADOR) + { + CerdoVoladorlista.Add(entity->data); + } + } +} + +void EntityManager::GetCerdoPatrullador(List& CerdoPatrulladorlista) const +{ + CerdoPatrulladorlista.Clear(); + + ListItem* entity; + + for (entity = entities.start; entity != NULL; entity = entity->next) + { + if (entity->data->type == EntityType::CERDO_PATRULLADOR) + { + CerdoPatrulladorlista.Add(entity->data); + } + } +} + +void EntityManager::GetDragon(List& Dragonlista) const +{ + Dragonlista.Clear(); + + ListItem* entity; + + for (entity = entities.start; entity != NULL; entity = entity->next) + { + if (entity->data->type == EntityType::DRAGON) + { + Dragonlista.Add(entity->data); + } + } +} + bool EntityManager::Update(float dt) { bool ret = true; diff --git a/Platformer-GameDev/Game/Source/EntityManager.h b/Platformer-GameDev/Game/Source/EntityManager.h index 155a21c..a2caca9 100644 --- a/Platformer-GameDev/Game/Source/EntityManager.h +++ b/Platformer-GameDev/Game/Source/EntityManager.h @@ -33,6 +33,11 @@ class EntityManager : public Module void AddEntity(Entity* entity); + void GetCerdo(List& Cerdolista) const; + void GetCerdoVolador(List& CerdoVoladorlista) const; + void GetCerdoPatrullador(List& CerdoPatrulladorlista) const; + void GetDragon(List& Dragonlista) const; + public: List entities; diff --git a/Platformer-GameDev/Game/Source/Scene.cpp b/Platformer-GameDev/Game/Source/Scene.cpp index 2fda161..f3bbec1 100644 --- a/Platformer-GameDev/Game/Source/Scene.cpp +++ b/Platformer-GameDev/Game/Source/Scene.cpp @@ -53,24 +53,28 @@ bool Scene::Awake(pugi::xml_node& config) Cerdo* cerdo = (Cerdo*)app->entityManager->CreateEntity(EntityType::CERDO); cerdo->parameters = cerdoNode; } + Cerdolista; for (pugi::xml_node cerdoPatrulladorNode = enemiesNode.child("cerdoPatrullador"); cerdoPatrulladorNode; cerdoPatrulladorNode = cerdoPatrulladorNode.next_sibling("cerdoPatrullador")) { CerdoPatrullador* cerdoPatrullador = (CerdoPatrullador*)app->entityManager->CreateEntity(EntityType::CERDO_PATRULLADOR); cerdoPatrullador->parameters = cerdoPatrulladorNode; } + CerdoPatrulladorlista; for (pugi::xml_node dragonNode = enemiesNode.child("dragon"); dragonNode; dragonNode = dragonNode.next_sibling("dragon")) { Dragon* dragon = (Dragon*)app->entityManager->CreateEntity(EntityType::DRAGON); dragon->parameters = dragonNode; } + Dragonlista; for (pugi::xml_node cerdoVoladorNode = enemiesNode.child("cerdoVolador"); cerdoVoladorNode; cerdoVoladorNode = cerdoVoladorNode.next_sibling("cerdoVolador")) { Cerdo_Volador* cerdoVolador = (Cerdo_Volador*)app->entityManager->CreateEntity(EntityType::CERDO_VOLADOR); cerdoVolador->parameters = cerdoVoladorNode; } + CerdoVoladorlista; } for (pugi::xml_node foodNode = config.child("comida"); foodNode; foodNode = foodNode.next_sibling("comida")) @@ -100,6 +104,11 @@ bool Scene::Awake(pugi::xml_node& config) rey->parameters = reyNode; } + app->entityManager->GetCerdo(Cerdolista); + app->entityManager->GetCerdoVolador(CerdoVoladorlista); + app->entityManager->GetCerdoPatrullador(CerdoPatrulladorlista); + app->entityManager->GetDragon(Dragonlista); + if (config.child("map")) { //Get the map name from the config file and assigns the value in the module app->map->name = config.child("map").attribute("name").as_string(); @@ -222,6 +231,67 @@ bool Scene::LoadState(pugi::xml_node node) player->pbody->body->SetTransform({ PIXEL_TO_METERS(player->position.x), PIXEL_TO_METERS(player->position.y) }, 0); + for (int Cerdocount = 0; Cerdocount < Cerdolista.Count(); Cerdocount++) { + + Entity* cerdo = Cerdolista.At(Cerdocount)->data; + + // Convert the current count to a string for constructing XML attribute names. + std::string count = std::to_string(Cerdocount + 1); + + // Update the position of the slime entity based on XML attributes. + cerdo->position.x = node.child(("Cerdo" + count).c_str()).child("CerdoPosition").attribute("x").as_int(); + cerdo->position.y = node.child(("Cerdo" + count).c_str()).child("CerdoPosition").attribute("y").as_int(); + cerdo->isDead = node.child(("Cerdo" + count).c_str()).child("CerdoPosition").attribute("isDead").as_bool(); + cerdo->tp = true; + } + + for (int CerdoPatcount = 0; CerdoPatcount < CerdoPatrulladorlista.Count(); CerdoPatcount++) { + + Entity* cerdopat = CerdoPatrulladorlista.At(CerdoPatcount)->data; + + // Convert the current count to a string for constructing XML attribute names. + std::string count = std::to_string(CerdoPatcount + 1); + + // Update the position of the slime entity based on XML attributes. + cerdopat->position.x = node.child(("CerdoPat" + count).c_str()).child("CerdoPatPosition").attribute("x").as_int(); + cerdopat->position.y = node.child(("CerdoPat" + count).c_str()).child("CerdoPatPosition").attribute("y").as_int(); + cerdopat->isDead = node.child(("CerdoPat" + count).c_str()).child("CerdoPatPosition").attribute("isDead").as_bool(); + cerdopat->tp = true; + } + + for (int CerdoVolcount = 0; CerdoVolcount < CerdoVoladorlista.Count(); CerdoVolcount++) { + + Entity* cerdovol = CerdoVoladorlista.At(CerdoVolcount)->data; + + // Convert the current count to a string for constructing XML attribute names. + std::string count = std::to_string(CerdoVolcount + 1); + + // Update the position of the slime entity based on XML attributes. + cerdovol->position.x = node.child(("CerdoVol" + count).c_str()).child("CerdoVolPosition").attribute("x").as_int(); + cerdovol->position.y = node.child(("CerdoVol" + count).c_str()).child("CerdoVolPosition").attribute("y").as_int(); + cerdovol->isDead = node.child(("CerdoVol" + count).c_str()).child("CerdoVolPosition").attribute("isDead").as_bool(); + cerdovol->tp = true; + } + + for (int Dragoncount = 0; Dragoncount < Dragonlista.Count(); Dragoncount++) { + + Entity* dragon = Dragonlista.At(Dragoncount)->data; + + // Convert the current count to a string for constructing XML attribute names. + std::string count = std::to_string(Dragoncount + 1); + + // Update the position of the slime entity based on XML attributes. + dragon->position.x = node.child(("Dragon" + count).c_str()).child("DragonPosition").attribute("x").as_int(); + dragon->position.y = node.child(("Dragon" + count).c_str()).child("DragonPosition").attribute("y").as_int(); + dragon->isDead = node.child(("Dragon" + count).c_str()).child("DragonPosition").attribute("isDead").as_bool(); + dragon->tp = true; + } + + List Cerdolista; + List CerdoPatrulladorlista; + List CerdoVoladorlista; + List Dragonlista; + return true; } @@ -238,6 +308,39 @@ bool Scene::SaveState(pugi::xml_node node) pconditionsNode.append_attribute("running").set_value(player->running); pconditionsNode.append_attribute("left_right").set_value(player->looksRight); + for (int CerdoCount = 0; CerdoCount < Cerdolista.Count(); CerdoCount++) { + std::string count = std::to_string(CerdoCount + 1); + Entity* cerdo = Cerdolista.At(CerdoCount)->data; + pugi::xml_node enemyNode = node.append_child(("Cerdo" + count).c_str()).append_child("CerdoPosition"); + enemyNode.append_attribute("x").set_value(cerdo->position.x); + enemyNode.append_attribute("y").set_value(cerdo->position.y); + enemyNode.append_attribute("isDead").set_value(cerdo->isDead); + } + for (int CerdoPatCount = 0; CerdoPatCount < CerdoPatrulladorlista.Count(); CerdoPatCount++) { + std::string count = std::to_string(CerdoPatCount + 1); + Entity* cerdopat = CerdoPatrulladorlista.At(CerdoPatCount)->data; + pugi::xml_node enemyNode = node.append_child(("Cerdopat" + count).c_str()).append_child("CerdoPatPosition"); + enemyNode.append_attribute("x").set_value(cerdopat->position.x); + enemyNode.append_attribute("y").set_value(cerdopat->position.y); + enemyNode.append_attribute("isDead").set_value(cerdopat->isDead); + } + for (int CerdoVolCount = 0; CerdoVolCount < CerdoVoladorlista.Count(); CerdoVolCount++) { + std::string count = std::to_string(CerdoVolCount + 1); + Entity* cerdovol = CerdoVoladorlista.At(CerdoVolCount)->data; + pugi::xml_node enemyNode = node.append_child(("Cerdovol" + count).c_str()).append_child("CerdovVolPosition"); + enemyNode.append_attribute("x").set_value(cerdovol->position.x); + enemyNode.append_attribute("y").set_value(cerdovol->position.y); + enemyNode.append_attribute("isDead").set_value(cerdovol->isDead); + } + for (int DragonCount = 0; DragonCount < Dragonlista.Count(); DragonCount++) { + std::string count = std::to_string(DragonCount + 1); + Entity* dragon = Dragonlista.At(DragonCount)->data; + pugi::xml_node enemyNode = node.append_child(("Dragon" + count).c_str()).append_child("DragonPosition"); + enemyNode.append_attribute("x").set_value(dragon->position.x); + enemyNode.append_attribute("y").set_value(dragon->position.y); + enemyNode.append_attribute("isDead").set_value(dragon->isDead); + } + return true; } diff --git a/Platformer-GameDev/Game/Source/Scene.h b/Platformer-GameDev/Game/Source/Scene.h index 4f975ad..d1b43fa 100644 --- a/Platformer-GameDev/Game/Source/Scene.h +++ b/Platformer-GameDev/Game/Source/Scene.h @@ -67,6 +67,11 @@ class Scene : public Module SDL_Texture* fondo4; pugi::xml_document configFile; pugi::xml_node configNode; + List Cerdolista; + List CerdoVoladorlista; + List CerdoPatrulladorlista; + List Dragonlista; + }; diff --git a/Platformer-GameDev/Output/config.xml b/Platformer-GameDev/Output/config.xml index 5652b48..fb7641f 100644 --- a/Platformer-GameDev/Output/config.xml +++ b/Platformer-GameDev/Output/config.xml @@ -33,16 +33,16 @@ - - - - - - - - - - + + + + + + + + + + diff --git a/Platformer-GameDev/Output/save_game.xml b/Platformer-GameDev/Output/save_game.xml index 28d142d..488644d 100644 --- a/Platformer-GameDev/Output/save_game.xml +++ b/Platformer-GameDev/Output/save_game.xml @@ -6,12 +6,42 @@