Skip to content

Commit

Permalink
Load & save
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelIglesiasAbarca committed Dec 20, 2023
1 parent bdaed94 commit d6af40f
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 15 deletions.
12 changes: 12 additions & 0 deletions Platformer-GameDev/Game/Source/Cerdo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions Platformer-GameDev/Game/Source/Cerdo_Volador.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -148,6 +160,7 @@ bool Cerdo_Volador::Update(float dt)
app->render->DrawTexture(pathTexture, pos.x + 8, pos.y + 8);
}
}

return true;
}

Expand Down
16 changes: 15 additions & 1 deletion Platformer-GameDev/Game/Source/Cerdo_bomba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -156,6 +169,7 @@ bool CerdoPatrullador::Update(float dt)
app->render->DrawTexture(texture, position.x, position.y - 12, &currentAnimation->GetCurrentFrame(),flip);
currentAnimation->Update();
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion Platformer-GameDev/Game/Source/Cerdo_bomba.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions Platformer-GameDev/Game/Source/Dragon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -139,6 +151,7 @@ bool Dragon::Update(float dt)
app->render->DrawTexture(pathTexture, pos.x + 8, pos.y + 8);
}
}

return true;
}

Expand Down
2 changes: 2 additions & 0 deletions Platformer-GameDev/Game/Source/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
60 changes: 60 additions & 0 deletions Platformer-GameDev/Game/Source/EntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,66 @@ void EntityManager::AddEntity(Entity* entity)
if ( entity != nullptr) entities.Add(entity);
}

void EntityManager::GetCerdo(List<Entity*>& Cerdolista) const
{
Cerdolista.Clear();

ListItem<Entity*>* entity;

for (entity = entities.start; entity != NULL; entity = entity->next)
{
if (entity->data->type == EntityType::CERDO)
{
Cerdolista.Add(entity->data);
}
}
}

void EntityManager::GetCerdoVolador(List<Entity*>& CerdoVoladorlista) const
{
CerdoVoladorlista.Clear();

ListItem<Entity*>* 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<Entity*>& CerdoPatrulladorlista) const
{
CerdoPatrulladorlista.Clear();

ListItem<Entity*>* 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<Entity*>& Dragonlista) const
{
Dragonlista.Clear();

ListItem<Entity*>* 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;
Expand Down
5 changes: 5 additions & 0 deletions Platformer-GameDev/Game/Source/EntityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class EntityManager : public Module

void AddEntity(Entity* entity);

void GetCerdo(List<Entity*>& Cerdolista) const;
void GetCerdoVolador(List<Entity*>& CerdoVoladorlista) const;
void GetCerdoPatrullador(List<Entity*>& CerdoPatrulladorlista) const;
void GetDragon(List<Entity*>& Dragonlista) const;

public:

List<Entity*> entities;
Expand Down
103 changes: 103 additions & 0 deletions Platformer-GameDev/Game/Source/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<Entity> Cerdolista;
List<Entity> CerdoPatrulladorlista;
List<Entity> CerdoVoladorlista;
List<Entity> Dragonlista;

return true;
}

Expand All @@ -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;
}

Expand Down
Loading

0 comments on commit d6af40f

Please sign in to comment.