Trabajando en el marcador
This commit is contained in:
@@ -19,8 +19,8 @@
|
|||||||
#define GAMECANVAS_HEIGHT 240
|
#define GAMECANVAS_HEIGHT 240
|
||||||
|
|
||||||
// Zona de juego
|
// Zona de juego
|
||||||
const int PLAY_AREA_TOP = (0 * BLOCK);
|
const int PLAY_AREA_TOP = (4 * BLOCK);
|
||||||
const int PLAY_AREA_BOTTOM = (26 * BLOCK);
|
const int PLAY_AREA_BOTTOM = (30 * BLOCK);
|
||||||
const int PLAY_AREA_LEFT = (0 * BLOCK);
|
const int PLAY_AREA_LEFT = (0 * BLOCK);
|
||||||
const int PLAY_AREA_RIGHT = (40 * BLOCK);
|
const int PLAY_AREA_RIGHT = (40 * BLOCK);
|
||||||
const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;
|
const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
|
|||||||
this->debug->setEnabled(true);
|
this->debug->setEnabled(true);
|
||||||
|
|
||||||
// Reserva memoria para los objetos
|
// Reserva memoria para los objetos
|
||||||
|
scoreboard = new ScoreBoard(renderer, asset, &board);
|
||||||
eventHandler = new SDL_Event();
|
eventHandler = new SDL_Event();
|
||||||
itemTracker = new ItemTracker();
|
itemTracker = new ItemTracker();
|
||||||
map = new Map(asset->get("01.map"), renderer, asset, itemTracker);
|
map = new Map(asset->get("01.map"), renderer, asset, itemTracker);
|
||||||
@@ -27,12 +28,15 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
|
|||||||
section.subsection = SUBSECTION_GAME_PLAY;
|
section.subsection = SUBSECTION_GAME_PLAY;
|
||||||
|
|
||||||
musicEnabled = !debug->getEnabled();
|
musicEnabled = !debug->getEnabled();
|
||||||
;
|
|
||||||
|
board.diamonds = 0;
|
||||||
|
board.lives = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
Game::~Game()
|
Game::~Game()
|
||||||
{
|
{
|
||||||
|
delete scoreboard;
|
||||||
delete eventHandler;
|
delete eventHandler;
|
||||||
delete itemTracker;
|
delete itemTracker;
|
||||||
delete map;
|
delete map;
|
||||||
@@ -79,6 +83,7 @@ void Game::update()
|
|||||||
|
|
||||||
// Actualiza los objetos
|
// Actualiza los objetos
|
||||||
debug->clear();
|
debug->clear();
|
||||||
|
scoreboard->update();
|
||||||
map->update();
|
map->update();
|
||||||
enemyEngine->update();
|
enemyEngine->update();
|
||||||
player->update();
|
player->update();
|
||||||
@@ -99,6 +104,7 @@ void Game::render()
|
|||||||
map->render();
|
map->render();
|
||||||
enemyEngine->render();
|
enemyEngine->render();
|
||||||
player->render();
|
player->render();
|
||||||
|
scoreboard->render();
|
||||||
renderDebugInfo();
|
renderDebugInfo();
|
||||||
|
|
||||||
// Actualiza la pantalla
|
// Actualiza la pantalla
|
||||||
@@ -212,7 +218,7 @@ void Game::renderDebugInfo()
|
|||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
// Pinta el texto
|
// Pinta el texto
|
||||||
debug->setPos({1, 1});
|
debug->setPos({1, PLAY_AREA_TOP});
|
||||||
debug->render();
|
debug->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "item_tracker.h"
|
#include "item_tracker.h"
|
||||||
#include "enemy_engine.h"
|
#include "enemy_engine.h"
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
|
#include "scoreboard.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
|
|
||||||
#ifndef GAME_H
|
#ifndef GAME_H
|
||||||
@@ -28,6 +29,8 @@ private:
|
|||||||
Player *player; // Objeto para gestionar el jugador
|
Player *player; // Objeto para gestionar el jugador
|
||||||
ItemTracker *itemTracker; // Objeto para gestionar los items recogidos
|
ItemTracker *itemTracker; // Objeto para gestionar los items recogidos
|
||||||
EnemyEngine *enemyEngine; // Objeto encargado de gestionar los enemigos
|
EnemyEngine *enemyEngine; // Objeto encargado de gestionar los enemigos
|
||||||
|
ScoreBoard *scoreboard; // Objeto encargado de gestionar el marcador
|
||||||
|
board_t board; // Estructura con los datos del marcador
|
||||||
Debug *debug; // Objeto para gestionar la información de debug
|
Debug *debug; // Objeto para gestionar la información de debug
|
||||||
section_t section; // Seccion actual dentro del programa
|
section_t section; // Seccion actual dentro del programa
|
||||||
int ticks; // Contador de ticks para ajustar la velocidad del programa
|
int ticks; // Contador de ticks para ajustar la velocidad del programa
|
||||||
|
|||||||
@@ -407,11 +407,11 @@ void Map::fillMapTexture()
|
|||||||
SDL_RenderClear(renderer);
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
// Dibuja el degradado de fondo
|
// Dibuja el degradado de fondo
|
||||||
const int num_lines = 208;
|
const float num_lines = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
|
||||||
|
|
||||||
for (int i = 0; i < num_lines; i++)
|
for (int i = PLAY_AREA_TOP; i < PLAY_AREA_BOTTOM; i++)
|
||||||
{
|
{
|
||||||
float step = ((float)i / (float)num_lines);
|
float step = ((float)i / num_lines);
|
||||||
int r = bgColor1.r + ((bgColor2.r - bgColor1.r) * step);
|
int r = bgColor1.r + ((bgColor2.r - bgColor1.r) * step);
|
||||||
int g = bgColor1.g + ((bgColor2.g - bgColor1.g) * step);
|
int g = bgColor1.g + ((bgColor2.g - bgColor1.g) * step);
|
||||||
int b = bgColor1.b + ((bgColor2.b - bgColor1.b) * step);
|
int b = bgColor1.b + ((bgColor2.b - bgColor1.b) * step);
|
||||||
@@ -428,16 +428,7 @@ void Map::fillMapTexture()
|
|||||||
// con lo que esta pintando desde fuera de la textura
|
// con lo que esta pintando desde fuera de la textura
|
||||||
clip.x = ((tilemap_l0[(y * map_width) + x] - 1) % tileset_width) * tile_size;
|
clip.x = ((tilemap_l0[(y * map_width) + x] - 1) % tileset_width) * tile_size;
|
||||||
clip.y = ((tilemap_l0[(y * map_width) + x] - 1) / tileset_width) * tile_size;
|
clip.y = ((tilemap_l0[(y * map_width) + x] - 1) / tileset_width) * tile_size;
|
||||||
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
|
texture_tile->render(renderer, x * tile_size, (y * tile_size) + PLAY_AREA_TOP, &clip);
|
||||||
}
|
|
||||||
|
|
||||||
// Dibuja el degradado del marcador
|
|
||||||
int color = 105;
|
|
||||||
for (int i = 208; i < 240; i++)
|
|
||||||
{
|
|
||||||
SDL_SetRenderDrawColor(renderer, 0x69, color, 0x69, 0xFF);
|
|
||||||
SDL_RenderDrawLine(renderer, 0, i, 320, i);
|
|
||||||
color--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -457,7 +448,7 @@ void Map::fillMapTexture()
|
|||||||
// con lo que esta pintando desde fuera de la textura
|
// con lo que esta pintando desde fuera de la textura
|
||||||
clip.x = ((tilemap_l1[(y * map_width) + x] - 1) % tileset_width) * tile_size;
|
clip.x = ((tilemap_l1[(y * map_width) + x] - 1) % tileset_width) * tile_size;
|
||||||
clip.y = ((tilemap_l1[(y * map_width) + x] - 1) / tileset_width) * tile_size;
|
clip.y = ((tilemap_l1[(y * map_width) + x] - 1) / tileset_width) * tile_size;
|
||||||
texture_tile->render(renderer, x * tile_size, y * tile_size, &clip);
|
texture_tile->render(renderer, x * tile_size, (y * tile_size) + PLAY_AREA_TOP, &clip);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,7 +487,7 @@ e_tile_map Map::getTile(SDL_Point p)
|
|||||||
const int y = std::max(getPlayArea(b_top), (std::min(p.y, getPlayArea(b_bottom) - 1)));
|
const int y = std::max(getPlayArea(b_top), (std::min(p.y, getPlayArea(b_bottom) - 1)));
|
||||||
|
|
||||||
// Calcula el tile
|
// Calcula el tile
|
||||||
const int tile = collisionmap[((y / tile_size) * map_width) + (x / tile_size)];
|
const int tile = collisionmap[(((y + PLAY_AREA_TOP) / tile_size) * map_width) + (x / tile_size)];
|
||||||
|
|
||||||
// Las 8 primeras filas son tiles de fondo
|
// Las 8 primeras filas son tiles de fondo
|
||||||
if (tile == 0)
|
if (tile == 0)
|
||||||
|
|||||||
56
source/scoreboard.cpp
Normal file
56
source/scoreboard.cpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include "scoreboard.h"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
ScoreBoard::ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board)
|
||||||
|
{
|
||||||
|
// Obten punteros a objetos
|
||||||
|
this->asset = asset;
|
||||||
|
this->renderer = renderer;
|
||||||
|
this->board = board;
|
||||||
|
|
||||||
|
// Reserva memoria para los objetos
|
||||||
|
texture = new LTexture(renderer, asset->get("player.png"));
|
||||||
|
sprite = new Sprite({0, 0, 0, 0}, texture, renderer);
|
||||||
|
text = new Text(asset->get("dogica.png"), asset->get("dogica.txt"), renderer);
|
||||||
|
|
||||||
|
// Inicializa las variables
|
||||||
|
counter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
ScoreBoard::~ScoreBoard()
|
||||||
|
{
|
||||||
|
delete texture;
|
||||||
|
delete sprite;
|
||||||
|
delete text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pinta el objeto en pantalla
|
||||||
|
void ScoreBoard::render()
|
||||||
|
{
|
||||||
|
// Ponenegro el fondo del marcador
|
||||||
|
const SDL_Rect rect = {0, 0, PLAY_AREA_WIDTH, 2 * 8};
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||||
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
|
||||||
|
// Escribe los textos
|
||||||
|
text->writeCentered(PLAY_AREA_CENTER_FIRST_QUARTER_X, 1, "-LIVES-");
|
||||||
|
text->writeCentered(PLAY_AREA_CENTER_FIRST_QUARTER_X, 1 + text->getCharacterSize(), std::to_string(board->lives));
|
||||||
|
text->writeCentered(PLAY_AREA_CENTER_THIRD_QUARTER_X, 1, "-DIAMONDS-");
|
||||||
|
text->writeCentered(PLAY_AREA_CENTER_THIRD_QUARTER_X, 1 + text->getCharacterSize(), std::to_string(board->diamonds));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualiza las variables del objeto
|
||||||
|
void ScoreBoard::update()
|
||||||
|
{
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recarga la textura
|
||||||
|
void ScoreBoard::reLoadTexture()
|
||||||
|
{
|
||||||
|
texture->reLoad();
|
||||||
|
text->reLoadTexture();
|
||||||
|
}
|
||||||
49
source/scoreboard.h
Normal file
49
source/scoreboard.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "utils.h"
|
||||||
|
#include "text.h"
|
||||||
|
#include "asset.h"
|
||||||
|
#include "sprite.h"
|
||||||
|
#include "const.h"
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef SCOREBOARD_H
|
||||||
|
#define SCOREBOARD_H
|
||||||
|
|
||||||
|
struct board_t
|
||||||
|
{
|
||||||
|
int diamonds; // Lleva la cuenta de los objetos recogidos
|
||||||
|
int lives; // Lleva la cuenta de ls vidas restantes del jugador
|
||||||
|
};
|
||||||
|
|
||||||
|
// Clase ScoreBoard
|
||||||
|
class ScoreBoard
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
LTexture *texture; // Textura con los graficos
|
||||||
|
Sprite *sprite; // Sprite para mostrar los graficos
|
||||||
|
SDL_Renderer *renderer; // El renderizador de la ventana
|
||||||
|
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
|
||||||
|
Text *text; // Objeto para escribir texto
|
||||||
|
int counter; // Contador interno
|
||||||
|
board_t *board; // Contiene las variables a mostrar en el marcador
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
ScoreBoard(SDL_Renderer *renderer, Asset *asset, board_t *board);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~ScoreBoard();
|
||||||
|
|
||||||
|
// Pinta el objeto en pantalla
|
||||||
|
void render();
|
||||||
|
|
||||||
|
// Actualiza las variables del objeto
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// Recarga la textura
|
||||||
|
void reLoadTexture();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user