Ya se pueden pillar los items. Falta llevar el control de los iterms conseguidos

This commit is contained in:
2022-07-12 19:33:09 +02:00
parent f8db0e3a90
commit 6152dc4255
12 changed files with 140 additions and 51 deletions

View File

@@ -12,27 +12,27 @@ room1.tmx
[enemy]
tileset=enemy01.png
x=8
x=1
y=0
vx=0
vy=0.3
x1=8
x2=8
x1=1
y1=0
y2=48
x2=1
y2=6
color=red
[enemy-end]
[enemy]
tileset=enemy01.png
x=40
y=72
vx=0.3
x=5
y=9
vx=0.6
vy=0
x1=40
x2=184
y1=72
y2=72
x1=5
y1=9
x2=22
y2=9
color=yellow
[enemy-end]

View File

@@ -12,13 +12,20 @@ room2.tmx
[enemy]
tileset=enemy01.png
x=120
y=8
x=14
y=0
vx=0
vy=1
x1=120
x2=120
y1=8
y2=104
x1=14
y1=0
x2=14
y2=13
color=purple
[enemy-end]
[item]
tileset=items.png
tile=0
x=19
y=6
[item-end]

View File

@@ -9,7 +9,7 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
0,0,0,0,0,0,0,0,0,0,0,203,203,203,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
0,0,0,0,0,0,0,0,0,0,0,203,203,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,23,23,23,23,0,0,0,0,0,0,0,0,0,23,
23,23,23,23,23,23,0,0,0,203,0,0,0,0,0,0,0,0,0,263,0,0,0,0,203,203,0,0,0,0,0,23,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,263,0,0,0,0,0,0,0,0,0,0,0,23,

View File

@@ -98,6 +98,7 @@ void Director::init(Uint8 name)
mInput->bindKey(INPUT_ACCEPT, SDL_SCANCODE_RETURN);
mInput->bindKey(INPUT_CANCEL, SDL_SCANCODE_ESCAPE);
mInput->bindKey(INPUT_BUTTON_1, SDL_SCANCODE_SPACE);
mInput->bindKey(INPUT_BUTTON_2, SDL_SCANCODE_D);
mInput->bindKey(INPUT_BUTTON_PAUSE, SDL_SCANCODE_ESCAPE);
mInput->bindKey(INPUT_BUTTON_ESCAPE, SDL_SCANCODE_ESCAPE);

View File

@@ -6,6 +6,7 @@ Game::Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang,
// Inicia variables
mCurrentRoom = "01.room";
mSpawnPoint = {2 * 8, 12 * 8, 0, 0, 0, STATUS_STANDING, SDL_FLIP_NONE};
mDebug = false;
// Copia los punteros
mRenderer = renderer;
@@ -122,6 +123,7 @@ void Game::update()
checkPlayerAndWalls(); // Debe ir detras del player update, por si se ha metido en algun muro
checkPlayerOnBorder();
checkPlayerOnFloor();
checkPlayerAndItems();
if (checkPlayerAndEnemies())
{
// Destruye la habitacion y el jugador
@@ -132,6 +134,8 @@ void Game::update()
mRoom = new Room(mAsset->get(mCurrentRoom), mRenderer, mAsset);
mPlayer = new Player(mSpawnPoint, mAsset->get("player01.png"), mRenderer, mAsset, mInput, mRoom);
}
checkInput();
}
}
@@ -156,6 +160,8 @@ void Game::draw()
mText->writeCentered(GAMECANVAS_CENTER_X, 16 * 8, mRoom->getName());
// Debug info
if (mDebug)
{
std::string text;
text = "status: " + std::to_string(mPlayer->status);
mText->write(0, 17 * 8, text);
@@ -171,28 +177,33 @@ void Game::draw()
const bool collision = checkPlayerAndEnemies();
text = "collision: " + std::to_string(collision);
mText->write(0, 20 * 8, text);
}
// Actualiza la pantalla
mScreen->blit();
}
// Comprueba la entrada
/*
void Game::checkInput()
{
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
/*
if (mInput->checkInput(INPUT_UP, REPEAT_FALSE))
changeRoom(mRoom->getRoomUp());
if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
if (mInput->checkInput(INPUT_DOWN, REPEAT_FALSE))
changeRoom(mRoom->getRoomDown());
if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE))
if (mInput->checkInput(INPUT_LEFT, REPEAT_FALSE))
changeRoom(mRoom->getRoomLeft());
if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE))
if (mInput->checkInput(INPUT_RIGHT, REPEAT_FALSE))
changeRoom(mRoom->getRoomRight());
*/
if (mInput->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
mDebug = !mDebug;
}
*/
// Cambia de habitación
bool Game::changeRoom(std::string file)
@@ -319,3 +330,9 @@ bool Game::checkPlayerAndEnemies()
{
return mRoom->enemyCollision(mPlayer->getCollider());
}
// Comprueba las colisiones del jugador con los objetos
void Game::checkPlayerAndItems()
{
mRoom->itemCollision(mPlayer->getCollider());
}

View File

@@ -39,6 +39,7 @@ private:
section_t mSection; // Seccion actual dentro del juego
std::string mCurrentRoom; // Fichero de la habitación actual
player_t mSpawnPoint; // Lugar de la habitación donde aparece el jugador
bool mDebug; // Indica si el modo debug está activo
// Inicializa las variables
void init();
@@ -53,7 +54,7 @@ private:
void draw();
// Comprueba la entrada y actua
// void checkInput();
void checkInput();
// Cambia de habitación
bool changeRoom(std::string file);
@@ -70,6 +71,9 @@ private:
// Comprueba las colisiones del jugador con los enemigos
bool checkPlayerAndEnemies();
// Comprueba las colisiones del jugador con los objetos
void checkPlayerAndItems();
public:
// Constructor
Game(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Lang *lang, Input *input);

View File

@@ -18,6 +18,7 @@ Item::Item(item_t item)
// Inicia variables
sprite->setSpriteClip(item.tile * 8, 0, 8, 8);
collider = sprite->getRect();
// Inicializa los colores
color_t c = stringToColor("blue");
@@ -50,13 +51,29 @@ Item::~Item()
sprite = nullptr;
}
// Pinta el enemigo en pantalla
// Pinta el objeto en pantalla
void Item::draw()
{
const int index = (counter / 2) % color.size();
sprite->getTexture()->setColor(color[index].r, color[index].g, color[index].b);
sprite->render();
sprite->getTexture()->setColor(255, 255, 255);
}
// Actualiza las variables del objeto
void Item::update()
{
counter++;
}
// Coge el objeto
void Item::pick()
{
sprite->setEnabled(false);
}
// Obtiene el rectangulo de colision del objeto
SDL_Rect &Item::getCollider()
{
return collider;
}

View File

@@ -34,6 +34,8 @@ private:
SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
std::vector<color_t> color; // Vector con los colores del objeto
int counter; // Contador interno
SDL_Rect collider; // Rectangulo de colisión
// Comprueba si ha llegado al limite del recorrido para darse media vuelta
void checkPath();
@@ -50,6 +52,12 @@ public:
// Actualiza las variables del objeto
void update();
// Coge el item
void pick();
// Obtiene el rectangulo de colision del objeto
SDL_Rect &getCollider();
};
#endif

View File

@@ -259,11 +259,11 @@ bool Room::setEnemy(enemy_t *enemy, std::string _var, std::string _value)
}
else if (_var == "x")
{
enemy->x = std::stof(_value);
enemy->x = std::stof(_value) * BLOCK;
}
else if (_var == "y")
{
enemy->y = std::stof(_value);
enemy->y = std::stof(_value) * BLOCK;
}
else if (_var == "vx")
{
@@ -275,19 +275,19 @@ bool Room::setEnemy(enemy_t *enemy, std::string _var, std::string _value)
}
else if (_var == "x1")
{
enemy->x1 = std::stoi(_value);
enemy->x1 = std::stoi(_value) * BLOCK;
}
else if (_var == "x2")
{
enemy->x2 = std::stoi(_value);
enemy->x2 = std::stoi(_value) * BLOCK;
}
else if (_var == "y1")
{
enemy->y1 = std::stoi(_value);
enemy->y1 = std::stoi(_value) * BLOCK;
}
else if (_var == "y2")
{
enemy->y2 = std::stoi(_value);
enemy->y2 = std::stoi(_value) * BLOCK;
}
else if (_var == "color")
{
@@ -407,6 +407,11 @@ void Room::update()
{
enemy->update();
}
for (auto item : item_list)
{
item->update();
}
}
// Devuelve la cadena del fichero de la habitación contigua segun el borde
@@ -472,3 +477,20 @@ bool Room::enemyCollision(SDL_Rect &rect)
return collision;
}
// Indica si hay colision con un objeto a partir de un rectangulo
bool Room::itemCollision(SDL_Rect &rect)
{
bool collision = false;
for (auto item : item_list)
{
collision |= checkCollision(rect, item->getCollider());
if (collision)
{
item->pick();
}
}
return collision;
}

View File

@@ -96,6 +96,9 @@ public:
// Indica si hay colision con un enemigo a partir de un rectangulo
bool enemyCollision(SDL_Rect &rect);
// Indica si hay colision con un objeto a partir de un rectangulo
bool itemCollision(SDL_Rect &rect);
};
#endif

View File

@@ -161,3 +161,10 @@ bool Sprite::isEnabled()
{
return mEnabled;
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect Sprite::getRect()
{
SDL_Rect rect = {getPosX(), getPosY(), getWidth(), getHeight()};
return rect;
}

View File

@@ -78,6 +78,9 @@ public:
// Comprueba si el objeto está habilitado
bool isEnabled();
// Devuelve el rectangulo donde está el sprite
SDL_Rect getRect();
};
#endif