Compare commits
7 Commits
a4abc02f88
...
4e083a8cdb
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e083a8cdb | |||
| cbe4315701 | |||
| 49d561b583 | |||
| 6e56a6fd79 | |||
| 267d9647e0 | |||
| 13b3702d00 | |||
| 4500845dcd |
@@ -95,6 +95,7 @@ set(APP_SOURCES
|
||||
|
||||
# --- Otros ---
|
||||
source/color.cpp
|
||||
source/demo.cpp
|
||||
source/define_buttons.cpp
|
||||
source/difficulty.cpp
|
||||
source/input_types.cpp
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
# Formato: PARAMETRO VALOR
|
||||
|
||||
# --- GAME ---
|
||||
game.item_size 20 # Tamaño de los items del juego (en píxeles)
|
||||
game.item_text_outline_color E0E0E0F0 # Color del outline del texto de los items (RGBA hex)
|
||||
game.width 320 # Ancho de la resolución nativa del juego (en píxeles)
|
||||
game.height 240 # Alto de la resolución nativa del juego (en píxeles)
|
||||
|
||||
@@ -15,7 +15,6 @@ namespace GameDefaults {
|
||||
namespace Game {
|
||||
constexpr float WIDTH = 320.0F;
|
||||
constexpr float HEIGHT = 256.0F;
|
||||
constexpr float ITEM_SIZE = 20.0F;
|
||||
constexpr int NAME_ENTRY_IDLE_TIME = 10;
|
||||
constexpr int NAME_ENTRY_TOTAL_TIME = 60;
|
||||
constexpr bool HIT_STOP = false;
|
||||
|
||||
70
source/demo.cpp
Normal file
70
source/demo.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include "demo.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_IOStream, SDL_IOFromConstMem, SDL_IOFromFile, SDL_ReadIO, SDL_WriteIO, SDL_CloseIO
|
||||
#include <stdexcept> // Para runtime_error
|
||||
|
||||
#include "resource_helper.h" // Para ResourceHelper
|
||||
#include "utils.h" // Para printWithDots, getFileName
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
|
||||
DemoData dd;
|
||||
|
||||
SDL_IOStream *file = nullptr;
|
||||
|
||||
// Intentar cargar desde ResourceHelper primero
|
||||
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||
if (!resource_data.empty()) {
|
||||
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
|
||||
} else {
|
||||
// Fallback a filesystem directo
|
||||
file = SDL_IOFromFile(file_path.c_str(), "r+b");
|
||||
}
|
||||
|
||||
if (file == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
|
||||
|
||||
// Lee todos los datos del fichero y los deja en el destino
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
|
||||
DemoKeys dk = DemoKeys();
|
||||
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
|
||||
dd.push_back(dk);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_CloseIO(file);
|
||||
|
||||
return dd;
|
||||
}
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
|
||||
auto success = true;
|
||||
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
if (file) {
|
||||
// Guarda los datos
|
||||
for (const auto &data : dd) {
|
||||
if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
|
||||
}
|
||||
// Cierra el fichero
|
||||
SDL_CloseIO(file);
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
#endif // RECORDING
|
||||
54
source/demo.h
Normal file
54
source/demo.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h> // Para Uint8
|
||||
#include <string> // Para string
|
||||
#include <vector> // Para vector
|
||||
|
||||
// --- Constantes ---
|
||||
constexpr int TOTAL_DEMO_DATA = 2000;
|
||||
|
||||
// --- Estructuras ---
|
||||
struct DemoKeys {
|
||||
Uint8 left;
|
||||
Uint8 right;
|
||||
Uint8 no_input;
|
||||
Uint8 fire;
|
||||
Uint8 fire_left;
|
||||
Uint8 fire_right;
|
||||
|
||||
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
|
||||
: left(l),
|
||||
right(r),
|
||||
no_input(ni),
|
||||
fire(f),
|
||||
fire_left(fl),
|
||||
fire_right(fr) {}
|
||||
};
|
||||
|
||||
// --- Tipos ---
|
||||
using DemoData = std::vector<DemoKeys>;
|
||||
|
||||
struct Demo {
|
||||
bool enabled = false; // Indica si está activo el modo demo
|
||||
bool recording = false; // Indica si está activado el modo para grabar la demo
|
||||
float elapsed_s = 0.0F; // Segundos transcurridos de demo
|
||||
int index = 0; // Contador para el modo demo
|
||||
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
|
||||
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
|
||||
|
||||
Demo() = default;
|
||||
|
||||
Demo(bool e, bool r, int c, const DemoKeys& k, const std::vector<DemoData>& d)
|
||||
: enabled(e),
|
||||
recording(r),
|
||||
index(c),
|
||||
keys(k),
|
||||
data(d) {}
|
||||
};
|
||||
|
||||
// --- Funciones ---
|
||||
auto loadDemoDataFromFile(const std::string& file_path) -> DemoData;
|
||||
|
||||
#ifdef RECORDING
|
||||
bool saveDemoFile(const std::string& file_path, const DemoData& dd);
|
||||
#endif
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
class Texture; // lines 6-6
|
||||
|
||||
Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation)
|
||||
Item::Item(ItemType type, float x, float y, SDL_FRect& play_area, const std::shared_ptr<Texture>& texture, const std::vector<std::string>& animation)
|
||||
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
|
||||
play_area_(play_area),
|
||||
type_(type) {
|
||||
@@ -26,8 +26,6 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
width_ = param.game.item_size;
|
||||
height_ = param.game.item_size;
|
||||
pos_x_ = x;
|
||||
pos_y_ = y;
|
||||
// 6 velocidades: 3 negativas (-1.0, -0.66, -0.33) y 3 positivas (0.33, 0.66, 1.0)
|
||||
@@ -35,13 +33,17 @@ Item::Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::sha
|
||||
if (direction < 3) {
|
||||
// Velocidades negativas: -1.0, -0.66, -0.33
|
||||
vel_x_ = -ITEM_VEL_X_BASE + (direction * ITEM_VEL_X_STEP);
|
||||
rotate_speed_ = -720.0F;
|
||||
} else {
|
||||
// Velocidades positivas: 0.33, 0.66, 1.0
|
||||
vel_x_ = ITEM_VEL_X_STEP + ((direction - 3) * ITEM_VEL_X_STEP);
|
||||
rotate_speed_ = 720.0F;
|
||||
}
|
||||
vel_y_ = ITEM_VEL_Y;
|
||||
accel_y_ = ITEM_ACCEL_Y;
|
||||
collider_.r = width_ / 2;
|
||||
sprite_->startRotate();
|
||||
sprite_->setRotateAmount(rotate_speed_);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -103,7 +105,8 @@ void Item::move(float deltaTime) {
|
||||
|
||||
// Si toca el borde lateral
|
||||
if (pos_x_ == MIN_X || pos_x_ == MAX_X) {
|
||||
vel_x_ = -vel_x_; // Invierte la velocidad horizontal
|
||||
vel_x_ = -vel_x_; // Invierte la velocidad horizontal
|
||||
sprite_->scaleRotateAmount(-1.0F); // Invierte la rotación
|
||||
}
|
||||
|
||||
// Si colisiona por arriba, rebota (excepto la máquina de café)
|
||||
@@ -117,8 +120,9 @@ void Item::move(float deltaTime) {
|
||||
|
||||
// Si colisiona con la parte inferior
|
||||
if (pos_y_ > play_area_.h - height_) {
|
||||
// Corrige la posición
|
||||
pos_y_ = play_area_.h - height_;
|
||||
pos_y_ = play_area_.h - height_; // Corrige la posición
|
||||
sprite_->scaleRotateAmount(0.5F); // Reduce la rotación
|
||||
sprite_->stopRotate(300.0F); // Detiene la rotacion
|
||||
|
||||
switch (type_) {
|
||||
case ItemType::COFFEE_MACHINE:
|
||||
|
||||
@@ -27,8 +27,10 @@ enum class ItemType : int {
|
||||
class Item {
|
||||
public:
|
||||
// --- Constantes ---
|
||||
static constexpr int COFFEE_MACHINE_WIDTH = 30; // Anchura de la máquina de café
|
||||
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
|
||||
static constexpr float WIDTH = 20.0F; // Anchura del item
|
||||
static constexpr float HEIGHT = 20.0F; // ALtura del item
|
||||
static constexpr int COFFEE_MACHINE_WIDTH = 30; // Anchura de la máquina de café
|
||||
static constexpr int COFFEE_MACHINE_HEIGHT = 39; // Altura de la máquina de café
|
||||
static constexpr float LIFETIME_DURATION_S = 10.0f; // Duración de vida del ítem en segundos
|
||||
|
||||
// Velocidades base (pixels/segundo) - Coffee Machine
|
||||
@@ -37,10 +39,10 @@ class Item {
|
||||
static constexpr float COFFEE_MACHINE_ACCEL_Y = 360.0F; // Aceleración Y de máquina de café (0.1*60²fps = 360 pixels/segundo²)
|
||||
|
||||
// Velocidades base (pixels/segundo) - Items normales
|
||||
static constexpr float ITEM_VEL_X_BASE = 60.0F; // Velocidad X base para items (1.0F*60fps)
|
||||
static constexpr float ITEM_VEL_X_STEP = 20.0F; // Incremento de velocidad X (0.33F*60fps)
|
||||
static constexpr float ITEM_VEL_Y = -240.0F; // Velocidad Y inicial de items (-4.0F*60fps)
|
||||
static constexpr float ITEM_ACCEL_Y = 720.0F; // Aceleración Y de items (0.2*60²fps = 720 pixels/segundo²)
|
||||
static constexpr float ITEM_VEL_X_BASE = 60.0F; // Velocidad X base para items (1.0F*60fps)
|
||||
static constexpr float ITEM_VEL_X_STEP = 20.0F; // Incremento de velocidad X (0.33F*60fps)
|
||||
static constexpr float ITEM_VEL_Y = -240.0F; // Velocidad Y inicial de items (-4.0F*60fps)
|
||||
static constexpr float ITEM_ACCEL_Y = 720.0F; // Aceleración Y de items (0.2*60²fps = 720 pixels/segundo²)
|
||||
|
||||
// Constantes de física de rebote
|
||||
static constexpr float BOUNCE_VEL_THRESHOLD = 60.0F; // Umbral de velocidad para parar (1.0F*60fps)
|
||||
@@ -49,14 +51,14 @@ class Item {
|
||||
static constexpr float HORIZONTAL_DAMPING = 0.75F; // Factor de amortiguación horizontal
|
||||
|
||||
// --- Constructor y destructor ---
|
||||
Item(ItemType type, float x, float y, SDL_FRect &play_area, const std::shared_ptr<Texture> &texture, const std::vector<std::string> &animation); // Constructor principal
|
||||
Item(ItemType type, float x, float y, SDL_FRect& play_area, const std::shared_ptr<Texture>& texture, const std::vector<std::string>& animation); // Constructor principal
|
||||
~Item() = default; // Destructor
|
||||
|
||||
// --- Métodos principales ---
|
||||
void alignTo(int x); // Centra el objeto en la posición X indicada
|
||||
void render(); // Renderiza el objeto en pantalla
|
||||
void disable(); // Desactiva el objeto
|
||||
void update(float deltaTime); // Actualiza la posición, animación y contadores (time-based)
|
||||
void alignTo(int x); // Centra el objeto en la posición X indicada
|
||||
void render(); // Renderiza el objeto en pantalla
|
||||
void disable(); // Desactiva el objeto
|
||||
void update(float deltaTime); // Actualiza la posición, animación y contadores (time-based)
|
||||
|
||||
// --- Getters ---
|
||||
[[nodiscard]] auto getPosX() const -> float { return pos_x_; } // Obtiene la posición X
|
||||
@@ -66,7 +68,7 @@ class Item {
|
||||
[[nodiscard]] auto getType() const -> ItemType { return type_; } // Obtiene el tipo
|
||||
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; } // Verifica si está habilitado
|
||||
[[nodiscard]] auto isOnFloor() const -> bool { return floor_collision_; } // Verifica si está en el suelo
|
||||
auto getCollider() -> Circle & { return collider_; } // Obtiene el colisionador
|
||||
auto getCollider() -> Circle& { return collider_; } // Obtiene el colisionador
|
||||
|
||||
private:
|
||||
// --- Objetos y punteros ---
|
||||
@@ -76,15 +78,16 @@ class Item {
|
||||
SDL_FRect play_area_; // Rectángulo con la zona de juego
|
||||
Circle collider_; // Círculo de colisión del objeto
|
||||
ItemType type_; // Tipo de objeto
|
||||
float pos_x_; // Posición X del objeto
|
||||
float pos_y_; // Posición Y del objeto
|
||||
float vel_x_; // Velocidad en el eje X
|
||||
float vel_y_; // Velocidad en el eje Y
|
||||
float pos_x_ = 0.0F; // Posición X del objeto
|
||||
float pos_y_ = 0.0F; // Posición Y del objeto
|
||||
float vel_x_ = 0.0F; // Velocidad en el eje X
|
||||
float vel_y_ = 0.0F; // Velocidad en el eje Y
|
||||
float accel_x_ = 0.0F; // Aceleración en el eje X
|
||||
float accel_y_; // Aceleración en el eje Y
|
||||
int width_; // Ancho del objeto
|
||||
int height_; // Alto del objeto
|
||||
float lifetime_timer_ = 0.0f; // Acumulador de tiempo de vida del ítem (segundos)
|
||||
float accel_y_ = 0.0F; // Aceleración en el eje Y
|
||||
float width_ = WIDTH; // Ancho del objeto
|
||||
float height_ = HEIGHT; // Alto del objeto
|
||||
float rotate_speed_ = 0.0F; // Velocidad de rotacion
|
||||
float lifetime_timer_ = 0.0f; // Acumulador de tiempo de vida del ítem (segundos)
|
||||
bool floor_collision_ = false; // Indica si el objeto colisiona con el suelo
|
||||
bool enabled_ = true; // Indica si el objeto está habilitado
|
||||
|
||||
@@ -92,6 +95,6 @@ class Item {
|
||||
void shiftColliders(); // Alinea el círculo de colisión con la posición del objeto
|
||||
void shiftSprite(); // Coloca el sprite en la posición del objeto
|
||||
void move(float deltaTime); // Actualiza la posición y estados del objeto (time-based)
|
||||
void updateTimeToLive(float deltaTime); // Actualiza el contador de tiempo de vida (time-based)
|
||||
void updateTimeToLive(float deltaTime); // Actualiza el contador de tiempo de vida (time-based)
|
||||
static auto getCoffeeMachineSpawn(int player_x, int item_width, int area_width, int margin = 2) -> int; // Calcula la zona de aparición de la máquina de café
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "moving_sprite.h"
|
||||
|
||||
#include <cmath> // Para std::abs
|
||||
#include <utility>
|
||||
|
||||
#include "texture.h" // Para Texture
|
||||
@@ -90,6 +91,21 @@ void MovingSprite::setRotate(bool enable) {
|
||||
rotate_.enabled = enable;
|
||||
}
|
||||
|
||||
// Habilita la rotación y establece el centro en el centro del sprite
|
||||
void MovingSprite::startRotate() {
|
||||
rotate_.enabled = true;
|
||||
rotate_.center.x = pos_.w / 2.0F;
|
||||
rotate_.center.y = pos_.h / 2.0F;
|
||||
}
|
||||
|
||||
// Detiene la rotación y resetea el ángulo a cero
|
||||
void MovingSprite::stopRotate(float threshold) {
|
||||
if (threshold == 0.0F || std::abs(rotate_.amount) <= threshold) {
|
||||
rotate_.enabled = false;
|
||||
rotate_.angle = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
// Establece la posición y_ el tamaño del objeto
|
||||
void MovingSprite::setPos(SDL_FRect rect) {
|
||||
x_ = rect.x;
|
||||
|
||||
@@ -15,7 +15,6 @@ class MovingSprite : public Sprite {
|
||||
// --- Estructuras ---
|
||||
struct Rotate {
|
||||
bool enabled{false}; // Indica si ha de rotar
|
||||
int speed{1}; // Velocidad de giro
|
||||
double angle{0.0}; // Ángulo para dibujarlo
|
||||
float amount{0.0F}; // Cantidad de grados a girar en cada iteración
|
||||
SDL_FPoint center{.x = 0.0F, .y = 0.0F}; // Centro de rotación
|
||||
@@ -28,10 +27,10 @@ class MovingSprite : public Sprite {
|
||||
~MovingSprite() override = default;
|
||||
|
||||
// --- Métodos principales ---
|
||||
virtual void update(float deltaTime); // Actualiza las variables internas del objeto (time-based)
|
||||
void clear() override; // Reinicia todas las variables a cero
|
||||
void stop(); // Elimina el movimiento del sprite
|
||||
void render() override; // Muestra el sprite por pantalla
|
||||
virtual void update(float deltaTime); // Actualiza las variables internas del objeto (time-based)
|
||||
void clear() override; // Reinicia todas las variables a cero
|
||||
void stop(); // Elimina el movimiento del sprite
|
||||
void render() override; // Muestra el sprite por pantalla
|
||||
|
||||
// --- Configuración ---
|
||||
void setPos(SDL_FRect rect); // Establece la posición y el tamaño del objeto
|
||||
@@ -47,8 +46,10 @@ class MovingSprite : public Sprite {
|
||||
void setAngle(double value) { rotate_.angle = value; } // Establece el ángulo
|
||||
void setRotatingCenter(SDL_FPoint point) { rotate_.center = point; } // Establece el centro de rotación
|
||||
void setRotate(bool enable); // Activa o desactiva el efecto de rotación
|
||||
void setRotateSpeed(int value) { rotate_.speed = std::max(1, value); } // Establece la velocidad de rotación
|
||||
void setRotateAmount(double value) { rotate_.amount = value; } // Establece la cantidad de rotación
|
||||
void startRotate(); // Habilita la rotación con centro automático
|
||||
void stopRotate(float threshold = 0.0F); // Detiene la rotación y resetea ángulo
|
||||
void setRotateAmount(double value) { rotate_.amount = value; } // Establece la velocidad de rotación
|
||||
void scaleRotateAmount(float value) { rotate_.amount *= value; } // Modifica la velocidad de rotacion
|
||||
void switchRotate() { rotate_.amount *= -1; } // Cambia el sentido de la rotación
|
||||
void setFlip(SDL_FlipMode flip) { flip_ = flip; } // Establece el flip
|
||||
void flip() { flip_ = (flip_ == SDL_FLIP_HORIZONTAL) ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL; } // Cambia el flip
|
||||
|
||||
@@ -87,7 +87,6 @@ auto setParams(const std::string& var, const std::string& value) -> bool {
|
||||
static const std::unordered_map<std::string, std::function<void(const std::string&)>> INT_PARAMS = {
|
||||
{"game.width", [](const std::string& v) { param.game.width = std::stoi(v); }},
|
||||
{"game.height", [](const std::string& v) { param.game.height = std::stoi(v); }},
|
||||
{"game.item_size", [](const std::string& v) { param.game.item_size = std::stoi(v); }},
|
||||
{"game.play_area.rect.x", [](const std::string& v) { param.game.play_area.rect.x = std::stoi(v); }},
|
||||
{"game.play_area.rect.y", [](const std::string& v) { param.game.play_area.rect.y = std::stoi(v); }},
|
||||
{"game.play_area.rect.w", [](const std::string& v) { param.game.play_area.rect.w = std::stoi(v); }},
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
struct ParamGame {
|
||||
float width = GameDefaults::Game::WIDTH;
|
||||
float height = GameDefaults::Game::HEIGHT;
|
||||
float item_size = GameDefaults::Game::ITEM_SIZE;
|
||||
Zone play_area{}; // Se inicializa en el constructor de Param
|
||||
Zone game_area{}; // Se inicializa en el constructor de Param
|
||||
int name_entry_idle_time = GameDefaults::Game::NAME_ENTRY_IDLE_TIME;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "animated_sprite.h" // Para AnimationsFileBuffer
|
||||
#include "text.h" // Para Text, TextFile
|
||||
#include "texture.h" // Para Texture
|
||||
#include "utils.h" // Para DemoData
|
||||
#include "demo.h" // Para DemoData
|
||||
|
||||
struct JA_Music_t;
|
||||
struct JA_Sound_t;
|
||||
|
||||
@@ -786,8 +786,8 @@ void Game::throwCoffee(int x, int y) {
|
||||
|
||||
smart_sprites_.back()->setPosX(x - 8);
|
||||
smart_sprites_.back()->setPosY(y - 8);
|
||||
smart_sprites_.back()->setWidth(param.game.item_size);
|
||||
smart_sprites_.back()->setHeight(param.game.item_size);
|
||||
smart_sprites_.back()->setWidth(Item::WIDTH);
|
||||
smart_sprites_.back()->setHeight(Item::HEIGHT);
|
||||
smart_sprites_.back()->setVelX((-1.0F + ((rand() % 5) * 0.5F)) * 60.0f); // Convertir a pixels/segundo
|
||||
smart_sprites_.back()->setVelY(-4.0F * 60.0f); // Convertir a pixels/segundo
|
||||
smart_sprites_.back()->setAccelX(0.0F);
|
||||
@@ -796,10 +796,9 @@ void Game::throwCoffee(int x, int y) {
|
||||
smart_sprites_.back()->setDestY(param.game.height + 1);
|
||||
smart_sprites_.back()->setEnabled(true);
|
||||
smart_sprites_.back()->setFinishedDelay(0.0F);
|
||||
smart_sprites_.back()->setSpriteClip(0, param.game.item_size, param.game.item_size, param.game.item_size);
|
||||
smart_sprites_.back()->setRotatingCenter({param.game.item_size / 2, param.game.item_size / 2});
|
||||
smart_sprites_.back()->setSpriteClip(0, Item::HEIGHT, Item::WIDTH, Item::HEIGHT);
|
||||
smart_sprites_.back()->setRotatingCenter({Item::WIDTH / 2, Item::HEIGHT / 2});
|
||||
smart_sprites_.back()->setRotate(true);
|
||||
smart_sprites_.back()->setRotateSpeed(10);
|
||||
smart_sprites_.back()->setRotateAmount(90.0);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,8 @@
|
||||
#include "player.h" // Para Player
|
||||
#include "smart_sprite.h" // Para SmartSprite
|
||||
#include "stage.h" // Para StageManager
|
||||
#include "utils.h" // Para Demo
|
||||
#include "demo.h" // Para Demo
|
||||
#include "utils.h" // Para otras utilidades
|
||||
|
||||
class Background;
|
||||
class Balloon;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "global_events.h" // Para check
|
||||
#include "global_inputs.h" // Para check
|
||||
#include "input.h" // Para Input
|
||||
#include "item.h" // Para Item
|
||||
#include "lang.h" // Para getText
|
||||
#include "param.h" // Para Param, param, ParamGame, ParamFade, Param...
|
||||
#include "resource.h" // Para Resource
|
||||
@@ -78,43 +79,43 @@ void Instructions::iniSprites() {
|
||||
|
||||
// Inicializa los sprites
|
||||
for (int i = 0; i < (int)item_textures_.size(); ++i) {
|
||||
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, param.game.item_size, param.game.item_size);
|
||||
sprite->setPosition((SDL_FPoint){sprite_pos_.x, sprite_pos_.y + ((param.game.item_size + item_space_) * i)});
|
||||
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, Item::WIDTH, Item::HEIGHT);
|
||||
sprite->setPosition((SDL_FPoint){sprite_pos_.x, sprite_pos_.y + ((Item::HEIGHT + item_space_) * i)});
|
||||
sprites_.push_back(std::move(sprite));
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los sprites
|
||||
void Instructions::updateSprites() {
|
||||
SDL_FRect src_rect = {0, 0, param.game.item_size, param.game.item_size};
|
||||
SDL_FRect src_rect = {0, 0, Item::WIDTH, Item::HEIGHT};
|
||||
|
||||
// Disquito (desplazamiento 12/60 = 0.2s)
|
||||
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.2f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
sprites_[0]->setSpriteClip(src_rect);
|
||||
|
||||
// Gavina (desplazamiento 9/60 = 0.15s)
|
||||
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.15f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
sprites_[1]->setSpriteClip(src_rect);
|
||||
|
||||
// Pacmar (desplazamiento 6/60 = 0.1s)
|
||||
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.1f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
sprites_[2]->setSpriteClip(src_rect);
|
||||
|
||||
// Time Stopper (desplazamiento 3/60 = 0.05s)
|
||||
src_rect.y = param.game.item_size * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
src_rect.y = Item::HEIGHT * (static_cast<int>((elapsed_time_ + 0.05f) / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
sprites_[3]->setSpriteClip(src_rect);
|
||||
|
||||
// Coffee (sin desplazamiento)
|
||||
src_rect.y = param.game.item_size * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
src_rect.y = Item::HEIGHT * (static_cast<int>(elapsed_time_ / SPRITE_ANIMATION_CYCLE_S) % 2);
|
||||
sprites_[4]->setSpriteClip(src_rect);
|
||||
}
|
||||
|
||||
// Rellena la textura de texto
|
||||
void Instructions::fillTexture() {
|
||||
const int X_OFFSET = param.game.item_size + 8;
|
||||
const int X_OFFSET = Item::WIDTH + 8;
|
||||
|
||||
// Modifica el renderizador para pintar en la textura
|
||||
auto *temp = SDL_GetRenderTarget(renderer_);
|
||||
auto* temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, texture_);
|
||||
|
||||
// Limpia la textura
|
||||
@@ -130,7 +131,7 @@ void Instructions::fillTexture() {
|
||||
constexpr int SPACE_POST_HEADER = 20;
|
||||
constexpr int SPACE_PRE_HEADER = 28;
|
||||
const int SPACE_BETWEEN_LINES = text_->getCharacterSize() * 1.5F;
|
||||
const int SPACE_BETWEEN_ITEM_LINES = param.game.item_size + item_space_;
|
||||
const int SPACE_BETWEEN_ITEM_LINES = Item::HEIGHT + item_space_;
|
||||
const int SPACE_NEW_PARAGRAPH = SPACE_BETWEEN_LINES * 0.5F;
|
||||
|
||||
const int SIZE = (NUM_LINES * SPACE_BETWEEN_LINES) + (NUM_ITEM_LINES * SPACE_BETWEEN_ITEM_LINES) + (NUM_POST_HEADERS * SPACE_POST_HEADER) + (NUM_PRE_HEADERS * SPACE_PRE_HEADER) + (SPACE_NEW_PARAGRAPH);
|
||||
@@ -144,7 +145,7 @@ void Instructions::fillTexture() {
|
||||
Lang::getText("[INSTRUCTIONS] 09"),
|
||||
Lang::getText("[INSTRUCTIONS] 10"),
|
||||
Lang::getText("[INSTRUCTIONS] 11")};
|
||||
for (const auto &desc : ITEM_DESCRIPTIONS) {
|
||||
for (const auto& desc : ITEM_DESCRIPTIONS) {
|
||||
const int L = text_->length(desc);
|
||||
length = L > length ? L : length;
|
||||
}
|
||||
@@ -178,13 +179,13 @@ void Instructions::fillTexture() {
|
||||
|
||||
// Da valor a la variable
|
||||
sprite_pos_.x = ANCHOR_ITEM;
|
||||
sprite_pos_.y = ANCHOR3 - ((param.game.item_size - text_->getCharacterSize()) / 2);
|
||||
sprite_pos_.y = ANCHOR3 - ((Item::HEIGHT - text_->getCharacterSize()) / 2);
|
||||
}
|
||||
|
||||
// Rellena el backbuffer
|
||||
void Instructions::fillBackbuffer() {
|
||||
// Modifica el renderizador para pintar en la textura
|
||||
auto *temp = SDL_GetRenderTarget(renderer_);
|
||||
auto* temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, backbuffer_);
|
||||
|
||||
// Limpia la textura
|
||||
@@ -195,7 +196,7 @@ void Instructions::fillBackbuffer() {
|
||||
SDL_RenderTexture(renderer_, texture_, nullptr, nullptr);
|
||||
|
||||
// Dibuja los sprites
|
||||
for (auto &sprite : sprites_) {
|
||||
for (auto& sprite : sprites_) {
|
||||
sprite->render();
|
||||
}
|
||||
|
||||
@@ -207,7 +208,7 @@ void Instructions::fillBackbuffer() {
|
||||
void Instructions::update(float delta_time) {
|
||||
elapsed_time_ += delta_time; // Incrementa el tiempo transcurrido
|
||||
|
||||
static auto *const SCREEN = Screen::get();
|
||||
static auto* const SCREEN = Screen::get();
|
||||
SCREEN->update(delta_time); // Actualiza el objeto screen
|
||||
Audio::update(); // Actualiza el objeto audio
|
||||
|
||||
@@ -220,7 +221,7 @@ void Instructions::update(float delta_time) {
|
||||
|
||||
// Pinta en pantalla
|
||||
void Instructions::render() {
|
||||
static auto *const SCREEN = Screen::get();
|
||||
static auto* const SCREEN = Screen::get();
|
||||
|
||||
SCREEN->start(); // Prepara para empezar a dibujar en la textura de juego
|
||||
SCREEN->clean(); // Limpia la pantalla
|
||||
@@ -287,11 +288,11 @@ auto Instructions::initializeLines(int height) -> std::vector<Line> {
|
||||
}
|
||||
|
||||
// Método para mover las líneas con suavizado
|
||||
auto Instructions::moveLines(std::vector<Line> &lines, int width, float duration, Uint32 start_delay) -> bool {
|
||||
auto Instructions::moveLines(std::vector<Line>& lines, int width, float duration, Uint32 start_delay) -> bool {
|
||||
Uint32 current_time = SDL_GetTicks();
|
||||
bool all_lines_off_screen = true;
|
||||
|
||||
for (auto &line : lines) {
|
||||
for (auto& line : lines) {
|
||||
// Establecer start_time en el primer cuadro de animación
|
||||
if (line.start_time == 0) {
|
||||
line.start_time = current_time + line.y * start_delay;
|
||||
@@ -316,8 +317,8 @@ auto Instructions::moveLines(std::vector<Line> &lines, int width, float duration
|
||||
}
|
||||
|
||||
// Método para renderizar las líneas
|
||||
void Instructions::renderLines(SDL_Renderer *renderer, SDL_Texture *texture, const std::vector<Line> &lines) {
|
||||
for (const auto &line : lines) {
|
||||
void Instructions::renderLines(SDL_Renderer* renderer, SDL_Texture* texture, const std::vector<Line>& lines) {
|
||||
for (const auto& line : lines) {
|
||||
SDL_FRect src_rect = {0, static_cast<float>(line.y), 320, 1};
|
||||
SDL_FRect dst_rect = {static_cast<float>(line.x), static_cast<float>(line.y), 320, 1};
|
||||
SDL_RenderTexture(renderer, texture, &src_rect, &dst_rect);
|
||||
|
||||
@@ -320,68 +320,6 @@ void printWithDots(const std::string &text1, const std::string &text2, const std
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", formatted_text.c_str());
|
||||
}
|
||||
|
||||
// Carga el fichero de datos para la demo
|
||||
auto loadDemoDataFromFile(const std::string &file_path) -> DemoData {
|
||||
DemoData dd;
|
||||
|
||||
SDL_IOStream *file = nullptr;
|
||||
|
||||
// Intentar cargar desde ResourceHelper primero
|
||||
auto resource_data = ResourceHelper::loadFile(file_path);
|
||||
if (!resource_data.empty()) {
|
||||
file = SDL_IOFromConstMem(resource_data.data(), resource_data.size());
|
||||
} else {
|
||||
// Fallback a filesystem directo
|
||||
file = SDL_IOFromFile(file_path.c_str(), "r+b");
|
||||
}
|
||||
|
||||
if (file == nullptr) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Fichero no encontrado %s", file_path.c_str());
|
||||
throw std::runtime_error("Fichero no encontrado: " + file_path);
|
||||
}
|
||||
printWithDots("DemoData : ", getFileName(file_path), "[ LOADED ]");
|
||||
|
||||
// Lee todos los datos del fichero y los deja en el destino
|
||||
for (int i = 0; i < TOTAL_DEMO_DATA; ++i) {
|
||||
DemoKeys dk = DemoKeys();
|
||||
SDL_ReadIO(file, &dk, sizeof(DemoKeys));
|
||||
dd.push_back(dk);
|
||||
}
|
||||
|
||||
// Cierra el fichero
|
||||
SDL_CloseIO(file);
|
||||
|
||||
return dd;
|
||||
}
|
||||
|
||||
#ifdef RECORDING
|
||||
// Guarda el fichero de datos para la demo
|
||||
bool saveDemoFile(const std::string &file_path, const DemoData &dd) {
|
||||
auto success = true;
|
||||
auto file = SDL_IOFromFile(file_path.c_str(), "w+b");
|
||||
|
||||
if (file) {
|
||||
// Guarda los datos
|
||||
for (const auto &data : dd) {
|
||||
if (SDL_WriteIO(file, &data, sizeof(DemoKeys)) != sizeof(DemoKeys)) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error al escribir el fichero %s", getFileName(file_path).c_str());
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Writing file %s", getFileName(file_path).c_str());
|
||||
}
|
||||
// Cierra el fichero
|
||||
SDL_CloseIO(file);
|
||||
} else {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Unable to save %s file! %s", getFileName(file_path).c_str(), SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
#endif // RECORDING
|
||||
|
||||
// Obtiene el nombre de un fichero a partir de una ruta completa
|
||||
auto getFileName(const std::string &path) -> std::string {
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
|
||||
// --- Constantes ---
|
||||
constexpr int BLOCK = 8;
|
||||
constexpr int TOTAL_DEMO_DATA = 2000;
|
||||
|
||||
// --- Estructuras ---
|
||||
struct Overrides {
|
||||
@@ -32,43 +31,6 @@ struct Circle {
|
||||
r(radius) {}
|
||||
};
|
||||
|
||||
struct DemoKeys {
|
||||
Uint8 left;
|
||||
Uint8 right;
|
||||
Uint8 no_input;
|
||||
Uint8 fire;
|
||||
Uint8 fire_left;
|
||||
Uint8 fire_right;
|
||||
|
||||
explicit DemoKeys(Uint8 l = 0, Uint8 r = 0, Uint8 ni = 0, Uint8 f = 0, Uint8 fl = 0, Uint8 fr = 0)
|
||||
: left(l),
|
||||
right(r),
|
||||
no_input(ni),
|
||||
fire(f),
|
||||
fire_left(fl),
|
||||
fire_right(fr) {}
|
||||
};
|
||||
|
||||
// --- Tipos ---
|
||||
using DemoData = std::vector<DemoKeys>;
|
||||
|
||||
struct Demo {
|
||||
bool enabled = false; // Indica si está activo el modo demo
|
||||
bool recording = false; // Indica si está activado el modo para grabar la demo
|
||||
float elapsed_s = 0.0F; // Segundos transcurridos de demo
|
||||
int index = 0; // Contador para el modo demo
|
||||
DemoKeys keys; // Variable con las pulsaciones de teclas del modo demo
|
||||
std::vector<DemoData> data; // Vector con diferentes sets de datos con los movimientos para la demo
|
||||
|
||||
Demo() = default;
|
||||
|
||||
Demo(bool e, bool r, int c, const DemoKeys& k, const std::vector<DemoData>& d)
|
||||
: enabled(e),
|
||||
recording(r),
|
||||
index(c),
|
||||
keys(k),
|
||||
data(d) {}
|
||||
};
|
||||
|
||||
struct Zone {
|
||||
SDL_FRect rect; // Rectangulo que define la zona
|
||||
@@ -126,12 +88,6 @@ auto stringInVector(const std::vector<std::string>& vec, const std::string& str)
|
||||
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Imprime una línea con puntos
|
||||
auto truncateWithEllipsis(const std::string& input, size_t length) -> std::string; // Trunca un string y le añade puntos suspensivos
|
||||
|
||||
// Demo
|
||||
auto loadDemoDataFromFile(const std::string& file_path) -> DemoData;
|
||||
|
||||
#ifdef RECORDING
|
||||
bool saveDemoFile(const std::string& file_path, const DemoData& dd);
|
||||
#endif
|
||||
|
||||
// Ficheros y rutas
|
||||
auto getFileName(const std::string& path) -> std::string; // Obtiene el nombre de un fichero a partir de una ruta
|
||||
|
||||
Reference in New Issue
Block a user