forked from jaildesigner-jailgames/jaildoctors_dilemma
Añadido soporte preliminar para estadisticas
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
.vscode
|
.vscode
|
||||||
*config.txt
|
*config.txt
|
||||||
|
*stats.txt
|
||||||
*.DS_Store
|
*.DS_Store
|
||||||
thumbs.db
|
thumbs.db
|
||||||
*.exe
|
*.exe
|
||||||
|
|||||||
@@ -1039,6 +1039,7 @@ bool Director::setFileList()
|
|||||||
// Configuración
|
// Configuración
|
||||||
asset->add(prefix + "/data/input/gamecontrollerdb.txt", t_data);
|
asset->add(prefix + "/data/input/gamecontrollerdb.txt", t_data);
|
||||||
asset->add(prefix + "/data/config/config.txt", t_data, false);
|
asset->add(prefix + "/data/config/config.txt", t_data, false);
|
||||||
|
asset->add(prefix + "/data/config/stats.txt", t_data, false);
|
||||||
|
|
||||||
// Habitaciones
|
// Habitaciones
|
||||||
asset->add(prefix + "/data/room/01.room", t_room);
|
asset->add(prefix + "/data/room/01.room", t_room);
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Resource *resource, Asset *as
|
|||||||
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
text = new Text(resource->getOffset("smb2.txt"), resource->getTexture("smb2.png"), renderer);
|
||||||
music = JA_LoadMusic(asset->get("game.ogg").c_str());
|
music = JA_LoadMusic(asset->get("game.ogg").c_str());
|
||||||
deathSound = JA_LoadSound(asset->get("death.wav").c_str());
|
deathSound = JA_LoadSound(asset->get("death.wav").c_str());
|
||||||
|
stats = new Stats(asset->get("stats.txt"));
|
||||||
|
stats->addVisit(room->getName());
|
||||||
|
|
||||||
// Inicializa el resto de variables
|
// Inicializa el resto de variables
|
||||||
ticks = 0;
|
ticks = 0;
|
||||||
@@ -70,6 +72,7 @@ Game::~Game()
|
|||||||
delete player;
|
delete player;
|
||||||
delete eventHandler;
|
delete eventHandler;
|
||||||
delete text;
|
delete text;
|
||||||
|
delete stats;
|
||||||
|
|
||||||
JA_DeleteMusic(music);
|
JA_DeleteMusic(music);
|
||||||
JA_DeleteSound(deathSound);
|
JA_DeleteSound(deathSound);
|
||||||
@@ -210,7 +213,7 @@ void Game::update()
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
debug->clear();
|
debug->clear();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Actualiza los objetos
|
// Actualiza los objetos
|
||||||
room->update();
|
room->update();
|
||||||
player->update();
|
player->update();
|
||||||
@@ -336,6 +339,9 @@ bool Game::changeRoom(std::string file)
|
|||||||
// Pasa la nueva habitación al jugador
|
// Pasa la nueva habitación al jugador
|
||||||
player->setRoom(room);
|
player->setRoom(room);
|
||||||
|
|
||||||
|
// Actualiza las estadisticas
|
||||||
|
stats->addVisit(room->getName());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,6 +412,9 @@ void Game::killPlayer()
|
|||||||
board.lives--;
|
board.lives--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Actualiza las estadisticas
|
||||||
|
stats->addDeath(room->getName());
|
||||||
|
|
||||||
// Destruye la habitacion y el jugador
|
// Destruye la habitacion y el jugador
|
||||||
delete room;
|
delete room;
|
||||||
delete this->player;
|
delete this->player;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include "room_tracker.h"
|
#include "room_tracker.h"
|
||||||
#include "room.h"
|
#include "room.h"
|
||||||
#include "scoreboard.h"
|
#include "scoreboard.h"
|
||||||
|
#include "stats.h"
|
||||||
|
|
||||||
#ifndef GAME_H
|
#ifndef GAME_H
|
||||||
#define GAME_H
|
#define GAME_H
|
||||||
@@ -39,6 +40,7 @@ private:
|
|||||||
Resource *resource; // Objeto con los recursos
|
Resource *resource; // Objeto con los recursos
|
||||||
Debug *debug; // Objeto para gestionar la información de debug
|
Debug *debug; // Objeto para gestionar la información de debug
|
||||||
options_t *options; // Puntero a las opciones del juego
|
options_t *options; // Puntero a las opciones del juego
|
||||||
|
Stats *stats; // Objeto encargado de gestionar las estadísticas
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
JA_Music music; // Musica que suena durante el juego
|
JA_Music music; // Musica que suena durante el juego
|
||||||
|
|||||||
149
source/stats.cpp
Normal file
149
source/stats.cpp
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
#include "stats.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Stats::Stats(std::string file)
|
||||||
|
{
|
||||||
|
filePath = file;
|
||||||
|
list.clear();
|
||||||
|
loadFromFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Stats::~Stats()
|
||||||
|
{
|
||||||
|
saveToFile();
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade una muerte a las estadisticas
|
||||||
|
void Stats::addDeath(std::string name)
|
||||||
|
{
|
||||||
|
// Primero busca si ya hay una entrada con ese nombre
|
||||||
|
const int index = findByName(name);
|
||||||
|
if (index != -1)
|
||||||
|
{
|
||||||
|
list.at(index).died++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En caso contrario crea la entrada
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stats_t item;
|
||||||
|
item.name = name;
|
||||||
|
item.visited = 0;
|
||||||
|
item.died = 1;
|
||||||
|
list.push_back(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade una visita a las estadisticas
|
||||||
|
void Stats::addVisit(std::string name)
|
||||||
|
{
|
||||||
|
// Primero busca si ya hay una entrada con ese nombre
|
||||||
|
const int index = findByName(name);
|
||||||
|
if (index != -1)
|
||||||
|
{
|
||||||
|
list.at(index).visited++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En caso contrario crea la entrada
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stats_t item;
|
||||||
|
item.name = name;
|
||||||
|
item.visited = 1;
|
||||||
|
item.died = 0;
|
||||||
|
list.push_back(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Busca una entrada en la lista por nombre
|
||||||
|
int Stats::findByName(std::string name)
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
for (auto l : list)
|
||||||
|
{
|
||||||
|
if (l.name == name)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carga las estadisticas desde un fichero
|
||||||
|
bool Stats::loadFromFile()
|
||||||
|
{
|
||||||
|
// Indicador de éxito en la carga
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
// Variables para manejar el fichero
|
||||||
|
std::string line;
|
||||||
|
std::ifstream file(filePath);
|
||||||
|
|
||||||
|
// Si el fichero se puede abrir
|
||||||
|
if (file.good())
|
||||||
|
{
|
||||||
|
// Procesa el fichero linea a linea
|
||||||
|
while (std::getline(file, line))
|
||||||
|
{
|
||||||
|
// Comprueba que la linea no sea un comentario
|
||||||
|
if (line.substr(0, 1) != "#")
|
||||||
|
{
|
||||||
|
stats_t stat;
|
||||||
|
std::stringstream ss(line);
|
||||||
|
std::string tmp;
|
||||||
|
|
||||||
|
// Obtiene el nombre
|
||||||
|
getline(ss, tmp, ';');
|
||||||
|
stat.name = tmp;
|
||||||
|
|
||||||
|
// Obtiene las visitas
|
||||||
|
getline(ss, tmp, ';');
|
||||||
|
stat.visited = std::stoi(tmp);
|
||||||
|
|
||||||
|
// Obtiene las muertes
|
||||||
|
getline(ss, tmp, ';');
|
||||||
|
stat.died = std::stoi(tmp);
|
||||||
|
|
||||||
|
list.push_back(stat);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cierra el fichero
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// El fichero no existe
|
||||||
|
else
|
||||||
|
{ // Crea el fichero con los valores por defecto
|
||||||
|
saveToFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guarda las estadisticas en un fichero
|
||||||
|
void Stats::saveToFile()
|
||||||
|
{
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
// Crea y abre el fichero de texto
|
||||||
|
std::ofstream file(filePath);
|
||||||
|
|
||||||
|
// Escribe en el fichero
|
||||||
|
file << "# NOMBRE DE LA HABITACION;VISITAS;MUERTES" << std::endl;
|
||||||
|
for (auto item : list)
|
||||||
|
{
|
||||||
|
file << item.name << ";" << item.visited << ";" << item.died << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cierra el fichero
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
47
source/stats.h
Normal file
47
source/stats.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "common/utils.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef STATS_H
|
||||||
|
#define STATS_H
|
||||||
|
|
||||||
|
struct stats_t
|
||||||
|
{
|
||||||
|
std::string name; // Nombre de la habitación donde se encuentra el objeto
|
||||||
|
int visited; // Cuenta las veces que se ha visitado una habitación
|
||||||
|
int died; // Cuenta las veces que se ha muerto en una habitación
|
||||||
|
};
|
||||||
|
|
||||||
|
class Stats
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Variables
|
||||||
|
std::vector<stats_t> list; // Lista con las estadisticas por habitación
|
||||||
|
std::string filePath; // Fichero con las estadísticas
|
||||||
|
|
||||||
|
// Busca una entrada en la lista por nombre
|
||||||
|
int findByName(std::string name);
|
||||||
|
|
||||||
|
// Carga las estadisticas desde un fichero
|
||||||
|
bool loadFromFile();
|
||||||
|
|
||||||
|
// Guarda las estadisticas en un fichero
|
||||||
|
void saveToFile();
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Stats(std::string file);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Stats();
|
||||||
|
|
||||||
|
// Añade una muerte a las estadisticas
|
||||||
|
void addDeath(std::string name);
|
||||||
|
|
||||||
|
// Añade una visita a las estadisticas
|
||||||
|
void addVisit(std::string name);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
#include "test.h"
|
|
||||||
|
|
||||||
// Constructor
|
|
||||||
Test::Test(SDL_Renderer *renderer, Screen *screen, Asset *asset, Debug *debug)
|
|
||||||
{
|
|
||||||
// Copia la dirección de los objetos
|
|
||||||
this->renderer = renderer;
|
|
||||||
this->screen = screen;
|
|
||||||
this->asset = asset;
|
|
||||||
this->debug = debug;
|
|
||||||
|
|
||||||
// Inicializa variables
|
|
||||||
for (int i = 0; i < 4; ++i)
|
|
||||||
{
|
|
||||||
point_t p;
|
|
||||||
p.x = rand() % 256;
|
|
||||||
p.y = rand() % 192;
|
|
||||||
p.vx = (float)((rand() % 10) + 1) / 10.0f;
|
|
||||||
p.vy = (float)((rand() % 10) + 1) / 10.0f;
|
|
||||||
rand() % 2 == 0 ? p.dx = -1 : p.dx = 1;
|
|
||||||
rand() % 2 == 0 ? p.dy = -1 : p.dy = 1;
|
|
||||||
p.vx *= p.dx;
|
|
||||||
p.vy *= p.dy;
|
|
||||||
points.push_back(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Actualiza las variables
|
|
||||||
void Test::update()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < (int)points.size(); ++i)
|
|
||||||
{
|
|
||||||
points[i].x += points[i].vx;
|
|
||||||
points[i].y += points[i].vy;
|
|
||||||
|
|
||||||
if (points[i].x > 255)
|
|
||||||
{
|
|
||||||
points[i].x = 255;
|
|
||||||
points[i].vx = -(float)((rand() % 10) + 1) / 10.0f;
|
|
||||||
}
|
|
||||||
else if (points[i].x < 0)
|
|
||||||
{
|
|
||||||
points[i].x = 0;
|
|
||||||
points[i].vx = (float)((rand() % 10) + 1) / 10.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (points[i].y > 191)
|
|
||||||
{
|
|
||||||
points[i].y = 191;
|
|
||||||
points[i].vy = -(float)((rand() % 10) + 1) / 10.0f;
|
|
||||||
}
|
|
||||||
else if (points[i].y < 0)
|
|
||||||
{
|
|
||||||
points[i].y = 0;
|
|
||||||
points[i].vy = (float)((rand() % 10) + 1) / 10.0f;
|
|
||||||
}
|
|
||||||
std::string text = "P" + std::to_string(i) + ": x=" + std::to_string(points[i].x).substr(0,3) + " y=" + std::to_string(points[i].y).substr(0,3) + " vx=" + std::to_string(points[i].vx).substr(0,3) + " vy=" + std::to_string(points[i].vy).substr(0,3);
|
|
||||||
debug->add(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dibuja en pantalla
|
|
||||||
void Test::render()
|
|
||||||
{
|
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
||||||
line_t l1 = {(int)points[0].x, (int)points[0].y, (int)points[1].x, (int)points[1].y};
|
|
||||||
line_t l2 = {(int)points[2].x, (int)points[2].y, (int)points[3].x, (int)points[3].y};
|
|
||||||
SDL_RenderDrawLine(renderer, l1.x1, l1.y1, l1.x2, l1.y2);
|
|
||||||
SDL_RenderDrawLine(renderer, l2.x1, l2.y1, l2.x2, l2.y2);
|
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
|
|
||||||
SDL_Point p = checkCollision(l1, l2);
|
|
||||||
SDL_RenderDrawPoint(renderer, p.x, p.y);
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
#include "common/asset.h"
|
|
||||||
#include "common/debug.h"
|
|
||||||
#include "common/screen.h"
|
|
||||||
#include "common/text.h"
|
|
||||||
#include "common/utils.h"
|
|
||||||
#include "const.h"
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#ifndef TEST_H
|
|
||||||
#define TEST_H
|
|
||||||
|
|
||||||
struct point_t
|
|
||||||
{
|
|
||||||
float x, y;
|
|
||||||
float vx, vy;
|
|
||||||
int dx, dy;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Test
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
// Objetos y punteros
|
|
||||||
SDL_Renderer *renderer; // El renderizador de la ventana
|
|
||||||
Screen *screen; // Objeto encargado de dibujar en pantalla
|
|
||||||
Asset *asset; // Objeto con los ficheros de recursos
|
|
||||||
Debug *debug;
|
|
||||||
|
|
||||||
// Variables
|
|
||||||
std::vector<point_t> points;
|
|
||||||
|
|
||||||
public:
|
|
||||||
// Constructor
|
|
||||||
Test(SDL_Renderer *renderer, Screen *screen, Asset *asset, Debug *debug);
|
|
||||||
|
|
||||||
// Actualiza las variables
|
|
||||||
void update();
|
|
||||||
|
|
||||||
// Dibuja en pantalla
|
|
||||||
void render();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Reference in New Issue
Block a user