Ya coge los diamantes, aunque no los recuerda

This commit is contained in:
2022-08-27 14:15:46 +02:00
parent 2e102160e6
commit 58b238fa45
25 changed files with 173 additions and 22 deletions

13
source/actor_diamond.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "actor_diamond.h"
#include <fstream>
#include <sstream>
// Constructor
ActorDiamond::ActorDiamond(actor_t actor) : Actor(actor)
{
}
// Destructor
ActorDiamond::~ActorDiamond()
{
}

25
source/actor_diamond.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <SDL2/SDL.h>
#include "actor.h"
#include <string>
#ifndef ACTOR_DIAMOND_H
#define ACTOR_DIAMOND_H
// Clase Actor
class ActorDiamond : public Actor
{
private:
public:
// Constructor
ActorDiamond(actor_t actor);
// Destructor
~ActorDiamond();
};
#endif

View File

@@ -193,6 +193,9 @@ void Game::renderDebugInfo()
text = "hookedOn = " + std::to_string(player->hookedOnMovingPlatform);
debugText->write(0, line += 6, text, -1);
text = "DIAMONDS = " + std::to_string(player->diamonds);
debugText->write(0, line += 6, text, -1);
// Pinta mascaras
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 128);

View File

@@ -135,10 +135,40 @@ bool Map::load(std::string file_path)
} while (line != "[/moving platform]");
printf("actor loaded\n");
printf("** actor moving platform loaded\n");
actors.push_back(new ActorMovingPlatform(actor, p1, p2));
}
if (line == "[diamond]")
{
actor_t actor;
actor.asset = asset;
actor.renderer = renderer;
actor.name = a_diamond;
actor.vx = 0.0f;
actor.vy = 0.0f;
SDL_Point p1, p2;
do
{
std::getline(file, line);
// Encuentra la posición del caracter '='
int pos = line.find("=");
// Procesa las dos subcadenas
if (!setActor(&actor, &p1, &p2, line.substr(0, pos), line.substr(pos + 1, line.length())))
{
printf("Warning: file %s\n, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
} while (line != "[/diamond]");
printf("** actor diamond loaded\n");
actors.push_back(new ActorDiamond(actor));
}
} while (line != "[/actors]");
/*actor_t actor;
actor.asset = asset;
@@ -305,7 +335,7 @@ bool Map::setActor(actor_t *actor, SDL_Point *p1, SDL_Point *p2, std::string var
{
p2->y = std::stoi(value) * tile_size;
}
else if (var == "[/moving platform]")
else if ((var == "[/moving platform]") || (var == "[/diamond]"))
{
}
else
@@ -554,4 +584,18 @@ int Map::getActorIncX(int index)
}
return shift;
}
// Elimina un actor
bool Map::deleteActor(int index)
{
bool success = false;
if (actors[index] != nullptr)
{
delete actors[index];
success = true;
}
actors.erase(actors.begin() + index);
return success;
}

View File

@@ -5,6 +5,7 @@
#include "asset.h"
#include "const.h"
#include "actor_moving_platform.h"
#include "actor_diamond.h"
#include <string>
#include <vector>
#include <sstream>
@@ -106,6 +107,9 @@ public:
// Devuelve el desplazamiento relativo del actor
int getActorIncX(int index);
// Elimina un actor
bool deleteActor(int index);
};
#endif

View File

@@ -41,6 +41,7 @@ Player::Player(SDL_Renderer *renderer, Asset *asset, Input *input, Map *map)
collider.insert(collider.end(), {p, p, p, p, p, p, p, p, p, p, p, p});
underFeet.insert(underFeet.end(), {p, p, p});
hookedOnMovingPlatform = -1;
diamonds = 0;
}
// Destructor
@@ -106,6 +107,7 @@ void Player::checkInput()
vy -= jumpStrenght;
state = jumping;
jumpPressed = true;
JA_PlaySound(sound_jump);
}
}
else if (state == jumping)
@@ -356,6 +358,12 @@ bool Player::isOnMovingPlatform()
onMovingPlatform |= (map->getActorName(map->actorCollision(f)) == a_moving_platform);
hookedOnMovingPlatform = std::max(hookedOnMovingPlatform, (map->actorCollision(f)));
}
if (!onMovingPlatform)
{
hookedOnMovingPlatform = -1;
}
return onMovingPlatform;
}
@@ -433,5 +441,15 @@ void Player::setMap(Map *map)
int Player::checkActors()
{
SDL_Rect rect = sprite->getRect();
return map->actorCollision(rect);
const int index = map->actorCollision(rect);
const int name = map->getActorName(index);
if (name == a_diamond)
{
diamonds++;
JA_PlaySound(sound_coin);
map->deleteActor(index);
}
return index;
}

View File

@@ -49,6 +49,7 @@ public:
float maxVX; // Velocidad mazima de desplazamiento horizontal
float maxVY; // Velocidad mazima de desplazamiento vertical
int diamonds; // Cantidad de diamantes recogidos por el jugador
std::vector<bool> key; // Indica las llaves que posee el jugador
std::vector<SDL_Point> collider; // Contiene los puntos de colisión del jugador con el mapa
std::vector<SDL_Point> underFeet; // Contiene los puntos que hay bajo cada pie del jugador

View File

@@ -190,6 +190,8 @@ bool Prog::setFileList()
// Ficheros de actores
asset->add("/data/actors/moving_platform.png", bitmap);
asset->add("/data/actors/moving_platform.ani", data);
asset->add("/data/actors/items/diamond.png", bitmap);
asset->add("/data/actors/items/diamond.ani", data);
// Ficheros del logo
asset->add("/data/logo/logo.png", bitmap);

View File

@@ -57,9 +57,9 @@ void Text::init()
offset[i].y = ((i - 32) / 15) * boxHeight;
}
printf("Cargando %s\n", file.c_str());
const std::string texto = "w = "+ std::to_string(boxWidth) + ", h = " + std::to_string(boxHeight);
printf("%s\n",texto.c_str());
//printf("Cargando %s\n", file.c_str());
//const std::string texto = "w = "+ std::to_string(boxWidth) + ", h = " + std::to_string(boxHeight);
//printf("%s\n",texto.c_str());
}
// Escribe texto en pantalla