Compare commits

34 Commits

Author SHA1 Message Date
8941072357 Els items reboten al tocar el piso 2024-11-20 21:55:55 +01:00
77bf1d73b3 Afegits els grafics de caure derrotat per al segon jugador 2024-11-19 19:30:30 +01:00
700d3846fb Afegits rebots en la animació de morir 2024-11-19 18:59:35 +01:00
185a1b47d1 Afegida llengua fora i ulls en X a la animació de derrotat 2024-11-19 18:59:22 +01:00
121774e460 Nova animació de jugador derrotat 2024-11-19 18:33:52 +01:00
47e468034f Nova animació per a la mort del personatge
Nova lògica al morir
2024-11-17 08:43:24 +01:00
da74b8dfce La powerball ja no mata
La powerball no pillava la rotació si es creava amb el rellotge actiu
2024-11-16 12:45:58 +01:00
065336c310 Els globos fills ja no ixen centrats al pare, si no al final havia varios apilats 2024-11-16 12:24:20 +01:00
79d25fb812 Nous grafics per a la powerball
Nou comportament per a la powerball
2024-11-16 12:13:00 +01:00
6262b5814d Arreglos varios relacionats amb el nom al obtenir la màxima puntuació:
No canviava al marcador, ni el nom del que tenia la maxima puntuació en calent ni al posar nom
retallat el nom de 8 a 6 caracters, i tots en majuscula pa que capia en el marcador
ja actualitza be la cadena amb el nom al posar nom per segona vegada en la mateixa partida
2024-11-08 20:49:07 +01:00
f9520185a2 Acabat BalloonManager 2024-11-08 18:29:08 +01:00
2fb7e88e4b Continue amb BalloonManager 2024-11-07 20:56:56 +01:00
0e527ff9d9 Au, a dormir que tinc son 2024-11-06 22:22:35 +01:00
d902bb9088 canvi de pc 2024-11-06 17:22:16 +01:00
caf04e3a7e Treballant en BalloonManager 2024-11-05 22:06:15 +01:00
12213a3dab Toooooornem a commitaaaar, aci vinga jugar al ping pong entre windows i macos i el puto copilot. Apanye aci i trenque allà 2024-11-05 17:54:23 +01:00
1f2a8ae38d Canvis en CMakeLists.txt 2024-11-05 17:38:26 +01:00
aa8d3502e2 Actualitzat make i cmake 2024-11-05 16:51:41 +01:00
e445a0b218 CMakeLists.txt crea el executable en la arrel del projecte 2024-11-05 13:13:43 +01:00
3f9c4b887f Apareixia brossa en la textura al crear un text i no netejarla primer (en el windows de la faena) 2024-11-05 13:06:32 +01:00
27ccae6132 Refet CMakeLists.txt 2024-11-05 13:05:52 +01:00
443f0f3254 Añadido CMakeLists.txt 2024-11-05 07:15:54 +01:00
2e62214a4b Canviat un #include <bits/chrono.h> per #include <chrono> 2024-11-04 22:26:31 +01:00
7b1c2a6005 Nova font de text per al text gran amb el doble de definició 2024-11-04 20:28:19 +01:00
2256ee46eb Modificada la paleta verda del primer jugador per a fer la mes pareguda a la del coffee crisis original 2024-11-04 19:39:27 +01:00
087fd3377c fix: els globos verds, al popparlos, uno tirava cap avall el molt cabró 2024-11-04 19:16:44 +01:00
30735f00e8 Els objectes de Text es precarreguen al inici 2024-11-04 19:08:18 +01:00
e0e82ee273 Retocs en Texture::loadPaletteFromFile 2024-11-03 20:43:52 +01:00
371c477d0d Varios arreglos 2024-11-03 20:28:01 +01:00
f29eb2f411 Les notificacions ara accepten un vector de cadenes en lloc de una o dos cadenes 2024-11-03 18:12:46 +01:00
69a92cba66 Apanyats alguns bugs que quedaven respecte a lo del teclat 2024-11-03 17:25:31 +01:00
86cd7b0f16 Ja es pot gastar el teclat com a control independent del primer mando
Ja pot jugar un jugador amb teclat i altre amb mando
Es pot asignar el teclat a qualsevol dels dos jugadors
Continua podentse gastar mando i teclat a l'hora per al mateix jugador
2024-11-03 11:07:58 +01:00
a1ccb6102a Abans de clavar-li ma a Input 2024-11-01 20:01:17 +01:00
2dd8bbbbf7 Moguts els checkInputs de Screen a GlobalInputs 2024-11-01 19:07:19 +01:00
60 changed files with 2529 additions and 2540 deletions

85
CMakeLists.txt Normal file
View File

@@ -0,0 +1,85 @@
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(coffee_crisis_arcade_edition VERSION 0.01)
# Configuración de compilador para MinGW en Windows, si es necesario
if(WIN32 AND NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_C_COMPILER "gcc")
endif()
# Establecer estándar de C++
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# Configuración global de flags de compilación
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os -ffunction-sections -fdata-sections")
# Define el directorio de los archivos fuente
set(DIR_SOURCES "${CMAKE_SOURCE_DIR}/source")
# Cargar todos los archivos fuente en DIR_SOURCES
file(GLOB SOURCES "${DIR_SOURCES}/*.cpp")
# Verificar si se encontraron archivos fuente
if(NOT SOURCES)
message(FATAL_ERROR "No se encontraron archivos fuente en ${DIR_SOURCES}. Verifica que el directorio existe y contiene archivos .cpp.")
endif()
# Configuración de SDL2
find_package(SDL2 REQUIRED)
if(SDL2_FOUND)
message(STATUS "SDL2 encontrado: ${SDL2_INCLUDE_DIRS}")
include_directories(${SDL2_INCLUDE_DIRS})
link_directories(${SDL2_LIBDIR})
else()
message(FATAL_ERROR "SDL2 no encontrado")
endif()
# Incluye rutas de SDL2 obtenidas con pkg-config
include_directories(/usr/local/include /usr/local/include/SDL2)
link_directories(/usr/local/lib)
# Definir las bibliotecas comunes
set(LIBS SDL2)
# Configuración común de salida de ejecutables en el directorio raíz
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
# Añadir ejecutable principal
add_executable(${PROJECT_NAME} ${SOURCES})
# Añadir definiciones de compilación dependiendo del tipo de build
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:DEBUG VERBOSE>)
# Enlazar bibliotecas
target_link_libraries(${PROJECT_NAME} ${LIBS})
# Configuración específica para cada plataforma
if(WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
target_link_libraries(${PROJECT_NAME} mingw32 opengl32 gdi32 winmm imm32 ole32 version)
elseif(APPLE)
set(LIBS ${LIBS} "-framework OpenGL")
target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated")
# Configurar compilación para Apple Silicon
set(CMAKE_OSX_ARCHITECTURES "arm64")
elseif(UNIX AND NOT APPLE)
set(LIBS ${LIBS} GL)
target_compile_definitions(${PROJECT_NAME} PRIVATE LINUX_BUILD)
target_link_libraries(${PROJECT_NAME} ${LIBS})
endif()
# Añadir OpenGL a las bibliotecas enlazadas
if(NOT WIN32)
find_package(OpenGL REQUIRED)
if(OPENGL_FOUND)
message(STATUS "OpenGL encontrado: ${OPENGL_LIBRARIES}")
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES})
else()
message(FATAL_ERROR "OpenGL no encontrado")
endif()
endif()

View File

@@ -50,84 +50,7 @@ else
endif endif
endif endif
OBJECTS := $(subst $(DIR_SOURCES), $(DIR_BUILD), $(SOURCES)) # Rules
OBJECTS := $(OBJECTS:.cpp=.o)
DEPENDENCIES:= $(OBJECTS:.o=.d)
###############################################################################
# #
# RULES #
# #
###############################################################################
.PHONY: all a1
all: a1
a1: $(TARGET_FILE)
$(TARGET_FILE): $(OBJECTS)
$(MKD) $(@D)
$(CXX) $(OBJECTS) $(LDFLAGS) -o $(TARGET_FILE)
$(DIR_BUILD)%.o: $(DIR_SOURCES)%.cpp
$(MKD) $(@D)
$(CXX) -c $< $(CXXFLAGS) $(INCLUDES) -o $@
-include $(DEPENDENCIES)
###############################################################################
# #
# CLEAN #
# #
###############################################################################
.PHONY: clean
clean:
$(RM) $(call FixPath,$(DIR_BUILD))
###############################################################################
# #
# PRINT-VARIABLES #
# #
###############################################################################
.PHONY: print-variables
print-variables:
@echo MAKEFILE_LIST: $(MAKEFILE_LIST)
@echo "DIR_ROOT :" $(DIR_ROOT)
@echo "DIR_SOURCES:" $(DIR_SOURCES)
@echo "DIR_BIN :" $(DIR_BIN)
@echo "DIR_BUILD :" $(DIR_BUILD)
@echo "DIR_IMGUI :" $(DIR_IMGUI)
@echo "DIR_IMGUI_SFML:" $(DIR_IMGUI_SFML)
@echo "INCLUDES :" $(INCLUDES)
@echo CXX: $(CXX)
@echo CXXFLAGS: $(CXXFLAGS)
@echo LDFLAGS: $(LDFLAGS)
@echo SOURCES: $(SOURCES)
@echo OBJECTS: $(OBJECTS)
@echo DEPENDENCIES: $(DEPENDENCIES)
@echo TARGET_NAME: $(TARGET_NAME)
@echo TARGET_FILE: $(TARGET_FILE)
@echo RM: $(RM)
raspi:
$(CXX) $(SOURCES) -D ARCADE -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(TARGET_FILE)
strip -s -R .comment -R .gnu.version $(TARGET_FILE) --strip-unneeded
raspi_debug:
$(CXX) $(SOURCES) -D ARCADE -D VERBOSE -D DEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
windows: windows:
@echo off @echo off
$(CXX) $(SOURCES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE).exe" $(CXX) $(SOURCES) $(CXXFLAGS) $(LDFLAGS) -o "$(TARGET_FILE).exe"
@@ -249,6 +172,13 @@ linux_release:
# Elimina la carpeta temporal # Elimina la carpeta temporal
$(RM) "$(RELEASE_FOLDER)" $(RM) "$(RELEASE_FOLDER)"
raspi:
$(CXX) $(SOURCES) -D ARCADE -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(TARGET_FILE)
strip -s -R .comment -R .gnu.version $(TARGET_FILE) --strip-unneeded
raspi_debug:
$(CXX) $(SOURCES) -D ARCADE -D VERBOSE -D DEBUG $(CXXFLAGS_DEBUG) $(LDFLAGS) -o "$(TARGET_FILE)_debug"
anbernic: anbernic:
# Elimina carpetas previas # Elimina carpetas previas
$(RM) "$(RELEASE_FOLDER)"_anbernic $(RM) "$(RELEASE_FOLDER)"_anbernic
@@ -259,6 +189,5 @@ anbernic:
# Copia ficheros # Copia ficheros
cp -R data "$(RELEASE_FOLDER)"_anbernic cp -R data "$(RELEASE_FOLDER)"_anbernic
# Complia # Compila
$(CXX) $(SOURCES) -D ANBERNIC -D NO_SHADERS -D ARCADE -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(RELEASE_FOLDER)_anbernic/$(TARGET_NAME) $(CXX) $(SOURCES) -D ANBERNIC -D NO_SHADERS -D ARCADE -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(RELEASE_FOLDER)_anbernic/$(TARGET_NAME)
$(CXX) $(SOURCES) -D ANBERNIC -D ARCADE -D VERBOSE $(CXXFLAGS) $(LDFLAGS) -o $(RELEASE_FOLDER)_anbernic/$(TARGET_NAME).shaders

BIN
data/font/04b_25_2x.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

194
data/font/04b_25_2x.txt Normal file
View File

@@ -0,0 +1,194 @@
# box width
28
# box height
28
# 32 espacio ( )
16
# 33 !
10
# 34 "
16
# 35
20
# 36 $
20
# 37 %
18
# 38 &
22
# 39 '
10
# 40 (
14
# 41 )
14
# 42 *
14
# 43 +
18
# 44 ,
10
# 45 -
18
# 46 .
10
# 47 /
24
# 48 0
16
# 49 1
12
# 50 2
16
# 51 3
16
# 52 4
16
# 53 5
16
# 54 6
16
# 55 7
16
# 56 8
16
# 57 9
16
# 58 :
10
# 59 ;
10
# 60 <
16
# 61 =
16
# 62 >
16
# 63 ?
16
# 64 @
22
# 65 A
16
# 66 B
16
# 67 C
16
# 68 D
16
# 69 E
16
# 70 F
16
# 71 G
16
# 72 H
16
# 73 I
10
# 74 J
16
# 75 K
16
# 76 L
16
# 77 M
22
# 78 N
16
# 79 O
16
# 80 P
16
# 81 Q
16
# 82 R
16
# 83 S
16
# 84 T
18
# 85 U
16
# 86 V
16
# 87 W
22
# 88 X
16
# 89 Y
16
# 90 Z
16
# 91 [
14
# 92 \
22
# 93 ]
14
# 94 ^
12
# 95 _
14
# 96 `
12
# 97 a
16
# 98 b
16
# 99 c
16
# 100 d
16
# 101 e
16
# 102 f
16
# 103 g
16
# 104 h
16
# 105 i
10
# 106 j
16
# 107 k
16
# 108 l
16
# 109 m
22
# 110 n
16
# 111 o
16
# 112 p
16
# 113 q
16
# 114 r
16
# 115 s
16
# 116 t
18
# 117 u
16
# 118 v
16
# 119 w
22
# 120 x
16
# 121 y
16
# 122 z
16
# 123 {
2
# 124 |
2
# 125 }
2
# 126 ~
2

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -72,12 +72,19 @@ frames=27
[/animation] [/animation]
[animation] [animation]
name=death name=dying
speed=10 speed=10
loop=0 loop=0
frames=32,33,34,35 frames=32,33,34,35
[/animation] [/animation]
[animation]
name=dead
speed=3
loop=0
frames=44,45,46,47,48,49,50
[/animation]
[animation] [animation]
name=celebration name=celebration
speed=10 speed=10

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 176 B

After

Width:  |  Height:  |  Size: 173 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

View File

@@ -158,19 +158,19 @@ Felicitats!!
2 JUGADORS 2 JUGADORS
## 53 MARCADOR ## 53 MARCADOR
jugador 1 Jugador 1
## 54 MARCADOR ## 54 MARCADOR
jugador 2 Jugador 2
## 55 MARCADOR ## 55 MARCADOR
mult Multiplicador
## 56 MARCADOR ## 56 MARCADOR
max. puntuacio Max. puntuacio
## 57 MARCADOR ## 57 MARCADOR
fase Fase
## 58 - MENU DE OPCIONES ## 58 - MENU DE OPCIONES
MODE DE VISUALITZACIO MODE DE VISUALITZACIO
@@ -206,7 +206,7 @@ NORMAL
DIFICIL DIFICIL
## 69 - MENU DE OPCIONES ## 69 - MENU DE OPCIONES
TECLAT Teclat
## 70 - MENU DE OPCIONES ## 70 - MENU DE OPCIONES
MANDO MANDO

View File

@@ -158,19 +158,19 @@ Congratulations!!
2 PLAYERS 2 PLAYERS
## 53 - MARCADOR ## 53 - MARCADOR
player 1 Player 1
## 54 - MARCADOR ## 54 - MARCADOR
player 2 Player 2
## 55 - MARCADOR ## 55 - MARCADOR
mult Multiplier
## 56 - MARCADOR ## 56 - MARCADOR
high score High Score
## 57 - MARCADOR ## 57 - MARCADOR
stage Stage
## 58 - MENU DE OPCIONES ## 58 - MENU DE OPCIONES
DISPLAY MODE DISPLAY MODE
@@ -206,7 +206,7 @@ NORMAL
HARD HARD
## 69 - MENU DE OPCIONES ## 69 - MENU DE OPCIONES
KEYBOARD Keyboard
## 70 - MENU DE OPCIONES ## 70 - MENU DE OPCIONES
GAME CONTROLLER GAME CONTROLLER

View File

@@ -158,19 +158,19 @@ Felicidades!!
2 JUGADORES 2 JUGADORES
## 53 - MARCADOR ## 53 - MARCADOR
jugador 1 Jugador 1
## 54 - MARCADOR ## 54 - MARCADOR
jugador 2 Jugador 2
## 55 - MARCADOR ## 55 - MARCADOR
mult Multiplicador
## 56 - MARCADOR ## 56 - MARCADOR
max. puntuacion Max. puntuacion
## 57 - MARCADOR ## 57 - MARCADOR
FASE Fase
## 58 - MENU DE OPCIONES ## 58 - MENU DE OPCIONES
MODO DE VISUALIZACION MODO DE VISUALIZACION
@@ -206,7 +206,7 @@ NORMAL
DIFICIL DIFICIL
## 69 - MENU DE OPCIONES ## 69 - MENU DE OPCIONES
TECLADO Teclado
## 70 - MENU DE OPCIONES ## 70 - MENU DE OPCIONES
MANDO MANDO

View File

@@ -56,7 +56,7 @@ Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel
case BalloonType::POWERBALL: case BalloonType::POWERBALL:
{ {
const int index = 3; const int index = 3;
h_ = w_ = BALLOON_SIZE[index]; h_ = w_ = BALLOON_SIZE[4];
power_ = score_ = menace_ = 0; power_ = score_ = menace_ = 0;
vy_ = 0; vy_ = 0;
@@ -64,8 +64,7 @@ Balloon::Balloon(float x, float y, BalloonType type, BalloonSize size, float vel
gravity_ = param.balloon.at(index).grav; gravity_ = param.balloon.at(index).grav;
default_vy_ = param.balloon.at(index).vel; default_vy_ = param.balloon.at(index).vel;
sprite_->disableRotate(); sprite_->setRotate(creation_timer <= 0);
sprite_->setRotateSpeed(0);
sprite_->setRotateAmount(vx_ > 0.0f ? 2.0 : -2.0); sprite_->setRotateAmount(vx_ > 0.0f ? 2.0 : -2.0);
break; break;
@@ -100,32 +99,42 @@ void Balloon::alignTo(int x)
// Pinta el globo en la pantalla // Pinta el globo en la pantalla
void Balloon::render() void Balloon::render()
{ {
if (isBeingCreated()) if (type_ == BalloonType::POWERBALL)
{ {
// Aplica alpha blending // Renderizado para la PowerBall
sprite_->getTexture()->setAlpha(255 - (int)((float)creation_counter_ * (255.0f / (float)creation_counter_ini_))); SDL_Point p = {24, 24};
sprite_->setRotatingCenter(&p);
sprite_->render(); sprite_->render();
sprite_->getTexture()->setAlpha(255); // Añade la máscara del borde y los reflejos
auto sp = std::make_unique<Sprite>(sprite_->getTexture(), sprite_->getPosition());
sp->setSpriteClip(BALLOON_SIZE[4], 0, BALLOON_SIZE[4], BALLOON_SIZE[4]);
sp->render();
} }
else else
{ {
if (bouncing_.enabled) // Renderizado para el resto de globos
if (isBeingCreated())
{ {
// Aplica efecto de bouncing // Renderizado con transparencia
sprite_->setPos(x_ + bouncing_.despX, y_ + bouncing_.despY); sprite_->getTexture()->setAlpha(255 - (int)((float)creation_counter_ * (255.0f / (float)creation_counter_ini_)));
sprite_->render(); sprite_->render();
sprite_->setPos(x_ - bouncing_.despX, y_ - bouncing_.despY); sprite_->getTexture()->setAlpha(255);
} }
else else
sprite_->render(); {
} if (bouncing_.enabled)
{
// Añade la máscara del borde a la PowerBall // Renderizado con efecto de bouncing
if (type_ == BalloonType::POWERBALL && !isBeingCreated()) sprite_->setPos(x_ + bouncing_.despX, y_ + bouncing_.despY);
{ sprite_->render();
auto sp = std::make_unique<Sprite>(sprite_->getTexture(), sprite_->getPosition()); // sprite_->setPos(x_ - bouncing_.despX, y_ - bouncing_.despY);
sp->setSpriteClip(BALLOON_SIZE[3], 0, BALLOON_SIZE[3], BALLOON_SIZE[3]); }
sp->render(); else
{
// Renderizado normal
sprite_->render();
}
}
} }
} }
@@ -148,22 +157,28 @@ void Balloon::move()
vx_ = -vx_; vx_ = -vx_;
// Activa el efecto de rebote o invierte la rotación // Activa el efecto de rebote o invierte la rotación
if (type_ == BalloonType::POWERBALL) if (type_ == BalloonType::POWERBALL)
{
sprite_->switchRotate(); sprite_->switchRotate();
}
else else
{
enableBounce(); enableBounce();
}
} }
// Mueve el globo en vertical // Mueve el globo en vertical
y_ += vy_ * speed_; y_ += vy_ * speed_;
// Colisión en la parte superior de la zona de juego // Colisión en la parte superior de la zona de juego excepto para la PowerBall
const int min_y = param.game.play_area.rect.y; if (type_ != BalloonType::POWERBALL)
if (y_ < min_y)
{ {
y_ = min_y; const int min_y = param.game.play_area.rect.y;
vy_ = -vy_; if (y_ < min_y)
if (type_ != BalloonType::POWERBALL) {
y_ = min_y;
vy_ = -vy_;
enableBounce(); enableBounce();
}
} }
// Colisión en la parte inferior de la zona de juego // Colisión en la parte inferior de la zona de juego
@@ -173,7 +188,9 @@ void Balloon::move()
y_ = max_y; y_ = max_y;
vy_ = -default_vy_; vy_ = -default_vy_;
if (type_ != BalloonType::POWERBALL) if (type_ != BalloonType::POWERBALL)
{
enableBounce(); enableBounce();
}
} }
/* /*
@@ -294,16 +311,20 @@ void Balloon::setAnimation()
void Balloon::stop() void Balloon::stop()
{ {
stopped_ = true; stopped_ = true;
if (type_ == BalloonType::POWERBALL) if (isPowerBall())
sprite_->disableRotate(); {
sprite_->setRotate(!stopped_);
}
} }
// Pone el globo en movimiento // Pone el globo en movimiento
void Balloon::start() void Balloon::start()
{ {
stopped_ = false; stopped_ = false;
if (type_ == BalloonType::POWERBALL) if (isPowerBall())
sprite_->enableRotate(); {
sprite_->setRotate(!stopped_);
}
} }
// Alinea el circulo de colisión con la posición del objeto globo // Alinea el circulo de colisión con la posición del objeto globo
@@ -379,26 +400,3 @@ void Balloon::useNormalColor()
use_reversed_colors_ = false; use_reversed_colors_ = false;
setAnimation(); setAnimation();
} }
// Getters
float Balloon::getPosX() const { return x_; }
float Balloon::getPosY() const { return y_; }
int Balloon::getWidth() const { return w_; }
int Balloon::getHeight() const { return h_; }
BalloonSize Balloon::getSize() const { return size_; }
BalloonType Balloon::getType() const { return type_; }
Uint16 Balloon::getScore() const { return score_; }
Circle &Balloon::getCollider() { return collider_; }
Uint8 Balloon::getMenace() const { return isEnabled() ? menace_ : 0; }
Uint8 Balloon::getPower() const { return power_; }
bool Balloon::isStopped() const { return stopped_; }
bool Balloon::isInvulnerable() const { return invulnerable_; }
bool Balloon::isBeingCreated() const { return being_created_; }
bool Balloon::isEnabled() const { return enabled_; }
bool Balloon::isUsingReversedColor() { return use_reversed_colors_; }
bool Balloon::canBePopped() const { return !isBeingCreated(); }
// Setters
void Balloon::setVelY(float vel_y) { vy_ = vel_y; }
void Balloon::setSpeed(float speed) { speed_ = speed; }
void Balloon::setInvulnerable(bool value) { invulnerable_ = value; }

View File

@@ -15,7 +15,7 @@ constexpr int MAX_BOUNCE = 10;
constexpr int BALLOON_SCORE[] = {50, 100, 200, 400}; constexpr int BALLOON_SCORE[] = {50, 100, 200, 400};
constexpr int BALLOON_POWER[] = {1, 3, 7, 15}; constexpr int BALLOON_POWER[] = {1, 3, 7, 15};
constexpr int BALLOON_MENACE[] = {1, 2, 4, 8}; constexpr int BALLOON_MENACE[] = {1, 2, 4, 8};
constexpr int BALLOON_SIZE[] = {10, 16, 26, 46}; constexpr int BALLOON_SIZE[] = {10, 16, 26, 48, 49};
// Tamaños de globo // Tamaños de globo
enum class BalloonSize : Uint8 enum class BalloonSize : Uint8
@@ -174,25 +174,26 @@ public:
void useNormalColor(); void useNormalColor();
// Getters // Getters
float getPosX() const; float getPosX() const { return x_; }
float getPosY() const; float getPosY() const { return y_; }
int getWidth() const; int getWidth() const { return w_; }
int getHeight() const; int getHeight() const { return h_; }
BalloonSize getSize() const; BalloonSize getSize() const { return size_; }
BalloonType getType() const; BalloonType getType() const { return type_; }
Uint16 getScore() const; Uint16 getScore() const { return score_; }
Circle &getCollider(); Circle &getCollider() { return collider_; }
Uint8 getMenace() const; Uint8 getMenace() const { return isEnabled() ? menace_ : 0; }
Uint8 getPower() const; Uint8 getPower() const { return power_; }
bool isStopped() const; bool isStopped() const { return stopped_; }
bool isInvulnerable() const; bool isPowerBall() const { return type_ == BalloonType::POWERBALL; }
bool isBeingCreated() const; bool isInvulnerable() const { return invulnerable_; }
bool isEnabled() const; bool isBeingCreated() const { return being_created_; }
bool isUsingReversedColor(); bool isEnabled() const { return enabled_; }
bool canBePopped() const; bool isUsingReversedColor() { return use_reversed_colors_; }
bool canBePopped() const { return !isBeingCreated(); }
// Setters // Setters
void setVelY(float vel_y); void setVelY(float vel_y) { vy_ = vel_y; }
void setSpeed(float speed); void setSpeed(float speed) { speed_ = speed; }
void setInvulnerable(bool value); void setInvulnerable(bool value) { invulnerable_ = value; }
}; };

File diff suppressed because it is too large Load Diff

View File

@@ -1,13 +1,13 @@
#pragma once #pragma once
#include "balloon.h" #include "balloon.h" // para BALLOON_VELX_NEGATIVE, BALLOON_VELX_POSITIVE
#include <vector>
constexpr int NUMBER_OF_BALLOON_FORMATIONS = 100; constexpr int NUMBER_OF_BALLOON_FORMATIONS = 100;
constexpr int MAX_NUMBER_OF_BALLOONS_IN_A_FORMATION = 50; constexpr int MAX_NUMBER_OF_BALLOONS_IN_A_FORMATION = 50;
constexpr int NUMBER_OF_SETS_PER_POOL = 10; constexpr int NUMBER_OF_SETS_PER_POOL = 10;
constexpr int NUMBER_OF_STAGES = 10; constexpr int NUMBER_OF_STAGES = 10;
// Estructuras
struct BalloonFormationParams struct BalloonFormationParams
{ {
int x = 0; // Posición en el eje X donde crear el globo int x = 0; // Posición en el eje X donde crear el globo
@@ -25,34 +25,27 @@ struct BalloonFormationParams
: x(x_val), y(y_val), vel_x(vel_x_val), type(type_val), size(size_val), creation_counter(creation_counter_val) {} : x(x_val), y(y_val), vel_x(vel_x_val), type(type_val), size(size_val), creation_counter(creation_counter_val) {}
}; };
struct BalloonFormationUnit // Contiene la información de una formación enemiga struct BalloonFormationUnit
{ {
int number_of_balloons; // Cantidad de globos que forman la formación int number_of_balloons; // Cantidad de globos que forman la formación
BalloonFormationParams init[MAX_NUMBER_OF_BALLOONS_IN_A_FORMATION]; // Vector con todas las inicializaciones de los globos de la formación std::vector<BalloonFormationParams> init; // Vector con todas las inicializaciones de los globos de la formación
// Constructor
BalloonFormationUnit(int num_balloons, const std::vector<BalloonFormationParams> &init_params)
: number_of_balloons(num_balloons), init(init_params) {}
// Default constructor
BalloonFormationUnit() : number_of_balloons(0), init() {}
}; };
struct BalloonFormationPool
{
BalloonFormationUnit set[NUMBER_OF_SETS_PER_POOL]; // Conjunto de formaciones de globos
};
struct Stage // Contiene todas las variables relacionadas con una fase using BalloonFormationPool = std::vector<const BalloonFormationUnit *>;
{
BalloonFormationPool balloon_pool; // El conjunto de formaciones de globos de la fase
int power_to_complete; // Cantidad de poder que se necesita para completar la fase
int max_menace; // Umbral máximo de amenaza de la fase
int min_menace; // Umbral mínimo de amenaza de la fase
int number; // Número de fase
};
// Clase BalloonFormations, para gestionar las formaciones de globos
class BalloonFormations class BalloonFormations
{ {
private: private:
// Variables std::vector<BalloonFormationUnit> balloon_formation_; // Vector con todas las formaciones enemigas
Stage stage_[NUMBER_OF_STAGES]; // Variable con los datos de cada pantalla std::vector<BalloonFormationPool> balloon_formation_pool_; // Variable con los diferentes conjuntos de formaciones enemigas
BalloonFormationUnit balloon_formation_[NUMBER_OF_BALLOON_FORMATIONS]; // Vector con todas las formaciones enemigas
BalloonFormationPool balloon_formation_pool_[NUMBER_OF_STAGES]; // Variable con los diferentes conjuntos de formaciones enemigas
// Inicializa las formaciones enemigas // Inicializa las formaciones enemigas
void initBalloonFormations(); void initBalloonFormations();
@@ -60,16 +53,18 @@ private:
// Inicializa los conjuntos de formaciones // Inicializa los conjuntos de formaciones
void initBalloonFormationPools(); void initBalloonFormationPools();
// Inicializa las fases del juego
void initGameStages();
public: public:
// Constructor // Constructor
BalloonFormations(); BalloonFormations()
{
initBalloonFormations();
initBalloonFormationPools();
}
// Destructor // Destructor
~BalloonFormations() = default; ~BalloonFormations() = default;
// Devuelve una fase // Getters
Stage getStage(int index) const; const BalloonFormationPool &getPool(int pool) { return balloon_formation_pool_.at(pool); }
const BalloonFormationUnit &getSet(int pool, int set) { return *balloon_formation_pool_.at(pool).at(set); }
}; };

356
source/balloon_manager.cpp Normal file
View File

@@ -0,0 +1,356 @@
#include "balloon_manager.h"
#include <stdlib.h> // Para rand
#include <algorithm> // Para remove_if
#include <numeric> // Para accumulate
#include "balloon.h" // Para Balloon, BALLOON_SCORE, BALLOON_VELX...
#include "balloon_formations.h" // Para BalloonFormationParams, BalloonForma...
#include "explosions.h" // Para Explosions
#include "jail_audio.h" // Para JA_PlaySound
#include "param.h" // Para Param, ParamGame, param
#include "resource.h" // Para Resource
#include "screen.h" // Para Screen
#include "stage.h" // Para power
#include "texture.h" // Para Texture
#include "utils.h" // Para Zone, BLOCK, Color, flash_color
// Constructor
BalloonManager::BalloonManager()
: explosions_(std::make_unique<Explosions>()),
balloon_formations_(std::make_unique<BalloonFormations>()) { init(); }
// Inicializa
void BalloonManager::init()
{
// Texturas - Globos
balloon_textures_.emplace_back(Resource::get()->getTexture("balloon1.png"));
balloon_textures_.emplace_back(Resource::get()->getTexture("balloon2.png"));
balloon_textures_.emplace_back(Resource::get()->getTexture("balloon3.png"));
balloon_textures_.emplace_back(Resource::get()->getTexture("balloon4.png"));
balloon_textures_.emplace_back(Resource::get()->getTexture("powerball.png"));
// Animaciones -- Globos
balloon_animations_.emplace_back(Resource::get()->getAnimation("balloon1.ani"));
balloon_animations_.emplace_back(Resource::get()->getAnimation("balloon2.ani"));
balloon_animations_.emplace_back(Resource::get()->getAnimation("balloon3.ani"));
balloon_animations_.emplace_back(Resource::get()->getAnimation("balloon4.ani"));
balloon_animations_.emplace_back(Resource::get()->getAnimation("powerball.ani"));
// Texturas - Explosiones
explosions_textures_.emplace_back(Resource::get()->getTexture("explosion1.png"));
explosions_textures_.emplace_back(Resource::get()->getTexture("explosion2.png"));
explosions_textures_.emplace_back(Resource::get()->getTexture("explosion3.png"));
explosions_textures_.emplace_back(Resource::get()->getTexture("explosion4.png"));
// Animaciones -- Explosiones
explosions_animations_.emplace_back(Resource::get()->getAnimation("explosion1.ani"));
explosions_animations_.emplace_back(Resource::get()->getAnimation("explosion2.ani"));
explosions_animations_.emplace_back(Resource::get()->getAnimation("explosion3.ani"));
explosions_animations_.emplace_back(Resource::get()->getAnimation("explosion4.ani"));
// Añade texturas
explosions_->addTexture(1, explosions_textures_[0], explosions_animations_[0]);
explosions_->addTexture(2, explosions_textures_[1], explosions_animations_[1]);
explosions_->addTexture(3, explosions_textures_[2], explosions_animations_[2]);
explosions_->addTexture(4, explosions_textures_[3], explosions_animations_[3]);
}
// Actualiza
void BalloonManager::update()
{
for (auto balloon : balloons_)
{
balloon->update();
}
updateBalloonDeployCounter();
explosions_->update();
}
// Renderiza los objetos
void BalloonManager::render()
{
for (auto &balloon : balloons_)
{
balloon->render();
}
explosions_->render();
}
// Crea una formación de enemigos
void BalloonManager::deployBalloonFormation(int stage)
{
// Solo despliega una formación enemiga si ha pasado cierto tiempo desde la última
if (balloon_deploy_counter_ == 0)
{
// En este punto se decide entre crear una powerball o una formación enemiga
if ((rand() % 100 < 15) && (canPowerBallBeCreated()))
{
// Crea una powerball
createPowerBall();
// Da un poco de margen para que se creen mas enemigos
balloon_deploy_counter_ = 10;
}
else
{
// Decrementa el contador de despliegues enemigos de la PowerBall
power_ball_counter_ = (power_ball_counter_ > 0) ? (power_ball_counter_ - 1) : 0;
// Elige una formación enemiga la azar
auto formation = rand() % 10;
// Evita repetir la ultima formación enemiga desplegada
if (formation == last_balloon_deploy_)
{
++formation %= 10;
}
last_balloon_deploy_ = formation;
const auto set = balloon_formations_->getSet(stage, formation);
const auto numEnemies = set.number_of_balloons;
for (int i = 0; i < numEnemies; ++i)
{
auto p = set.init[i];
createBalloon(p.x, p.y, p.type, p.size, p.vel_x, balloon_speed_, p.creation_counter);
}
balloon_deploy_counter_ = 300;
}
}
}
// Vacia del vector de globos los globos que ya no sirven
void BalloonManager::freeBalloons()
{
auto it = std::remove_if(balloons_.begin(), balloons_.end(), [](const auto &balloon)
{ return !balloon->isEnabled(); });
balloons_.erase(it, balloons_.end());
}
// Actualiza la variable enemyDeployCounter
void BalloonManager::updateBalloonDeployCounter()
{
if (balloon_deploy_counter_ > 0)
{
--balloon_deploy_counter_;
}
}
// Indica si se puede crear una powerball
bool BalloonManager::canPowerBallBeCreated() { return (!power_ball_enabled_) && (calculateScreenPower() > POWERBALL_SCREENPOWER_MINIMUM) && (power_ball_counter_ == 0); }
// Calcula el poder actual de los globos en pantalla
int BalloonManager::calculateScreenPower()
{
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon)
{ return sum + (balloon->isEnabled() ? balloon->getPower() : 0); });
}
// Crea un globo nuevo en el vector de globos
std::shared_ptr<Balloon> BalloonManager::createBalloon(float x, int y, BalloonType type, BalloonSize size, float velx, float speed, int creation_timer)
{
const int index = static_cast<int>(size);
balloons_.emplace_back(std::make_shared<Balloon>(x, y, type, size, velx, speed, creation_timer, balloon_textures_.at(index), balloon_animations_.at(index)));
return balloons_.back();
}
// Crea un globo a partir de otro globo
void BalloonManager::createChildBalloon(const std::shared_ptr<Balloon> &balloon, const std::string &direction)
{
const float vx = direction == "LEFT" ? BALLOON_VELX_NEGATIVE : BALLOON_VELX_POSITIVE;
const auto lower_size = static_cast<BalloonSize>(static_cast<int>(balloon->getSize()) - 1);
auto b = createBalloon(0, balloon->getPosY(), balloon->getType(), lower_size, vx, balloon_speed_, 0);
const int x = direction == "LEFT" ? balloon->getPosX() + (balloon->getWidth() / 3) : balloon->getPosX() + 2 * (balloon->getWidth() / 3);
b->alignTo(x);
b->setVelY(b->getType() == BalloonType::BALLOON ? -2.50f : BALLOON_VELX_NEGATIVE * 2.0f);
if (balloon->isStopped())
{
b->stop();
}
if (balloon->isUsingReversedColor())
{
b->useReverseColor();
}
}
// Crea una PowerBall
void BalloonManager::createPowerBall()
{
constexpr int values = 6;
constexpr int pos_y = -BALLOON_SIZE[4];
constexpr int creation_time = 0;
const auto left = param.game.play_area.rect.x;
const auto center = param.game.play_area.center_x - (BALLOON_SIZE[4] / 2);
const auto right = param.game.play_area.rect.w - BALLOON_SIZE[4];
const auto luck = rand() % values;
const int x[values] = {left, left, center, center, right, right};
const float vx[values] = {BALLOON_VELX_POSITIVE, BALLOON_VELX_POSITIVE, BALLOON_VELX_POSITIVE, BALLOON_VELX_NEGATIVE, BALLOON_VELX_NEGATIVE, BALLOON_VELX_NEGATIVE};
balloons_.emplace_back(std::make_unique<Balloon>(x[luck], pos_y, BalloonType::POWERBALL, BalloonSize::SIZE4, vx[luck], balloon_speed_, creation_time, balloon_textures_[4], balloon_animations_[4]));
power_ball_enabled_ = true;
power_ball_counter_ = POWERBALL_COUNTER;
}
// Establece la velocidad de los globos
void BalloonManager::setBalloonSpeed(float speed)
{
balloon_speed_ = speed;
for (auto &balloon : balloons_)
{
balloon->setSpeed(speed);
}
}
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
int BalloonManager::popBalloon(std::shared_ptr<Balloon> balloon)
{
Stage::addPower(1);
int score = 0;
if (balloon->getType() == BalloonType::POWERBALL)
{
score = destroyAllBalloons();
power_ball_enabled_ = false;
balloon_deploy_counter_ = 20;
}
else
{
score = balloon->getScore();
if (balloon->getSize() != BalloonSize::SIZE1)
{
createChildBalloon(balloon, "LEFT");
createChildBalloon(balloon, "RIGHT");
}
// Agrega la explosión y elimina el globo
explosions_->add(balloon->getPosX(), balloon->getPosY(), static_cast<int>(balloon->getSize()));
balloon->pop();
}
return score;
}
// Explosiona un globo. Lo destruye = no crea otros globos
int BalloonManager::destroyBalloon(std::shared_ptr<Balloon> &balloon)
{
int score = 0;
// Calcula la puntuación y el poder que generaria el globo en caso de romperlo a él y a sus hijos
switch (balloon->getSize())
{
case BalloonSize::SIZE4:
score = BALLOON_SCORE[3] + (2 * BALLOON_SCORE[2]) + (4 * BALLOON_SCORE[1]) + (8 * BALLOON_SCORE[0]);
break;
case BalloonSize::SIZE3:
score = BALLOON_SCORE[2] + (2 * BALLOON_SCORE[1]) + (4 * BALLOON_SCORE[0]);
break;
case BalloonSize::SIZE2:
score = BALLOON_SCORE[1] + (2 * BALLOON_SCORE[0]);
break;
case BalloonSize::SIZE1:
score = BALLOON_SCORE[0];
break;
default:
score = 0;
break;
}
// Aumenta el poder de la fase
Stage::addPower(balloon->getPower());
// Destruye el globo
explosions_->add(balloon->getPosX(), balloon->getPosY(), static_cast<int>(balloon->getSize()));
balloon->pop();
return score;
}
// Destruye todos los globos
int BalloonManager::destroyAllBalloons()
{
int score = 0;
for (auto &balloon : balloons_)
{
score += destroyBalloon(balloon);
}
balloon_deploy_counter_ = 300;
JA_PlaySound(Resource::get()->getSound("powerball.wav"));
Screen::get()->flash(flash_color, 100);
Screen::get()->shake();
return score;
}
// Detiene todos los globos
void BalloonManager::stopAllBalloons()
{
for (auto &balloon : balloons_)
{
balloon->stop();
}
}
// Pone en marcha todos los globos
void BalloonManager::startAllBalloons()
{
for (auto &balloon : balloons_)
{
if (!balloon->isBeingCreated())
{
balloon->start();
}
}
}
// Cambia el color de todos los globos
void BalloonManager::reverseColorsToAllBalloons()
{
for (auto &balloon : balloons_)
{
if (balloon->isStopped())
{
balloon->useReverseColor();
}
}
}
// Cambia el color de todos los globos
void BalloonManager::normalColorsToAllBalloons()
{
for (auto &balloon : balloons_)
{
balloon->useNormalColor();
}
}
// Recarga las texturas
void BalloonManager::reLoad()
{
for (auto &texture : balloon_textures_)
{
texture->reLoad();
}
}
// Crea dos globos gordos
void BalloonManager::createTwoBigBalloons()
{
const auto set = balloon_formations_->getSet(0, 1);
const auto numEnemies = set.number_of_balloons;
for (int i = 0; i < numEnemies; ++i)
{
auto p = set.init[i];
createBalloon(p.x, p.y, p.type, p.size, p.vel_x, balloon_speed_, p.creation_counter);
}
}
// Obtiene el nivel de ameza actual generado por los globos
int BalloonManager::getMenace()
{
return std::accumulate(balloons_.begin(), balloons_.end(), 0, [](int sum, const auto &balloon)
{ return sum + (balloon->isEnabled() ? balloon->getMenace() : 0); });
}

113
source/balloon_manager.h Normal file
View File

@@ -0,0 +1,113 @@
#pragma once
#include <memory> // Para shared_ptr, unique_ptr
#include <string> // Para string
#include <vector> // Para vector
#include "balloon.h" // Para BALLOON_SPEED, Balloon
#include "balloon_formations.h" // Para BalloonFormations
#include "explosions.h" // Para Explosions
class Texture;
using Balloons = std::vector<std::shared_ptr<Balloon>>;
class BalloonManager
{
private:
Balloons balloons_; // Vector con los globos
std::unique_ptr<Explosions> explosions_; // Objeto para dibujar explosiones
std::unique_ptr<BalloonFormations> balloon_formations_; // Objeto para gestionar las oleadas enemigas
std::vector<std::shared_ptr<Texture>> balloon_textures_; // Vector con las texturas de los globos
std::vector<std::shared_ptr<Texture>> explosions_textures_; // Vector con las texturas de las explosiones
std::vector<std::vector<std::string>> balloon_animations_; // Vector con las animaciones de los globos
std::vector<std::vector<std::string>> explosions_animations_; // Vector con las animaciones de las explosiones
float balloon_speed_ = BALLOON_SPEED[0]; // Velocidad a la que se mueven los enemigos
float default_balloon_speed_ = BALLOON_SPEED[0]; // Velocidad base de los enemigos, sin incrementar
int balloon_deploy_counter_ = 0; // Cuando se lanza una formación, se le da un valor y no sale otra hasta que llegue a cero
bool power_ball_enabled_ = false; // Indica si hay una powerball ya activa
int power_ball_counter_ = 0; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra
int last_balloon_deploy_ = 0; // Guarda cual ha sido la última formación desplegada para no repetir;
// Inicializa
void init();
public:
// Constructor
BalloonManager();
// Destructor
~BalloonManager() = default;
// Actualiza
void update();
// Renderiza los globos
void render();
// Vacia del vector de globos los globos que ya no sirven
void freeBalloons();
// Crea una formación de enemigos
void deployBalloonFormation(int stage);
// Actualiza la variable enemyDeployCounter
void updateBalloonDeployCounter();
// Indica si se puede crear una powerball
bool canPowerBallBeCreated();
// Calcula el poder actual de los globos en pantalla
int calculateScreenPower();
// Crea un globo nuevo en el vector de globos
std::shared_ptr<Balloon> createBalloon(float x, int y, BalloonType type, BalloonSize size, float velx, float speed, int creation_timer);
// Crea un globo a partir de otro globo
void createChildBalloon(const std::shared_ptr<Balloon> &balloon, const std::string &direction);
// Crea una PowerBall
void createPowerBall();
// Establece la velocidad de los globos
void setBalloonSpeed(float speed);
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
int popBalloon(std::shared_ptr<Balloon> balloon);
// Explosiona un globo. Lo destruye = no crea otros globos
int destroyBalloon(std::shared_ptr<Balloon> &balloon);
// Destruye todos los globos
int destroyAllBalloons();
// Detiene todos los globos
void stopAllBalloons();
// Pone en marcha todos los globos
void startAllBalloons();
// Cambia el color de todos los globos
void reverseColorsToAllBalloons();
// Cambia el color de todos los globos
void normalColorsToAllBalloons();
// Recarga las texturas
void reLoad();
// Crea dos globos gordos
void createTwoBigBalloons();
// Obtiene el nivel de ameza actual generado por los globos
int getMenace();
// Getters
float getBalloonSpeed() const { return balloon_speed_; }
Balloons &getBalloons() { return balloons_; }
// Setters
void setDefaultBalloonSpeed(float speed) { default_balloon_speed_ = speed; }
void resetBalloonSpeed() { setBalloonSpeed(default_balloon_speed_); }
};

View File

@@ -1,26 +1,22 @@
#include "define_buttons.h" #include "define_buttons.h"
#include <utility> // Para move #include "input.h" // Para Input, InputType
#include "input.h" // Para Input, InputType #include "lang.h" // Para getText
#include "lang.h" // Para getText #include "options.h" // Para OptionsController, Options, options
#include "options.h" // Para OptionsController, Options, options #include "param.h" // Para Param, param, ParamGame, ParamTitle
#include "param.h" // Para Param, param, ParamGame, ParamTitle #include "resource.h" // Para Resource
#include "section.h" // Para Name, Options, name, options #include "section.h" // Para Name, Options, name, options
#include "text.h" // Para Text #include "text.h" // Para Text
// Constructor // Constructor
DefineButtons::DefineButtons(std::unique_ptr<Text> text_) DefineButtons::DefineButtons()
: input_(Input::get()), : input_(Input::get()),
text_(std::move(text_)) text_(Resource::get()->getText("8bithud"))
{ {
// Inicializa variables // Inicializa variables
x_ = param.game.width / 2; x_ = param.game.width / 2;
y_ = param.title.press_start_position; y_ = param.title.press_start_position;
buttons_.emplace_back(lang::getText(95), InputType::FIRE_LEFT, SDL_CONTROLLER_BUTTON_INVALID); clearButtons();
buttons_.emplace_back(lang::getText(96), InputType::FIRE_CENTER, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(97), InputType::FIRE_RIGHT, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(99), InputType::SERVICE, SDL_CONTROLLER_BUTTON_INVALID);
for (int i = 0; i < input_->getNumControllers(); ++i) for (int i = 0; i < input_->getNumControllers(); ++i)
{ {
@@ -33,14 +29,14 @@ void DefineButtons::render()
{ {
if (enabled_) if (enabled_)
{ {
text_->writeCentered(x_, y_ - 10, lang::getText(100) + std::to_string(options.controller.at(index_controller_).player_id)); text_->writeCentered(x_, y_ - 10, lang::getText(100) + std::to_string(options.controllers.at(index_controller_).player_id));
text_->writeCentered(x_, y_, controller_names_.at(index_controller_)); text_->writeCentered(x_, y_, controller_names_.at(index_controller_));
text_->writeCentered(x_, y_ + 10, buttons_.at(index_button_).label); text_->writeCentered(x_, y_ + 10, buttons_.at(index_button_).label);
} }
} }
// Comprueba el botón que se ha pulsado // Comprueba el botón que se ha pulsado
void DefineButtons::doControllerButtonDown(SDL_ControllerButtonEvent &event) void DefineButtons::doControllerButtonDown(const SDL_ControllerButtonEvent &event)
{ {
// Solo pilla botones del mando que toca // Solo pilla botones del mando que toca
if (input_->getJoyIndex(event.which) != static_cast<int>(index_controller_)) if (input_->getJoyIndex(event.which) != static_cast<int>(index_controller_))
@@ -103,6 +99,7 @@ bool DefineButtons::enable(int index)
enabled_ = true; enabled_ = true;
index_controller_ = index; index_controller_ = index;
index_button_ = 0; index_button_ = 0;
clearButtons();
return true; return true;
} }
@@ -126,9 +123,7 @@ void DefineButtons::incIndexButton()
// Guarda los cambios en las opciones // Guarda los cambios en las opciones
saveBindingsToOptions(); saveBindingsToOptions();
// Reinicia variables // Deshabilita
index_button_ = 0;
index_controller_ = 0;
enabled_ = false; enabled_ = false;
} }
} }
@@ -137,7 +132,7 @@ void DefineButtons::incIndexButton()
void DefineButtons::saveBindingsToOptions() void DefineButtons::saveBindingsToOptions()
{ {
// Modifica las opciones para colocar los valores asignados // Modifica las opciones para colocar los valores asignados
auto &controller = options.controller.at(index_controller_); auto &controller = options.controllers.at(index_controller_);
controller.name = input_->getControllerName(index_controller_); controller.name = input_->getControllerName(index_controller_);
for (size_t j = 0; j < controller.inputs.size(); ++j) for (size_t j = 0; j < controller.inputs.size(); ++j)
{ {
@@ -145,12 +140,6 @@ void DefineButtons::saveBindingsToOptions()
} }
} }
// Intercambia los jugadores asignados a los dos primeros mandos
void DefineButtons::swapControllers()
{
std::swap(options.controller[0].player_id, options.controller[1].player_id);
}
// Comprueba que un botón no esté ya asignado // Comprueba que un botón no esté ya asignado
bool DefineButtons::checkButtonNotInUse(SDL_GameControllerButton button) bool DefineButtons::checkButtonNotInUse(SDL_GameControllerButton button)
{ {
@@ -163,3 +152,14 @@ bool DefineButtons::checkButtonNotInUse(SDL_GameControllerButton button)
} }
return true; return true;
} }
// Limpia la asignación de botones
void DefineButtons::clearButtons()
{
buttons_.clear();
buttons_.emplace_back(lang::getText(95), InputType::FIRE_LEFT, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(96), InputType::FIRE_CENTER, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(97), InputType::FIRE_RIGHT, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(98), InputType::START, SDL_CONTROLLER_BUTTON_INVALID);
buttons_.emplace_back(lang::getText(99), InputType::SERVICE, SDL_CONTROLLER_BUTTON_INVALID);
}

View File

@@ -1,13 +1,14 @@
#pragma once #pragma once
#include <SDL2/SDL_events.h> // para SDL_ControllerButtonEvent #include <SDL2/SDL_events.h> // Para SDL_ControllerButtonEvent
#include <SDL2/SDL_gamecontroller.h> // para SDL_GameControllerButton #include <SDL2/SDL_gamecontroller.h> // Para SDL_GameControllerButton
#include <memory> // para shared_ptr, unique_ptr #include <stddef.h> // Para size_t
#include <string> // para string #include <memory> // Para shared_ptr, unique_ptr
#include <vector> // para vector #include <string> // Para string
class Input; #include <vector> // Para vector
class Text; class Input; // lines 8-8
enum class InputType : int; class Text; // lines 9-9
enum class InputType : int; // lines 10-10
struct DefineButtonsButton struct DefineButtonsButton
{ {
@@ -41,7 +42,7 @@ private:
void incIndexButton(); void incIndexButton();
// Comprueba el botón que se ha pulsado // Comprueba el botón que se ha pulsado
void doControllerButtonDown(SDL_ControllerButtonEvent &event); void doControllerButtonDown(const SDL_ControllerButtonEvent &event);
// Asigna los botones definidos al input // Asigna los botones definidos al input
void bindButtons(); void bindButtons();
@@ -52,9 +53,12 @@ private:
// Comprueba que un botón no esté ya asignado // Comprueba que un botón no esté ya asignado
bool checkButtonNotInUse(SDL_GameControllerButton button); bool checkButtonNotInUse(SDL_GameControllerButton button);
// Limpia la asignación de botones
void clearButtons();
public: public:
// Constructor // Constructor
explicit DefineButtons(std::unique_ptr<Text> text); DefineButtons();
// Destructor // Destructor
~DefineButtons() = default; ~DefineButtons() = default;
@@ -70,7 +74,4 @@ public:
// Comprueba si está habilitado // Comprueba si está habilitado
bool isEnabled(); bool isEnabled();
// Intercambia los jugadores asignados a los dos primeros mandos
void swapControllers();
}; };

View File

@@ -7,17 +7,17 @@
#include <SDL2/SDL_hints.h> // Para SDL_SetHint, SDL_HINT_RENDER_DR... #include <SDL2/SDL_hints.h> // Para SDL_SetHint, SDL_HINT_RENDER_DR...
#include <SDL2/SDL_scancode.h> // Para SDL_SCANCODE_0, SDL_SCANCODE_DOWN #include <SDL2/SDL_scancode.h> // Para SDL_SCANCODE_0, SDL_SCANCODE_DOWN
#include <SDL2/SDL_stdinc.h> // Para SDL_bool, Uint32 #include <SDL2/SDL_stdinc.h> // Para SDL_bool, Uint32
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks #include <bits/chrono.h> // Para duration, system_clock
#include <errno.h> // Para errno, EEXIST, EACCES, ENAMETOO... #include <errno.h> // Para errno, EEXIST, EACCES, ENAMETOO...
#include <stdio.h> // Para printf, perror #include <stdio.h> // Para printf, perror
#include <string.h> // Para strcmp
#include <sys/stat.h> // Para mkdir, stat, S_IRWXU #include <sys/stat.h> // Para mkdir, stat, S_IRWXU
#include <unistd.h> // Para getuid #include <unistd.h> // Para getuid
#include <cstdlib> // Para exit, EXIT_FAILURE, rand, srand #include <algorithm> // Para min
#include <iostream> // Para basic_ostream, operator<<, cout #include <cstdlib> // Para exit, EXIT_FAILURE, size_t, rand
#include <memory> // Para make_unique, unique_ptr, make_s... #include <iostream> // Para basic_ostream, operator<<, basi...
#include <memory> // Para make_unique, unique_ptr
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include <string> // Para operator+, allocator, char_traits #include <string> // Para operator+, char_traits, allocator
#include <vector> // Para vector #include <vector> // Para vector
#include "asset.h" // Para Asset, AssetType #include "asset.h" // Para Asset, AssetType
#include "dbgtxt.h" // Para dbg_init #include "dbgtxt.h" // Para dbg_init
@@ -27,21 +27,19 @@
#include "input.h" // Para Input, InputType #include "input.h" // Para Input, InputType
#include "instructions.h" // Para Instructions #include "instructions.h" // Para Instructions
#include "intro.h" // Para Intro #include "intro.h" // Para Intro
#include "jail_audio.h" // Para JA_EnableMusic, JA_EnableSound #include "jail_audio.h" // Para JA_SetMusicVolume, JA_SetSoundV...
#include "lang.h" // Para Code, loadFromFile #include "lang.h" // Para Code, loadFromFile
#include "logo.h" // Para Logo #include "logo.h" // Para Logo
#include "manage_hiscore_table.h" // Para ManageHiScoreTable #include "manage_hiscore_table.h" // Para ManageHiScoreTable
#include "notifier.h" // Para Notifier #include "notifier.h" // Para Notifier
#include "on_screen_help.h" // Para OnScreenHelp #include "on_screen_help.h" // Para OnScreenHelp
#include "options.h" // Para Options, options, OptionsContro... #include "options.h" // Para Options, OptionsController, opt...
#include "param.h" // Para Param, ParamGame, param, loadPa... #include "param.h" // Para Param, ParamGame, param, loadPa...
#include "resource.h" // Para Resource #include "resource.h" // Para Resource
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "section.h" // Para Name, Options, name, options #include "section.h" // Para Name, Options, name, options
#include "text.h" // Para Text
#include "title.h" // Para Title #include "title.h" // Para Title
#include "utils.h" // Para Overrides, overrides #include "utils.h" // Para Overrides, overrides
#include <chrono>
#ifndef _WIN32 #ifndef _WIN32
#include <pwd.h> // para getpwuid, passwd #include <pwd.h> // para getpwuid, passwd
@@ -110,30 +108,17 @@ Director::Director(int argc, const char *argv[])
manager->loadFromFile(Asset::get()->get("score.bin")); manager->loadFromFile(Asset::get()->get("score.bin"));
} }
// Inicializa SDL // Inicializa todo
initSDL(); initSDL();
// Inicializa JailAudio
initJailAudio(); initJailAudio();
// Inicializa el texto de debug
dbg_init(renderer_); dbg_init(renderer_);
lang::loadFromFile(getLangFile(static_cast<lang::Code>(options.game.language)));
// Crea los objetos
lang::loadFromFile(getLangFile((lang::Code)options.game.language));
Screen::init(window_, renderer_); Screen::init(window_, renderer_);
Resource::init(); Resource::init();
Input::init(Asset::get()->get("gamecontrollerdb.txt")); Input::init(Asset::get()->get("gamecontrollerdb.txt"));
bindInputs(); bindInputs();
Notifier::init(std::string(), Resource::get()->getText("8bithud"), Asset::get()->get("notify.wav"));
auto notifier_text = std::make_shared<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"));
Notifier::init(std::string(), notifier_text, Asset::get()->get("notify.wav"));
OnScreenHelp::init(); OnScreenHelp::init();
globalInputs::init(); globalInputs::init();
} }
@@ -204,16 +189,20 @@ void Director::bindInputs()
} }
// Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso // Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso
for (int i = 0; i < num_gamepads; ++i) const size_t max_controllers = std::min(2, num_gamepads);
for (int index = 0; index < (int)options.controller.size(); ++index) for (size_t i = 0; i < max_controllers; ++i)
if (Input::get()->getControllerName(i) == options.controller[index].name) {
for (auto &controller : options.controllers)
{
if (Input::get()->getControllerName(i) == controller.name)
{ {
options.controller[index].plugged = true; for (size_t j = 0; j < controller.inputs.size(); ++j)
for (int j = 0; j < (int)options.controller[index].inputs.size(); ++j)
{ {
Input::get()->bindGameControllerButton(i, options.controller[index].inputs[j], options.controller[index].buttons[j]); Input::get()->bindGameControllerButton(i, controller.inputs.at(j), controller.buttons.at(j));
} }
} }
}
}
// Asigna botones a inputs desde otros inputs // Asigna botones a inputs desde otros inputs
for (int i = 0; i < num_gamepads; ++i) for (int i = 0; i < num_gamepads; ++i)
@@ -228,15 +217,25 @@ void Director::bindInputs()
Input::get()->bindGameControllerButton(i, InputType::SWAP_CONTROLLERS, InputType::UP); Input::get()->bindGameControllerButton(i, InputType::SWAP_CONTROLLERS, InputType::UP);
} }
// Guarda las asignaciones de botones en las opciones // Guarda las asignaciones de botones en las opciones de los dos primeros mandos
for (int i = 0; i < num_gamepads; ++i) for (size_t i = 0; i < max_controllers; ++i)
{ {
options.controller.at(i).name = Input::get()->getControllerName(i); // Variables asociadas al mando
for (size_t j = 0; j < options.controller.at(i).inputs.size(); ++j) options.controllers.at(i).index = i;
options.controllers.at(i).name = Input::get()->getControllerName(i);
options.controllers.at(i).plugged = true;
// Asignaciones de botones
for (size_t j = 0; j < options.controllers.at(i).inputs.size(); ++j)
{ {
options.controller.at(i).buttons.at(j) = Input::get()->getControllerBinding(i, options.controller.at(i).inputs.at(j)); options.controllers.at(i).buttons.at(j) = Input::get()->getControllerBinding(i, options.controllers.at(i).inputs.at(j));
} }
} }
// Asegura que algún jugador tenga el teclado asignado
if (getPlayerWhoUsesKeyboard() == 0)
{
setKeyboardToPlayer(1);
}
} }
// Inicializa JailAudio // Inicializa JailAudio
@@ -495,6 +494,8 @@ void Director::setFileList()
Asset::get()->add(prefix + "/data/font/smb2.txt", AssetType::FONT); Asset::get()->add(prefix + "/data/font/smb2.txt", AssetType::FONT);
Asset::get()->add(prefix + "/data/font/04b_25.png", AssetType::BITMAP); Asset::get()->add(prefix + "/data/font/04b_25.png", AssetType::BITMAP);
Asset::get()->add(prefix + "/data/font/04b_25.txt", AssetType::FONT); Asset::get()->add(prefix + "/data/font/04b_25.txt", AssetType::FONT);
Asset::get()->add(prefix + "/data/font/04b_25_2x.png", AssetType::BITMAP);
Asset::get()->add(prefix + "/data/font/04b_25_2x.txt", AssetType::FONT);
// Textos // Textos
Asset::get()->add(prefix + "/data/lang/es_ES.txt", AssetType::LANG); Asset::get()->add(prefix + "/data/lang/es_ES.txt", AssetType::LANG);
@@ -509,8 +510,6 @@ void Director::setFileList()
// Comprueba los parametros del programa // Comprueba los parametros del programa
void Director::checkProgramArguments(int argc, const char *argv[]) void Director::checkProgramArguments(int argc, const char *argv[])
{ {
const std::vector<std::string> argument_list = {"--320x240", "--clear_score"};
// Establece la ruta del programa // Establece la ruta del programa
executable_path_ = argv[0]; executable_path_ = argv[0];
@@ -616,7 +615,7 @@ void Director::runGame()
{ {
const auto player_id = section::options == section::Options::GAME_PLAY_1P ? 1 : 2; const auto player_id = section::options == section::Options::GAME_PLAY_1P ? 1 : 2;
#ifdef DEBUG #ifdef DEBUG
constexpr auto current_stage = 9; constexpr auto current_stage = 0;
#else #else
constexpr auto current_stage = 0; constexpr auto current_stage = 0;
#endif #endif

View File

@@ -2,7 +2,7 @@
#include <string> #include <string>
constexpr int NAME_LENGHT = 8; constexpr int NAME_LENGHT = 6;
/* /*
Un array, "characterList", contiene la lista de caracteres Un array, "characterList", contiene la lista de caracteres

File diff suppressed because it is too large Load Diff

View File

@@ -1,31 +1,29 @@
#pragma once #pragma once
#include <SDL2/SDL_render.h> // for SDL_Renderer, SDL_Texture #include <SDL2/SDL_render.h> // Para SDL_Renderer, SDL_Texture
#include <SDL2/SDL_stdinc.h> // for Uint32, Uint8 #include <SDL2/SDL_stdinc.h> // Para Uint32, Uint8
#include <memory> // for shared_ptr, unique_ptr #include <memory> // Para shared_ptr, unique_ptr
#include <string> // for string #include <string> // Para string
#include <vector> // for vector #include <vector> // Para vector
#include "balloon.h" // for Balloon #include "manage_hiscore_table.h" // Para HiScoreEntry
#include "manage_hiscore_table.h" // for HiScoreEntry #include "options.h" // Para Options, OptionsGame, options
#include "options.h" // for Options, OptionsGame, options #include "player.h" // Para Player
#include "player.h" // for Player #include "utils.h" // Para Demo
#include "utils.h" // for Demo class Asset; // lines 13-13
class Asset; // lines 14-14 class Background; // lines 14-14
class Background; // lines 15-15 class BalloonManager;
class BalloonFormations; // lines 16-16 class Bullet; // lines 15-15
class Bullet; // lines 17-17 class Fade; // lines 16-16
class Explosions; // lines 18-18 class Input; // lines 17-17
class Fade; // lines 19-19 class Item; // lines 18-18
class Input; // lines 20-20 class PathSprite; // lines 19-19
class Item; // lines 21-21 class Scoreboard; // lines 20-20
class PathSprite; // lines 22-22 class Screen; // lines 21-21
class Scoreboard; // lines 23-23 class SmartSprite; // lines 22-22
class Screen; // lines 24-24 class Texture; // lines 23-23
class SmartSprite; // lines 25-25 enum class BulletType : Uint8; // lines 24-24
class Texture; // lines 27-27 enum class ItemType; // lines 25-25
enum class BulletType : Uint8; // lines 28-28 struct Path; // lines 26-26
enum class ItemType; // lines 29-29
struct Path;
// Modo demo // Modo demo
constexpr bool GAME_MODE_DEMO_OFF = false; constexpr bool GAME_MODE_DEMO_OFF = false;
@@ -123,14 +121,11 @@ private:
Input *input_; // Manejador de entrada Input *input_; // Manejador de entrada
Scoreboard *scoreboard_; // Objeto para dibujar el marcador Scoreboard *scoreboard_; // Objeto para dibujar el marcador
std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego
std::unique_ptr<Explosions> explosions_; // Objeto para dibujar explosiones
std::unique_ptr<BalloonFormations> balloon_formations_; // Objeto para gestionar las oleadas enemigas
SDL_Texture *canvas_; // Textura para dibujar la zona de juego SDL_Texture *canvas_; // Textura para dibujar la zona de juego
std::vector<std::shared_ptr<Player>> players_; // Vector con los jugadores std::vector<std::shared_ptr<Player>> players_; // Vector con los jugadores
std::vector<std::shared_ptr<Balloon>> balloons_; // Vector con los globos
std::vector<std::unique_ptr<Bullet>> bullets_; // Vector con las balas std::vector<std::unique_ptr<Bullet>> bullets_; // Vector con las balas
std::vector<std::unique_ptr<Item>> items_; // Vector con los items std::vector<std::unique_ptr<Item>> items_; // Vector con los items
std::vector<std::unique_ptr<SmartSprite>> smart_sprites_; // Vector con los smartsprites std::vector<std::unique_ptr<SmartSprite>> smart_sprites_; // Vector con los smartsprites
@@ -138,26 +133,22 @@ private:
std::shared_ptr<Texture> bullet_texture_; // Textura para las balas std::shared_ptr<Texture> bullet_texture_; // Textura para las balas
std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items
std::vector<std::shared_ptr<Texture>> balloon_textures_; // Vector con las texturas de los globos
std::vector<std::shared_ptr<Texture>> explosions_textures_; // Vector con las texturas de las explosiones
std::vector<std::vector<std::shared_ptr<Texture>>> player_textures_; // Vector con todas las texturas de los jugadores; std::vector<std::vector<std::shared_ptr<Texture>>> player_textures_; // Vector con todas las texturas de los jugadores;
std::vector<std::shared_ptr<Texture>> game_text_textures_; // Vector con las texturas para los sprites con textos std::vector<std::shared_ptr<Texture>> game_text_textures_; // Vector con las texturas para los sprites con textos
std::vector<std::vector<std::string>> item_animations_; // Vector con las animaciones de los items std::vector<std::vector<std::string>> item_animations_; // Vector con las animaciones de los items
std::vector<std::vector<std::string>> player_animations_; // Vector con las animaciones del jugador std::vector<std::vector<std::string>> player_animations_; // Vector con las animaciones del jugador
std::vector<std::vector<std::string>> balloon_animations_; // Vector con las animaciones de los globos
std::vector<std::vector<std::string>> explosions_animations_; // Vector con las animaciones de las explosiones
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
std::vector<Path> paths_; // Vector con los recorridos precalculados almacenados std::unique_ptr<BalloonManager> balloon_manager_; // Objeto para gestionar los globos
std::vector<Path> paths_; // Vector con los recorridos precalculados almacenados
// Variables // Variables
HiScoreEntry hi_score_ = HiScoreEntry( HiScoreEntry hi_score_ = HiScoreEntry(
options.game.hi_score_table[0].name, options.game.hi_score_table[0].name,
options.game.hi_score_table[0].score); // Máxima puntuación y nombre de quien la ostenta options.game.hi_score_table[0].score); // Máxima puntuación y nombre de quien la ostenta
int current_stage_; // Indica la fase actual
Demo demo_; // Variable con todas las variables relacionadas con el modo demo Demo demo_; // Variable con todas las variables relacionadas con el modo demo
GameDifficulty difficulty_ = options.game.difficulty; // Dificultad del juego GameDifficulty difficulty_ = options.game.difficulty; // Dificultad del juego
Helper helper_; // Variable para gestionar las ayudas Helper helper_; // Variable para gestionar las ayudas
@@ -165,22 +156,14 @@ private:
bool coffee_machine_enabled_ = false; // Indica si hay una máquina de café en el terreno de juego bool coffee_machine_enabled_ = false; // Indica si hay una máquina de café en el terreno de juego
bool hi_score_achieved_ = false; // Indica si se ha superado la puntuación máxima bool hi_score_achieved_ = false; // Indica si se ha superado la puntuación máxima
bool paused_ = false; // Indica si el juego está pausado (no se deberia de poder utilizar en el modo arcade) bool paused_ = false; // Indica si el juego está pausado (no se deberia de poder utilizar en el modo arcade)
bool power_ball_enabled_ = false; // Indica si hay una powerball ya activa
float balloon_speed_; // Velocidad a la que se mueven los enemigos
float default_balloon_speed_; // Velocidad base de los enemigos, sin incrementar
float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad float difficulty_score_multiplier_; // Multiplicador de puntos en función de la dificultad
int balloon_deploy_counter_ = 0; // Cuando se lanza una formación, se le da un valor y no sale otra hasta que llegue a cero
int balloons_popped_ = 0; // Lleva la cuenta de los globos explotados
int counter_ = 0; // Contador para el juego int counter_ = 0; // Contador para el juego
int current_power_ = 0; // Poder actual almacenado para completar la fase
int game_completed_counter_ = 0; // Contador para el tramo final, cuando se ha completado la partida y ya no aparecen más enemigos int game_completed_counter_ = 0; // Contador para el tramo final, cuando se ha completado la partida y ya no aparecen más enemigos
int game_over_counter_ = GAME_OVER_COUNTER_; // Contador para el estado de fin de partida int game_over_counter_ = GAME_OVER_COUNTER_; // Contador para el estado de fin de partida
int last_balloon_deploy_ = 0; // Guarda cual ha sido la última formación desplegada para no repetir;
int menace_current_ = 0; // Nivel de amenaza actual
int menace_threshold_ = 0; // Umbral del nivel de amenaza. Si el nivel de amenaza cae por debajo del umbral, se generan más globos. Si el umbral aumenta, aumenta el número de globos
int power_ball_counter_ = 0; // Contador de formaciones enemigas entre la aparicion de una PowerBall y otra
int time_stopped_counter_ = 0; // Temporizador para llevar la cuenta del tiempo detenido int time_stopped_counter_ = 0; // Temporizador para llevar la cuenta del tiempo detenido
int total_power_to_complete_game_; // La suma del poder necesario para completar todas las fases int total_power_to_complete_game_; // La suma del poder necesario para completar todas las fases
int menace_current_ = 0; // Nivel de amenaza actual
int menace_threshold_ = 0; // Umbral del nivel de amenaza. Si el nivel de amenaza cae por debajo del umbral, se generan más globos. Si el umbral aumenta, aumenta el número de globos
GameState state_ = GameState::PLAYING; // Estado GameState state_ = GameState::PLAYING; // Estado
#ifdef DEBUG #ifdef DEBUG
bool auto_pop_balloons_ = false; // Si es true, incrementa automaticamente los globos explotados bool auto_pop_balloons_ = false; // Si es true, incrementa automaticamente los globos explotados
@@ -198,12 +181,6 @@ private:
// Asigna texturas y animaciones // Asigna texturas y animaciones
void setResources(); void setResources();
// Crea una formación de enemigos
void deployBalloonFormation();
// Aumenta el poder de la fase
void increaseStageCurrentPower(int power);
// Actualiza el valor de HiScore en caso necesario // Actualiza el valor de HiScore en caso necesario
void updateHiScore(); void updateHiScore();
@@ -219,54 +196,9 @@ private:
// Actualiza el estado de fin de la partida // Actualiza el estado de fin de la partida
void updateGameOverState(); void updateGameOverState();
// Actualiza los globos
void updateBalloons();
// Pinta en pantalla todos los globos activos
void renderBalloons();
// Crea un globo nuevo en el vector de globos
std::shared_ptr<Balloon> createBalloon(float x, int y, BalloonType type, BalloonSize size, float velx, float speed, int stopped_counter);
// Crea un globo a partir de otro globo
void createChildBalloon(const std::shared_ptr<Balloon> &balloon, const std::string &direction);
// Crea una PowerBall
void createPowerBall();
// Establece la velocidad de los globos
void setBalloonSpeed(float speed);
// Actualiza la velocidad de los globos en funcion del poder acumulado de la fase
void checkAndUpdateBalloonSpeed();
// Explosiona un globo. Lo destruye y crea otros dos si es el caso
void popBalloon(std::shared_ptr<Balloon> balloon);
// Explosiona un globo. Lo destruye
void destroyBalloon(std::shared_ptr<Balloon> &balloon);
// Destruye todos los globos
void destroyAllBalloons();
// Destruye todos los items // Destruye todos los items
void destroyAllItems(); void destroyAllItems();
// Detiene todos los globos
void stopAllBalloons();
// Pone en marcha todos los globos
void startAllBalloons();
// Cambia el color de todos los globos
void reverseColorsToAllBalloons();
// Cambia el color de todos los globos
void normalColorsToAllBalloons();
// Vacia el vector de globos
void freeBalloons();
// Comprueba la colisión entre el jugador y los globos activos // Comprueba la colisión entre el jugador y los globos activos
bool checkPlayerBalloonCollision(std::shared_ptr<Player> &player); bool checkPlayerBalloonCollision(std::shared_ptr<Player> &player);
@@ -333,18 +265,9 @@ private:
// Acciones a realizar cuando el jugador muere // Acciones a realizar cuando el jugador muere
void killPlayer(std::shared_ptr<Player> &player); void killPlayer(std::shared_ptr<Player> &player);
// Calcula y establece el valor de amenaza en funcion de los globos activos
void evaluateAndSetMenace();
// Actualiza la variable EnemyDeployCounter
void updateBalloonDeployCounter();
// Actualiza y comprueba el valor de la variable // Actualiza y comprueba el valor de la variable
void updateTimeStopped(); void updateTimeStopped();
// Gestiona el nivel de amenaza
void updateMenace();
// Actualiza el fondo // Actualiza el fondo
void updateBackground(); void updateBackground();
@@ -357,12 +280,6 @@ private:
// Deshabilita el efecto del item de detener el tiempo // Deshabilita el efecto del item de detener el tiempo
void disableTimeStopItem(); void disableTimeStopItem();
// Indica si se puede crear una powerball
bool canPowerBallBeCreated();
// Calcula el poder actual de los globos en pantalla
int calculateScreenPower();
// Actualiza las variables de ayuda // Actualiza las variables de ayuda
void updateHelper(); void updateHelper();
@@ -450,9 +367,6 @@ private:
// Inicializa los jugadores // Inicializa los jugadores
void initPlayers(int player_id); void initPlayers(int player_id);
// Crea dos globos gordos
void createTwoBigBalloons();
// Pausa la música // Pausa la música
void pauseMusic(); void pauseMusic();
@@ -471,9 +385,6 @@ private:
// Actualiza las variables durante el transcurso normal del juego // Actualiza las variables durante el transcurso normal del juego
void updateGame(); void updateGame();
// Actualiza las variables durante el transcurso del final del juego
void updateCompletedGame();
// Gestiona eventos para el estado del final del juego // Gestiona eventos para el estado del final del juego
void updateCompletedState(); void updateCompletedState();
@@ -483,6 +394,15 @@ private:
// Vacía los vectores de elementos deshabilitados // Vacía los vectores de elementos deshabilitados
void cleanVectors(); void cleanVectors();
// Gestiona el nivel de amenaza
void updateMenace();
// Calcula y establece el valor de amenaza en funcion de los globos activos
void evaluateAndSetMenace();
// Actualiza la velocidad de los globos en funcion del poder acumulado de la fase
void checkAndUpdateBalloonSpeed();
public: public:
// Constructor // Constructor
Game(int playerID, int current_stage, bool demo); Game(int playerID, int current_stage, bool demo);

View File

@@ -8,6 +8,7 @@
#include "options.h" // Para Options, OptionsAudio, options, OptionsM... #include "options.h" // Para Options, OptionsAudio, options, OptionsM...
#include "section.h" // Para Name, Options, name, options #include "section.h" // Para Name, Options, name, options
#include "utils.h" // Para boolToOnOff, stringInVector #include "utils.h" // Para boolToOnOff, stringInVector
#include "screen.h"
namespace globalInputs namespace globalInputs
{ {
@@ -39,9 +40,9 @@ namespace globalInputs
{ {
#ifdef ARCADE #ifdef ARCADE
const int index = code == section::Options::QUIT_WITH_CONTROLLER ? 116 : 94; const int index = code == section::Options::QUIT_WITH_CONTROLLER ? 116 : 94;
Notifier::get()->showText(lang::getText(index), std::string(), -1, exit_code); Notifier::get()->showText({lang::getText(index), std::string()}, -1, exit_code);
#else #else
Notifier::get()->showText(lang::getText(94), std::string(), -1, exit_code); Notifier::get()->showText({lang::getText(94), std::string()}, -1, exit_code);
#endif #endif
} }
} }
@@ -50,11 +51,11 @@ namespace globalInputs
void reset() void reset()
{ {
section::name = section::Name::INIT; section::name = section::Name::INIT;
Notifier::get()->showText("Reset"); Notifier::get()->showText({"Reset"});
} }
// Activa o desactiva el audio // Activa o desactiva el audio
void switchAudio() void toggleAudio()
{ {
options.audio.enabled = !options.audio.enabled; options.audio.enabled = !options.audio.enabled;
if (options.audio.enabled) if (options.audio.enabled)
@@ -67,85 +68,153 @@ namespace globalInputs
JA_SetMusicVolume(0); JA_SetMusicVolume(0);
JA_SetSoundVolume(0); JA_SetSoundVolume(0);
} }
Notifier::get()->showText("Audio " + boolToOnOff(options.audio.enabled)); Notifier::get()->showText({"Audio " + boolToOnOff(options.audio.enabled)});
} }
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
void check() void check()
{ {
// Comprueba si se sale con el teclado // Teclado
if (Input::get()->checkInput(InputType::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{ {
quit(section::Options::QUIT_WITH_KEYBOARD); #ifndef ARCADE
return; // Comprueba el teclado para cambiar entre pantalla completa y ventana
} if (Input::get()->checkInput(InputType::WINDOW_FULLSCREEN, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
// Comprueba si se va a resetear el juego
if (Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
reset();
return;
}
if (Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
switchAudio();
return;
}
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
service_pressed_counter[0]++;
if (service_pressed_counter[0] >= 3000)
{ {
OnScreenHelp::get()->toggleState(); Screen::get()->toggleVideoMode();
service_pressed_counter[0] = 0; const std::string mode = options.video.mode == ScreenVideoMode::WINDOW ? "Window" : "Fullscreen";
} Notifier::get()->showText({mode + " mode"});
return;
}
service_pressed_counter[0] = 0;
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
// Comprueba si se sale con el mando
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
Input::get()->checkInput(InputType::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
quit(section::Options::QUIT_WITH_CONTROLLER);
return; return;
} }
// Comprueba si se va a resetear el juego // Comprueba el teclado para decrementar el tamaño de la ventana
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) && if (Input::get()->checkInput(InputType::WINDOW_DEC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) {
Screen::get()->decWindowSize();
const std::string size = std::to_string(options.video.window.size);
Notifier::get()->showText({"Window size x" + size});
return;
}
// Comprueba el teclado para incrementar el tamaño de la ventana
if (Input::get()->checkInput(InputType::WINDOW_INC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
Screen::get()->incWindowSize();
const std::string size = std::to_string(options.video.window.size);
Notifier::get()->showText({"Window size x" + size});
return;
}
#endif
// Salir
if (Input::get()->checkInput(InputType::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
quit(section::Options::QUIT_WITH_KEYBOARD);
return;
}
// Reset
if (Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
reset(); reset();
return; return;
} }
// Comprueba si se va a activar o desactivar el audio // Audio
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) && if (Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{ {
switchAudio(); toggleAudio();
return; return;
} }
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) // Shaders
if (Input::get()->checkInput(InputType::VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{ {
service_pressed_counter[i + 1]++; Screen::get()->toggleShaders();
return;
}
if (service_pressed_counter[i + 1] >= 3000) #ifdef DEBUG
// Comprueba el teclado para mostrar la información de debug
if (Input::get()->checkInput(InputType::SHOWINFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
Screen::get()->toggleDebugInfo();
return;
}
#endif
// OnScreenHelp
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::KEYBOARD))
{
service_pressed_counter[0]++;
if (service_pressed_counter[0] >= 3000)
{ {
OnScreenHelp::get()->toggleState(); OnScreenHelp::get()->toggleState();
service_pressed_counter[i + 1] = 0; service_pressed_counter[0] = 0;
} }
return; return;
} }
service_pressed_counter[i + 1] = 0; service_pressed_counter[0] = 0;
}
// Mandos
{
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
// Salir
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i) &&
Input::get()->checkInput(InputType::EXIT, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
quit(section::Options::QUIT_WITH_CONTROLLER);
return;
}
// Reset
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i) &&
Input::get()->checkInput(InputType::RESET, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
reset();
return;
}
// Audio
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i) &&
Input::get()->checkInput(InputType::MUTE, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
toggleAudio();
return;
}
// Shaders
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i) &&
Input::get()->checkInput(InputType::VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
Screen::get()->toggleShaders();
return;
}
#ifdef DEBUG
// Debug Info
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i) &&
Input::get()->checkInput(InputType::SHOWINFO, INPUT_DO_NOT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
Screen::get()->toggleDebugInfo();
return;
}
#endif
// OnScreenHelp
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::CONTROLLER, i))
{
service_pressed_counter[i + 1]++;
if (service_pressed_counter[i + 1] >= 3000)
{
OnScreenHelp::get()->toggleState();
service_pressed_counter[i + 1] = 0;
}
return;
}
service_pressed_counter[i + 1] = 0;
}
} }
} }
} }

View File

@@ -27,7 +27,7 @@ HiScoreTable::HiScoreTable()
backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)),
fade_(std::make_unique<Fade>()), fade_(std::make_unique<Fade>()),
background_(std::make_unique<Background>()), background_(std::make_unique<Background>()),
text_(std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), text_(Resource::get()->getText("smb2")),
counter_(0), counter_(0),
ticks_(0), ticks_(0),
view_area_({0, 0, param.game.width, param.game.height}), view_area_({0, 0, param.game.width, param.game.height}),
@@ -208,9 +208,6 @@ void HiScoreTable::checkInput()
return; return;
} }
// Comprueba el input para el resto de objetos
Screen::get()->checkInput();
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
globalInputs::check(); globalInputs::check();
} }

View File

@@ -33,7 +33,7 @@ private:
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades std::unique_ptr<Fade> fade_; // Objeto para renderizar fades
std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego std::unique_ptr<Background> background_; // Objeto para dibujar el fondo del juego
std::unique_ptr<Text> text_; // Objeto para escribir texto std::shared_ptr<Text> text_; // Objeto para escribir texto
// Variables // Variables
Uint16 counter_; // Contador Uint16 counter_; // Contador

View File

@@ -1,11 +1,13 @@
#include "input.h" #include "input.h"
#include <SDL2/SDL.h> // para SDL_INIT_GAMECONTROLLER, SDL_InitSubS... #include <SDL2/SDL.h> // Para SDL_INIT_GAMECONTROLLER, SDL_InitSubS...
#include <SDL2/SDL_error.h> // para SDL_GetError #include <SDL2/SDL_error.h> // Para SDL_GetError
#include <SDL2/SDL_events.h> // para SDL_ENABLE #include <SDL2/SDL_events.h> // Para SDL_ENABLE
#include <SDL2/SDL_keyboard.h> // para SDL_GetKeyboardState #include <SDL2/SDL_keyboard.h> // Para SDL_GetKeyboardState
#include <iostream> // para basic_ostream, operator<<, cout, basi... #include <algorithm> // Para find
#include <unordered_map> #include <iostream> // Para basic_ostream, operator<<, cout, endl
#include <algorithm> #include <iterator> // Para distance
#include <unordered_map> // Para unordered_map, operator==, _Node_cons...
#include <utility> // Para pair
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Input *Input::input_ = nullptr; Input *Input::input_ = nullptr;
@@ -68,31 +70,19 @@ void Input::bindGameControllerButton(int controller_index, InputType input_targe
} }
// Comprueba si un input esta activo // Comprueba si un input esta activo
bool Input::checkInput(InputType input, bool repeat, int device, int controller_index) bool Input::checkInput(InputType input, bool repeat, InputDeviceToUse device, int controller_index)
{ {
bool success_keyboard = false; bool success_keyboard = false;
bool success_controller = false; bool success_controller = false;
const int input_index = static_cast<int>(input); const int input_index = static_cast<int>(input);
if (device == INPUT_USE_ANY) if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
{
controller_index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{ {
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr); const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
if (repeat) if (repeat)
{ {
if (keyStates[key_bindings_[input_index].scancode] != 0) success_keyboard = keyStates[key_bindings_[input_index].scancode] != 0;
{
success_keyboard = true;
}
else
{
success_keyboard = false;
}
} }
else else
{ {
@@ -113,40 +103,29 @@ bool Input::checkInput(InputType input, bool repeat, int device, int controller_
if (keyStates[key_bindings_[input_index].scancode] == 0) if (keyStates[key_bindings_[input_index].scancode] == 0)
{ {
key_bindings_[input_index].active = false; key_bindings_[input_index].active = false;
success_keyboard = false;
}
else
{
success_keyboard = false;
} }
success_keyboard = false;
} }
} }
} }
if (gameControllerFound() && controller_index < num_gamepads_) if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) if ((device == InputDeviceToUse::CONTROLLER) || (device == InputDeviceToUse::ANY))
{ {
success_controller = checkAxisInput(input, controller_index); success_controller = checkAxisInput(input, controller_index);
if (!success_controller) if (!success_controller)
{ {
if (repeat) if (repeat)
{ {
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0) success_controller = SDL_GameControllerGetButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0;
{
success_controller = true;
}
else
{
success_controller = false;
}
} }
else else
{ {
if (!controller_bindings_[controller_index][input_index].active) if (!controller_bindings_.at(controller_index).at(input_index).active)
{ {
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0) if (SDL_GameControllerGetButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0)
{ {
controller_bindings_[controller_index][input_index].active = true; controller_bindings_.at(controller_index).at(input_index).active = true;
success_controller = true; success_controller = true;
} }
else else
@@ -156,15 +135,11 @@ bool Input::checkInput(InputType input, bool repeat, int device, int controller_
} }
else else
{ {
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) == 0) if (SDL_GameControllerGetButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) == 0)
{ {
controller_bindings_[controller_index][input_index].active = false; controller_bindings_.at(controller_index).at(input_index).active = false;
success_controller = false;
}
else
{
success_controller = false;
} }
success_controller = false;
} }
} }
} }
@@ -174,14 +149,9 @@ bool Input::checkInput(InputType input, bool repeat, int device, int controller_
} }
// Comprueba si hay almenos un input activo // Comprueba si hay almenos un input activo
bool Input::checkAnyInput(int device, int controller_index) bool Input::checkAnyInput(InputDeviceToUse device, int controller_index)
{ {
if (device == INPUT_USE_ANY) if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY)
{
controller_index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{ {
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr); const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
@@ -197,7 +167,7 @@ bool Input::checkAnyInput(int device, int controller_index)
if (gameControllerFound()) if (gameControllerFound())
{ {
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY)
{ {
for (int i = 0; i < (int)controller_bindings_.size(); ++i) for (int i = 0; i < (int)controller_bindings_.size(); ++i)
{ {
@@ -217,7 +187,7 @@ bool Input::checkAnyInput(int device, int controller_index)
int Input::checkAnyButtonPressed(bool repeat) int Input::checkAnyButtonPressed(bool repeat)
{ {
// Si está pulsado el botón de servicio, ningún botón se puede considerar pulsado // Si está pulsado el botón de servicio, ningún botón se puede considerar pulsado
if (checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_ANY)) if (checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, InputDeviceToUse::ANY))
{ {
return 0; return 0;
} }
@@ -226,7 +196,7 @@ int Input::checkAnyButtonPressed(bool repeat)
for (auto bi : button_inputs_) for (auto bi : button_inputs_)
{ {
// Comprueba el teclado // Comprueba el teclado
if (checkInput(bi, repeat, INPUT_USE_KEYBOARD)) if (checkInput(bi, repeat, InputDeviceToUse::KEYBOARD))
{ {
return 1; return 1;
} }
@@ -234,7 +204,7 @@ int Input::checkAnyButtonPressed(bool repeat)
// Comprueba los mandos // Comprueba los mandos
for (int i = 0; i < num_gamepads_; ++i) for (int i = 0; i < num_gamepads_; ++i)
{ {
if (checkInput(bi, repeat, INPUT_USE_GAMECONTROLLER, i)) if (checkInput(bi, repeat, InputDeviceToUse::CONTROLLER, i))
{ {
return i + 1; return i + 1;
} }
@@ -328,14 +298,14 @@ int Input::getJoyIndex(int id) const
} }
// Muestra por consola los controles asignados // Muestra por consola los controles asignados
void Input::printBindings(int device, int controller_index) const void Input::printBindings(InputDeviceToUse device, int controller_index) const
{ {
if (device == INPUT_USE_ANY || device == INPUT_USE_KEYBOARD) if (device == InputDeviceToUse::ANY || device == InputDeviceToUse::KEYBOARD)
{ {
return; return;
} }
if (device == INPUT_USE_GAMECONTROLLER) if (device == InputDeviceToUse::CONTROLLER)
{ {
if (controller_index >= num_gamepads_) if (controller_index >= num_gamepads_)
{ {

View File

@@ -11,9 +11,9 @@
connectedControllers es un vector donde estan todos los mandos encontrados [0 .. n] connectedControllers es un vector donde estan todos los mandos encontrados [0 .. n]
checkInput requiere de un indice para comprobar las pulsaciónes de un controlador en concreto [0 .. n] checkInput requiere de un indice para comprobar las pulsaciónes de un controlador en concreto [0 .. n]
device contiene el tipo de dispositivo a comprobar: device contiene el tipo de dispositivo a comprobar:
INPUT_USE_KEYBOARD solo mirará el teclado InputDeviceToUse::KEYBOARD solo mirará el teclado
INPUT_USE_GAMECONTROLLER solo mirará el controlador especificado (Si no se especifica, el primero) InputDeviceToUse::CONTROLLER solo mirará el controlador especificado (Si no se especifica, el primero)
INPUT_USE_ANY mirará tanto el teclado como el PRIMER controlador InputDeviceToUse::ANY mirará tanto el teclado como el PRIMER controlador
*/ */
enum class InputType : int enum class InputType : int
@@ -52,9 +52,12 @@ enum class InputType : int
constexpr bool INPUT_ALLOW_REPEAT = true; constexpr bool INPUT_ALLOW_REPEAT = true;
constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false; constexpr bool INPUT_DO_NOT_ALLOW_REPEAT = false;
constexpr int INPUT_USE_KEYBOARD = 0; enum class InputDeviceToUse : int
constexpr int INPUT_USE_GAMECONTROLLER = 1; {
constexpr int INPUT_USE_ANY = 2; KEYBOARD = 0,
CONTROLLER = 1,
ANY = 2,
};
class Input class Input
{ {
@@ -120,10 +123,10 @@ public:
void bindGameControllerButton(int controller_index, InputType inputTarget, InputType inputSource); void bindGameControllerButton(int controller_index, InputType inputTarget, InputType inputSource);
// Comprueba si un input esta activo // Comprueba si un input esta activo
bool checkInput(InputType input, bool repeat = true, int device = INPUT_USE_ANY, int controller_index = 0); bool checkInput(InputType input, bool repeat = true, InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0);
// Comprueba si hay almenos un input activo // Comprueba si hay almenos un input activo
bool checkAnyInput(int device = INPUT_USE_ANY, int controller_index = 0); bool checkAnyInput(InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0);
// Comprueba si hay algún botón pulsado // Comprueba si hay algún botón pulsado
int checkAnyButtonPressed(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT); int checkAnyButtonPressed(bool repeat = INPUT_DO_NOT_ALLOW_REPEAT);
@@ -144,7 +147,7 @@ public:
int getJoyIndex(int id) const; int getJoyIndex(int id) const;
// Muestra por consola los controles asignados // Muestra por consola los controles asignados
void printBindings(int device = INPUT_USE_KEYBOARD, int controller_index = 0) const; void printBindings(InputDeviceToUse device = InputDeviceToUse::KEYBOARD, int controller_index = 0) const;
// Obtiene el SDL_GameControllerButton asignado a un input // Obtiene el SDL_GameControllerButton asignado a un input
SDL_GameControllerButton getControllerBinding(int controller_index, InputType input) const; SDL_GameControllerButton getControllerBinding(int controller_index, InputType input) const;

View File

@@ -26,7 +26,7 @@ Instructions::Instructions()
: renderer_(Screen::get()->getRenderer()), : renderer_(Screen::get()->getRenderer()),
texture_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), texture_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)),
backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)), backbuffer_(SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, param.game.width, param.game.height)),
text_(std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), text_(Resource::get()->getText("smb2")),
tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::STATIC)), tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::STATIC)),
fade_(std::make_unique<Fade>()) fade_(std::make_unique<Fade>())
{ {
@@ -313,9 +313,6 @@ void Instructions::checkInput()
return; return;
} }
// Comprueba el input para el resto de objetos
Screen::get()->checkInput();
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
globalInputs::check(); globalInputs::check();
} }

View File

@@ -35,7 +35,7 @@ private:
std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items std::vector<std::shared_ptr<Texture>> item_textures_; // Vector con las texturas de los items
std::vector<std::unique_ptr<Sprite>> sprites_; // Vector con los sprites de los items std::vector<std::unique_ptr<Sprite>> sprites_; // Vector con los sprites de los items
std::unique_ptr<Text> text_; // Objeto para escribir texto std::shared_ptr<Text> text_; // Objeto para escribir texto
std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo
std::unique_ptr<Fade> fade_; // Objeto para renderizar fades std::unique_ptr<Fade> fade_; // Objeto para renderizar fades

View File

@@ -20,7 +20,7 @@
// Constructor // Constructor
Intro::Intro() Intro::Intro()
: texture_(Resource::get()->getTexture("intro.png")), : texture_(Resource::get()->getTexture("intro.png")),
text_(std::make_shared<Text>(Resource::get()->getTexture("nokia.png"), Resource::get()->getTextFile("nokia.txt"))) text_(Resource::get()->getText("nokia"))
{ {
// Inicializa variables // Inicializa variables
@@ -191,9 +191,6 @@ void Intro::checkInput()
return; return;
} }
// Comprueba el input para el resto de objetos
Screen::get()->checkInput();
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
globalInputs::check(); globalInputs::check();
} }

View File

@@ -1,10 +1,10 @@
#include "item.h" #include "item.h"
#include <algorithm> // para std::clamp
#include <stdlib.h> // para rand #include <stdlib.h> // para rand
#include "animated_sprite.h" // para SpriteAnimated #include "animated_sprite.h" // para SpriteAnimated
#include "param.h" // para param #include "param.h" // para param
class Texture; class Texture;
// Constructor
Item::Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation) Item::Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation)
: sprite_(std::make_unique<AnimatedSprite>(texture, animation)), : sprite_(std::make_unique<AnimatedSprite>(texture, animation)),
type_(type), type_(type),
@@ -38,34 +38,26 @@ Item::Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr
} }
} }
sprite_->setPosX(pos_x_); // Actualiza el sprite
sprite_->setPosY(pos_y_); shiftSprite();
shiftColliders(); shiftColliders();
} }
// Centra el objeto en la posición X
void Item::alignTo(int x) void Item::alignTo(int x)
{ {
pos_x_ = static_cast<float>(x - (width_ / 2)); const float min_x = param.game.play_area.rect.x + 1;
const float max_x = play_area_.w - width_ - 1;
if (pos_x_ < param.game.play_area.rect.x) pos_x_ = x - (width_ / 2);
{
pos_x_ = param.game.play_area.rect.x + 1;
}
else if (pos_x_ + width_ > play_area_.w)
{
pos_x_ = static_cast<float>(play_area_.w - width_ - 1);
}
// Posición X,Y del sprite // Ajusta para que no quede fuera de la zona de juego
sprite_->setPosX(pos_x_); pos_x_ = std::clamp(pos_x_, min_x, max_x);
sprite_->setPosY(pos_y_);
// Alinea el circulo de colisión con el objeto // Actualiza el sprite
shiftSprite();
shiftColliders(); shiftColliders();
} }
// Pinta el objeto en la pantalla
void Item::render() void Item::render()
{ {
if (enabled_) if (enabled_)
@@ -81,7 +73,8 @@ void Item::render()
} }
} }
// Actualiza la posición y estados del objeto #include <algorithm> // Necesario para std::clamp
void Item::move() void Item::move()
{ {
floor_collision_ = false; floor_collision_ = false;
@@ -94,129 +87,90 @@ void Item::move()
vel_x_ += accel_x_; vel_x_ += accel_x_;
vel_y_ += accel_y_; vel_y_ += accel_y_;
// Si queda fuera de pantalla, corregimos su posición y cambiamos su sentido // Comprueba los laterales de la zona de juego
if ((pos_x_ < param.game.play_area.rect.x) || (pos_x_ + width_ > play_area_.w)) const float min_x = param.game.play_area.rect.x;
{ const float max_x = play_area_.w - width_;
// Corregir posición pos_x_ = std::clamp(pos_x_, min_x, max_x);
pos_x_ -= vel_x_;
// Invertir sentido // Si toca el borde lateral, invierte la velocidad horizontal
if (pos_x_ == min_x || pos_x_ == max_x)
{
vel_x_ = -vel_x_; vel_x_ = -vel_x_;
} }
// Si se sale por arriba rebota (excepto la maquina de café) // Si colisiona por arriba, rebota (excepto la máquina de café)
if ((pos_y_ < param.game.play_area.rect.y) && !(type_ == ItemType::COFFEE_MACHINE)) if ((pos_y_ < param.game.play_area.rect.y) && !(type_ == ItemType::COFFEE_MACHINE))
{ {
// Corrige // Corrige
pos_y_ = param.game.play_area.rect.y; pos_y_ = param.game.play_area.rect.y;
// Invierte el sentido // Invierte la velocidad
vel_y_ = -vel_y_; vel_y_ = -vel_y_;
} }
// Si el objeto se sale por la parte inferior // Si colisiona con la parte inferior
if (pos_y_ + height_ > play_area_.h) if (pos_y_ > play_area_.h - height_)
{ {
// Detiene el objeto // Corrige la posición
vel_y_ = 0;
vel_x_ = 0;
accel_x_ = 0;
accel_y_ = 0;
pos_y_ = play_area_.h - height_; pos_y_ = play_area_.h - height_;
if (type_ == ItemType::COFFEE_MACHINE) if (type_ == ItemType::COFFEE_MACHINE)
{ {
// Si es una máquina de café, detiene el objeto
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
floor_collision_ = true; floor_collision_ = true;
} }
else
{
// Si no es una máquina de café
if (vel_y_ < 1.0f)
{
// Si la velocidad vertical es baja, detiene el objeto
vel_y_ = vel_x_ = accel_x_ = accel_y_ = 0;
}
else
{
// Si la velocidad vertical es alta, el objeto rebota y pierde velocidad
vel_y_ *= -0.5f;
vel_x_ *= 0.75f;
}
}
} }
// Actualiza la posición del sprite // Actualiza la posición del sprite
sprite_->setPosX(int(pos_x_)); shiftSprite();
sprite_->setPosY(int(pos_y_));
shiftColliders(); shiftColliders();
} }
// Pone a cero todos los valores del objeto void Item::disable() { enabled_ = false; }
void Item::disable()
{
enabled_ = false;
}
// Actualiza el objeto a su posicion, animación y controla los contadores
void Item::update() void Item::update()
{ {
move(); move();
sprite_->update(); sprite_->update();
updateTimeToLive(); updateTimeToLive();
checkTimeToLive();
} }
// Actualiza el contador
void Item::updateTimeToLive() void Item::updateTimeToLive()
{ {
if (time_to_live_ > 0) if (time_to_live_ > 0)
{ {
time_to_live_--; time_to_live_--;
} }
} else
{
// Comprueba si el objeto sigue vivo
void Item::checkTimeToLive()
{
if (time_to_live_ == 0)
disable(); disable();
}
} }
// Obtiene del valor de la variable
float Item::getPosX()
{
return pos_x_;
}
// Obtiene del valor de la variable
float Item::getPosY()
{
return pos_y_;
}
// Obtiene del valor de la variable
int Item::getWidth()
{
return width_;
}
// Obtiene del valor de la variable
int Item::getHeight()
{
return height_;
}
// Obtiene del valor de la variable
ItemType Item::getType()
{
return type_;
}
// Obtiene el valor de la variable
bool Item::isEnabled()
{
return enabled_;
}
// Obtiene el circulo de colisión
Circle &Item::getCollider()
{
return collider_;
}
// Alinea el circulo de colisión con la posición del objeto
void Item::shiftColliders() void Item::shiftColliders()
{ {
collider_.x = int(pos_x_ + (width_ / 2)); collider_.x = static_cast<int>(pos_x_ + (width_ / 2));
collider_.y = int(pos_y_ + (height_ / 2)); collider_.y = static_cast<int>(pos_y_ + (height_ / 2));
} }
// Informa si el objeto ha colisionado con el suelo void Item::shiftSprite()
bool Item::isOnFloor()
{ {
return floor_collision_; sprite_->setPosX(pos_x_);
sprite_->setPosY(pos_y_);
} }

View File

@@ -9,93 +9,147 @@
#include "utils.h" // para Circle #include "utils.h" // para Circle
class Texture; class Texture;
// Tipos de objetos /**
* @brief Tipos de objetos disponibles en el juego.
*
* Esta enumeración define los diferentes tipos de objetos que pueden existir en el juego,
* cada uno con un identificador único.
*/
enum class ItemType : int enum class ItemType : int
{ {
DISK = 1, DISK = 1, /**< Disco */
GAVINA = 2, GAVINA = 2, /**< Gavina */
PACMAR = 3, PACMAR = 3, /**< Pacman */
CLOCK = 4, CLOCK = 4, /**< Reloj */
COFFEE = 5, COFFEE = 5, /**< Café */
COFFEE_MACHINE = 6, COFFEE_MACHINE = 6,/**< Máquina de café */
NONE = 7, NONE = 7, /**< Ninguno */
}; };
// Clase Item /**
* @brief Clase Item.
*
* Esta clase representa un objeto en el juego, con sus propiedades y métodos para gestionar su comportamiento.
*/
class Item class Item
{ {
private: private:
// Objetos y punteros // Objetos y punteros
std::unique_ptr<AnimatedSprite> sprite_; // Sprite con los graficos del objeto std::unique_ptr<AnimatedSprite> sprite_; /**< Sprite con los gráficos del objeto */
// Variables // Variables
float pos_x_; // Posición X del objeto float pos_x_; /**< Posición X del objeto */
float pos_y_; // Posición Y del objeto float pos_y_; /**< Posición Y del objeto */
int width_; // Ancho del objeto int width_; /**< Ancho del objeto */
int height_; // Alto del objeto int height_; /**< Alto del objeto */
float vel_x_; // Velocidad en el eje X float vel_x_; /**< Velocidad en el eje X */
float vel_y_; // Velocidad en el eje Y float vel_y_; /**< Velocidad en el eje Y */
float accel_x_ = 0.0f; // Aceleración en el eje X float accel_x_ = 0.0f; /**< Aceleración en el eje X */
float accel_y_; // Aceleración en el eje Y float accel_y_; /**< Aceleración en el eje Y */
bool floor_collision_ = false; // Indica si el objeto colisiona con el suelo bool floor_collision_ = false; /**< Indica si el objeto colisiona con el suelo */
ItemType type_; // Especifica el tipo de objeto que es ItemType type_; /**< Especifica el tipo de objeto que es */
bool enabled_ = true; // Especifica si el objeto está habilitado bool enabled_ = true; /**< Especifica si el objeto está habilitado */
Circle collider_; // Circulo de colisión del objeto Circle collider_; /**< Círculo de colisión del objeto */
SDL_Rect play_area_; // Rectangulo con la zona de juego SDL_Rect play_area_; /**< Rectángulo con la zona de juego */
Uint16 time_to_live_ = 600; // Temporizador con el tiempo que el objeto está presente Uint16 time_to_live_ = 600; /**< Temporizador con el tiempo que el objeto está presente */
// Alinea el circulo de colisión con la posición del objeto /**
* @brief Alinea el círculo de colisión con la posición del objeto.
*
* Esta función ajusta la posición del círculo de colisión para que coincida con la posición del objeto.
* Actualiza las coordenadas X e Y del colisionador basándose en las coordenadas del objeto.
*/
void shiftColliders(); void shiftColliders();
// Actualiza la posición y estados del objeto /**
* @brief Coloca el sprite en la posición del objeto.
*
* Esta función ajusta la posición del sprite para que coincida con la posición del objeto.
* Actualiza las coordenadas X e Y del sprite basándose en las coordenadas del objeto.
*/
void shiftSprite();
/**
* @brief Actualiza la posición y estados del objeto.
*
* Esta función actualiza la posición del objeto basándose en su velocidad y aceleración.
* Controla las colisiones con los límites del área de juego, ajustando la posición y la velocidad en consecuencia.
* También actualiza la posición del sprite y el colisionador del objeto.
*/
void move(); void move();
// Actualiza el contador /**
* @brief Actualiza el contador de tiempo de vida del objeto.
*
* Esta función decrementa el contador de tiempo de vida del objeto.
* Si el tiempo de vida es mayor a 0, se decrementa en 1.
* Si el tiempo de vida es 0 o menos, se desactiva el objeto llamando a la función `disable()`.
*/
void updateTimeToLive(); void updateTimeToLive();
// Comprueba si el objeto sigue vivo
void checkTimeToLive();
public: public:
// Constructor /**
* @brief Constructor de la clase Item.
*
* Este constructor inicializa un objeto Item con el tipo especificado, posición inicial, área de juego, textura y animación.
*
* @param type El tipo de objeto (ItemType).
* @param x La posición X inicial del objeto.
* @param y La posición Y inicial del objeto.
* @param play_area El área de juego donde el objeto se moverá.
* @param texture La textura del objeto.
* @param animation La animación asociada al objeto.
*/
Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation); Item(ItemType type, float x, float y, SDL_Rect &play_area, std::shared_ptr<Texture> texture, const std::vector<std::string> &animation);
// Destructor /**
* @brief Destructor de la clase Item.
*
* Este destructor libera los recursos asociados con el objeto Item.
*/
~Item() = default; ~Item() = default;
// Centra el objeto en la posición X /**
* @brief Centra el objeto en la posición X.
*
* Esta función ajusta la posición X del objeto para que esté centrado en la posición X especificada.
* Además, asegura que el objeto no se salga de los límites del área de juego.
*
* @param x La posición X en la que se desea centrar el objeto.
*/
void alignTo(int x); void alignTo(int x);
// Pinta el objeto en la pantalla /**
* @brief Pinta el objeto en la pantalla.
*
* Esta función renderiza el objeto en la pantalla si está habilitado.
* Si el tiempo de vida (`time_to_live_`) es mayor que 200, renderiza el sprite.
* Si el tiempo de vida es menor o igual a 200, renderiza el sprite de forma intermitente, basándose en un cálculo de módulo.
*/
void render(); void render();
// Pone a cero todos los valores del objeto /**
* @brief Pone a cero todos los valores del objeto.
*
* Esta función desactiva el objeto estableciendo su estado `enabled_` a `false`.
*/
void disable(); void disable();
// Actualiza al objeto a su posicion, animación y controla los contadores /**
* @brief Actualiza el objeto a su posición, animación y controla los contadores.
*
* Esta función mueve el objeto, actualiza su animación y controla el contador de tiempo de vida.
* Llama a las funciones `move()`, `sprite_->update()` y `updateTimeToLive()`.
*/
void update(); void update();
// Obtiene del valor de la variable // Getters
float getPosX(); float getPosX() const { return pos_x_; }
float getPosY() const { return pos_y_; }
// Obtiene del valor de la variable int getWidth() const { return width_; }
float getPosY(); int getHeight() const { return height_; }
ItemType getType() const { return type_; }
// Obtiene del valor de la variable bool isEnabled() const { return enabled_; }
int getWidth(); bool isOnFloor() const { return floor_collision_; }
Circle &getCollider() { return collider_; }
// Obtiene del valor de la variable
int getHeight();
// Obtiene del valor de la variable
ItemType getType();
// Obtiene el valor de la variable
bool isEnabled();
// Obtiene el circulo de colisión
Circle &getCollider();
// Informa si el objeto ha colisionado con el suelo
bool isOnFloor();
}; };

View File

@@ -12,6 +12,7 @@
#include "section.h" // Para Name, name, Options, options #include "section.h" // Para Name, name, Options, options
#include "sprite.h" // Para Sprite #include "sprite.h" // Para Sprite
#include "texture.h" // Para Texture #include "texture.h" // Para Texture
#include "utils.h" // Para Color, Zone
// Constructor // Constructor
Logo::Logo() Logo::Logo()
@@ -105,9 +106,6 @@ void Logo::checkInput()
return; return;
} }
// Comprueba el input para el resto de objetos
Screen::get()->checkInput();
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
globalInputs::check(); globalInputs::check();
} }

View File

@@ -1,12 +1,12 @@
#pragma once #pragma once
#include <SDL2/SDL_rect.h> // para SDL_Point #include <SDL2/SDL_rect.h> // Para SDL_Point
#include <SDL2/SDL_stdinc.h> // para Uint32 #include <SDL2/SDL_stdinc.h> // Para Uint32
#include <memory> // para unique_ptr, shared_ptr #include <memory> // Para shared_ptr, unique_ptr
#include <vector> // para vector #include <vector> // Para vector
#include "sprite.h" // para Sprite class Sprite;
#include "utils.h" // para Color class Texture; // lines 9-9
class Texture; struct Color;
/* /*
Esta clase gestiona un estado del programa. Se encarga de dibujar por pantalla el Esta clase gestiona un estado del programa. Se encarga de dibujar por pantalla el

View File

@@ -12,16 +12,18 @@ void ManageHiScoreTable::clear()
table_.clear(); table_.clear();
// Añade 10 entradas predefinidas // Añade 10 entradas predefinidas
table_.push_back(HiScoreEntry("Bry", 1000000)); table_.push_back(HiScoreEntry("BRY", 1000000));
table_.push_back(HiScoreEntry("Usufondo", 500000)); table_.push_back(HiScoreEntry("USUFON", 500000));
table_.push_back(HiScoreEntry("G.Lucas", 100000)); table_.push_back(HiScoreEntry("GLUCAS", 100000));
table_.push_back(HiScoreEntry("P.Delgat", 50000)); table_.push_back(HiScoreEntry("PDLGAT", 50000));
table_.push_back(HiScoreEntry("P.Arrabalera", 10000)); table_.push_back(HiScoreEntry("PARRAB", 10000));
table_.push_back(HiScoreEntry("Pelechano", 5000)); table_.push_back(HiScoreEntry("PELECH", 5000));
table_.push_back(HiScoreEntry("Sahuquillo", 1000)); table_.push_back(HiScoreEntry("SAHUQU", 1000));
table_.push_back(HiScoreEntry("Bacteriol", 500)); table_.push_back(HiScoreEntry("BACTER", 500));
table_.push_back(HiScoreEntry("Pepe", 200)); table_.push_back(HiScoreEntry("PEPE", 200));
table_.push_back(HiScoreEntry("Rosita", 100)); table_.push_back(HiScoreEntry("ROSITA", 100));
sort();
} }
// Añade un elemento a la tabla // Añade un elemento a la tabla

View File

@@ -96,6 +96,12 @@ void MovingSprite::setAngle(double value)
rotate_.angle = value; rotate_.angle = value;
} }
// Establece el valor de la variable
void MovingSprite::setRotatingCenter(SDL_Point *point)
{
rotate_.center = point;
}
// Incrementa el valor del ángulo // Incrementa el valor del ángulo
void MovingSprite::updateAngle() void MovingSprite::updateAngle()
{ {
@@ -122,17 +128,10 @@ void MovingSprite::rotate()
} }
} }
// Establece el valor de la variable // Activa o desactiva el efecto de rotación
void MovingSprite::enableRotate() void MovingSprite::setRotate(bool enable)
{ {
rotate_.enabled = true; rotate_.enabled = enable;
rotate_.counter = 0;
}
// Establece el valor de la variable
void MovingSprite::disableRotate()
{
rotate_.enabled = false;
rotate_.counter = 0; rotate_.counter = 0;
} }
@@ -172,41 +171,6 @@ SDL_RendererFlip MovingSprite::getFlip()
return flip_; return flip_;
} }
// Obtiene el valor de la variable
float MovingSprite::getPosX() const
{
return x_;
}
// Obtiene el valor de la variable
float MovingSprite::getPosY() const
{
return y_;
}
// Obtiene el valor de la variable
float MovingSprite::getVelX() const
{
return vx_;
}
// Obtiene el valor de la variable
float MovingSprite::getVelY() const
{
return vy_;
}
// Obtiene el valor de la variable
float MovingSprite::getAccelX() const
{
return ax_;
}
// Obtiene el valor de la variable
float MovingSprite::getAccelY() const
{
return ay_;
}
// Establece la posición y_ el tamaño del objeto // Establece la posición y_ el tamaño del objeto
void MovingSprite::setPos(SDL_Rect rect) void MovingSprite::setPos(SDL_Rect rect)

View File

@@ -19,7 +19,7 @@ public:
float amount; // Cantidad de grados a girar en cada iteración float amount; // Cantidad de grados a girar en cada iteración
SDL_Point *center; // Centro de rotación SDL_Point *center; // Centro de rotación
Rotate() : enabled(false), counter(0), speed(0), angle(0.0), amount(0.0f), center(nullptr) {} Rotate() : enabled(false), counter(0), speed(1), angle(0.0), amount(0.0f), center(nullptr) {}
}; };
protected: protected:
@@ -65,12 +65,12 @@ public:
void render() override; void render() override;
// Obtiene la variable // Obtiene la variable
float getPosX() const; float getPosX() const { return x_; }
float getPosY() const; float getPosY() const { return y_; }
float getVelX() const; float getVelX() const { return vx_; }
float getVelY() const; float getVelY() const { return vy_; }
float getAccelX() const; float getAccelX() const { return ax_; }
float getAccelY() const; float getAccelY() const { return ay_; }
// Establece la variable // Establece la variable
void setVelX(float value); void setVelX(float value);
@@ -87,10 +87,10 @@ public:
// Establece el valor de la variable // Establece el valor de la variable
void setAngle(double vaue); void setAngle(double vaue);
void setRotatingCenter(SDL_Point *point);
// Activa o desactiva el efecto derotación // Activa o desactiva el efecto de rotación
void enableRotate(); void setRotate(bool enable);
void disableRotate();
// Establece el valor de la variable // Establece el valor de la variable
void setRotateSpeed(int value); void setRotateSpeed(int value);

View File

@@ -2,12 +2,14 @@
#include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND #include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888 #include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
#include <string> // Para string #include <string> // Para string
#include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla... #include <algorithm>
#include "param.h" // Para Param, param, ParamNotification, Par... #include <vector>
#include "screen.h" // Para Screen #include "jail_audio.h" // Para JA_DeleteSound, JA_LoadSound, JA_Pla...
#include "sprite.h" // Para Sprite #include "param.h" // Para Param, param, ParamNotification, Par...
#include "text.h" // Para Text #include "screen.h" // Para Screen
#include "texture.h" // Para Texture #include "sprite.h" // Para Sprite
#include "text.h" // Para Text
#include "texture.h" // Para Texture
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Notifier *Notifier::notifier_ = nullptr; Notifier *Notifier::notifier_ = nullptr;
@@ -51,7 +53,9 @@ Notifier::~Notifier()
void Notifier::render() void Notifier::render()
{ {
for (int i = (int)notifications_.size() - 1; i >= 0; --i) for (int i = (int)notifications_.size() - 1; i >= 0; --i)
{
notifications_[i].sprite->render(); notifications_[i].sprite->render();
}
} }
// Actualiza el estado de las notificaiones // Actualiza el estado de las notificaiones
@@ -154,39 +158,41 @@ void Notifier::clearFinishedNotifications()
} }
} }
void Notifier::showText(std::string text1, std::string text2, int icon, const std::string &code) void Notifier::showText(std::vector<std::string> texts, int icon, const std::string &code)
{ {
// Cuenta el número de textos a mostrar
const int num_texts = !text1.empty() + !text2.empty();
// Si no hay texto, acaba // Si no hay texto, acaba
if (num_texts == 0) if (texts.empty())
{ {
return; return;
} }
// Si solo hay un texto, lo coloca en la primera variable
if (num_texts == 1)
{
text1 += text2;
text2.clear();
}
// Si las notificaciones no se apilan, elimina las anteriores // Si las notificaciones no se apilan, elimina las anteriores
if (!stack_) if (!stack_)
{ {
clearNotifications(); clearNotifications();
} }
// Elimina las cadenas vacías
texts.erase(std::remove_if(texts.begin(), texts.end(), [](const std::string &s)
{ return s.empty(); }),
texts.end());
// Encuentra la cadena más larga
std::string longest;
for (const auto &text : texts)
{
if (text.length() > longest.length())
longest = text;
}
// Inicializa variables // Inicializa variables
constexpr auto icon_size = 16; constexpr int icon_size = 16;
constexpr auto padding_out = 1; constexpr int padding_out = 1;
const auto padding_in_h = text_->getCharacterSize(); const auto padding_in_h = text_->getCharacterSize();
const auto padding_in_v = text_->getCharacterSize() / 2; const auto padding_in_v = text_->getCharacterSize() / 2;
const auto icon_space = icon >= 0 ? icon_size + padding_in_h : 0; const int icon_space = icon >= 0 ? icon_size + padding_in_h : 0;
const std::string txt = text1.length() > text2.length() ? text1 : text2; const int width = text_->lenght(longest) + (padding_in_h * 2) + icon_space;
const auto width = text_->lenght(txt) + (padding_in_h * 2) + icon_space; const int height = (text_->getCharacterSize() * texts.size()) + (padding_in_v * 2);
const auto height = (text_->getCharacterSize() * num_texts) + (padding_in_v * 2);
const auto shape = NotificationShape::SQUARED; const auto shape = NotificationShape::SQUARED;
// Posición horizontal // Posición horizontal
@@ -212,11 +218,11 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
auto offset = 0; auto offset = 0;
if (param.notification.pos_v == NotifyPosition::TOP) if (param.notification.pos_v == NotifyPosition::TOP)
{ {
offset = (int)notifications_.size() > 0 ? notifications_.back().y + travel_dist : desp_v; offset = !notifications_.empty() ? notifications_.back().y + travel_dist : desp_v;
} }
else else
{ {
offset = (int)notifications_.size() > 0 ? notifications_.back().y - travel_dist : desp_v; offset = !notifications_.empty() ? notifications_.back().y - travel_dist : desp_v;
} }
// Crea la notificacion // Crea la notificacion
@@ -226,8 +232,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
n.code = code; n.code = code;
n.y = offset; n.y = offset;
n.travel_dist = travel_dist; n.travel_dist = travel_dist;
n.text1 = text1; n.texts = texts;
n.text2 = text2;
n.shape = shape; n.shape = shape;
auto y_pos = offset + (param.notification.pos_v == NotifyPosition::TOP ? -travel_dist : travel_dist); auto y_pos = offset + (param.notification.pos_v == NotifyPosition::TOP ? -travel_dist : travel_dist);
n.rect = {desp_h, y_pos, width, height}; n.rect = {desp_h, y_pos, width, height};
@@ -264,7 +269,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
} }
// Dibuja el icono de la notificación // Dibuja el icono de la notificación
if (has_icons_ && icon >= 0 && num_texts == 2) if (has_icons_ && icon >= 0 && texts.size() >= 2)
{ {
auto sp = std::make_unique<Sprite>(icon_texture_, (SDL_Rect){0, 0, icon_size, icon_size}); auto sp = std::make_unique<Sprite>(icon_texture_, (SDL_Rect){0, 0, icon_size, icon_size});
sp->setPosition({padding_in_h, padding_in_v, icon_size, icon_size}); sp->setPosition({padding_in_h, padding_in_v, icon_size, icon_size});
@@ -273,15 +278,12 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
} }
// Escribe el texto de la notificación // Escribe el texto de la notificación
Color color{255, 255, 255}; const Color color{255, 255, 255};
if (num_texts == 2) int iterator = 0;
{ // Dos lineas de texto for (const auto &text : texts)
text_->writeColored(padding_in_h + icon_space, padding_in_v, text1, color); {
text_->writeColored(padding_in_h + icon_space, padding_in_v + text_->getCharacterSize() + 1, text2, color); text_->writeColored(padding_in_h + icon_space, padding_in_v + iterator * (text_->getCharacterSize() + 1), text, color);
} ++iterator;
else
{ // Una linea de texto
text_->writeColored(padding_in_h + icon_space, padding_in_v, text1, color);
} }
// Deja de dibujar en la textura // Deja de dibujar en la textura
@@ -294,26 +296,18 @@ void Notifier::showText(std::string text1, std::string text2, int icon, const st
n.texture->setAlpha(0); n.texture->setAlpha(0);
// Añade la notificación a la lista // Añade la notificación a la lista
notifications_.push_back(n); notifications_.emplace_back(n);
} }
// Indica si hay notificaciones activas // Indica si hay notificaciones activas
bool Notifier::isActive() bool Notifier::isActive() { return !notifications_.empty(); }
{
if ((int)notifications_.size() > 0)
{
return true;
}
return false;
}
// Finaliza y elimnina todas las notificaciones activas // Finaliza y elimnina todas las notificaciones activas
void Notifier::clearNotifications() void Notifier::clearNotifications()
{ {
for (int i = 0; i < (int)notifications_.size(); ++i) for (auto &notification : notifications_)
{ {
notifications_[i].status = NotificationStatus::FINISHED; notification.status = NotificationStatus::FINISHED;
} }
clearFinishedNotifications(); clearFinishedNotifications();
@@ -323,9 +317,9 @@ void Notifier::clearNotifications()
std::vector<std::string> Notifier::getCodes() std::vector<std::string> Notifier::getCodes()
{ {
std::vector<std::string> codes; std::vector<std::string> codes;
for (int i = 0; i < (int)notifications_.size(); ++i) for (const auto &notification : notifications_)
{ {
codes.push_back(notifications_[i].code); codes.emplace_back(notification.code);
} }
return codes; return codes;
} }

View File

@@ -35,8 +35,7 @@ private:
{ {
std::shared_ptr<Texture> texture; std::shared_ptr<Texture> texture;
std::shared_ptr<Sprite> sprite; std::shared_ptr<Sprite> sprite;
std::string text1; std::vector<std::string> texts;
std::string text2;
int counter; int counter;
NotificationStatus status; NotificationStatus status;
NotificationShape shape; NotificationShape shape;
@@ -47,7 +46,7 @@ private:
// Constructor // Constructor
explicit Notification() explicit Notification()
: texture(nullptr), sprite(nullptr), text1(""), text2(""), counter(0), status(NotificationStatus::RISING), : texture(nullptr), sprite(nullptr), texts(), counter(0), status(NotificationStatus::RISING),
shape(NotificationShape::SQUARED), rect{0, 0, 0, 0}, y(0), travel_dist(0), code("") {} shape(NotificationShape::SQUARED), rect{0, 0, 0, 0}, y(0), travel_dist(0), code("") {}
}; };
@@ -95,15 +94,8 @@ public:
// Actualiza el estado de las notificaiones // Actualiza el estado de las notificaiones
void update(); void update();
/** // Muestra una notificación de texto por pantalla
* @brief Muestra una notificación de texto por pantalla. void showText(std::vector<std::string> texts, int icon = -1, const std::string &code = std::string());
*
* @param text1 Primer texto opcional para mostrar (valor predeterminado: cadena vacía).
* @param text2 Segundo texto opcional para mostrar (valor predeterminado: cadena vacía).
* @param icon Icono opcional para mostrar (valor predeterminado: -1).
* @param code Permite asignar un código a la notificación (valor predeterminado: cadena vacía).
*/
void showText(std::string text1 = std::string(), std::string text2 = std::string(), int icon = -1, const std::string &code = std::string());
// Indica si hay notificaciones activas // Indica si hay notificaciones activas
bool isActive(); bool isActive();

View File

@@ -94,7 +94,7 @@ void OnScreenHelp::fillTexture()
SDL_SetRenderTarget(Screen::get()->getRenderer(), texture); SDL_SetRenderTarget(Screen::get()->getRenderer(), texture);
// Crea el objeto para el texto // Crea el objeto para el texto
auto text = std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); auto text = Resource::get()->getText("8bithud");
// Crea la textura con los gráficos // Crea la textura con los gráficos
auto controllersTexture = Resource::get()->getTexture("controllers.png"); auto controllersTexture = Resource::get()->getTexture("controllers.png");
@@ -169,7 +169,7 @@ void OnScreenHelp::toggleState()
// Calcula la longitud en pixels del texto más largo // Calcula la longitud en pixels del texto más largo
auto OnScreenHelp::getLargestStringSize() -> int const auto OnScreenHelp::getLargestStringSize() -> int const
{ {
auto text = std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt")); auto text = Resource::get()->getText("8bithud");
auto size = 0; auto size = 0;
for (int i = 107; i <= 113; ++i) for (int i = 107; i <= 113; ++i)

View File

@@ -1,13 +1,14 @@
#include "options.h" #include "options.h"
#include <SDL2/SDL_gamecontroller.h> // para SDL_GameControllerButton, SDL_C... #include <SDL2/SDL_gamecontroller.h> // Para SDL_GameControllerButton
#include <algorithm> // para max, min #include <algorithm> // Para clamp
#include <fstream> // para basic_ostream, operator<<, basi... #include <fstream> // Para basic_ostream, operator<<, basi...
#include <iostream> // para cout #include <iostream> // Para cout
#include <vector> // para vector #include <utility> // Para swap
#include "input.h" // para inputs_e, INPUT_USE_ANY, INPUT_... #include <vector> // Para vector
#include "lang.h" // para lang_e #include "input.h" // Para InputDeviceToUse
#include "screen.h" // para ScreenVideoMode, ScreenFilter #include "lang.h" // Para Code
#include "utils.h" // para OptionsController, Options, op_... #include "screen.h" // Para ScreenVideoMode, ScreenFilter
#include "utils.h" // Para boolToString, stringToBool, get...
// Variables // Variables
Options options; Options options;
@@ -45,37 +46,11 @@ void initOptions()
options.game.autofire = true; options.game.autofire = true;
// Opciones de control // Opciones de control
options.controller.clear(); options.controllers.clear();
OptionsController c; options.controllers.resize(2);
options.controllers.at(0).player_id = 1;
constexpr int num_players = 2; options.controllers.at(1).player_id = 2;
for (int index = 0; index < num_players; ++index) setKeyboardToPlayer(1);
{
c.index = index;
c.player_id = index + 1;
c.device_type = INPUT_USE_GAMECONTROLLER;
c.name = "NO NAME";
c.plugged = false;
// Inputs que se guardan en las opciones y, por tanto, a disco
c.inputs.clear();
c.inputs.push_back(InputType::FIRE_LEFT);
c.inputs.push_back(InputType::FIRE_CENTER);
c.inputs.push_back(InputType::FIRE_RIGHT);
c.inputs.push_back(InputType::START);
c.inputs.push_back(InputType::SERVICE);
// Botones asociados a los inputs anteriores
c.buttons.clear();
c.buttons.push_back(SDL_CONTROLLER_BUTTON_X);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_Y);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_B);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_START);
c.buttons.push_back(SDL_CONTROLLER_BUTTON_BACK);
options.controller.push_back(c);
}
options.controller[0].device_type = INPUT_USE_ANY; // El primer jugador puede usar tanto el teclado como el primer mando
} }
// Carga el fichero de configuración // Carga el fichero de configuración
@@ -129,12 +104,11 @@ bool loadOptionsFile(std::string file_path)
options.video.mode = ScreenVideoMode::WINDOW; options.video.mode = ScreenVideoMode::WINDOW;
} }
if (options.video.window.size < 1 || options.video.window.size > 4) options.video.window.size = std::clamp(options.video.window.size, 1, 4);
{
options.video.window.size = 3;
}
if (options.game.language != lang::Code::en_UK && options.game.language != lang::Code::ba_BA && options.game.language != lang::Code::es_ES) if (options.game.language != lang::Code::en_UK &&
options.game.language != lang::Code::ba_BA &&
options.game.language != lang::Code::es_ES)
{ {
options.game.language = lang::Code::en_UK; options.game.language = lang::Code::en_UK;
} }
@@ -156,73 +130,58 @@ bool saveOptionsFile(std::string file_path)
std::cout << "Writing file: " << getFileName(file_path) << std::endl; std::cout << "Writing file: " << getFileName(file_path) << std::endl;
// Opciones de video // Opciones de video
const auto value_video_mode_winow = std::to_string(static_cast<int>(ScreenVideoMode::WINDOW));
const auto value_video_mode_fullscreen = std::to_string(static_cast<int>(ScreenVideoMode::FULLSCREEN));
const auto value_filter_nearest = std::to_string(static_cast<int>(ScreenFilter::NEAREST));
const auto value_filter_lineal = std::to_string(static_cast<int>(ScreenFilter::LINEAL));
file << "## VIDEO\n"; file << "## VIDEO\n";
file << "## video.mode [" << value_video_mode_winow << ": window, " << value_video_mode_fullscreen << ": fullscreen]\n"; file << "## video.mode [" << static_cast<int>(ScreenVideoMode::WINDOW) << ": window, " << static_cast<int>(ScreenVideoMode::FULLSCREEN) << ": fullscreen]\n";
file << "## video.filter [" << value_filter_nearest << ": nearest, " << value_filter_lineal << ": lineal]\n"; file << "## video.filter [" << static_cast<int>(ScreenFilter::NEAREST) << ": nearest, " << static_cast<int>(ScreenFilter::LINEAL) << ": lineal]\n";
file << "\n"; file << "\n";
const auto valueVideoMode = std::to_string(static_cast<int>(options.video.mode)); file << "video.mode=" << static_cast<int>(options.video.mode) << "\n";
file << "video.mode=" << valueVideoMode << "\n"; file << "video.window.size=" << options.video.window.size << "\n";
file << "video.filter=" << static_cast<int>(options.video.filter) << "\n";
file << "video.window.size=" + std::to_string(options.video.window.size) + "\n"; file << "video.v_sync=" << boolToString(options.video.v_sync) << "\n";
file << "video.integer_scale=" << boolToString(options.video.integer_scale) << "\n";
const auto valueFilter = std::to_string(static_cast<int>(options.video.filter)); file << "video.shaders=" << boolToString(options.video.shaders) << "\n";
file << "video.filter=" << valueFilter << "\n";
file << "video.v_sync=" + boolToString(options.video.v_sync) + "\n";
file << "video.integer_scale=" + boolToString(options.video.integer_scale) + "\n";
file << "video.shaders=" + boolToString(options.video.shaders) + "\n";
// Opciones de audio // Opciones de audio
file << "\n\n## AUDIO\n"; file << "\n\n## AUDIO\n";
file << "## volume [0 .. 100]\n"; file << "## volume [0 .. 100]\n";
file << "\n"; file << "\n";
file << "audio.enabled=" + boolToString(options.audio.enabled) + "\n"; file << "audio.enabled=" << boolToString(options.audio.enabled) << "\n";
file << "audio.volume=" + std::to_string(options.audio.volume) + "\n"; file << "audio.volume=" << options.audio.volume << "\n";
file << "audio.music.enabled=" + boolToString(options.audio.music.enabled) + "\n"; file << "audio.music.enabled=" << boolToString(options.audio.music.enabled) << "\n";
file << "audio.music.volume=" + std::to_string(options.audio.music.volume) + "\n"; file << "audio.music.volume=" << options.audio.music.volume << "\n";
file << "audio.sound.enabled=" + boolToString(options.audio.sound.enabled) + "\n"; file << "audio.sound.enabled=" << boolToString(options.audio.sound.enabled) << "\n";
file << "audio.sound.volume=" + std::to_string(options.audio.sound.volume) + "\n"; file << "audio.sound.volume=" << options.audio.sound.volume << "\n";
// Opciones del juego // Opciones del juego
const auto value_difficulty_easy = std::to_string(static_cast<int>(GameDifficulty::EASY));
const auto value_difficulty_normal = std::to_string(static_cast<int>(GameDifficulty::NORMAL));
const auto value_difficulty_hard = std::to_string(static_cast<int>(GameDifficulty::HARD));
file << "\n\n## GAME\n"; file << "\n\n## GAME\n";
file << "## game.language [0: spanish, 1: valencian, 2: english]\n"; file << "## game.language [0: spanish, 1: valencian, 2: english]\n";
file << "## game.difficulty [" << value_difficulty_easy << ": easy, " << value_difficulty_normal << ": normal, " << value_difficulty_hard << ": hard]\n"; file << "## game.difficulty [" << static_cast<int>(GameDifficulty::EASY) << ": easy, " << static_cast<int>(GameDifficulty::NORMAL) << ": normal, " << static_cast<int>(GameDifficulty::HARD) << ": hard]\n";
file << "\n"; file << "\n";
file << "game.language=" + std::to_string(static_cast<int>(options.game.language)) + "\n"; file << "game.language=" << static_cast<int>(options.game.language) << "\n";
file << "game.difficulty=" + std::to_string(static_cast<int>(options.game.difficulty)) + "\n"; file << "game.difficulty=" << static_cast<int>(options.game.difficulty) << "\n";
file << "game.autofire=" + boolToString(options.game.autofire) + "\n"; file << "game.autofire=" << boolToString(options.game.autofire) << "\n";
// Opciones de mandos // Opciones de mandos
file << "\n\n## CONTROLLERS\n"; file << "\n\n## CONTROLLERS\n";
file << "\n";
const int num_players = 2; int controller_index = 0;
for (int index = 0; index < num_players; ++index) for (const auto &controller : options.controllers)
{ {
const std::string joyIndex = std::to_string(index + 1); file << "\n";
file << "controller" + joyIndex + ".name=" + options.controller[index].name + "\n"; file << "controller." << controller_index << ".name=" << controller.name << "\n";
file << "controller" + joyIndex + ".player=" + std::to_string(options.controller[index].player_id) + "\n"; file << "controller." << controller_index << ".player=" << controller.player_id << "\n";
file << "controller" + joyIndex + ".button.fire_left=" + std::to_string((int)options.controller[index].buttons[0]) + "\n"; file << "controller." << controller_index << ".type=" << static_cast<int>(controller.type) << "\n";
file << "controller" + joyIndex + ".button.fire_center=" + std::to_string((int)options.controller[index].buttons[1]) + "\n"; file << "controller." << controller_index << ".button.fire_left=" << controller.buttons.at(0) << "\n";
file << "controller" + joyIndex + ".button.fire_right=" + std::to_string((int)options.controller[index].buttons[2]) + "\n"; file << "controller." << controller_index << ".button.fire_center=" << controller.buttons.at(1) << "\n";
file << "controller" + joyIndex + ".button.start=" + std::to_string((int)options.controller[index].buttons[3]) + "\n"; file << "controller." << controller_index << ".button.fire_right=" << controller.buttons.at(2) << "\n";
file << "controller" + joyIndex + ".button.service=" + std::to_string((int)options.controller[index].buttons[4]) + "\n"; file << "controller." << controller_index << ".button.start=" << controller.buttons.at(3) << "\n";
file << "controller." << controller_index << ".button.service=" << controller.buttons.at(4) << "\n";
if (index < num_players - 1) // Incrementa el índice
{ ++controller_index;
file << "\n";
}
} }
// Cierra el fichero // Cierra el fichero
@@ -242,7 +201,6 @@ bool setOptions(const std::string &var, const std::string &value)
{ {
options.video.mode = static_cast<ScreenVideoMode>(std::stoi(value)); options.video.mode = static_cast<ScreenVideoMode>(std::stoi(value));
} }
else if (var == "video.window.size") else if (var == "video.window.size")
{ {
options.video.window.size = std::stoi(value); options.video.window.size = std::stoi(value);
@@ -251,22 +209,18 @@ bool setOptions(const std::string &var, const std::string &value)
options.video.window.size = 3; options.video.window.size = 3;
} }
} }
else if (var == "video.filter") else if (var == "video.filter")
{ {
options.video.filter = static_cast<ScreenFilter>(std::stoi(value)); options.video.filter = static_cast<ScreenFilter>(std::stoi(value));
} }
else if (var == "video.shaders") else if (var == "video.shaders")
{ {
options.video.shaders = stringToBool(value); options.video.shaders = stringToBool(value);
} }
else if (var == "video.integer_scale") else if (var == "video.integer_scale")
{ {
options.video.integer_scale = stringToBool(value); options.video.integer_scale = stringToBool(value);
} }
else if (var == "video.v_sync") else if (var == "video.v_sync")
{ {
options.video.v_sync = stringToBool(value); options.video.v_sync = stringToBool(value);
@@ -277,7 +231,6 @@ bool setOptions(const std::string &var, const std::string &value)
{ {
options.audio.enabled = stringToBool(value); options.audio.enabled = stringToBool(value);
} }
else if (var == "audio.volume") else if (var == "audio.volume")
{ {
options.audio.volume = std::stoi(value); options.audio.volume = std::stoi(value);
@@ -286,17 +239,14 @@ bool setOptions(const std::string &var, const std::string &value)
{ {
options.audio.music.enabled = stringToBool(value); options.audio.music.enabled = stringToBool(value);
} }
else if (var == "audio.music.volume") else if (var == "audio.music.volume")
{ {
options.audio.music.volume = std::stoi(value); options.audio.music.volume = std::stoi(value);
} }
else if (var == "audio.sound.enabled") else if (var == "audio.sound.enabled")
{ {
options.audio.sound.enabled = stringToBool(value); options.audio.sound.enabled = stringToBool(value);
} }
else if (var == "audio.sound.volume") else if (var == "audio.sound.volume")
{ {
options.audio.sound.volume = std::stoi(value); options.audio.sound.volume = std::stoi(value);
@@ -307,93 +257,85 @@ bool setOptions(const std::string &var, const std::string &value)
{ {
options.game.language = static_cast<lang::Code>(std::stoi(value)); options.game.language = static_cast<lang::Code>(std::stoi(value));
} }
else if (var == "game.difficulty") else if (var == "game.difficulty")
{ {
options.game.difficulty = static_cast<GameDifficulty>(std::stoi(value)); options.game.difficulty = static_cast<GameDifficulty>(std::stoi(value));
} }
else if (var == "game.autofire") else if (var == "game.autofire")
{ {
options.game.autofire = stringToBool(value); options.game.autofire = stringToBool(value);
} }
// Opciones de mandos // Opciones de mandos
else if (var == "controller1.name") else if (var == "controller.0.name")
{ {
options.controller[0].name = value; options.controllers.at(0).name = value;
} }
else if (var == "controller.0.player")
else if (var == "controller1.player")
{ {
options.controller[0].player_id = std::max(1, std::min(2, std::stoi(value))); options.controllers.at(0).player_id = std::clamp(std::stoi(value), 1, 2);
} }
else if (var == "controller.0.type")
else if (var == "controller1.button.fire_left")
{ {
options.controller[0].buttons[0] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(0).type = static_cast<InputDeviceToUse>(std::stoi(value));
} }
else if (var == "controller.0.button.fire_left")
else if (var == "controller1.button.fire_center")
{ {
options.controller[0].buttons[1] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(0).buttons.at(0) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.0.button.fire_center")
else if (var == "controller1.button.fire_right")
{ {
options.controller[0].buttons[2] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(0).buttons.at(1) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.0.button.fire_right")
else if (var == "controller1.button.start")
{ {
options.controller[0].buttons[3] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(0).buttons.at(2) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.0.button.start")
else if (var == "controller1.button.service")
{ {
options.controller[0].buttons[4] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(0).buttons.at(3) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.0.button.service")
else if (var == "controller2.name")
{ {
options.controller[1].name = value; options.controllers.at(0).buttons.at(4) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.1.name")
else if (var == "controller2.player")
{ {
options.controller[1].player_id = std::max(1, std::min(2, std::stoi(value))); options.controllers.at(1).name = value;
} }
else if (var == "controller.1.player")
else if (var == "controller2.button.fire_left")
{ {
options.controller[1].buttons[0] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(1).player_id = std::clamp(std::stoi(value), 1, 2);
} }
else if (var == "controller.1.type")
else if (var == "controller2.button.fire_center")
{ {
options.controller[1].buttons[1] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(1).type = static_cast<InputDeviceToUse>(std::stoi(value));
} }
else if (var == "controller.1.button.fire_left")
else if (var == "controller2.button.fire_right")
{ {
options.controller[1].buttons[2] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(1).buttons.at(0) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.1.button.fire_center")
else if (var == "controller2.button.start")
{ {
options.controller[1].buttons[3] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(1).buttons.at(1) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
else if (var == "controller.1.button.fire_right")
else if (var == "controller2.button.service")
{ {
options.controller[1].buttons[4] = (SDL_GameControllerButton)std::stoi(value); options.controllers.at(1).buttons.at(2) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller.1.button.start")
{
options.controllers.at(1).buttons.at(3) = static_cast<SDL_GameControllerButton>(std::stoi(value));
}
else if (var == "controller.1.button.service")
{
options.controllers.at(1).buttons.at(4) = static_cast<SDL_GameControllerButton>(std::stoi(value));
} }
// Lineas vacias o que empiezan por comentario // Lineas vacias o que empiezan por comentario
else if (var.empty() || var.starts_with("#")) else if (var.empty() || var.starts_with("#"))
{ {
} }
else else
{ {
success = false; success = false;
@@ -408,3 +350,45 @@ int to_JA_volume(int vol)
vol = vol * 1.28f; vol = vol * 1.28f;
return std::clamp(vol, 0, 128); return std::clamp(vol, 0, 128);
} }
// Asigna el teclado al jugador
void setKeyboardToPlayer(int player_id)
{
for (auto &controller : options.controllers)
{
if (controller.player_id == player_id)
{
controller.type = InputDeviceToUse::ANY;
}
else
{
controller.type = InputDeviceToUse::CONTROLLER;
}
}
}
// Intercambia el teclado de jugador
void swapOptionsKeyboard()
{
std::swap(options.controllers.at(0).type, options.controllers.at(1).type);
}
// Intercambia los jugadores asignados a los dos primeros mandos
void swapOptionsControllers()
{
std::swap(options.controllers.at(0).player_id, options.controllers.at(1).player_id);
std::swap(options.controllers.at(0).type, options.controllers.at(1).type);
}
// Averigua quien está usando el teclado
int getPlayerWhoUsesKeyboard()
{
for (const auto &controller : options.controllers)
{
if (controller.type == InputDeviceToUse::ANY)
{
return controller.player_id;
}
}
return 0;
}

View File

@@ -1,17 +1,16 @@
#pragma once #pragma once
#include <SDL2/SDL_gamecontroller.h> // Para SDL_GameControllerButton #include <SDL2/SDL_gamecontroller.h> // Para SDL_CONTROLLER_BUTTON_B, SDL_CO...
#include <SDL2/SDL_stdinc.h> // Para Uint8 #include <string> // Para string
#include "manage_hiscore_table.h" #include <vector> // Para vector
#include <vector> // Para vector #include "input.h" // Para InputType, InputDeviceToUse
#include <string> #include "manage_hiscore_table.h" // Para HiScoreEntry
enum class InputType : int; enum class ScreenFilter : int; // lines 10-10
enum class ScreenFilter : int; enum class ScreenVideoMode : int; // lines 11-11
enum class ScreenVideoMode : int;
namespace lang namespace lang
{ {
enum class Code : int; enum class Code : int;
} } // lines 14-14
// Dificultad del juego // Dificultad del juego
enum class GameDifficulty enum class GameDifficulty
@@ -57,8 +56,8 @@ struct OptionsAudio
{ {
OptionsMusic music; // Opciones para la música OptionsMusic music; // Opciones para la música
OptionsSound sound; // Opciones para los efectos de sonido OptionsSound sound; // Opciones para los efectos de sonido
bool enabled; // Indica si el audio está activo o no bool enabled; // Indica si el audio está activo o no
int volume; // Volumen al que suenan el audio int volume; // Volumen al que suenan el audio
}; };
// Estructura para las opciones del juego // Estructura para las opciones del juego
@@ -75,25 +74,33 @@ struct OptionsController
{ {
int index; // Indice en el vector de mandos int index; // Indice en el vector de mandos
int player_id; // Jugador asociado al mando int player_id; // Jugador asociado al mando
Uint8 device_type; // Indica si se utilizará teclado o mando o ambos InputDeviceToUse type; // Indica si se utilizará teclado o mando o ambos
std::string name; // Nombre del dispositivo std::string name; // Nombre del dispositivo
bool plugged; // Indica si el mando se encuentra conectado bool plugged; // Indica si el mando se encuentra conectado
std::vector<InputType> inputs; // Listado de inputs std::vector<InputType> inputs; // Listado de inputs
std::vector<SDL_GameControllerButton> buttons; // Listado de botones asignados a cada input std::vector<SDL_GameControllerButton> buttons; // Listado de botones asignados a cada input
// Constructor por defecto
OptionsController()
: index(-1), player_id(-1), type(InputDeviceToUse::CONTROLLER), name(""), plugged(false),
inputs{InputType::FIRE_LEFT, InputType::FIRE_CENTER, InputType::FIRE_RIGHT, InputType::START, InputType::SERVICE},
buttons{SDL_CONTROLLER_BUTTON_X, SDL_CONTROLLER_BUTTON_Y, SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_START, SDL_CONTROLLER_BUTTON_BACK} {}
}; };
// Estructura con todas las opciones de configuración del programa // Estructura con todas las opciones de configuración del programa
struct Options struct Options
{ {
OptionsGame game; // Opciones para el propio juego OptionsGame game; // Opciones para el propio juego
OptionsVideo video; // Opciones relativas a la clase screen OptionsVideo video; // Opciones relativas a la clase screen
OptionsAudio audio; // Opciones para el audio OptionsAudio audio; // Opciones para el audio
std::vector<OptionsController> controller; // Opciones con las asignaciones del mando para cada jugador std::vector<OptionsController> controllers; // Opciones con las asignaciones del mando para cada jugador
}; };
// Variables // Variables
extern Options options; extern Options options;
void initOptions();
// Carga el fichero de configuración // Carga el fichero de configuración
bool loadOptionsFile(std::string file_path); bool loadOptionsFile(std::string file_path);
@@ -102,3 +109,15 @@ bool saveOptionsFile(std::string file_path);
// Convierte valores de 0 a 100 en valores de 0 a 128 // Convierte valores de 0 a 100 en valores de 0 a 128
int to_JA_volume(int vol); int to_JA_volume(int vol);
// Asigna el teclado al jugador
void setKeyboardToPlayer(int player_id);
// Intercambia el teclado de jugador
void swapOptionsKeyboard();
// Intercambia los jugadores asignados a los dos primeros mandos
void swapOptionsControllers();
// Averigua quien está usando el teclado
int getPlayerWhoUsesKeyboard();

View File

@@ -1,8 +1,8 @@
#include "path_sprite.h" #include "path_sprite.h"
#include <cmath> // for abs #include <cmath> // Para abs
#include <stdlib.h> // for abs #include <stdlib.h> // Para abs
#include <functional> // for function #include <functional> // Para function
#include <utility> // for move #include <utility> // Para move
// Devuelve un vector con los puntos que conforman la ruta // Devuelve un vector con los puntos que conforman la ruta
std::vector<SDL_Point> createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction) std::vector<SDL_Point> createPath(int start, int end, PathType type, int fixed_pos, int steps, const std::function<double(double)> &easingFunction)

View File

@@ -1,10 +1,10 @@
#pragma once #pragma once
#include <SDL2/SDL_rect.h> // for SDL_Point #include <SDL2/SDL_rect.h> // Para SDL_Point
#include <functional> // for function #include <functional> // Para function
#include <memory> // for shared_ptr #include <memory> // Para shared_ptr
#include <vector> // for vector #include <vector> // Para vector
#include "sprite.h" // for Sprite #include "sprite.h" // Para Sprite
class Texture; // lines 8-8 class Texture; // lines 8-8
enum class PathType enum class PathType

View File

@@ -29,7 +29,7 @@ Player::Player(int id, float x, int y, bool demo, SDL_Rect &play_area, std::vect
power_sprite_->setPosY(y - (power_sprite_->getHeight() - player_sprite_->getHeight())); power_sprite_->setPosY(y - (power_sprite_->getHeight() - player_sprite_->getHeight()));
// Inicializa variables // Inicializa variables
setRecordName(enter_name_->getName()); pos_x_ = default_pos_x_;
init(); init();
} }
@@ -37,7 +37,6 @@ Player::Player(int id, float x, int y, bool demo, SDL_Rect &play_area, std::vect
void Player::init() void Player::init()
{ {
// Inicializa variables de estado // Inicializa variables de estado
pos_x_ = default_pos_x_;
pos_y_ = default_pos_y_; pos_y_ = default_pos_y_;
walking_state_ = PlayerState::WALKING_STOP; walking_state_ = PlayerState::WALKING_STOP;
firing_state_ = PlayerState::FIRING_NONE; firing_state_ = PlayerState::FIRING_NONE;
@@ -58,6 +57,7 @@ void Player::init()
score_ = 0; score_ = 0;
score_multiplier_ = 1.0f; score_multiplier_ = 1.0f;
cooldown_ = 10; cooldown_ = 10;
enter_name_->init();
// Establece la posición del sprite // Establece la posición del sprite
player_sprite_->clear(); player_sprite_->clear();
@@ -147,12 +147,10 @@ void Player::setInputEnteringName(InputType input)
enter_name_->decIndex(); enter_name_->decIndex();
break; break;
case InputType::START: case InputType::START:
setRecordName(enter_name_->getName());
break; break;
default: default:
break; break;
} }
setRecordName(enter_name_->getName());
} }
// Mueve el jugador a la posición y animación que le corresponde // Mueve el jugador a la posición y animación que le corresponde
@@ -177,14 +175,33 @@ void Player::move()
{ {
// Si el cadaver abandona el area de juego por los laterales lo hace rebotar // Si el cadaver abandona el area de juego por los laterales lo hace rebotar
if ((player_sprite_->getPosX() < param.game.play_area.rect.x) || (player_sprite_->getPosX() + WIDTH_ > play_area_.w)) if ((player_sprite_->getPosX() < param.game.play_area.rect.x) || (player_sprite_->getPosX() + WIDTH_ > play_area_.w))
{
player_sprite_->setVelX(-player_sprite_->getVelX()); player_sprite_->setVelX(-player_sprite_->getVelX());
}
// Si el cadaver abandona el area de juego por abajo // Si el cadaver toca el suelo cambia el estado
if (player_sprite_->getPosY() > param.game.play_area.rect.h) if (player_sprite_->getPosY() > param.game.play_area.rect.h - HEIGHT_)
setPlayingState(PlayerState::DIED); {
if (player_sprite_->getVelY() < 2.0f)
{
// Si la velocidad de rebote es baja, termina de rebotar y cambia de estado
setPlayingState(PlayerState::DIED);
pos_x_ = player_sprite_->getPosX();
pos_y_ = default_pos_y_;
player_sprite_->clear();
shiftSprite();
}
else
{
// Decrementa las velocidades de rebote
player_sprite_->setPosY(param.game.play_area.rect.h - HEIGHT_);
player_sprite_->setVelY(player_sprite_->getVelY() * -0.5f);
player_sprite_->setVelX(player_sprite_->getVelX() * 0.75f);
}
}
break; break;
} }
case PlayerState::GAME_COMPLETED: case PlayerState::LEAVING_SCREEN:
{ {
switch (id_) switch (id_)
{ {
@@ -216,28 +233,38 @@ void Player::move()
void Player::render() void Player::render()
{ {
if (power_up_ && isPlaying()) if (power_up_ && isPlaying())
{
if (power_up_counter_ > (POWERUP_COUNTER_ / 4) || power_up_counter_ % 20 > 4) if (power_up_counter_ > (POWERUP_COUNTER_ / 4) || power_up_counter_ % 20 > 4)
{
power_sprite_->render(); power_sprite_->render();
}
}
if (isRenderable()) if (isRenderable())
{
player_sprite_->render(); player_sprite_->render();
}
} }
// Establece la animación correspondiente al estado // Establece la animación correspondiente al estado
void Player::setAnimation() void Player::setAnimation()
{ {
// Crea cadenas de texto para componer el nombre de la animación switch (playing_state_)
const std::string a_walking = walking_state_ == PlayerState::WALKING_STOP ? "stand" : "walk";
const std::string a_firing = firing_state_ == PlayerState::FIRING_UP ? "centershoot" : "sideshoot";
const std::string a_cooling = firing_state_ == PlayerState::COOLING_UP ? "centershoot" : "sideshoot";
const SDL_RendererFlip flip_walk = walking_state_ == PlayerState::WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
const SDL_RendererFlip flip_fire = firing_state_ == PlayerState::FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
const SDL_RendererFlip flip_cooling = firing_state_ == PlayerState::COOLING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
// Establece la animación a partir de las cadenas
if (isPlaying() || isEnteringNameGameCompleted() || isGameCompleted())
{ {
case PlayerState::PLAYING:
case PlayerState::ENTERING_NAME_GAME_COMPLETED:
case PlayerState::LEAVING_SCREEN:
{
// Crea cadenas de texto para componer el nombre de la animación
const std::string a_walking = walking_state_ == PlayerState::WALKING_STOP ? "stand" : "walk";
const std::string a_firing = firing_state_ == PlayerState::FIRING_UP ? "centershoot" : "sideshoot";
const std::string a_cooling = firing_state_ == PlayerState::COOLING_UP ? "centershoot" : "sideshoot";
const SDL_RendererFlip flip_walk = walking_state_ == PlayerState::WALKING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
const SDL_RendererFlip flip_fire = firing_state_ == PlayerState::FIRING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
const SDL_RendererFlip flip_cooling = firing_state_ == PlayerState::COOLING_RIGHT ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
// Establece la animación a partir de las cadenas
if (firing_state_ == PlayerState::FIRING_NONE) if (firing_state_ == PlayerState::FIRING_NONE)
{ // No esta disparando { // No esta disparando
player_sprite_->setCurrentAnimation(a_walking); player_sprite_->setCurrentAnimation(a_walking);
@@ -255,14 +282,27 @@ void Player::setAnimation()
// Si dispara recto, invierte el sprite segun hacia donde camina // Si dispara recto, invierte el sprite segun hacia donde camina
player_sprite_->setFlip(a_firing == "centershoot" ? flip_walk : flip_fire); player_sprite_->setFlip(a_firing == "centershoot" ? flip_walk : flip_fire);
} }
break;
} }
else if (isDying() || hasDied()) case PlayerState::DYING:
{ {
player_sprite_->setCurrentAnimation("death"); player_sprite_->setCurrentAnimation("dying");
break;
} }
else if (isCelebrating()) case PlayerState::DIED:
case PlayerState::ENTERING_NAME:
case PlayerState::CONTINUE:
{
player_sprite_->setCurrentAnimation("dead");
break;
}
case PlayerState::CELEBRATING:
{ {
player_sprite_->setCurrentAnimation("celebration"); player_sprite_->setCurrentAnimation("celebration");
break;
}
default:
break;
} }
// Actualiza las animaciones de los sprites // Actualiza las animaciones de los sprites
@@ -344,7 +384,7 @@ void Player::updateScoreboard()
case PlayerState::ENTERING_NAME: case PlayerState::ENTERING_NAME:
case PlayerState::ENTERING_NAME_GAME_COMPLETED: case PlayerState::ENTERING_NAME_GAME_COMPLETED:
{ {
Scoreboard::get()->setRecordName(getScoreBoardPanel(), getRecordName()); Scoreboard::get()->setRecordName(getScoreBoardPanel(), enter_name_->getName());
Scoreboard::get()->setSelectorPos(getScoreBoardPanel(), getRecordNamePos()); Scoreboard::get()->setSelectorPos(getScoreBoardPanel(), getRecordNamePos());
break; break;
} }
@@ -381,7 +421,6 @@ void Player::setPlayingState(PlayerState state)
// Inicializa el contador de continuar // Inicializa el contador de continuar
continue_ticks_ = SDL_GetTicks(); continue_ticks_ = SDL_GetTicks();
continue_counter_ = 9; continue_counter_ = 9;
enter_name_->init();
setScoreboardMode(ScoreboardMode::CONTINUE); setScoreboardMode(ScoreboardMode::CONTINUE);
break; break;
} }
@@ -426,7 +465,7 @@ void Player::setPlayingState(PlayerState state)
setScoreboardMode(ScoreboardMode::ENTER_NAME); setScoreboardMode(ScoreboardMode::ENTER_NAME);
break; break;
} }
case PlayerState::GAME_COMPLETED: case PlayerState::LEAVING_SCREEN:
{ {
setScoreboardMode(ScoreboardMode::GAME_COMPLETED); setScoreboardMode(ScoreboardMode::GAME_COMPLETED);
break; break;
@@ -578,7 +617,7 @@ void Player::decEnterNameCounter()
if (playing_state_ == PlayerState::ENTERING_NAME) if (playing_state_ == PlayerState::ENTERING_NAME)
setPlayingState(PlayerState::CONTINUE); setPlayingState(PlayerState::CONTINUE);
else else
setPlayingState(PlayerState::GAME_COMPLETED); setPlayingState(PlayerState::LEAVING_SCREEN);
} }
} }
@@ -598,49 +637,3 @@ void Player::shiftSprite()
player_sprite_->setPosY(pos_y_); player_sprite_->setPosY(pos_y_);
power_sprite_->setPosX(getPosX() - power_up_desp_x_); power_sprite_->setPosX(getPosX() - power_up_desp_x_);
} }
int Player::getScore() const { return score_; }
void Player::setScore(int score) { score_ = score; }
PlayerState Player::getPlayingState() const { return playing_state_; }
float Player::getScoreMultiplier() const { return score_multiplier_; }
void Player::setScoreMultiplier(float value) { score_multiplier_ = value; }
bool Player::isInvulnerable() const { return invulnerable_; }
int Player::getInvulnerableCounter() const { return invulnerable_counter_; }
void Player::setInvulnerableCounter(int value) { invulnerable_counter_ = value; }
bool Player::isPowerUp() const { return power_up_; }
int Player::getPowerUpCounter() const { return power_up_counter_; }
void Player::setPowerUpCounter(int value) { power_up_counter_ = value; }
bool Player::hasExtraHit() const { return extra_hit_; }
void Player::setScoreBoardPanel(int panel) { scoreboard_panel_ = panel; }
int Player::getScoreBoardPanel() const { return scoreboard_panel_; }
int Player::getCoffees() const { return coffees_; }
Circle &Player::getCollider() { return collider_; }
int Player::getContinueCounter() const { return continue_counter_; }
void Player::setName(const std::string &name) { name_ = name; }
void Player::setRecordName(const std::string &record_name) { record_name_ = record_name.substr(0, 8); }
std::string Player::getName() const { return name_; }
std::string Player::getRecordName() const { return record_name_; }
void Player::setController(int index) { controller_index_ = index; }
int Player::getController() const { return controller_index_; }
int Player::getId() const { return id_; }
bool Player::isRenderable() const { return isPlaying() || isDying() || isCelebrating() || isEnteringNameGameCompleted() || isGameCompleted(); }
bool Player::IsEligibleForHighScore() { return score_ > options.game.hi_score_table.back().score; }
bool Player::isCooling() { return firing_state_ == PlayerState::COOLING_LEFT || firing_state_ == PlayerState::COOLING_UP || firing_state_ == PlayerState::COOLING_RIGHT; }
int Player::getPosX() const { return static_cast<int>(pos_x_); }
int Player::getPosY() const { return pos_y_; }
int Player::getWidth() const { return WIDTH_; }
int Player::getHeight() const { return HEIGHT_; }
bool Player::canFire() const { return cooldown_ > 0 ? false : true; }
void Player::setFireCooldown(int time) { cooldown_ = time; }
void Player::setWalkingState(PlayerState state) { walking_state_ = state; }
void Player::setFiringState(PlayerState state) { firing_state_ = state; }
bool Player::isPlaying() const { return playing_state_ == PlayerState::PLAYING; }
bool Player::isContinue() const { return playing_state_ == PlayerState::CONTINUE; }
bool Player::isWaiting() const { return playing_state_ == PlayerState::WAITING; }
bool Player::isEnteringName() const { return playing_state_ == PlayerState::ENTERING_NAME; }
bool Player::isDying() const { return playing_state_ == PlayerState::DYING; }
bool Player::hasDied() const { return playing_state_ == PlayerState::DIED; }
bool Player::isGameOver() const { return playing_state_ == PlayerState::GAME_OVER; }
bool Player::isEnteringNameGameCompleted() const { return playing_state_ == PlayerState::ENTERING_NAME_GAME_COMPLETED; }
bool Player::isGameCompleted() const { return playing_state_ == PlayerState::GAME_COMPLETED; }
bool Player::isCelebrating() const { return playing_state_ == PlayerState::CELEBRATING; }

View File

@@ -8,6 +8,7 @@
#include <memory> // Para unique_ptr, shared_ptr #include <memory> // Para unique_ptr, shared_ptr
#include <string> // Para string #include <string> // Para string
#include <vector> // Para vector #include <vector> // Para vector
#include "options.h"
class Texture; // lines 12-12 class Texture; // lines 12-12
enum class InputType : int; // lines 13-13 enum class InputType : int; // lines 13-13
@@ -38,7 +39,7 @@ enum class PlayerState
GAME_OVER, // No está jugando y no puede entrar a jugar GAME_OVER, // No está jugando y no puede entrar a jugar
CELEBRATING, // Poniendo pose de victoria CELEBRATING, // Poniendo pose de victoria
ENTERING_NAME_GAME_COMPLETED, // Poniendo nombre en el tramo final del juego ENTERING_NAME_GAME_COMPLETED, // Poniendo nombre en el tramo final del juego
GAME_COMPLETED, // Moviendose fuera de la pantalla LEAVING_SCREEN, // Moviendose fuera de la pantalla
}; };
// Clase Player // Clase Player
@@ -85,7 +86,6 @@ private:
Uint32 continue_ticks_ = 0; // Variable para poder cambiar el contador de continue en función del tiempo Uint32 continue_ticks_ = 0; // Variable para poder cambiar el contador de continue en función del tiempo
int scoreboard_panel_ = 0; // Panel del marcador asociado al jugador int scoreboard_panel_ = 0; // Panel del marcador asociado al jugador
std::string name_; // Nombre del jugador std::string name_; // Nombre del jugador
std::string record_name_; // Nombre del jugador para la tabla de mejores puntuaciones
int controller_index_ = 0; // Indice del array de mandos que utilizará para moverse int controller_index_ = 0; // Indice del array de mandos que utilizará para moverse
bool demo_; // Para que el jugador sepa si está en el modo demostración bool demo_; // Para que el jugador sepa si está en el modo demostración
int enter_name_counter_; // Contador para poner nombre int enter_name_counter_; // Contador para poner nombre
@@ -109,17 +109,13 @@ private:
// Decrementa el contador de entrar nombre // Decrementa el contador de entrar nombre
void decEnterNameCounter(); void decEnterNameCounter();
// Indica si el jugador se puede dibujar
bool isRenderable() const;
// Actualiza el panel del marcador // Actualiza el panel del marcador
void updateScoreboard(); void updateScoreboard();
// Cambia el modo del marcador // Cambia el modo del marcador
void setScoreboardMode(ScoreboardMode mode); void setScoreboardMode(ScoreboardMode mode);
// Se encuentra en alguno de los estados "COOLING" bool isRenderable() const { return !isWaiting() && !isGameOver(); }
bool isCooling();
public: public:
// Constructor // Constructor
@@ -152,153 +148,91 @@ public:
// Mueve el jugador a la posición y animación que le corresponde // Mueve el jugador a la posición y animación que le corresponde
void move(); void move();
// Establece el estado del jugador
void setWalkingState(PlayerState state);
// Establece el estado del jugador
void setFiringState(PlayerState state);
// Establece la animación correspondiente al estado // Establece la animación correspondiente al estado
void setAnimation(); void setAnimation();
// Obtiene el valor de la variable
int getPosX() const;
// Obtiene el valor de la variable
int getPosY() const;
// Obtiene el valor de la variable
int getWidth() const;
// Obtiene el valor de la variable
int getHeight() const;
// Indica si el jugador puede disparar
bool canFire() const;
// Establece el valor de la variable
void setFireCooldown(int time);
// Actualiza el valor de la variable // Actualiza el valor de la variable
void updateCooldown(); void updateCooldown();
// Obtiene la puntuación del jugador
int getScore() const;
// Asigna un valor a la puntuación del jugador
void setScore(int score);
// Incrementa la puntuación del jugador // Incrementa la puntuación del jugador
void addScore(int score); void addScore(int score);
// Indica si el jugador se encuentra en ese estado
bool isPlaying() const;
bool isContinue() const;
bool isWaiting() const;
bool isEnteringName() const;
bool isDying() const;
bool hasDied() const;
bool isGameOver() const;
bool isEnteringNameGameCompleted() const;
bool isGameCompleted() const;
bool isCelebrating() const;
// Establece el estado del jugador en el juego // Establece el estado del jugador en el juego
void setPlayingState(PlayerState state); void setPlayingState(PlayerState state);
// Obtiene el estado del jugador en el juego
PlayerState getPlayingState() const;
// Obtiene el valor de la variable
float getScoreMultiplier() const;
// Establece el valor de la variable
void setScoreMultiplier(float value);
// Aumenta el valor de la variable hasta un máximo // Aumenta el valor de la variable hasta un máximo
void incScoreMultiplier(); void incScoreMultiplier();
// Decrementa el valor de la variable hasta un mínimo // Decrementa el valor de la variable hasta un mínimo
void decScoreMultiplier(); void decScoreMultiplier();
// Obtiene el valor de la variable
bool isInvulnerable() const;
// Establece el valor del estado // Establece el valor del estado
void setInvulnerable(bool value); void setInvulnerable(bool value);
// Obtiene el valor de la variable
int getInvulnerableCounter() const;
// Establece el valor de la variable
void setInvulnerableCounter(int value);
// Obtiene el valor de la variable
bool isPowerUp() const;
// Establece el valor de la variable a verdadero // Establece el valor de la variable a verdadero
void setPowerUp(); void setPowerUp();
// Obtiene el valor de la variable
int getPowerUpCounter() const;
// Establece el valor de la variable
void setPowerUpCounter(int value);
// Actualiza el valor de la variable // Actualiza el valor de la variable
void updatePowerUp(); void updatePowerUp();
// Obtiene el valor de la variable
bool hasExtraHit() const;
// Concede un toque extra al jugador // Concede un toque extra al jugador
void giveExtraHit(); void giveExtraHit();
// Quita el toque extra al jugador // Quita el toque extra al jugador
void removeExtraHit(); void removeExtraHit();
// Devuelve el número de cafes actuales
int getCoffees() const;
// Obtiene el circulo de colisión
Circle &getCollider();
// Obtiene el valor de la variable
int getContinueCounter() const;
// Le asigna un panel en el marcador al jugador
void setScoreBoardPanel(int panel);
// Obtiene el valor de la variable
int getScoreBoardPanel() const;
// Decrementa el contador de continuar // Decrementa el contador de continuar
void decContinueCounter(); void decContinueCounter();
// Establece el nombre del jugador
void setName(const std::string &name);
// Establece el nombre del jugador para la tabla de mejores puntuaciones
void setRecordName(const std::string &record_name);
// Obtiene el nombre del jugador
std::string getName() const;
// Obtiene el nombre del jugador para la tabla de mejores puntuaciones
std::string getRecordName() const;
// Obtiene la posición que se está editando del nombre del jugador para la tabla de mejores puntuaciones // Obtiene la posición que se está editando del nombre del jugador para la tabla de mejores puntuaciones
int getRecordNamePos() const; int getRecordNamePos() const;
// Establece el mando que usará para ser controlado // Comprobación de playing_state
void setController(int index); bool hasDied() const { return playing_state_ == PlayerState::DIED; }
bool isCelebrating() const { return playing_state_ == PlayerState::CELEBRATING; }
bool isContinue() const { return playing_state_ == PlayerState::CONTINUE; }
bool isDying() const { return playing_state_ == PlayerState::DYING; }
bool isEnteringName() const { return playing_state_ == PlayerState::ENTERING_NAME; }
bool isEnteringNameGameCompleted() const { return playing_state_ == PlayerState::ENTERING_NAME_GAME_COMPLETED; }
bool isLeavingScreen() const { return playing_state_ == PlayerState::LEAVING_SCREEN; }
bool isGameOver() const { return playing_state_ == PlayerState::GAME_OVER; }
bool isPlaying() const { return playing_state_ == PlayerState::PLAYING; }
bool isWaiting() const { return playing_state_ == PlayerState::WAITING; }
// Obtiene el mando que usa para ser controlado // Getters
int getController() const; bool canFire() const { return cooldown_ > 0 ? false : true; }
bool hasExtraHit() const { return extra_hit_; }
bool isCooling() { return firing_state_ == PlayerState::COOLING_LEFT || firing_state_ == PlayerState::COOLING_UP || firing_state_ == PlayerState::COOLING_RIGHT; }
bool IsEligibleForHighScore() { return score_ > options.game.hi_score_table.back().score; }
bool isInvulnerable() const { return invulnerable_; }
bool isPowerUp() const { return power_up_; }
Circle &getCollider() { return collider_; }
float getScoreMultiplier() const { return score_multiplier_; }
int getCoffees() const { return coffees_; }
int getContinueCounter() const { return continue_counter_; }
int getController() const { return controller_index_; }
int getHeight() const { return HEIGHT_; }
int getId() const { return id_; }
int getInvulnerableCounter() const { return invulnerable_counter_; }
int getPosX() const { return static_cast<int>(pos_x_); }
int getPosY() const { return pos_y_; }
int getPowerUpCounter() const { return power_up_counter_; }
std::string getRecordName() const { return enter_name_->getName(); }
int getScore() const { return score_; }
int getScoreBoardPanel() const { return scoreboard_panel_; }
int getWidth() const { return WIDTH_; }
PlayerState getPlayingState() const { return playing_state_; }
std::string getName() const { return name_; }
// Obtiene el "id" del jugador // Setters
int getId() const; void setController(int index) { controller_index_ = index; }
void setFireCooldown(int time) { cooldown_ = time; }
// Comprueba si la puntuación entra en la tabla de mejores puntuaciones void setFiringState(PlayerState state) { firing_state_ = state; }
bool IsEligibleForHighScore(); void setInvulnerableCounter(int value) { invulnerable_counter_ = value; }
void setName(const std::string &name) { name_ = name; }
void setPowerUpCounter(int value) { power_up_counter_ = value; }
void setScore(int score) { score_ = score; }
void setScoreBoardPanel(int panel) { scoreboard_panel_ = panel; }
void setScoreMultiplier(float value) { score_multiplier_ = value; }
void setWalkingState(PlayerState state) { walking_state_ = state; }
}; };

View File

@@ -2,13 +2,14 @@
#include <algorithm> // Para find_if #include <algorithm> // Para find_if
#include <iostream> // Para basic_ostream, operator<<, endl, cout, cerr #include <iostream> // Para basic_ostream, operator<<, endl, cout, cerr
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include <utility> // Para pair
#include "asset.h" // Para Asset, AssetType #include "asset.h" // Para Asset, AssetType
#include "lang.h" // Para lang
#include "jail_audio.h" // Para JA_LoadMusic, JA_LoadSound #include "jail_audio.h" // Para JA_LoadMusic, JA_LoadSound
#include "lang.h" // Para getText
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "text.h" // Para Text #include "text.h" // Para Text, loadTextFile
struct JA_Music_t; struct JA_Music_t; // lines 10-10
struct JA_Sound_t; struct JA_Sound_t; // lines 11-11
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Resource *Resource::resource_ = nullptr; Resource *Resource::resource_ = nullptr;
@@ -42,6 +43,7 @@ Resource::Resource()
loadAnimations(); loadAnimations();
loadDemoData(); loadDemoData();
addPalettes(); addPalettes();
createText();
createTextures(); createTextures();
std::cout << "\n** RESOURCES LOADED" << std::endl; std::cout << "\n** RESOURCES LOADED" << std::endl;
} }
@@ -106,6 +108,21 @@ std::shared_ptr<TextFile> Resource::getTextFile(const std::string &name)
throw std::runtime_error("TextFile no encontrado: " + name); throw std::runtime_error("TextFile no encontrado: " + name);
} }
// Obtiene el objeto de texto a partir de un nombre
std::shared_ptr<Text> Resource::getText(const std::string &name)
{
auto it = std::find_if(texts_.begin(), texts_.end(), [&name](const auto &t)
{ return t.name == name; });
if (it != texts_.end())
{
return it->text;
}
std::cerr << "Error: Text no encontrado " << name << std::endl;
throw std::runtime_error("Text no encontrado: " + name);
}
// Obtiene la animación a partir de un nombre // Obtiene la animación a partir de un nombre
AnimationsFileBuffer &Resource::getAnimation(const std::string &name) AnimationsFileBuffer &Resource::getAnimation(const std::string &name)
{ {
@@ -239,7 +256,6 @@ void Resource::createTextures()
}; };
std::cout << "\n>> CREATING TEXTURES" << std::endl; std::cout << "\n>> CREATING TEXTURES" << std::endl;
auto text = std::make_unique<Text>(getTexture("04b_25.png"), getTextFile("04b_25.txt"));
// Tamaño normal // Tamaño normal
std::vector<NameAndText> strings = { std::vector<NameAndText> strings = {
@@ -251,6 +267,7 @@ void Resource::createTextures()
NameAndText("game_text_stop", lang::getText(119)), NameAndText("game_text_stop", lang::getText(119)),
NameAndText("1000000_points", lang::getText(76))}; NameAndText("1000000_points", lang::getText(76))};
auto text = getText("04b_25");
for (const auto &s : strings) for (const auto &s : strings)
{ {
textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2))); textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 1, -2)));
@@ -264,9 +281,29 @@ void Resource::createTextures()
NameAndText("congratulations", lang::getText(50)), NameAndText("congratulations", lang::getText(50)),
NameAndText("game_over", "Game Over")}; NameAndText("game_over", "Game Over")};
auto text2 = getText("04b_25_2x");
for (const auto &s : strings2X) for (const auto &s : strings2X)
{ {
textures_.emplace_back(ResourceTexture(s.name, text->writeToTexture(s.text, 2, -2))); textures_.emplace_back(ResourceTexture(s.name, text2->writeToTexture(s.text, 1, -4)));
printWithDots("Texture : ", s.name, "[ DONE ]"); printWithDots("Texture : ", s.name, "[ DONE ]");
} }
} }
// Crea los objetos de texto
void Resource::createText()
{
std::cout << "\n>> CREATING TEXT_OBJECTS" << std::endl;
std::vector<std::pair<std::string, std::string>> resources = {
{"04b_25", "04b_25.png"},
{"04b_25_2x", "04b_25_2x.png"},
{"8bithud", "8bithud.png"},
{"nokia", "nokia.png"},
{"smb2", "smb2.gif"}};
for (const auto &resource : resources)
{
texts_.emplace_back(ResourceText(resource.first, std::make_shared<Text>(getTexture(resource.second), getTextFile(resource.first + ".txt"))));
printWithDots("Text : ", resource.first, "[ DONE ]");
}
}

View File

@@ -54,6 +54,17 @@ struct ResourceTextFile
: name(name), text_file(text_file) {} : name(name), text_file(text_file) {}
}; };
// Estructura para almacenar objetos Text y su nombre
struct ResourceText
{
std::string name; // Nombre del objeto
std::shared_ptr<Text> text; // Objeto
// Constructor
ResourceText(const std::string &name, std::shared_ptr<Text> text)
: name(name), text(text) {}
};
// Estructura para almacenar ficheros animaciones y su nombre // Estructura para almacenar ficheros animaciones y su nombre
struct ResourceAnimation struct ResourceAnimation
{ {
@@ -75,6 +86,7 @@ private:
std::vector<ResourceMusic> musics_; // Vector con las musicas std::vector<ResourceMusic> musics_; // Vector con las musicas
std::vector<ResourceTexture> textures_; // Vector con las musicas std::vector<ResourceTexture> textures_; // Vector con las musicas
std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto std::vector<ResourceTextFile> text_files_; // Vector con los ficheros de texto
std::vector<ResourceText> texts_; // Vector con los objetos de texto
std::vector<ResourceAnimation> animations_; // Vector con las animaciones std::vector<ResourceAnimation> animations_; // Vector con las animaciones
std::vector<DemoData> demos_; // Vector con los ficheros de datos para el modo demostración std::vector<DemoData> demos_; // Vector con los ficheros de datos para el modo demostración
@@ -102,6 +114,9 @@ private:
// Crea texturas // Crea texturas
void createTextures(); void createTextures();
// Crea los objetos de texto
void createText();
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos resource desde fuera
// Constructor // Constructor
@@ -132,6 +147,9 @@ public:
// Obtiene el fichero de texto a partir de un nombre // Obtiene el fichero de texto a partir de un nombre
std::shared_ptr<TextFile> getTextFile(const std::string &name); std::shared_ptr<TextFile> getTextFile(const std::string &name);
// Obtiene el objeto de texto a partir de un nombre
std::shared_ptr<Text> getText(const std::string &name);
// Obtiene la animación a partir de un nombre // Obtiene la animación a partir de un nombre
AnimationsFileBuffer &getAnimation(const std::string &name); AnimationsFileBuffer &getAnimation(const std::string &name);

View File

@@ -10,6 +10,7 @@
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "sprite.h" // Para Sprite #include "sprite.h" // Para Sprite
#include "text.h" // Para Text #include "text.h" // Para Text
#include "enter_name.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
Scoreboard *Scoreboard::scoreboard_ = nullptr; Scoreboard *Scoreboard::scoreboard_ = nullptr;
@@ -37,7 +38,7 @@ Scoreboard::Scoreboard()
: renderer_(Screen::get()->getRenderer()), : renderer_(Screen::get()->getRenderer()),
game_power_meter_texture_(Resource::get()->getTexture("game_power_meter.png")), game_power_meter_texture_(Resource::get()->getTexture("game_power_meter.png")),
power_meter_sprite_(std::make_unique<Sprite>(game_power_meter_texture_)), power_meter_sprite_(std::make_unique<Sprite>(game_power_meter_texture_)),
text_scoreboard_(std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"))) text_scoreboard_(Resource::get()->getText("8bithud"))
{ {
// Inicializa variables // Inicializa variables
for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i) for (int i = 0; i < SCOREBOARD_MAX_PANELS; ++i)
@@ -118,17 +119,6 @@ void Scoreboard::render()
SDL_RenderCopy(renderer_, background_, nullptr, &rect_); SDL_RenderCopy(renderer_, background_, nullptr, &rect_);
} }
void Scoreboard::setName(int panel, const std::string &name) { name_[panel] = name; }
void Scoreboard::setRecordName(int panel, const std::string &record_name) { record_name_[panel] = record_name; }
void Scoreboard::setSelectorPos(int panel, int pos) { selector_pos_[panel] = pos; }
void Scoreboard::setScore(int panel, int score) { score_[panel] = score; }
void Scoreboard::setMult(int panel, float mult) { mult_[panel] = mult; }
void Scoreboard::setContinue(int panel, int continue_counter) { continue_counter_[panel] = continue_counter; }
void Scoreboard::setStage(int stage) { stage_ = stage; }
void Scoreboard::setHiScore(int hi_score) { hi_score_ = hi_score; }
void Scoreboard::setPower(float power) { power_ = power; }
void Scoreboard::setHiScoreName(const std::string &name) { hi_score_name_ = name; }
// Establece el valor de la variable // Establece el valor de la variable
void Scoreboard::setColor(Color color) void Scoreboard::setColor(Color color)
{ {
@@ -180,7 +170,7 @@ void Scoreboard::fillPanelTextures()
// MULT // MULT
text_scoreboard_->writeCentered(slot4_3_.x, slot4_3_.y, lang::getText(55)); text_scoreboard_->writeCentered(slot4_3_.x, slot4_3_.y, lang::getText(55));
text_scoreboard_->writeCentered(slot4_4_.x, slot4_4_.y, std::to_string(mult_[i]).substr(0, 3)); text_scoreboard_->writeCentered(slot4_4_.x, slot4_4_.y, "x" + std::to_string(mult_[i]).substr(0, 3));
break; break;
} }
@@ -239,7 +229,8 @@ void Scoreboard::fillPanelTextures()
// HI-SCORE // HI-SCORE
text_scoreboard_->writeCentered(slot4_3_.x, slot4_3_.y, lang::getText(56)); text_scoreboard_->writeCentered(slot4_3_.x, slot4_3_.y, lang::getText(56));
text_scoreboard_->writeCentered(slot4_4_.x, slot4_4_.y, hi_score_name_ + " - " + updateScoreText(hi_score_)); const std::string name = hi_score_name_ == "" ? "" : hi_score_name_ + " - ";
text_scoreboard_->writeCentered(slot4_4_.x, slot4_4_.y, name + updateScoreText(hi_score_));
break; break;
} }
@@ -267,21 +258,6 @@ void Scoreboard::fillPanelTextures()
SDL_SetRenderDrawColor(renderer_, 0xFF, 0xFF, 0xEB, 255); SDL_SetRenderDrawColor(renderer_, 0xFF, 0xFF, 0xEB, 255);
for (size_t j = 0; j < record_name_[i].size(); ++j) for (size_t j = 0; j < record_name_[i].size(); ++j)
{ {
/*
if (j == selector_pos_[i])
{ // La letra seleccionada se pinta de forma intermitente
if (counter_ % 3 > 0)
{
SDL_RenderDrawLine(renderer_, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h);
text_scoreboard_->write(rect.x, rect.y, record_name_[i].substr(j, 1));
}
}
else
{
SDL_RenderDrawLine(renderer_, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h);
text_scoreboard_->write(rect.x, rect.y, record_name_[i].substr(j, 1));
}
*/
if (j != selector_pos_[i] || counter_ % 3 == 0) if (j != selector_pos_[i] || counter_ % 3 == 0)
{ {
SDL_RenderDrawLine(renderer_, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h); SDL_RenderDrawLine(renderer_, rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h);
@@ -372,7 +348,7 @@ void Scoreboard::recalculateAnchors()
slot4_4_ = {col, row4}; slot4_4_ = {col, row4};
// Primer cuadrado para poner el nombre de record // Primer cuadrado para poner el nombre de record
const int enterNameLenght = 8 * 7; const int enterNameLenght = NAME_LENGHT * 7;
enter_name_pos_.x = (panelWidth - enterNameLenght) / 2; enter_name_pos_.x = (panelWidth - enterNameLenght) / 2;
enter_name_pos_.y = row4; enter_name_pos_.y = row4;
@@ -384,9 +360,6 @@ void Scoreboard::recalculateAnchors()
} }
} }
// Establece el modo del marcador
void Scoreboard::setMode(int index, ScoreboardMode mode) { panel_[index].mode = mode; }
// Crea la textura de fondo // Crea la textura de fondo
void Scoreboard::createBackgroundTexture() void Scoreboard::createBackgroundTexture()
{ {

View File

@@ -1,16 +1,17 @@
#pragma once #pragma once
#include <SDL2/SDL_rect.h> // para SDL_Point, SDL_Rect #include <SDL2/SDL_rect.h> // Para SDL_Point, SDL_Rect
#include <SDL2/SDL_render.h> // para SDL_Renderer, SDL_Texture #include <SDL2/SDL_render.h> // Para SDL_Texture, SDL_Renderer
#include <SDL2/SDL_stdinc.h> // para Uint32 #include <SDL2/SDL_stdinc.h> // Para Uint32
#include <SDL2/SDL_timer.h> // para SDL_GetTicks #include <SDL2/SDL_timer.h> // Para SDL_GetTicks
#include <memory> // para unique_ptr, shared_ptr #include <stddef.h> // Para size_t
#include <string> // para string #include <memory> // Para unique_ptr, shared_ptr
#include <vector> // para vector #include <string> // Para string
#include "utils.h" // para Color #include <vector> // Para vector
class Sprite; #include "utils.h" // Para Color
class Text; class Sprite; // lines 11-11
class Texture; class Text; // lines 12-12
class Texture; // lines 13-13
// Defines // Defines
constexpr int SCOREBOARD_LEFT_PANEL = 0; constexpr int SCOREBOARD_LEFT_PANEL = 0;
@@ -51,7 +52,7 @@ private:
std::shared_ptr<Texture> game_power_meter_texture_; // Textura con el marcador de poder de la fase std::shared_ptr<Texture> game_power_meter_texture_; // Textura con el marcador de poder de la fase
std::unique_ptr<Sprite> power_meter_sprite_; // Sprite para el medidor de poder de la fase std::unique_ptr<Sprite> power_meter_sprite_; // Sprite para el medidor de poder de la fase
std::unique_ptr<Text> text_scoreboard_; // Fuente para el marcador del juego std::shared_ptr<Text> text_scoreboard_; // Fuente para el marcador del juego
SDL_Texture *background_ = nullptr; // Textura para dibujar el marcador SDL_Texture *background_ = nullptr; // Textura para dibujar el marcador
std::vector<SDL_Texture *> panel_texture_; // Texturas para dibujar cada panel std::vector<SDL_Texture *> panel_texture_; // Texturas para dibujar cada panel
@@ -125,42 +126,21 @@ public:
// Pinta el marcador // Pinta el marcador
void render(); void render();
// Establece el valor de la variable
void setName(int panel, const std::string &name);
// Establece el valor de la variable
void setRecordName(int panel, const std::string &record_name);
// Establece el valor de la variable
void setSelectorPos(int panel, int pos);
// Establece el valor de la variable
void setScore(int panel, int score);
// Establece el valor de la variable
void setMult(int panel, float mult);
// Establece el valor de la variable
void setContinue(int panel, int score);
// Establece el valor de la variable
void setStage(int stage);
// Establece el valor de la variable
void setHiScore(int hi_score);
// Establece el valor de la variable
void setPower(float power);
// Establece el valor de la variable
void setHiScoreName(const std::string &name);
// Establece el valor de la variable // Establece el valor de la variable
void setColor(Color color); void setColor(Color color);
// Establece el valor de la variable // Establece el valor de la variable
void setPos(SDL_Rect rect); void setPos(SDL_Rect rect);
// Establece el modo del marcador void setContinue(int panel, int continue_counter) { continue_counter_[panel] = continue_counter; }
void setMode(int index, ScoreboardMode mode); void setHiScore(int hi_score) { hi_score_ = hi_score; }
void setHiScoreName(const std::string &name) { hi_score_name_ = name; }
void setMode(int index, ScoreboardMode mode) { panel_[index].mode = mode; }
void setMult(int panel, float mult) { mult_[panel] = mult; }
void setName(int panel, const std::string &name) { name_[panel] = name; }
void setPower(float power) { power_ = power; }
void setRecordName(int panel, const std::string &record_name) { record_name_[panel] = record_name; }
void setScore(int panel, int score) { score_[panel] = score; }
void setSelectorPos(int panel, int pos) { selector_pos_[panel] = pos; }
void setStage(int stage) { stage_ = stage; }
}; };

View File

@@ -6,12 +6,12 @@
#include <algorithm> // Para clamp, max, min #include <algorithm> // Para clamp, max, min
#include <fstream> // Para basic_ifstream, ifstream #include <fstream> // Para basic_ifstream, ifstream
#include <iterator> // Para istreambuf_iterator, operator== #include <iterator> // Para istreambuf_iterator, operator==
#include <string> // Para allocator, operator+, to_string, char_t... #include <string> // Para allocator, operator+, char_traits, to_s...
#include <vector> // Para vector #include <vector> // Para vector
#include "asset.h" // Para Asset #include "asset.h" // Para Asset
#include "dbgtxt.h" // Para dbg_print #include "dbgtxt.h" // Para dbg_print
#include "global_inputs.h" // Para service_pressed_counter #include "global_inputs.h" // Para service_pressed_counter
#include "input.h" // Para Input, InputType, INPUT_DO_NOT_ALLOW_RE... #include "jail_shader.h" // Para init, render
#include "notifier.h" // Para Notifier #include "notifier.h" // Para Notifier
#include "on_screen_help.h" // Para OnScreenHelp #include "on_screen_help.h" // Para OnScreenHelp
#include "options.h" // Para Options, OptionsVideo, options, Options... #include "options.h" // Para Options, OptionsVideo, options, Options...
@@ -98,7 +98,7 @@ void Screen::blit()
OnScreenHelp::get()->render(); OnScreenHelp::get()->render();
// Muestra información por pantalla // Muestra información por pantalla
displayInfo(); renderInfo();
// Muestra las notificaciones // Muestra las notificaciones
Notifier::get()->render(); Notifier::get()->render();
@@ -210,7 +210,7 @@ void Screen::setVideoMode(ScreenVideoMode videoMode)
} }
// Camibia entre pantalla completa y ventana // Camibia entre pantalla completa y ventana
void Screen::switchVideoMode() void Screen::toggleVideoMode()
{ {
options.video.mode = options.video.mode == ScreenVideoMode::WINDOW ? ScreenVideoMode::FULLSCREEN : ScreenVideoMode::WINDOW; options.video.mode = options.video.mode == ScreenVideoMode::WINDOW ? ScreenVideoMode::FULLSCREEN : ScreenVideoMode::WINDOW;
setVideoMode(options.video.mode); setVideoMode(options.video.mode);
@@ -260,74 +260,6 @@ void Screen::update()
OnScreenHelp::get()->update(); OnScreenHelp::get()->update();
} }
// Comprueba las entradas
void Screen::checkInput()
{
#ifndef ARCADE
// Comprueba el teclado para cambiar entre pantalla completa y ventana
if (Input::get()->checkInput(InputType::WINDOW_FULLSCREEN, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
switchVideoMode();
const std::string mode = options.video.mode == ScreenVideoMode::WINDOW ? "Window" : "Fullscreen";
Notifier::get()->showText(mode + " mode");
return;
}
// Comprueba el teclado para decrementar el tamaño de la ventana
if (Input::get()->checkInput(InputType::WINDOW_DEC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
decWindowSize();
const std::string size = std::to_string(options.video.window.size);
Notifier::get()->showText("Window size x" + size);
return;
}
// Comprueba el teclado para incrementar el tamaño de la ventana
if (Input::get()->checkInput(InputType::WINDOW_INC_SIZE, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
incWindowSize();
const std::string size = std::to_string(options.video.window.size);
Notifier::get()->showText("Window size x" + size);
return;
}
#endif
// Comprueba el teclado para activar o desactivar los shaders
if (Input::get()->checkInput(InputType::VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
switchShaders();
return;
}
#ifdef DEBUG
// Comprueba el teclado para mostrar la información de debug
if (Input::get()->checkInput(InputType::SHOWINFO, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
show_info_ = !show_info_;
return;
}
#endif
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
// Comprueba los mandos para activar o desactivar los shaders
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
Input::get()->checkInput(InputType::VIDEO_SHADERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
switchShaders();
return;
}
// Comprueba los mandos para mostrar la información de debug
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) &&
Input::get()->checkInput(InputType::SHOWINFO, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
show_info_ = !show_info_;
return;
}
}
}
// Agita la pantalla // Agita la pantalla
void Screen::shake() void Screen::shake()
{ {
@@ -404,13 +336,19 @@ void Screen::doAttenuate()
} }
} }
// Activa/desactiva los shaders // Activa / desactiva los shaders
void Screen::switchShaders() void Screen::toggleShaders()
{ {
options.video.shaders = !options.video.shaders; options.video.shaders = !options.video.shaders;
setVideoMode(options.video.mode); setVideoMode(options.video.mode);
const std::string value = options.video.shaders ? "on" : "off"; const std::string value = options.video.shaders ? "on" : "off";
Notifier::get()->showText("Shaders " + value); Notifier::get()->showText({"Shaders " + value});
}
// Activa / desactiva la información de debug
void Screen::toggleDebugInfo()
{
show_info_ = !show_info_;
} }
// Atenua la pantalla // Atenua la pantalla
@@ -437,7 +375,7 @@ void Screen::updateFPS()
} }
// Muestra información por pantalla // Muestra información por pantalla
void Screen::displayInfo() void Screen::renderInfo()
{ {
if (show_info_) if (show_info_)
{ {

View File

@@ -92,7 +92,7 @@ private:
void updateFPS(); void updateFPS();
// Muestra información por pantalla // Muestra información por pantalla
void displayInfo(); void renderInfo();
// Calcula la nueva posición de la ventana a partir de la antigua al cambiarla de tamaño // Calcula la nueva posición de la ventana a partir de la antigua al cambiarla de tamaño
SDL_Point getNewPosition(); SDL_Point getNewPosition();
@@ -118,9 +118,6 @@ public:
// Actualiza la lógica de la clase // Actualiza la lógica de la clase
void update(); void update();
// Comprueba las entradas
void checkInput();
// Limpia la pantalla // Limpia la pantalla
void clean(Color color = Color(0x00, 0x00, 0x00)); void clean(Color color = Color(0x00, 0x00, 0x00));
@@ -133,8 +130,8 @@ public:
// Establece el modo de video // Establece el modo de video
void setVideoMode(ScreenVideoMode video_mode); void setVideoMode(ScreenVideoMode video_mode);
// Camibia entre pantalla completa y ventana // Cambia entre pantalla completa y ventana
void switchVideoMode(); void toggleVideoMode();
// Cambia el tamaño de la ventana // Cambia el tamaño de la ventana
void setWindowSize(int size); void setWindowSize(int size);
@@ -157,8 +154,11 @@ public:
// Pone la pantalla de color // Pone la pantalla de color
void flash(Color color, int lenght); void flash(Color color, int lenght);
// Activa/desactiva los shaders // Activa / desactiva los shaders
void switchShaders(); void toggleShaders();
// Activa / desactiva la información de debug
void toggleDebugInfo();
// Atenua la pantalla // Atenua la pantalla
void attenuate(bool value); void attenuate(bool value);

40
source/stage.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include "stage.h"
#include <vector>
namespace Stage
{
std::vector<Stage> stages; // Variable con los datos de cada pantalla
int power = 0; // Poder acumulado en la fase
int total_power = 0; // Poder total necesario para completar el juego
int number = 0; // Fase actual
// Devuelve una fase
Stage get(int index) { return stages.at(std::min(9, index)); }
// Inicializa las variables del namespace Stage
void init()
{
stages.emplace_back(Stage(200, 7 + (4 * 1), 7 + (4 * 3)));
stages.emplace_back(Stage(300, 7 + (4 * 2), 7 + (4 * 4)));
stages.emplace_back(Stage(600, 7 + (4 * 3), 7 + (4 * 5)));
stages.emplace_back(Stage(600, 7 + (4 * 3), 7 + (4 * 5)));
stages.emplace_back(Stage(600, 7 + (4 * 4), 7 + (4 * 6)));
stages.emplace_back(Stage(600, 7 + (4 * 4), 7 + (4 * 6)));
stages.emplace_back(Stage(650, 7 + (4 * 5), 7 + (4 * 7)));
stages.emplace_back(Stage(750, 7 + (4 * 5), 7 + (4 * 7)));
stages.emplace_back(Stage(850, 7 + (4 * 6), 7 + (4 * 8)));
stages.emplace_back(Stage(950, 7 + (4 * 7), 7 + (4 * 10)));
power = 0;
total_power = 0;
number = 0;
}
// Añade poder
void addPower(int amount)
{
power += amount;
total_power += amount;
}
}

31
source/stage.h Normal file
View File

@@ -0,0 +1,31 @@
#pragma once
#include <vector>
namespace Stage
{
struct Stage
{
int power_to_complete; // Cantidad de poder que se necesita para completar la fase
int min_menace; // Umbral mínimo de amenaza de la fase
int max_menace; // Umbral máximo de amenaza de la fase
// Constructor
Stage(int power_to_complete, int min_menace, int max_menace)
: power_to_complete(power_to_complete), min_menace(min_menace), max_menace(max_menace) {}
};
extern std::vector<Stage> stages; // Variable con los datos de cada pantalla
extern int power; // Poder acumulado en la fase
extern int total_power; // Poder total necesario para completar el juego
extern int number; // Fase actual
// Devuelve una fase
Stage get(int index);
// Inicializa las variables del namespace Stage
void init();
// Añade poder
void addPower(int amount);
}

View File

@@ -1,16 +1,16 @@
#include "text.h" #include "text.h"
#include <SDL2/SDL_blendmode.h> // for SDL_BLENDMODE_BLEND #include <SDL2/SDL_blendmode.h> // Para SDL_BLENDMODE_BLEND
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888 #include <SDL2/SDL_pixels.h> // Para SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_rect.h> // for SDL_Rect #include <SDL2/SDL_rect.h> // Para SDL_Rect
#include <SDL2/SDL_render.h> // for SDL_TEXTUREACCESS_TARGET #include <SDL2/SDL_render.h> // Para SDL_TEXTUREACCESS_TARGET
#include <stddef.h> // for size_t #include <stddef.h> // Para size_t
#include <fstream> // for basic_ifstream, basic_istream, basic... #include <fstream> // Para basic_ifstream, basic_istream, basic...
#include <iostream> // for cerr #include <iostream> // Para cerr
#include <stdexcept> // for runtime_error #include <stdexcept> // Para runtime_error
#include "screen.h" // for Screen #include "screen.h" // Para Screen
#include "sprite.h" // for Sprite #include "sprite.h" // Para Sprite
#include "texture.h" // for Texture #include "texture.h" // Para Texture
#include "utils.h" // for Color, getFileName, printWithDots #include "utils.h" // Para Color, getFileName, printWithDots
// Llena una estructuta TextFile desde un fichero // Llena una estructuta TextFile desde un fichero
std::shared_ptr<TextFile> loadTextFile(const std::string &file_path) std::shared_ptr<TextFile> loadTextFile(const std::string &file_path)
@@ -164,6 +164,8 @@ std::shared_ptr<Texture> Text::writeToTexture(const std::string &text, int zoom,
texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET); texture->createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET);
texture->setBlendMode(SDL_BLENDMODE_BLEND); texture->setBlendMode(SDL_BLENDMODE_BLEND);
texture->setAsRenderTarget(renderer); texture->setAsRenderTarget(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning); zoom == 1 ? write(0, 0, text, kerning) : write2X(0, 0, text, kerning);
return texture; return texture;

View File

@@ -2,13 +2,13 @@
#include "texture.h" #include "texture.h"
#include <SDL2/SDL_error.h> // Para SDL_GetError #include <SDL2/SDL_error.h> // Para SDL_GetError
#include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom #include <SDL2/SDL_surface.h> // Para SDL_CreateRGBSurfaceWithFormatFrom
#include <fcntl.h> // Para SEEK_END, SEEK_SET
#include <stdio.h> // Para fseek, fclose, fopen, fread, ftell, FILE
#include <stdlib.h> // Para free, malloc
#include <fstream> // Para basic_ostream, operator<<, basic_ifstream #include <fstream> // Para basic_ostream, operator<<, basic_ifstream
#include <iostream> // Para cerr, cout #include <iostream> // Para cerr, cout
#include <stdexcept> // Para runtime_error #include <stdexcept> // Para runtime_error
#include <string> // Para char_traits, operator<<, operator+
#include <vector> // Para vector
#include "gif.c" // Para LoadGif, LoadPalette #include "gif.c" // Para LoadGif, LoadPalette
#include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a...
#include "utils.h" // Para getFileName, printWithDots #include "utils.h" // Para getFileName, printWithDots
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@@ -315,8 +315,8 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
{ {
std::vector<Uint32> palette; std::vector<Uint32> palette;
FILE *f = fopen(file_path.c_str(), "rb"); std::ifstream file(file_path, std::ios::binary | std::ios::ate);
if (!f) if (!file)
{ {
std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl; std::cerr << "Error: Fichero no encontrado " << getFileName(file_path) << std::endl;
throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path)); throw std::runtime_error("Fichero no encontrado: " + getFileName(file_path));
@@ -326,21 +326,22 @@ std::vector<Uint32> Texture::loadPaletteFromFile(const std::string &file_path)
printWithDots("Image : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Image : ", getFileName(file_path), "[ LOADED ]");
} }
fseek(f, 0, SEEK_END); auto size = file.tellg();
long size = ftell(f); file.seekg(0, std::ios::beg);
fseek(f, 0, SEEK_SET);
Uint8 *buffer = static_cast<Uint8 *>(malloc(size));
fread(buffer, size, 1, f);
fclose(f);
const auto *pal = LoadPalette(buffer); std::vector<Uint8> buffer(size);
if (!file.read(reinterpret_cast<char *>(buffer.data()), size))
{
std::cerr << "Error: No se pudo leer completamente el fichero " << getFileName(file_path) << std::endl;
throw std::runtime_error("Error al leer el fichero: " + getFileName(file_path));
}
const auto pal = LoadPalette(buffer.data());
if (!pal) if (!pal)
{ {
return palette; return palette;
} }
free(buffer);
for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i)
{ {
palette.push_back((pal[i] << 8) + 255); palette.push_back((pal[i] << 8) + 255);

View File

@@ -1,10 +1,10 @@
#include "title.h" #include "title.h"
#include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN #include <SDL2/SDL_events.h> // Para SDL_PollEvent, SDL_Event, SDL_KEYDOWN
#include <SDL2/SDL_keycode.h> // Para SDLK_1, SDLK_2, SDLK_3 #include <SDL2/SDL_keycode.h> // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5
#include <SDL2/SDL_rect.h> // Para SDL_Rect #include <SDL2/SDL_rect.h> // Para SDL_Rect
#include <SDL2/SDL_timer.h> // Para SDL_GetTicks #include <SDL2/SDL_timer.h> // Para SDL_GetTicks
#include <string> // Para char_traits, operator+, to_string, bas... #include <stddef.h> // Para size_t
#include <utility> // Para move #include <string> // Para char_traits, operator+, basic_string
#include <vector> // Para vector #include <vector> // Para vector
#include "define_buttons.h" // Para DefineButtons #include "define_buttons.h" // Para DefineButtons
#include "fade.h" // Para Fade, FadeType #include "fade.h" // Para Fade, FadeType
@@ -18,23 +18,22 @@
#include "param.h" // Para Param, param, ParamGame, ParamTitle #include "param.h" // Para Param, param, ParamGame, ParamTitle
#include "resource.h" // Para Resource #include "resource.h" // Para Resource
#include "screen.h" // Para Screen #include "screen.h" // Para Screen
#include "section.h" // Para Options, options, name, Name #include "section.h" // Para Options, options, Name, name, AttractMode
#include "sprite.h" // Para Sprite #include "sprite.h" // Para Sprite
#include "text.h" // Para Text, TEXT_CENTER, TEXT_SHADOW #include "text.h" // Para TEXT_CENTER, TEXT_SHADOW, Text
#include "texture.h" // Para Texture #include "texture.h" // Para Texture
#include "tiled_bg.h" // Para TiledBG, TiledBGMode #include "tiled_bg.h" // Para TiledBG, TiledBGMode
#include "utils.h" // Para Color, Zone, fade_color, no_color, BLOCK #include "utils.h" // Para Color, Zone, fade_color, no_color, BLOCK
// Constructor // Constructor
Title::Title() Title::Title()
: text1_(std::make_unique<Text>(Resource::get()->getTexture("smb2.gif"), Resource::get()->getTextFile("smb2.txt"))), : text_(Resource::get()->getText("smb2")),
text2_(std::make_unique<Text>(Resource::get()->getTexture("8bithud.png"), Resource::get()->getTextFile("8bithud.txt"))),
fade_(std::make_unique<Fade>()), fade_(std::make_unique<Fade>()),
tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::RANDOM)), tiled_bg_(std::make_unique<TiledBG>((SDL_Rect){0, 0, param.game.width, param.game.height}, TiledBGMode::RANDOM)),
game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)), game_logo_(std::make_unique<GameLogo>(param.game.game_area.center_x, param.title.title_c_c_position)),
mini_logo_texture_(Resource::get()->getTexture("logo_jailgames_mini.png")), mini_logo_texture_(Resource::get()->getTexture("logo_jailgames_mini.png")),
mini_logo_sprite_(std::make_unique<Sprite>(mini_logo_texture_, param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight())), mini_logo_sprite_(std::make_unique<Sprite>(mini_logo_texture_, param.game.game_area.center_x - mini_logo_texture_->getWidth() / 2, 0, mini_logo_texture_->getWidth(), mini_logo_texture_->getHeight())),
define_buttons_(std::make_unique<DefineButtons>(std::move(text2_))), define_buttons_(std::make_unique<DefineButtons>()),
num_controllers_(Input::get()->getNumControllers()) num_controllers_(Input::get()->getNumControllers())
{ {
// Configura objetos // Configura objetos
@@ -145,7 +144,7 @@ void Title::render()
// 'PRESS TO PLAY' // 'PRESS TO PLAY'
if (counter_ % 50 > 14 && !define_buttons_->isEnabled()) if (counter_ % 50 > 14 && !define_buttons_->isEnabled())
{ {
text1_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, param.title.press_start_position, lang::getText(23), 1, no_color, 1, shadow); text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, param.title.press_start_position, lang::getText(23), 1, no_color, 1, shadow);
} }
// Mini logo // Mini logo
@@ -155,7 +154,7 @@ void Title::render()
mini_logo_sprite_->render(); mini_logo_sprite_->render();
// Texto con el copyright // Texto con el copyright
text1_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, pos2, TEXT_COPYRIGHT, 1, no_color, 1, shadow); text_->writeDX(TEXT_CENTER | TEXT_SHADOW, param.game.game_area.center_x, pos2, TEXT_COPYRIGHT, 1, no_color, 1, shadow);
} }
// Define Buttons // Define Buttons
@@ -171,6 +170,9 @@ void Title::render()
// Comprueba los eventos // Comprueba los eventos
void Title::checkEvents() void Title::checkEvents()
{ {
// Comprueba el input para el resto de objetos
define_buttons_->checkEvents();
// Si define_buttons_ está habilitado, es él quien gestiona los eventos // Si define_buttons_ está habilitado, es él quien gestiona los eventos
if (!define_buttons_->isEnabled()) if (!define_buttons_->isEnabled())
{ {
@@ -195,32 +197,36 @@ void Title::checkEvents()
{ {
switch (event.key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_1: case SDLK_1: // Redefine los botones del mando #0
{ {
if (define_buttons_->enable(0)) if (define_buttons_->enable(0))
resetCounter(); resetCounter();
break; break;
} }
case SDLK_2: // Redefine los botones del mando #1
case SDLK_2:
{ {
if (define_buttons_->enable(1)) if (define_buttons_->enable(1))
resetCounter(); resetCounter();
break; break;
} }
case SDLK_3: // Intercambia los mandos entre los dos jugadores
case SDLK_3:
{ {
swapControllers(); swapControllers();
resetCounter();
break; break;
} }
case SDLK_4: // Intercambia la asignación del teclado
case SDLK_4: {
swapKeyboard();
resetCounter();
break;
}
case SDLK_5: // Muestra la asignacion de mandos y teclado
{ {
showControllers(); showControllers();
resetCounter();
break; break;
} }
default: default:
break; break;
} }
@@ -235,56 +241,38 @@ void Title::checkInput()
// Comprueba los controladores solo si no se estan definiendo los botones // Comprueba los controladores solo si no se estan definiendo los botones
if (!define_buttons_->isEnabled()) if (!define_buttons_->isEnabled())
{ {
// Comprueba el teclado para empezar a jugar // Comprueba los métodos de control
if (Input::get()->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD)) for (const auto &controller : options.controllers)
{ {
if (section::options == section::Options::TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP) if (Input::get()->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index) &&
!Input::get()->checkInput(InputType::SERVICE, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index))
{ {
fade_->activate(); if (section::options == section::Options::TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP)
post_fade_ = options.controller[0].player_id; {
fade_->activate();
post_fade_ = controller.player_id;
return;
}
} }
}
// Comprueba los mandos
for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{
// Comprueba si se va a intercambiar la asignación de mandos a jugadores // Comprueba si se va a intercambiar la asignación de mandos a jugadores
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) && if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, controller.type, controller.index) &&
Input::get()->checkInput(InputType::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) Input::get()->checkInput(InputType::SWAP_CONTROLLERS, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index))
{ {
swapControllers(); swapControllers();
return; return;
} }
// Comprueba si algun mando quiere ser configurado // Comprueba si algun mando quiere ser configurado
if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i) && if (Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, controller.type, controller.index) &&
Input::get()->checkInput(InputType::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i)) Input::get()->checkInput(InputType::CONFIG, INPUT_DO_NOT_ALLOW_REPEAT, controller.type, controller.index))
{ {
define_buttons_->enable(i); define_buttons_->enable(controller.index);
return; return;
} }
// Comprueba el botón de START de los mandos
if (Input::get()->checkInput(InputType::START, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
// Si no está el botón de servicio activo
if (!Input::get()->checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
if (section::options == section::Options::TITLE_2 || ALLOW_TITLE_ANIMATION_SKIP)
{
fade_->activate();
post_fade_ = options.controller[i].player_id;
return;
}
}
}
} }
} }
// Comprueba el input para el resto de objetos
Screen::get()->checkInput();
define_buttons_->checkEvents();
// Comprueba los inputs que se pueden introducir en cualquier sección del juego // Comprueba los inputs que se pueden introducir en cualquier sección del juego
globalInputs::check(); globalInputs::check();
} }
@@ -317,39 +305,44 @@ void Title::swapControllers()
if (Input::get()->getNumControllers() == 0) if (Input::get()->getNumControllers() == 0)
return; return;
define_buttons_->swapControllers(); swapOptionsControllers();
showControllers(); showControllers();
} }
// Intercambia el teclado de jugador
void Title::swapKeyboard()
{
swapOptionsKeyboard();
std::string text = lang::getText(100) + std::to_string(getPlayerWhoUsesKeyboard()) + ": " + lang::getText(69);
Notifier::get()->showText({text});
}
// Muestra información sobre los controles y los jugadores // Muestra información sobre los controles y los jugadores
void Title::showControllers() void Title::showControllers()
{ {
// Crea cadenas de texto vacias para un numero máximo de mandos // Crea vectores de texto vacíos para un número máximo de mandos
constexpr int MAX_CONTROLLERS = 2; constexpr size_t NUM_CONTROLLERS = 2;
std::string text[MAX_CONTROLLERS]; std::vector<std::string> text(NUM_CONTROLLERS);
int playerControllerIndex[MAX_CONTROLLERS]; std::vector<int> playerControllerIndex(NUM_CONTROLLERS, -1);
for (int i = 0; i < MAX_CONTROLLERS; ++i)
{
text[i].clear();
playerControllerIndex[i] = -1;
}
// Obtiene para cada jugador el índice del mando correspondiente // Obtiene de cada jugador el índice del mando que tiene asignado
for (int i = 0; i < MAX_CONTROLLERS; ++i) for (size_t i = 0; i < NUM_CONTROLLERS; ++i)
{ {
playerControllerIndex[options.controller[i].player_id - 1] = i; // Ejemplo: el jugador 1 tiene el mando 2
playerControllerIndex.at(options.controllers.at(i).player_id - 1) = i;
} }
// Genera el texto correspondiente // Genera el texto correspondiente
for (int i = 0; i < MAX_CONTROLLERS; ++i) for (size_t i = 0; i < NUM_CONTROLLERS; ++i)
{ {
const int index = playerControllerIndex[i]; const size_t index = playerControllerIndex.at(i);
if (options.controller[index].plugged) if (options.controllers.at(index).plugged)
{ {
text[i] = lang::getText(100) + std::to_string(i + 1) + ": " + options.controller[index].name; text.at(i) = lang::getText(100) + std::to_string(i + 1) + ": " + options.controllers.at(index).name;
} }
} }
Notifier::get()->showText(text[0], text[1]); // std::string spaces(lang::getText(100).length() + 3, ' ');
resetCounter(); // std::string kb_text = spaces + lang::getText(69);
Notifier::get()->showText({text.at(0), text.at(1)});
} }

View File

@@ -2,14 +2,17 @@
#include <SDL2/SDL_stdinc.h> // Para Uint32 #include <SDL2/SDL_stdinc.h> // Para Uint32
#include <memory> // Para unique_ptr, shared_ptr #include <memory> // Para unique_ptr, shared_ptr
#include "section.h" // Para Name class DefineButtons; // lines 6-6
class DefineButtons; class Fade; // lines 7-7
class Fade; class GameLogo; // lines 8-8
class GameLogo; class Sprite; // lines 9-9
class Sprite; class Text; // lines 10-10
class Text; class Texture; // lines 11-11
class Texture; // lines 14-14 class TiledBG; // lines 12-12
class TiledBG; namespace section
{
enum class Name;
}
// Textos // Textos
constexpr const char TEXT_COPYRIGHT[] = "@2020,2024 JailDesigner"; constexpr const char TEXT_COPYRIGHT[] = "@2020,2024 JailDesigner";
@@ -37,8 +40,7 @@ class Title
{ {
private: private:
// Objetos y punteros // Objetos y punteros
std::unique_ptr<Text> text1_; // Objeto de texto para poder escribir textos en pantalla std::shared_ptr<Text> text_; // Objeto de texto para poder escribir textos en pantalla
std::unique_ptr<Text> text2_; // Objeto de texto para poder escribir textos en pantalla
std::unique_ptr<Fade> fade_; // Objeto para realizar fundidos en pantalla std::unique_ptr<Fade> fade_; // Objeto para realizar fundidos en pantalla
std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo std::unique_ptr<TiledBG> tiled_bg_; // Objeto para dibujar el mosaico animado de fondo
std::unique_ptr<GameLogo> game_logo_; // Objeto para dibujar el logo con el título del juego std::unique_ptr<GameLogo> game_logo_; // Objeto para dibujar el logo con el título del juego
@@ -74,6 +76,9 @@ private:
// Intercambia la asignación de mandos a los jugadores // Intercambia la asignación de mandos a los jugadores
void swapControllers(); void swapControllers();
// Intercambia el teclado de jugador
void swapKeyboard();
// Muestra información sobre los controles y los jugadores // Muestra información sobre los controles y los jugadores
void showControllers(); void showControllers();