Compare commits

...

49 Commits

Author SHA1 Message Date
JailDesigner 9c0502eefb feat(enemy): sistema d'events declaratius via YAML 2026-05-25 13:34:48 +02:00
JailDesigner 9b3da3a6e7 Merge branch 'feat/enemy-star': afegir tipus STAR i 3 nous shapes 2026-05-25 12:42:06 +02:00
JailDesigner bc41169176 feat(enemy): afegir tipus STAR (estrella de 5 puntes) i 3 nous shapes
- Nou enemic STAR amb shape star_5.shp, escala 0.7 i color groc pur.
  Reusa el comportament zigzag del Pentagon i carrega via EnemyRegistry.
- DistribucioEnemics estesa amb camp 'star' opcional (default 0) per
  mantenir compat amb stages antics.
- Stage 1 reconfigurat a 25/25/25/25 per mostrar els 4 tipus.
- Afegits també shapes bullet_long.shp i bullet_double.shp (encara no
  utilitzats; preparats per futures variants de bala).
2026-05-25 12:36:26 +02:00
JailDesigner b3a1afce06 Merge branch 'feat/entities-yaml-enemy-shared': paràmetres compartits dels enemics a cada YAML 2026-05-25 11:59:28 +02:00
JailDesigner 4b6dc8a47a feat(entities): migrar paràmetres compartits dels enemics a cada YAML 2026-05-25 11:54:40 +02:00
JailDesigner 3dadd5fc1a Merge branch 'feat/entities-yaml-bullet': migració de la bala a YAML 2026-05-25 11:47:36 +02:00
JailDesigner bea844d51e feat(entities): migrar bullet a data/entities/bullet/bullet.yaml 2026-05-25 11:42:43 +02:00
JailDesigner 5fb6c68df4 Merge branch 'feat/entities-shape-scale': collision_radius derivat del shape + scale al YAML 2026-05-25 11:33:52 +02:00
JailDesigner 866a057704 feat(entities): derivar collision_radius del shape + scale/collision_factor al YAML 2026-05-25 11:29:43 +02:00
JailDesigner da8eab330d Merge branch 'feat/entities-yaml-enemies': configuració dels enemics en YAML 2026-05-25 10:15:34 +02:00
JailDesigner 39bda0775e feat(entities): migrar la configuració dels 3 enemics a data/entities/<type>/*.yaml 2026-05-25 10:01:12 +02:00
JailDesigner ed4d3a3915 Merge branch 'feat/entities-yaml-player': configuració del player en YAML 2026-05-25 09:39:39 +02:00
JailDesigner 6447932212 feat(entities): migrar la configuració del player a data/entities/player/player.yaml 2026-05-25 08:32:49 +02:00
JailDesigner 9f278772bb Merge branch 'feat/pack-resources-align': alinear pack_resources amb projectes germans 2026-05-25 07:55:49 +02:00
JailDesigner 2d073b6055 feat(pack): alinear sortida i build amb projectes germans 2026-05-25 07:55:30 +02:00
JailDesigner 99b18d208d chore: bump version a 0.8.0
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 22:41:25 +02:00
JailDesigner 1321566910 Merge branch 'feature/sistema-gestio-inputs': sistema de gestio d'inputs
Modul DefineInputs per redefinir teclat i mando amb overlay modal,
pagina CONTROLS al menu de servei (picker de mando amb llista, swap
automatic en conflicte, slot SENSE MANDO, rebind per jugador), so
accept en cada captura, navegacio del menu amb dpad/stick i triggers
L2/R2, glyphs ( ) i / al charset, autoassignacio de mando al primer
arranque, i diversos fixes de pipeline d'events.
2026-05-24 22:39:47 +02:00
JailDesigner cefafe99e4 feat(service_menu): triggers L2/R2 navegables + so al rebind
El menu de servei nomes processava AXIS_MOTION dels sticks i descartava
els triggers. Com SDL3 mai emet button events per a L2/R2 (nomes axis),
rebindar FIRE o ACCEL a un trigger feia que no funcionaren al menu, fins
i tot estant correctament al joc per via del poll de Input::checkTriggerInput.
Afegim edge-detect dels dos triggers al handleGamepadAxis i, quan creuen
el llindar, mirem si el codi virtual (100=L2, 101=R2) coincideix amb el
binding de FIRE → activateCurrent, o ACCEL → popPage. Estat held per
trigger per evitar repeticions mentre es mante premut.

DefineInputs ara reprodueix el so accept del menu en cada captura
valida, que estava silent i no donava feedback al rebind.

Tambe extraiem processStickX/Y i processTriggerEdge per mantenir
handleGamepadAxis com a dispatcher i sota el llindar de complexitat
cognitiva del clang-tidy.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 22:38:10 +02:00
JailDesigner daa7eaf811 feat(service_menu): glyphs () + tanca picker al seleccionar mando
Afegim els glyphs ( i ) a VectorText (char_lparen.shp, char_rparen.shp,
arcs de 4 trams dins la caixa 20x40) perque el sufix (P1)/(P2) de la
picker de mando es renderitze net sense warnings.

A mes, al triar un mando o "SENSE MANDO" a la picker fem popPage
automatic, perque l'usuari no haja de tornar enrere a ma després
d'una assignacio.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 22:20:29 +02:00
JailDesigner 3dcf5c3a99 feat(service_menu): picker de mando per llista i fix SENSE MANDO
El cycle anterior fallava al desasignar perque Input::resolvePlayerGamepad
tenia un fallback per slot que reasignava gamepads_[player_index] quan
name+path eren buits. Això el contradeia el slot "SENSE MANDO" del cycle:
el YAML quedava buit pero el runtime seguia lligant el mando. Treure el
fallback i moure l'autoassignacio inicial al boot (nomes si tots dos
jugadors venen buits) restaura la semàntica: buit vol dir buit.

Sobre el fix, redissenyem la UX dels items MANDO P1/P2: ja no son CYCLE
sino SUBMENU que obrin una pàgina-llista (estil RESOLUCIÓ) amb tots els
mandos detectats. Cada item porta sufix (P1)/(P2) nomes si el mando el
te l'altre jugador, perque sapigues que assignar-lo li'l "robarà".
L'ultim item es "SENSE MANDO" per a desassignar explícitament. La
lògica de swap automatic en conflicte queda extreta a assignPadToPlayer
i es reutilitza des de la picker.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 22:12:53 +02:00
JailDesigner 99d0f62ab5 feat(service_menu): slot 'sense mando' al cycle i swap automatic en conflicte
El CYCLE de la pagina CONTROLS ara inclou un slot virtual al final que
desassigna el mando (gamepad_name + gamepad_path buits → padDisplayName
mostra "SENSE MANDO"). Aixi l'usuari pot recuperar el control teclat
sense haver d'editar el YAML.

A mes, si en assignar un mando l'altre jugador ja el tenia, fem swap
automatic: l'altre jugador rep l'assignacio previa d'aquest, evitant
que dos jugadors comparteixen el mateix dispositiu. La deteccio
prioritza path (mateixa branca que resolvePlayerGamepad).

Extracta tambe reapplyBindings per mantenir cyclePlayerPad llegible.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 21:22:23 +02:00
JailDesigner 85050c8da4 fix(define-inputs): deixa passar QUIT i ESC al pipeline global
L'overlay de redefinicio engolia tots els events mentre estava actiu, fet
que impedia tancar la finestra amb l'aspa (SDL_EVENT_QUIT) i deixava
prendre ESC com a cancel-lacio del rebind. Ara:
- QUIT i WINDOW_CLOSE_REQUESTED passen sempre al global per tancar
  l'aplicacio des de l'aspa.
- ESC ja no cancel-la la sequencia; cau al global on obre el prompt
  d'eixida com a la resta del joc.
- isReservedScancode (ESC/F1-F12/RETURN/BACKSPACE/TAB) deixa passar.

Tambe ajusta DISPAR -> DISPARAR a ca.yaml i treu el hint "ESC PER
CANCEL-LAR" del modal i les claus de locale corresponents.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 21:20:25 +02:00
JailDesigner 120c5502fd feat(vector-text): afegeix el glyph / al charset
El progres "i/n" del modal de redefinicio (ex. 1/4) sortia com a "14"
perque VectorText no tenia shape per a la barra i emetia un warning.
Afegim font/char_slash.shp (diagonal de baix-esquerra a dalt-dreta dins
de la caixa 20x40) i el registrem al loader i al getShapeFilename.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:58:52 +02:00
JailDesigner 64a6599e81 fix(title): manten animacions amb menu obert, bloqueja nomes els polls d'input
El fix anterior pausava tot el title quan el menu de servei estava obert,
trencant l'efecte d'animacio de fons. Ara title segueix animant-se i
nomes guardem handleSkipInput/handleStartInput mentre el menu o el modal
de rebind estan actius, per evitar START fantasma sense congelar el render.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:54:04 +02:00
JailDesigner a4b567588f feat(service_menu): navegacio amb mando (dpad, stick, fire = enter, accelerate = back)
ServiceMenu::handleEvent ara accepta tambe SDL_EVENT_GAMEPAD_BUTTON_DOWN
i SDL_EVENT_GAMEPAD_AXIS_MOTION. Mapeig: dpad UP/DOWN/LEFT/RIGHT mouen
el cursor, el boto FIRE configurat per qualsevol jugador equival a ENTER
(activa l'item), ACCELERATE equival a BACK (popPage). El stick esquerre
fa nav amb edge-detect: cal tornar a centre per disparar una altra entrada.
GlobalEvents::forwardToServiceMenu envia tots aquests events al menu
quan esta obert.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:42:33 +02:00
JailDesigner 2e74fea2d5 feat(input): stick com a font alternativa de LEFT/RIGHT al mando
LEFT i RIGHT no son redefinibles al mando i s'assumeix dpad O stick.
Input::update() ara llegeix SDL_GAMEPAD_AXIS_LEFTX i fa OR amb l'estat
del dpad: qualsevol de les dos fonts dispara l'accio. Llindar 30000
(coherent amb el constant AXIS_THRESHOLD ja existent).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:38:26 +02:00
JailDesigner c4933875dd fix(input): impedeix que els events traspassin al joc en acabar el rebind
El menu de servei queda obert per sota de l'overlay DefineInputs durant
tot el rebind (en lloc de tancar-se al activar la accio), de manera que
absorbeix qualsevol KEY_DOWN que arribi un cop l'overlay s'auto-cancela.
La pantalla de titol tambe pausa la seua logica mentre el menu de servei
esta obert, igual que GameScene, per evitar que detecti un START fantasma
si l'usuari encara te una tecla pulsada al moment de tancar-se el modal.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:36:51 +02:00
JailDesigner 10a54aef91 fix(ui): nom del mando en majuscules a la UI sense modificar el config
VectorText nomes admet ASCII en majuscules; els noms dels mandos (i el
git hash) passaven pel toUpperAscii local del service_menu, pero les
notificacions de hot-plug i el text del CYCLE de la pagina CONTROLS
es mostraven amb el case original. Mou el helper a un utils compartit i
l'aplica a tots els punts de display sense tocar gamepad_name al config.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:33:01 +02:00
JailDesigner 34be79192c feat(service_menu): pagina CONTROLS amb assignacio de pad i rebind per jugador
Afegeix submenu CONTROLS al menu de servei amb 2 items CYCLE per
seleccionar el mando assignat a cada jugador (persistit per name + path)
i 4 items ACTION per arrancar DefineInputs (teclat/mando per a P1/P2).

Tambe afegeix:
- Director: init/update/draw/destroy del singleton DefineInputs.
- GlobalEvents: routing prioritari de tots els events a DefineInputs
  mentre l'overlay esta actiu.
- Locale ca/en: claus del submenu CONTROLS i de l'overlay de rebind.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:18:49 +02:00
JailDesigner fcf13591be feat(input): modul DefineInputs per redefinir teclat i mando
Singleton inspirat en aee_arcade DefineButtons: pinta una caixa central
modal, captura events SDL i avança per una sequencia fixa d'accions per
jugador. Teclat: LEFT/RIGHT/FIRE/ACCELERATE. Mando: FIRE/ACCELERATE/
START/MENU. ESC cancel-la, duplicats dins la sessio es rebutgen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 20:17:30 +02:00
JailDesigner 3e8f2f35bf feat(input): accio MENU i assignacio de mando per path + name
Afegeix l'accio MENU a InputAction (obre el menu de servei des del mando,
equivalent a F12 al teclat) i els camps gamepad.button_start i
gamepad.button_menu al config per jugador. Tambe afegeix gamepad_path
per distingir dos mandos del mateix model i prioritza path > name >
slot a applyPlayerNBindings via el nou resolvePlayerGamepad.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 19:56:59 +02:00
JailDesigner e5a91825b1 feat(input): notifica connexio/desconnexio de mandos via Notifier
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 19:55:42 +02:00
JailDesigner b3271b17a2 Merge branch 'feat/preload-resources': precàrrega completa al boot 2026-05-24 19:32:04 +02:00
JailDesigner d4117e3505 feat(boot): precàrrega de música, sons i shapes a l'arrencada 2026-05-24 19:31:35 +02:00
JailDesigner 73c7e4ea76 Merge branch 'fix/fps-rounding': FPS arrodonit 2026-05-24 19:20:14 +02:00
JailDesigner 23cc5ce68d fix(debug-hud): FPS arrodonit en lloc de truncat 2026-05-24 19:20:06 +02:00
JailDesigner e42059e486 chore(sounds): normalitza sons a pcm_u8 48 kHz mono peak -1 dB 2026-05-24 19:11:43 +02:00
JailDesigner 00f40d194b Merge branch 'feat/audio-persistence': persistència d'àudio + toggles com a mute pur 2026-05-24 19:06:43 +02:00
JailDesigner 31f348328e fix(audio): toggles són mute pur, no aturen la reproducció 2026-05-24 18:52:05 +02:00
JailDesigner 8c48a9a772 feat(config): persistència de les opcions d'àudio al config.yaml 2026-05-24 18:40:33 +02:00
JailDesigner bacfbe6eac Merge branch 'tweak/debug-hud-layout': FPS gran, RES i DRIVER al HUD de debug 2026-05-24 14:15:05 +02:00
JailDesigner 63d08aef46 tweak(debug-hud): FPS més gran, afegeix RES i DRIVER 2026-05-24 14:14:50 +02:00
JailDesigner 87f818ef96 Merge branch 'feat/service-menu': menu de servei F12 amb VIDEO/AUDIO/OPCIONS/SISTEMA 2026-05-24 12:32:55 +02:00
JailDesigner 7eafe21623 feat(service-menu): submenu RESOLUCIO amb canvi en calent de l'offscreen 2026-05-24 12:30:47 +02:00
JailDesigner 22827c28fa feat(service-menu): pobla SISTEMA amb reinici, eixir i confirmacions 2026-05-24 12:18:39 +02:00
JailDesigner 8c21345f14 feat(service-menu): pobla OPCIONS amb idioma i toggle del HUD de debug 2026-05-24 11:56:11 +02:00
JailDesigner 56d7d4af52 feat(service-menu): pobla AUDIO amb toggles i sliders de volum 2026-05-24 11:49:14 +02:00
JailDesigner 71c43ec6fe feat(service-menu): pobla VIDEO amb zoom, fullscreen, vsync, AA i postfx 2026-05-24 11:37:36 +02:00
JailDesigner 443b461974 feat(service-menu): esquelet amb F12, brackets sci-fi i highlight animat 2026-05-24 11:25:09 +02:00
91 changed files with 5491 additions and 1407 deletions
+5 -2
View File
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.16)
project(orni VERSION 0.7.2 LANGUAGES CXX)
project(orni VERSION 0.8.0 LANGUAGES CXX)
# Info del projecte (font de veritat per a project.h)
set(PROJECT_LONG_NAME "Orni Attack")
@@ -110,7 +110,10 @@ add_executable(pack_resources EXCLUDE_FROM_ALL
tools/pack_resources/pack_resources.cpp
source/core/resources/resource_pack.cpp
)
target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source")
target_include_directories(pack_resources PRIVATE
"${CMAKE_SOURCE_DIR}/source"
"${CMAKE_BINARY_DIR}"
)
target_compile_options(pack_resources PRIVATE -Wall -Wextra -Wpedantic)
# --- REGENERACIÓ AUTOMÀTICA DE build/resources.pack ---
+22
View File
@@ -0,0 +1,22 @@
name: bullet
# Shape de la bala. El bounding_radius del .shp dóna el hitbox base (~3 px);
# scale el modula visualment i pel hitbox.
shape:
path: bullet.shp
scale: 1.0
collision_factor: 1.0
# Cinemàtica pura: la bala no col·lisiona físicament al PhysicsWorld
# (body_.radius = 0 al spawn), però sí participa al gameplay via
# checkCollisionSwept. La mass i l'impact_momentum_factor es fan servir
# només per calcular l'impuls que rep l'enemic en impactar.
physics:
mass: 0.5
restitution: 0.0 # irrelevant (no rebota)
linear_damping: 0.0 # movement rectilini uniforme
angular_damping: 0.0
impact_momentum_factor: 3.0 # factor de transferència de moment bala→enemic
colors:
normal: [155, 255, 175] # verd laser
+68
View File
@@ -0,0 +1,68 @@
name: pentagon
ai_type: pentagon # Validat contra el directori; mapeja a EnemyType::PENTAGON.
shape:
path: enemy_pentagon.shp
scale: 1.0 # multiplicador visual + hitbox sobre la mida nativa del .shp
collision_factor: 1.0 # ajust opcional del hitbox (default 1.0)
physics:
mass: 5.0
speed: 35.0 # px/s (esquivador lent)
rotation_delta_min: 0.75 # rad/s — rotació visual mínima
rotation_delta_max: 3.75 # rad/s — rotació visual màxima
restitution: 1.0 # rebot elàstic perfecte contra parets
linear_damping: 0.0 # manté velocitat (sense fricció)
angular_damping: 0.0
behavior:
# Pentagon: zigzag esquivador (canvi de direcció probabilístic per segon).
angle_change_max: 1.0 # rad — magnitud del canvi de direcció
zigzag_prob_per_second: 0.8
animation:
pulse: # respiració d'escala aleatòria
trigger_prob_per_second: 0.01
duration_min: 1.0
duration_max: 3.0
amplitude_min: 0.08
amplitude_max: 0.20
frequency_min: 1.5
frequency_max: 3.0
rotation_accel: # acceleració/desacceleració de rotació visual
trigger_prob_per_second: 0.02
duration_min: 3.0
duration_max: 8.0
multiplier_min: 0.3
multiplier_max: 4.0
wounded:
duration: 1.0 # segons en estat ferit abans d'explotar
blink_hz: 10.0 # parpelleig color normal ↔ wounded
spawn:
invulnerability_duration: 3.0
invulnerability_brightness_start: 0.3
invulnerability_brightness_end: 0.7
invulnerability_scale_start: 0.0
invulnerability_scale_end: 1.0
safety_distance: 36.0 # px mínim respecte al player al spawn
colors:
normal: [0, 255, 255] # Cyan pur "esquivador"
wounded: [255, 220, 60] # Daurat (parpelleig al rebre impacte)
score: 100
events:
# Comportament clàssic: dos impactes per matar (set_hurt entra wounded;
# el segon hit detecta wounded i destrueix automàticament).
on_hit:
- action: apply_impulse
- action: set_hurt
on_hurt_end:
- action: destroy
on_destroy:
- action: add_score
- action: create_debris
- action: create_fireworks
+66
View File
@@ -0,0 +1,66 @@
name: pinwheel
ai_type: pinwheel # Validat contra el directori; mapeja a EnemyType::PINWHEEL.
shape:
path: enemy_pinwheel.shp
scale: 1.0 # multiplicador visual + hitbox sobre la mida nativa del .shp
collision_factor: 1.0 # ajust opcional del hitbox (default 1.0)
physics:
mass: 4.0 # Més lleuger — àgil
speed: 50.0 # px/s (el més ràpid)
rotation_delta_min: 3.0 # rad/s — rotació base elevada
rotation_delta_max: 6.0
restitution: 1.0
linear_damping: 0.0
angular_damping: 0.0
behavior:
# Pinwheel: movement rectilíniauniforme + boost de rotació visual prop de la nau.
rotation_proximity_multiplier: 3.0 # Multiplicador de rotació quan és prop de la nau
proximity_distance: 100.0 # Llindar de distància (px)
animation:
pulse:
trigger_prob_per_second: 0.01
duration_min: 1.0
duration_max: 3.0
amplitude_min: 0.08
amplitude_max: 0.20
frequency_min: 1.5
frequency_max: 3.0
rotation_accel:
trigger_prob_per_second: 0.02
duration_min: 3.0
duration_max: 8.0
multiplier_min: 0.3
multiplier_max: 4.0
wounded:
duration: 1.0
blink_hz: 10.0
spawn:
invulnerability_duration: 3.0
invulnerability_brightness_start: 0.3
invulnerability_brightness_end: 0.7
invulnerability_scale_start: 0.0
invulnerability_scale_end: 1.0
safety_distance: 36.0
colors:
normal: [255, 0, 255] # Magenta pur "agressiu"
wounded: [255, 220, 60]
score: 200
events:
on_hit:
- action: apply_impulse
- action: set_hurt
on_hurt_end:
- action: destroy
on_destroy:
- action: add_score
- action: create_debris
- action: create_fireworks
+49
View File
@@ -0,0 +1,49 @@
name: player_ship
# Shape de la nau. Resolt per ShapeLoader (busca a "shapes/<path>").
# Nota: el segon jugador rep un override del shape ("ship2.shp") al ctor.
# Quan s'introdueixin variants reals de nau, es crearà un YAML separat
# per cada model.
#
# scale: multiplicador visual i de hitbox sobre la mida nativa del .shp (1.0 = mida del fitxer).
# collision_factor: ajust opcional del hitbox respecte el cercle circumscrit
# automàtic de la shape; tocar només si el feel del hitbox
# no quadra amb la silueta visual (default 1.0).
shape:
path: ship.shp
scale: 1.0
collision_factor: 1.0
physics:
mass: 10.0
restitution: 0.6
linear_damping: 1.5
angular_damping: 0.0
rotation_speed: 3.14 # rad/s (~180 deg/s, input-driven sense inercia)
acceleration: 400.0 # px/s^2 multiplicat per la massa quan THRUST
max_velocity: 180.0 # px/s (clamp post-integració per preservar feel arcade)
# Factor de transferència del moment lineal de la nau a l'enemic en el
# frame exacte que mor per col·lisió (afegit per damunt del rebot natural).
death_impact_factor: 0.3
invulnerability:
duration: 3.0 # segons d'invulnerabilitat post-respawn
blink_visible: 0.1 # segons visible per cicle de parpelleig
blink_invisible: 0.1 # segons invisible per cicle de parpelleig
hurt:
duration: 15.0 # segons en estat "ferit" abans de tornar a normal
blink_hz: 10.0 # freqüència parpelleig color normal <-> color hurt
# Empenta visual: la nau s'escala lleugerament amb la velocitat.
# Manté la sensació del Pascal original (0..MAX_VEL → 1.0..~1.5).
visual_thrust:
push_divisor: 33.33
scale_divisor: 12.0
colors:
normal: [255, 255, 255] # blanc neutre
hurt: [255, 220, 60] # daurat (estat ferit)
weapon:
bullet_speed: 700.0 # velocitat escalar de la bullet (px/s)
+66
View File
@@ -0,0 +1,66 @@
name: square
ai_type: square # Validat contra el directori; mapeja a EnemyType::SQUARE.
shape:
path: enemy_square.shp
scale: 1.0 # multiplicador visual + hitbox sobre la mida nativa del .shp
collision_factor: 1.0 # ajust opcional del hitbox (default 1.0)
physics:
mass: 8.0 # Més pesat — "tanc"
speed: 40.0 # px/s (velocitat mitjana)
rotation_delta_min: 0.3 # rad/s — rotació lenta
rotation_delta_max: 1.5
restitution: 1.0
linear_damping: 0.0
angular_damping: 0.0
behavior:
# Square: tracking discret cap a la nau cada N segons.
tracking_strength: 0.5 # Interpolació LERP cap a la direcció desitjada (0..1)
tracking_interval: 1.0 # segons entre updates d'angle
animation:
pulse:
trigger_prob_per_second: 0.01
duration_min: 1.0
duration_max: 3.0
amplitude_min: 0.08
amplitude_max: 0.20
frequency_min: 1.5
frequency_max: 3.0
rotation_accel:
trigger_prob_per_second: 0.02
duration_min: 3.0
duration_max: 8.0
multiplier_min: 0.3
multiplier_max: 4.0
wounded:
duration: 1.0
blink_hz: 10.0
spawn:
invulnerability_duration: 3.0
invulnerability_brightness_start: 0.3
invulnerability_brightness_end: 0.7
invulnerability_scale_start: 0.0
invulnerability_scale_end: 1.0
safety_distance: 36.0
colors:
normal: [255, 0, 0] # Roig pur "tanc"
wounded: [255, 220, 60]
score: 150
events:
on_hit:
- action: apply_impulse
- action: set_hurt
on_hurt_end:
- action: destroy
on_destroy:
- action: add_score
- action: create_debris
- action: create_fireworks
+65
View File
@@ -0,0 +1,65 @@
name: star
ai_type: star # Validat contra el directori; mapeja a EnemyType::STAR.
shape:
path: star_5.shp
scale: 0.7 # Lleugerament més petit que els altres enemics per diferenciar visualment.
collision_factor: 1.0
physics:
mass: 5.0
speed: 35.0 # Mateixos paràmetres que pentagon (esquivador lent).
rotation_delta_min: 0.75
rotation_delta_max: 3.75
restitution: 1.0
linear_damping: 0.0
angular_damping: 0.0
behavior:
# Hereta el comportament de Pentagon (zigzag esquivador).
angle_change_max: 1.0
zigzag_prob_per_second: 0.8
animation:
pulse:
trigger_prob_per_second: 0.01
duration_min: 1.0
duration_max: 3.0
amplitude_min: 0.08
amplitude_max: 0.20
frequency_min: 1.5
frequency_max: 3.0
rotation_accel:
trigger_prob_per_second: 0.02
duration_min: 3.0
duration_max: 8.0
multiplier_min: 0.3
multiplier_max: 4.0
wounded:
duration: 1.0
blink_hz: 10.0
spawn:
invulnerability_duration: 3.0
invulnerability_brightness_start: 0.3
invulnerability_brightness_end: 0.7
invulnerability_scale_start: 0.0
invulnerability_scale_end: 1.0
safety_distance: 36.0
colors:
normal: [255, 255, 0] # Groc estrella
wounded: [255, 220, 60]
score: 100
events:
# STAR: mor al primer impacte, sense passar per wounded.
on_hit:
- action: apply_impulse
- action: destroy
on_destroy:
- action: add_score
- action: create_debris
- action: create_fireworks
+57
View File
@@ -14,6 +14,8 @@ notification:
postfx_on: "POSTPROCESSAT ACTIU"
postfx_off: "POSTPROCESSAT INACTIU"
locale_switched: "IDIOMA: {lang}"
gamepad_connected: "{name} CONNECTAT"
gamepad_disconnected: "{name} DESCONNECTAT"
language:
ca: "CATALA"
@@ -50,6 +52,61 @@ service_menu:
title: "MENU DE SERVEI"
video: "VIDEO"
audio: "AUDIO"
options: "OPCIONS"
system: "SISTEMA"
controls: "CONTROLS"
back: "ENRERE"
exit: "EIXIR DEL JOC"
# Items del submenu VIDEO
video_zoom: "ZOOM"
video_fullscreen: "PANTALLA COMPLETA"
video_vsync: "VSYNC"
video_aa: "ANTIALIAS"
video_postfx: "POSTPROCESSAT"
video_resolution: "RESOLUCIO"
# Items del submenu OPCIONS
options_language: "IDIOMA"
options_show_info: "MOSTRAR INFO"
# Items del submenu AUDIO
audio_master: "AUDIO"
audio_master_volume: "VOLUM GENERAL"
audio_music: "MUSICA"
audio_music_volume: "VOLUM MUSICA"
audio_sound: "EFECTES"
audio_sound_volume: "VOLUM EFECTES"
# Items del submenu SISTEMA
system_restart: "REINICIAR"
# Pagines de confirmacio (estructura: titol + NO/SI)
confirm_restart: "ESTAS SEGUR DE REINICIAR?"
confirm_exit: "ESTAS SEGUR DE EIXIR?"
confirm_no: "NO"
confirm_yes: "SI"
# Valors comuns
value_on: "ACTIU"
value_off: "INACTIU"
# Items del submenu CONTROLS
controls_pad_p1: "MANDO JUGADOR 1"
controls_pad_p2: "MANDO JUGADOR 2"
controls_no_pad: "SENSE MANDO"
controls_define_keyboard_p1: "REDEFINIR TECLES P1"
controls_define_keyboard_p2: "REDEFINIR TECLES P2"
controls_define_gamepad_p1: "REDEFINIR BOTONS P1"
controls_define_gamepad_p2: "REDEFINIR BOTONS P2"
# Overlay modal de redefinicio (DefineInputs)
define:
title_keyboard_p1: "REDEFINIR TECLES P1"
title_keyboard_p2: "REDEFINIR TECLES P2"
title_gamepad_p1: "REDEFINIR BOTONS P1"
title_gamepad_p2: "REDEFINIR BOTONS P2"
press_key: "PREMEU UNA TECLA"
press_button: "PREMEU UN BOTO"
complete: "CONFIGURACIO COMPLETA"
no_gamepad: "CAP MANDO ASSIGNAT AL JUGADOR"
action:
left: "ESQUERRA"
right: "DRETA"
fire: "DISPARAR"
accelerate: "ACCELERAR"
start: "START"
menu: "MENU"
+57
View File
@@ -13,6 +13,8 @@ notification:
postfx_on: "POSTPROCESS ON"
postfx_off: "POSTPROCESS OFF"
locale_switched: "LANGUAGE: {lang}"
gamepad_connected: "{name} CONNECTED"
gamepad_disconnected: "{name} DISCONNECTED"
language:
ca: "CATALAN"
@@ -49,6 +51,61 @@ service_menu:
title: "SERVICE MENU"
video: "VIDEO"
audio: "AUDIO"
options: "OPTIONS"
system: "SYSTEM"
controls: "CONTROLS"
back: "BACK"
exit: "EXIT GAME"
# Items of VIDEO submenu
video_zoom: "ZOOM"
video_fullscreen: "FULLSCREEN"
video_vsync: "VSYNC"
video_aa: "ANTIALIAS"
video_postfx: "POSTPROCESS"
video_resolution: "RESOLUTION"
# Items of OPTIONS submenu
options_language: "LANGUAGE"
options_show_info: "SHOW INFO"
# Items of AUDIO submenu
audio_master: "AUDIO"
audio_master_volume: "MASTER VOLUME"
audio_music: "MUSIC"
audio_music_volume: "MUSIC VOLUME"
audio_sound: "SOUNDS"
audio_sound_volume: "SOUND VOLUME"
# Items of SYSTEM submenu
system_restart: "RESTART"
# Confirmation pages (structure: title + NO/YES)
confirm_restart: "REALLY RESTART?"
confirm_exit: "REALLY EXIT?"
confirm_no: "NO"
confirm_yes: "YES"
# Common values
value_on: "ON"
value_off: "OFF"
# Items of CONTROLS submenu
controls_pad_p1: "PLAYER 1 GAMEPAD"
controls_pad_p2: "PLAYER 2 GAMEPAD"
controls_no_pad: "NO GAMEPAD"
controls_define_keyboard_p1: "REDEFINE KEYS P1"
controls_define_keyboard_p2: "REDEFINE KEYS P2"
controls_define_gamepad_p1: "REDEFINE BUTTONS P1"
controls_define_gamepad_p2: "REDEFINE BUTTONS P2"
# Modal overlay for input redefinition (DefineInputs)
define:
title_keyboard_p1: "REDEFINE KEYS P1"
title_keyboard_p2: "REDEFINE KEYS P2"
title_gamepad_p1: "REDEFINE BUTTONS P1"
title_gamepad_p2: "REDEFINE BUTTONS P2"
press_key: "PRESS A KEY"
press_button: "PRESS A BUTTON"
complete: "CONFIGURATION COMPLETE"
no_gamepad: "NO GAMEPAD ASSIGNED TO PLAYER"
action:
left: "LEFT"
right: "RIGHT"
fire: "FIRE"
accelerate: "ACCELERATE"
start: "START"
menu: "MENU"
+17
View File
@@ -0,0 +1,17 @@
# bullet_double.shp - Bala anular (dos cercles concèntrics)
# © 2026 JailDesigner
#
# Dos octàgons concèntrics al centre (0,0):
# - Exterior: radi 4 (lleugerament més gran que la bala estàndard, radi 3)
# - Interior: radi 2 (lleugerament més petit que la bala estàndard)
# Aspecte d'anell / aura de plasma. Bounding radius natiu = 4.
name: bullet_double
scale: 1.0
center: 0, 0
# Cercle exterior (octàgon, radi 4)
polyline: 0,-4 2.83,-2.83 4,0 2.83,2.83 0,4 -2.83,2.83 -4,0 -2.83,-2.83 0,-4
# Cercle interior (octàgon, radi 2)
polyline: 0,-2 1.41,-1.41 2,0 1.41,1.41 0,2 -1.41,1.41 -2,0 -1.41,-1.41 0,-2
+28
View File
@@ -0,0 +1,28 @@
# bullet_long.shp - Bala allargada (dos octàgons tangents + tapes superior i inferior)
# © 2026 JailDesigner
#
# Dos cercles (octàgons radi 3) tangents externament al punt (0,0), units
# per una línia horitzontal superior i una d'inferior. La silueta resultant
# és una càpsula amb la separació visible dels dos cercles al centre.
#
# Geometria:
# Centre octàgon esquerre: (-3, 0)
# Centre octàgon dret: ( 3, 0)
# Punt de tangència: ( 0, 0)
# Bounding radius natiu ≈ 6 (extrem horitzontal a x=±6).
name: bullet_long
scale: 1.0
center: 0, 0
# Octàgon esquerre (centre x=-3, radi 3)
polyline: -3,-3 -0.88,-2.12 0,0 -0.88,2.12 -3,3 -5.12,2.12 -6,0 -5.12,-2.12 -3,-3
# Octàgon dret (centre x=3, radi 3)
polyline: 3,-3 5.12,-2.12 6,0 5.12,2.12 3,3 0.88,2.12 0,0 0.88,-2.12 3,-3
# Tapa superior: uneix el cim de l'octàgon esquerre amb el del dret
polyline: -3,-3 3,-3
# Tapa inferior: uneix la base de l'octàgon esquerre amb la del dret
polyline: -3,3 3,3
+9
View File
@@ -0,0 +1,9 @@
# char_lparen.shp - Símbol ( (parèntesi esquerre)
# Dimensions: 20×40 (blocky display)
name: char_lparen
scale: 1.0
center: 10, 20
# Arc cap a l'esquerra aproximat amb 4 trams rectes
polyline: 14,4 8,12 6,20 8,28 14,36
+9
View File
@@ -0,0 +1,9 @@
# char_rparen.shp - Símbol ) (parèntesi dret)
# Dimensions: 20×40 (blocky display)
name: char_rparen
scale: 1.0
center: 10, 20
# Arc cap a la dreta aproximat amb 4 trams rectes
polyline: 6,4 12,12 14,20 12,28 6,36
+9
View File
@@ -0,0 +1,9 @@
# char_slash.shp - Símbol / (barra)
# Dimensions: 20×40 (blocky display)
name: char_slash
scale: 1.0
center: 10, 20
# Línia diagonal de baix-esquerra a dalt-dreta
line: 4,36 16,4
+15
View File
@@ -0,0 +1,15 @@
# star_5.shp - ORNI enemic (estrella de 5 puntes, només perímetre)
# © 2026 JailDesigner
#
# Pentagrama clàssic: 5 vèrtexs exteriors (radi 20) alternant amb 5 vèrtexs
# interiors (radi 7.64 = 20/φ² ≈ proporció àuria) per donar puntes esveltes.
# Vèrtex apuntant amunt (igual que enemy_pentagon).
#
# Sense línies interiors: una única polyline que recorre el perímetre.
# Bounding radius natiu ≈ 20 (alineat amb pentagon/square/pinwheel).
name: star_5
scale: 1.0
center: 0, 0
polyline: 0,-20 4.49,-6.18 19.02,-6.18 7.27,2.36 11.76,16.18 0,7.64 -11.76,16.18 -7.27,2.36 -19.02,-6.18 -4.49,-6.18 0,-20
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+5 -4
View File
@@ -7,7 +7,7 @@ metadata:
description: "Progressive difficulty curve from novice to expert"
stages:
# STAGE 1: Tutorial - Mix de tots els tipus, velocitat lenta
# STAGE 1: Tutorial - Mix de tots 4 tipus al 25% per mostrar-los junts
- stage_id: 1
total_enemies: 50
spawn_config:
@@ -15,9 +15,10 @@ stages:
initial_delay: 0.3
spawn_interval: 0.4
enemy_distribution:
pentagon: 34
cuadrado: 33
molinillo: 33
pentagon: 25
cuadrado: 25
molinillo: 25
star: 25
difficulty_multipliers:
speed_multiplier: 0.7
rotation_multiplier: 0.8
+36 -33
View File
@@ -51,8 +51,6 @@ void Audio::playMusic(const std::string& name, const int loop, const int crossfa
return;
}
if (!music_enabled_) { return; }
auto* resource = AudioResource::getMusic(name);
if (resource == nullptr) { return; }
@@ -62,7 +60,7 @@ void Audio::playMusic(const std::string& name, const int loop, const int crossfa
// Reprodueix la música per punter (amb crossfade opcional)
void Audio::playMusic(Ja::Music* music, const int loop, const int crossfade_ms) {
if (!music_enabled_ || music == nullptr) { return; }
if (music == nullptr) { return; }
playMusicInternal(music, loop, crossfade_ms);
// Si el Ja::Music es va crear con filename (loadMusic con 3 arguments), el
@@ -72,9 +70,12 @@ void Audio::playMusic(Ja::Music* music, const int loop, const int crossfade_ms)
}
// Camí comú dels dos overloads: fa el dispatch crossfade vs stop+play i
// actualitza el loop cachejat. Els callers s'encarreguen del gating
// (music_enabled_, nullptr, same-track early return) y del nom. L'estat el
// manté Ja (Ja::playMusic posa PLAYING al Ja::Music* corresponent).
// actualitza el loop cachejat. Els callers s'encarreguen del same-track early
// return i del nom. El gate de música deshabilitada NO atura la reproducció:
// effectiveVolume porta el volum efectiu a 0 i la pista continua sonant
// silenciada, per garantir que reactivar la música la torne a sentir sense
// haver de reiniciar la pista. L'estat el manté Ja (Ja::playMusic posa
// PLAYING al Ja::Music* corresponent).
void Audio::playMusicInternal(Ja::Music* music, const int loop, const int crossfade_ms) {
const bool CURRENTLY_PLAYING = (getMusicState() == MusicState::PLAYING);
if (crossfade_ms > 0 && CURRENTLY_PLAYING) {
@@ -91,41 +92,35 @@ void Audio::playMusicInternal(Ja::Music* music, const int loop, const int crossf
// Pausa la música (l'estat el transiciona Engine::pauseMusic)
void Audio::pauseMusic() {
if (music_enabled_ && getMusicState() == MusicState::PLAYING) {
if (getMusicState() == MusicState::PLAYING) {
engine_->pauseMusic();
}
}
// Continua la música pausada (l'estat el transiciona Engine::resumeMusic)
void Audio::resumeMusic() {
if (music_enabled_ && getMusicState() == MusicState::PAUSED) {
if (getMusicState() == MusicState::PAUSED) {
engine_->resumeMusic();
}
}
// Atura la música (l'estat el transiciona Engine::stopMusic)
void Audio::stopMusic() {
if (music_enabled_) {
engine_->stopMusic();
}
engine_->stopMusic();
}
void Audio::setMusicSpeed(float ratio) {
if (music_enabled_) {
engine_->setMusicSpeed(ratio);
}
engine_->setMusicSpeed(ratio);
}
// Reprodueix un so per nom
void Audio::playSound(const std::string& name, Group group) {
if (sound_enabled_) {
engine_->playSound(AudioResource::getSound(name), 0, static_cast<int>(group));
}
engine_->playSound(AudioResource::getSound(name), 0, static_cast<int>(group));
}
// Reprodueix un so per punter directe
void Audio::playSound(Ja::Sound* sound, Group group) {
if (sound_enabled_ && sound != nullptr) {
if (sound != nullptr) {
engine_->playSound(sound, 0, static_cast<int>(group));
}
}
@@ -136,7 +131,6 @@ void Audio::playSound(Ja::Sound* sound, Group group) {
// Si l'engine torna -1 (sense canal lliure) o el so no existeix, no fem
// la crida al ratio — sin efectes col·laterals.
void Audio::playSound(const std::string& name, Group group, float speed) {
if (!sound_enabled_) { return; }
auto* sound = AudioResource::getSound(name);
if (sound == nullptr) { return; }
const int CH = engine_->playSound(sound, 0, static_cast<int>(group));
@@ -149,7 +143,6 @@ void Audio::playSound(const std::string& name, Group group, float speed) {
// existeix o l'engine retorna -1 (sin de canals d'efecte plé), cau a playSound
// sec — l'usuari sent el so aún que la cua no s'apliqui.
void Audio::playSoundWithEcho(const std::string& name, const std::string& preset_name, Group group) {
if (!sound_enabled_) { return; }
auto* sound = AudioResource::getSound(name);
if (sound == nullptr) { return; }
@@ -168,7 +161,6 @@ void Audio::playSoundWithEcho(const std::string& name, const std::string& preset
// Reprodueix un so processat per un reverb definit a sounds.yaml. Mateix
// fallback que playSoundWithEcho.
void Audio::playSoundWithReverb(const std::string& name, const std::string& preset_name, Group group) {
if (!sound_enabled_) { return; }
auto* sound = AudioResource::getSound(name);
if (sound == nullptr) { return; }
@@ -186,14 +178,12 @@ void Audio::playSoundWithReverb(const std::string& name, const std::string& pres
// Atura tots los sons
void Audio::stopAllSounds() {
if (sound_enabled_) {
engine_->stopChannel(-1);
}
engine_->stopChannel(-1);
}
// Fa una fosa de sortida de la música
void Audio::fadeOutMusic(int milliseconds) {
if (music_enabled_ && getMusicState() == MusicState::PLAYING) {
if (getMusicState() == MusicState::PLAYING) {
engine_->fadeOutMusic(milliseconds);
}
}
@@ -238,14 +228,27 @@ auto Audio::effectiveVolume(float volume, bool channel_enabled) const -> float {
return (enabled_ && channel_enabled) ? volume * config_.volume : 0.0F;
}
// Estableix el volum dels sons (float 0.0..1.0)
// Estableix el volum dels sons (float 0.0..1.0). Actualitza el valor cachejat
// a config_ perquè els getters i les re-aplicacions internes (enableSound,
// setMasterVolume) puguin tornar al volum que l'usuari va triar.
void Audio::setSoundVolume(float sound_volume, Group group) {
engine_->setSoundVolume(effectiveVolume(sound_volume, sound_enabled_), static_cast<int>(group));
config_.sound_volume = std::clamp(sound_volume, MIN_VOLUME, MAX_VOLUME);
engine_->setSoundVolume(effectiveVolume(config_.sound_volume, sound_enabled_), static_cast<int>(group));
}
// Estableix el volum de la música (float 0.0..1.0)
// Estableix el volum de la música (float 0.0..1.0). Cf. setSoundVolume.
void Audio::setMusicVolume(float music_volume) {
engine_->setMusicVolume(effectiveVolume(music_volume, music_enabled_));
config_.music_volume = std::clamp(music_volume, MIN_VOLUME, MAX_VOLUME);
engine_->setMusicVolume(effectiveVolume(config_.music_volume, music_enabled_));
}
// Estableix el volum master (multiplicador aplicat a sound + music). Re-aplica
// els canals perquè el canvi tingui efecte immediat sense esperar al següent
// setSoundVolume/setMusicVolume explícit.
void Audio::setMasterVolume(float master_volume) {
config_.volume = std::clamp(master_volume, MIN_VOLUME, MAX_VOLUME);
setSoundVolume(config_.sound_volume);
setMusicVolume(config_.music_volume);
}
// Aplica una nueva configuración (substitueix la config cachejada i reaplica enables/volums)
@@ -256,12 +259,12 @@ void Audio::applySettings(const Config& config) {
enable(config_.enabled);
}
// Estableix l'estat general
// Estableix l'estat general. Re-aplica els volums actuals; effectiveVolume
// retalla a 0 quan enabled_ és false, sense perdre els valors guardats.
void Audio::enable(bool value) {
enabled_ = value;
setSoundVolume(enabled_ ? config_.sound_volume : MIN_VOLUME);
setMusicVolume(enabled_ ? config_.music_volume : MIN_VOLUME);
setSoundVolume(config_.sound_volume);
setMusicVolume(config_.music_volume);
}
// Estableix l'estat dels sons i reaplica el volum porque los canals actius
+8
View File
@@ -101,6 +101,14 @@ class Audio {
// --- Control de volum (API interna: float 0.0..1.0) ---
void setSoundVolume(float volume, Group group = Group::ALL); // Ajusta el volum dels efectes
void setMusicVolume(float volume); // Ajusta el volum de la música
void setMasterVolume(float volume); // Ajusta el master (re-aplica sound + music)
// Getters dels volums actuals (lectura de la config_ cachejada). Reflexen
// el valor que l'usuari ha triat l'última vegada, independent del gating
// d'enabled/channel.
[[nodiscard]] auto getMasterVolume() const -> float { return config_.volume; }
[[nodiscard]] auto getSoundVolume() const -> float { return config_.sound_volume; }
[[nodiscard]] auto getMusicVolume() const -> float { return config_.music_volume; }
// --- Helpers de conversió para la capa de presentació ---
// UI (menús, notificacions) manega enters 0..100; internament viu float 0..1.
+13
View File
@@ -48,17 +48,30 @@ namespace Config {
int button_right{SDL_GAMEPAD_BUTTON_DPAD_RIGHT};
int button_thrust{SDL_GAMEPAD_BUTTON_WEST}; // X button
int button_shoot{SDL_GAMEPAD_BUTTON_SOUTH}; // A button
int button_start{SDL_GAMEPAD_BUTTON_START}; // Start button
int button_menu{SDL_GAMEPAD_BUTTON_BACK}; // Select/Back -> obre menu servei
};
struct PlayerBindings {
KeyboardBindings keyboard{};
GamepadBindings gamepad{};
std::string gamepad_name; // Empty = auto-assign by index
std::string gamepad_path; // Prioritari sobre name per distingir mateixos models
};
struct AudioConfig {
bool enabled{true};
float volume{1.0F}; // Master 0..1
bool music_enabled{true};
float music_volume{1.0F};
bool sound_enabled{true};
float sound_volume{0.25F};
};
struct EngineConfig {
WindowConfig window{};
RenderingConfig rendering{};
AudioConfig audio{};
PlayerBindings player1{};
PlayerBindings player2{};
KeyboardBindings keyboard_controls{}; // Defaults globals per Input
-1
View File
@@ -25,7 +25,6 @@
#include "core/defaults/physics.hpp"
#include "core/defaults/playfield.hpp"
#include "core/defaults/rendering.hpp"
#include "core/defaults/ship.hpp"
#include "core/defaults/starfield_parallax.hpp"
#include "core/defaults/title.hpp"
#include "core/defaults/trail.hpp"
+12 -95
View File
@@ -1,101 +1,18 @@
// enemies.hpp - Configuració per tipus d'enemic (Pentagon/Square/Molinillo), spawn i scoring
// enemies.hpp - Constants tècniques compartides per al sistema d'enemics.
// © 2026 JailDesigner
//
// Tots els paràmetres jugables (physics, animation, wounded, spawn,
// behavior, colors, scoring) viuen a data/entities/<type>/<type>.yaml i
// s'accedeixen via EnemyRegistry::get(EnemyType). Aquí només queda el
// que no és per personalitzar per tipus.
#pragma once
#include "core/defaults/entities.hpp"
namespace Defaults::Enemies::Spawn {
namespace Defaults::Enemies {
// Sostre de reintents al cercar una posició de spawn que respecti el
// safety_distance del tipus. No és un paràmetre jugable: és el llindar
// tècnic abans de caure a un fallback aleatori amb advertència.
constexpr int MAX_SPAWN_ATTEMPTS = 50;
// Cuerpo físico común (valores por defecto del constructor)
namespace Body {
constexpr float DEFAULT_MASS = 5.0F; // Más liviano que la nave (10.0)
constexpr float RESTITUTION = 1.0F; // Rebote elástico perfecto contra paredes
constexpr float LINEAR_DAMPING = 0.0F; // Sin fricción: mantienen velocidad
constexpr float ANGULAR_DAMPING = 0.0F;
} // namespace Body
// Pentagon (esquivador - zigzag evasion)
namespace Pentagon {
constexpr float SPEED = 35.0F; // px/s (slightly slower)
constexpr float MASS = 5.0F; // Masa estándar
constexpr float ANGLE_CHANGE_PROB = 0.20F; // 20% per wall hit (frequent zigzag)
constexpr float ANGLE_CHANGE_MAX = 1.0F; // Max random angle change (rad)
constexpr float ZIGZAG_PROB_PER_SECOND = 0.8F; // Probabilidad de zigzag por segundo
constexpr float ROTATION_DELTA_MIN = 0.75F; // Min visual rotation (rad/s) [+50%]
constexpr float ROTATION_DELTA_MAX = 3.75F; // Max visual rotation (rad/s) [+50%]
constexpr const char* SHAPE_FILE = "enemy_pentagon.shp";
} // namespace Pentagon
// Square (perseguidor - tracks player)
namespace Square {
constexpr float SPEED = 40.0F; // px/s (medium speed)
constexpr float MASS = 8.0F; // Más pesado, "tanque"
constexpr float TRACKING_STRENGTH = 0.5F; // Interpolation toward player (0.0-1.0)
constexpr float TRACKING_INTERVAL = 1.0F; // Seconds between angle updates
constexpr float ROTATION_DELTA_MIN = 0.3F; // Slow rotation [+50%]
constexpr float ROTATION_DELTA_MAX = 1.5F; // [+50%]
constexpr const char* SHAPE_FILE = "enemy_square.shp";
} // namespace Square
// Molinillo (agressiu - fast straight lines, proximity spin-up)
namespace Pinwheel {
constexpr float SPEED = 50.0F; // px/s (fastest)
constexpr float MASS = 4.0F; // Más liviano, ágil
constexpr float ANGLE_CHANGE_PROB = 0.05F; // 5% per wall hit (rare direction change)
constexpr float ANGLE_CHANGE_MAX = 0.3F; // Small angle adjustments
constexpr float ROTATION_DELTA_MIN = 3.0F; // Base rotation (rad/s) [+50%]
constexpr float ROTATION_DELTA_MAX = 6.0F; // [+50%]
constexpr float ROTATION_DELTA_PROXIMITY_MULTIPLIER = 3.0F; // Spin-up multiplier when near ship
constexpr float PROXIMITY_DISTANCE = 100.0F; // Distance threshold (px)
constexpr const char* SHAPE_FILE = "enemy_pinwheel.shp";
} // namespace Pinwheel
// Animation parameters (shared)
namespace Animation {
// Palpitation
constexpr float PULSE_TRIGGER_PROB = 0.01F; // 1% chance per second
constexpr float PULSE_DURATION_MIN = 1.0F; // Min duration (seconds)
constexpr float PULSE_DURATION_MAX = 3.0F; // Max duration (seconds)
constexpr float PULSE_AMPLITUD_MIN = 0.08F; // Min scale variation
constexpr float PULSE_AMPLITUD_MAX = 0.20F; // Max scale variation
constexpr float PULSE_FREQ_MIN = 1.5F; // Min frequency (Hz)
constexpr float PULSE_FREQ_MAX = 3.0F; // Max frequency (Hz)
// Rotation acceleration
constexpr float ROTATION_ACCEL_TRIGGER_PROB = 0.02F; // 2% chance per second [4x more frequent]
constexpr float ROTATION_ACCEL_DURATION_MIN = 3.0F; // Min transition time
constexpr float ROTATION_ACCEL_DURATION_MAX = 8.0F; // Max transition time
constexpr float ROTATION_ACCEL_MULTIPLIER_MIN = 0.3F; // Min speed multiplier [more dramatic]
constexpr float ROTATION_ACCEL_MULTIPLIER_MAX = 4.0F; // Max speed multiplier [more dramatic]
} // namespace Animation
// Wounded state (entre primer impacto y explosión)
namespace Wounded {
constexpr float DURATION = 1.0F; // Segundos en estado herido antes de explotar
constexpr float BLINK_HZ = 10.0F; // Frecuencia de parpadeo color tipo ↔ dorado
} // namespace Wounded
// Spawn safety and invulnerability system
namespace Spawn {
// Safe spawn distance from player
constexpr float SAFETY_DISTANCE_MULTIPLIER = 3.0F; // 3x ship radius
constexpr float SAFETY_DISTANCE = Defaults::Entities::SHIP_RADIUS * SAFETY_DISTANCE_MULTIPLIER; // 36.0f px
constexpr int MAX_SPAWN_ATTEMPTS = 50; // Max attempts to find safe position
// Invulnerability system
constexpr float INVULNERABILITY_DURATION = 3.0F; // Seconds
constexpr float INVULNERABILITY_BRIGHTNESS_START = 0.3F; // Dim
constexpr float INVULNERABILITY_BRIGHTNESS_END = 0.7F; // Normal (same as Defaults::Brightness::ENEMIC)
constexpr float INVULNERABILITY_SCALE_START = 0.0F; // Invisible
constexpr float INVULNERABILITY_SCALE_END = 1.0F; // Full size
} // namespace Spawn
// Scoring system (puntuación per type de enemy)
namespace Scoring {
constexpr int PENTAGON_SCORE = 100; // Pentágono (esquivador, 35 px/s)
constexpr int SQUARE_SCORE = 150; // Square (perseguidor, 40 px/s)
constexpr int PINWHEEL_SCORE = 200; // Molinillo (agressiu, 50 px/s)
} // namespace Scoring
} // namespace Defaults::Enemies
} // namespace Defaults::Enemies::Spawn
+4 -3
View File
@@ -8,8 +8,9 @@ namespace Defaults::Entities {
constexpr int MAX_ORNIS = 15;
constexpr int MAX_BULLETS = 50;
constexpr float SHIP_RADIUS = 12.0F;
constexpr float ENEMY_RADIUS = 20.0F;
constexpr float BULLET_RADIUS = 3.0F;
// SHIP_RADIUS / ENEMY_RADIUS / BULLET_RADIUS han migrat: ara cada entitat
// calcula el seu collision_radius com a
// shape.bounding_radius × shape.scale × shape.collision_factor
// a partir del seu YAML (data/entities/<name>/<name>.yaml).
} // namespace Defaults::Entities
+1 -1
View File
@@ -29,7 +29,7 @@ namespace Defaults::Game {
// Friendly fire system
constexpr bool FRIENDLY_FIRE_ENABLED = true; // Activar friendly fire
constexpr float COLLISION_BULLET_PLAYER_AMPLIFIER = 1.0F; // Hitbox exacto (100%)
constexpr float BULLET_SPEED = 700.0F; // Velocidad escalar (px/s). Pascal: 7 px/frame × 20 FPS
// BULLET_SPEED migrat a data/entities/player/player.yaml (weapon.bullet_speed).
// Transición LEVEL_START (mensajes aleatorios PRE-level)
constexpr float LEVEL_START_DURATION = 3.0F; // Duración total
+3 -1
View File
@@ -33,7 +33,9 @@ namespace Defaults::Hud {
namespace DebugOverlay {
constexpr float X = 30.0F;
constexpr float Y_FPS = 24.0F;
constexpr float LINE_HEIGHT = 18.0F; // separació entre línies (scale 0.4 → ~16 px alt)
constexpr float FPS_LINE_HEIGHT = 28.0F; // separació després del FPS (scale 0.7 → ~28 px)
constexpr float LINE_HEIGHT = 18.0F; // separació entre línies (scale 0.4 → ~16 px alt)
constexpr float FPS_SCALE = 0.7F; // FPS més gran que la resta
constexpr float TEXT_SCALE = 0.4F;
constexpr float TEXT_SPACING = 2.0F;
constexpr float BRIGHTNESS = 1.0F;
+5 -6
View File
@@ -14,11 +14,10 @@ namespace Defaults::Palette {
// brillantor perceptual sota el bloom (sense alterar la identitat de color).
// El canal dominant es manté a 255 a cada color per maximitzar la saturació
// visible quan el halo s'expandeix.
constexpr SDL_Color SHIP = {.r = 255, .g = 255, .b = 255, .a = 255}; // Blanco neutro
constexpr SDL_Color BULLET = {.r = 155, .g = 255, .b = 175, .a = 255}; // Verde laser
constexpr SDL_Color PENTAGON = {.r = 0, .g = 255, .b = 255, .a = 255}; // Cyan pur "esquivador"
constexpr SDL_Color SQUARE = {.r = 255, .g = 0, .b = 0, .a = 255}; // Roig pur "tank"
constexpr SDL_Color PINWHEEL = {.r = 255, .g = 0, .b = 255, .a = 255}; // Magenta pur "agressiu"
constexpr SDL_Color WOUNDED = {.r = 255, .g = 220, .b = 60, .a = 255}; // Dorado: enemigo herido
// Tots els colors d'entitats han migrat al seu YAML respectiu
// (data/entities/<name>/<name>.yaml, secció `colors`):
// - SHIP → player.yaml
// - PENTAGON / SQUARE / PINWHEEL / WOUNDED → cada enemy.yaml
// - BULLET → bullet.yaml
} // namespace Defaults::Palette
+42 -59
View File
@@ -3,72 +3,55 @@
#pragma once
namespace Defaults::Physics {
// NOTA: els paràmetres del player (rotation_speed, acceleration,
// max_velocity, death_impact_factor) viuen a data/entities/player/player.yaml.
// Els paràmetres específics de la bala (mass, restitution, damping,
// impact_momentum_factor) viuen a data/entities/bullet/bullet.yaml.
// Aquest fitxer només conté els paràmetres compartits del subsistema de
// debris (explosions visuals).
constexpr float ROTATION_SPEED = 3.14F; // rad/s (~180°/s)
constexpr float ACCELERATION = 400.0F; // px/s²
constexpr float MAX_VELOCITY = 180.0F; // px/s
constexpr float FRICTION = 20.0F; // px/s²
namespace Defaults::Physics::Debris {
// Bullet — impacto físico contra enemigo (impulse mass-aware).
// Model: el impulse és el moment lineal de la bala (m·v) multiplicat per
// un factor de transferència [0..1]. 1.0 = transfereix tot el moment
// (col·lisió perfectament inelàstica), 0.5 = transfereix la meitat.
namespace Bullet {
constexpr float IMPACT_MOMENTUM_FACTOR = 3.0F; // Factor de transferència de moment bala→enemic
} // namespace Bullet
constexpr float SPEED_BASE = 80.0F; // Velocidad inicial (px/s)
constexpr float VARIACIO_SPEED = 40.0F; // ±variació aleatòria (px/s)
constexpr float ACCELERACIO = -60.0F; // Fricció/desacceleració (px/s²)
constexpr float ROTATION_MIN = 0.1F; // Rotación mínima (rad/s ~5.7°/s)
constexpr float ROTATION_MAX = 0.3F; // Rotación màxima (rad/s ~17.2°/s)
constexpr float TEMPS_VIDA = 2.0F; // Vida mínima garantida (s) — després pot morir per velocitat baixa
constexpr float TEMPS_VIDA_NAU = 3.0F; // Ship debris min lifetime (matches DEATH_DURATION)
constexpr float SHRINK_RATE = 1.0F; // Reducció de mida (1.0 = encoge a 0 al final del min_lifetime)
// Ship → enemy: impuls explícit aplicat a l'enemic en el moment exacte
// que la nau mor per col·lisió amb ell (afegit per damunt del rebot
// natural de PhysicsWorld, que ja és present però subtil amb la
// damping de la nau).
namespace Ship {
constexpr float DEATH_IMPACT_MOMENTUM_FACTOR = 0.3F;
} // namespace Ship
// Política de mort: passat el min_lifetime, el fragment mor quan la
// seva velocity cau per sota d'aquest llindar. Així els fragments
// ràpids no "popen" en moviment.
constexpr float MIN_SPEED_TO_DIE = 5.0F; // px/s — al cuadrat per evitar sqrt en update
constexpr float MIN_SPEED_TO_DIE_SQ = MIN_SPEED_TO_DIE * MIN_SPEED_TO_DIE;
// Explosions (debris physics)
namespace Debris {
constexpr float SPEED_BASE = 80.0F; // Velocidad inicial (px/s)
constexpr float VARIACIO_SPEED = 40.0F; // ±variació aleatòria (px/s)
constexpr float ACCELERACIO = -60.0F; // Fricció/desacceleració (px/s²)
constexpr float ROTATION_MIN = 0.1F; // Rotación mínima (rad/s ~5.7°/s)
constexpr float ROTATION_MAX = 0.3F; // Rotación màxima (rad/s ~17.2°/s)
constexpr float TEMPS_VIDA = 2.0F; // Vida mínima garantida (s) — després pot morir per velocitat baixa
constexpr float TEMPS_VIDA_NAU = 3.0F; // Ship debris min lifetime (matches DEATH_DURATION)
constexpr float SHRINK_RATE = 1.0F; // Reducció de mida (1.0 = encoge a 0 al final del min_lifetime)
// Rebot contra els límits del PLAYAREA (mateix patró que enemics/ship).
// 0.7 = 70% de l'energia conservada al rebot.
constexpr float RESTITUTION_BOUNDS = 0.7F;
// Política de mort: passat el min_lifetime, el fragment mor quan la
// seva velocity cau per sota d'aquest llindar. Així els fragments
// ràpids no "popen" en moviment.
constexpr float MIN_SPEED_TO_DIE = 5.0F; // px/s — al cuadrat per evitar sqrt en update
constexpr float MIN_SPEED_TO_DIE_SQ = MIN_SPEED_TO_DIE * MIN_SPEED_TO_DIE;
// Herència de velocity angular (trayectorias curvas)
constexpr float INHERITANCE_FACTOR_MIN = 0.7F; // Mínimo 70% del drotacio heredat
constexpr float INHERITANCE_FACTOR_MAX = 1.0F; // Màxim 100% del drotacio heredat
constexpr float FRICCIO_ANGULAR = 0.5F; // Desacceleració angular (rad/s²)
// Rebot contra els límits del PLAYAREA (mateix patró que enemics/ship).
// 0.7 = 70% de l'energia conservada al rebot.
constexpr float RESTITUTION_BOUNDS = 0.7F;
// Velocity heredada de la nau a l'explosió (80% del feel original).
constexpr float SHIP_VELOCITY_INHERITANCE = 0.8F;
// Herència de velocity angular (trayectorias curvas)
constexpr float INHERITANCE_FACTOR_MIN = 0.7F; // Mínimo 70% del drotacio heredat
constexpr float INHERITANCE_FACTOR_MAX = 1.0F; // Màxim 100% del drotacio heredat
constexpr float FRICCIO_ANGULAR = 0.5F; // Desacceleració angular (rad/s²)
// Velocity heredada de l'enemic a l'explosió (palanca per a tuneo).
// 1.0 = inèrcia completa; >1.0 amplifica la deriva; <1.0 la atenua.
constexpr float ENEMY_VELOCITY_INHERITANCE = 1.0F;
// Velocity heredada de la nau a l'explosió (80% del feel original).
constexpr float SHIP_VELOCITY_INHERITANCE = 0.8F;
// Tuneig específic de l'explosió d'enemic (overrides als defaults
// que es passen com a paràmetres opcionals a explode()).
constexpr float ENEMY_LIFETIME = 2.5F; // Vida mínima del debris (s) — els que segueixen movent-se viuen més
constexpr float ENEMY_FRICTION = -30.0F; // Fricció més suau perquè s'estenguin més
constexpr int ENEMY_SEGMENT_MULTIPLIER = 1; // Sense còpies (5 cares = 5 trossos); >1 produeix grups sincronitzats
// Velocity heredada de l'enemic a l'explosió (palanca per a tuneo).
// 1.0 = inèrcia completa; >1.0 amplifica la deriva; <1.0 la atenua.
constexpr float ENEMY_VELOCITY_INHERITANCE = 1.0F;
// Angular velocity sin for trajectory inheritance
// Excess above this threshold is converted to tangential linear velocity
// Prevents "vortex trap" problem with high-rotation enemies
constexpr float SPEED_ROT_MAX = 1.5F; // rad/s (~86°/s)
// Tuneig específic de l'explosió d'enemic (overrides als defaults
// que es passen com a paràmetres opcionals a explode()).
constexpr float ENEMY_LIFETIME = 2.5F; // Vida mínima del debris (s) — els que segueixen movent-se viuen més
constexpr float ENEMY_FRICTION = -30.0F; // Fricció més suau perquè s'estenguin més
constexpr int ENEMY_SEGMENT_MULTIPLIER = 1; // Sense còpies (5 cares = 5 trossos); >1 produeix grups sincronitzats
// Angular velocity sin for trajectory inheritance
// Excess above this threshold is converted to tangential linear velocity
// Prevents "vortex trap" problem with high-rotation enemies
constexpr float SPEED_ROT_MAX = 1.5F; // rad/s (~86°/s)
} // namespace Debris
} // namespace Defaults::Physics
} // namespace Defaults::Physics::Debris
+63
View File
@@ -0,0 +1,63 @@
// service_menu.hpp - Constants del menu de servei (F12)
// © 2026 JailDesigner
#pragma once
#include <SDL3/SDL.h>
namespace Defaults::ServiceMenu {
// ---- Mides en coordenades logiques del joc (1280×720) ----
// BOX_WIDTH_MIN es el minim: si el titol o algun item no hi caben, el
// marc s'expandeix dinamicament amb animacio (cf. WIDTH_RATE).
constexpr int BOX_WIDTH_MIN = 460;
constexpr int GAP_Y = 22;
constexpr int TITLE_HEIGHT = 36; // scale 0.85 ≈ 34 px de text
constexpr int SUBTITLE_HEIGHT = 18; // scale 0.4 ≈ 16 px de text
constexpr int SEPARATOR_HEIGHT = 1;
constexpr int ITEM_HEIGHT = 38; // scale 0.55 ≈ 22 px de text + padding per al highlight
constexpr int ITEM_GAP_Y = 6;
// Brackets als 4 cantons (substitueixen la vora completa: estètica sci-fi).
constexpr int CORNER_ARM_H = 48;
constexpr int CORNER_ARM_V = 28;
constexpr int CORNER_THICKNESS = 2;
// ---- Animacio open/close (mateixos parametres que aee_arcade) ----
constexpr float OPEN_SPEED = 8.0F; // ~125 ms a obrir
constexpr float CLOSE_SPEED = 10.0F; // ~100 ms a tancar
constexpr float HEIGHT_RATE = 12.0F; // smoothing exponencial de l'alçada de la caixa
constexpr float WIDTH_RATE = 12.0F; // smoothing per a canvis d'ample entre pagines
// ---- Animacio del highlight (rectangle del cursor) ----
// Rate=18 dona settling ~0.17 s al 95% (ease-out exponencial).
constexpr float HIGHLIGHT_RATE = 18.0F;
constexpr int HIGHLIGHT_TICK_LEN = 10; // longitud dels ticks a cada cantonada
constexpr int HIGHLIGHT_THICKNESS = 1;
constexpr int HIGHLIGHT_PAD_X = 18; // padding lateral del rect respecte al text
constexpr int HIGHLIGHT_PAD_Y = 4; // padding vertical
constexpr int TEXT_INSET_X = 16; // marge intern del text dins del highlight (label esq / valor dre)
constexpr int MIN_LABEL_VALUE_GAP = 30; // mínim gap entre label i valor (per al càlcul d'ample dinàmic)
// ---- Colors RGBA ----
constexpr SDL_Color BG_COLOR{.r = 0, .g = 12, .b = 24, .a = 215};
constexpr SDL_Color CORNER_COLOR{.r = 120, .g = 220, .b = 255, .a = 255}; // cian neon
constexpr SDL_Color TITLE_COLOR{.r = 200, .g = 240, .b = 255, .a = 255};
constexpr SDL_Color SUBTITLE_COLOR{.r = 110, .g = 170, .b = 210, .a = 220}; // cian apagat
constexpr SDL_Color SEPARATOR_COLOR{.r = 60, .g = 120, .b = 180, .a = 180};
constexpr SDL_Color LABEL_COLOR{.r = 170, .g = 210, .b = 240, .a = 255};
constexpr SDL_Color CURSOR_COLOR{.r = 255, .g = 230, .b = 120, .a = 255}; // groc per al text sel·leccionat
constexpr SDL_Color HIGHLIGHT_OUTLINE{.r = 255, .g = 230, .b = 120, .a = 255}; // mateix groc, opac
constexpr SDL_Color HIGHLIGHT_FILL{.r = 255, .g = 230, .b = 120, .a = 36}; // wash translucid
// ---- Tipografia (VectorText). Scale 1.0 = caracter 20×40 px ----
constexpr float TITLE_SCALE = 0.85F; // mateixa escala que el HUD del scoreboard
constexpr float SUBTITLE_SCALE = 0.40F; // sota el titol, info decorativa (versio/hash)
constexpr float ITEM_SCALE = 0.55F; // mateixa escala que les notificacions
constexpr float TEXT_SPACING = 2.0F;
// ---- Sons UI (relatius a data/sounds/), portats d'aee_arcade ----
constexpr const char* SELECT_SOUND = "ui/menu_select.wav";
constexpr const char* ACCEPT_SOUND = "ui/menu_accept.wav";
} // namespace Defaults::ServiceMenu
-33
View File
@@ -1,33 +0,0 @@
// ship.hpp - Configuració de la nau (invulnerabilitat, parpelleig)
// © 2026 JailDesigner
#pragma once
namespace Defaults::Ship {
// Invulnerabilidad post-respawn
constexpr float INVULNERABILITY_DURATION = 3.0F; // Segundos de invulnerabilidad
// Parpadeo visual durante invulnerabilidad
constexpr float BLINK_VISIBLE_TIME = 0.1F; // Tiempo visible (segundos)
constexpr float BLINK_INVISIBLE_TIME = 0.1F; // Tiempo invisible (segundos)
// Frecuencia total: 0.2s/ciclo = 5 Hz (~15 parpadeos en 3s)
// Cuerpo físico
constexpr float MASS = 10.0F; // Masa de referencia para choques
constexpr float RESTITUTION = 0.6F; // Rebote moderado contra paredes
constexpr float LINEAR_DAMPING = 1.5F; // Fricción exponencial (s⁻¹)
constexpr float ANGULAR_DAMPING = 0.0F; // Rotación 100% por input (no inercial)
// Empuje visual: escala proporcional a la velocidad (0..200 px/s → 1.0..1.5)
// Mantiene la sensación del Pascal original.
constexpr float VISUAL_PUSH_DIVISOR = 33.33F; // SPEED / DIVISOR = empuje visual
constexpr float VISUAL_SCALE_DIVISOR = 12.0F; // SCALE = 1 + (PUSH / DIVISOR)
// Estat "ferit": entre primera col·lisió amb enemic i recuperació o segona col·lisió mortal.
namespace Hurt {
constexpr float DURATION = 15.0F; // Segons en estat ferit (provisional)
constexpr float BLINK_HZ = 10.0F; // Freqüència parpelleig color normal ↔ ferit
} // namespace Hurt
} // namespace Defaults::Ship
+1 -1
View File
@@ -7,7 +7,7 @@ namespace Defaults::Trail {
constexpr int POOL_SIZE = 200;
constexpr float SPEED_THRESHOLD_PX_S = 54.0F; // 30% de Physics::MAX_VELOCITY (180)
constexpr float SPEED_THRESHOLD_PX_S = 54.0F; // 30% de player.yaml::physics.max_velocity (180 px/s)
constexpr float EMIT_INTERVAL_S = 0.04F; // ~25 Hz nominal
constexpr float EMIT_JITTER_S = 0.015F; // ±15 ms al cooldown
constexpr float POSITION_JITTER_PX = 2.5F; // jitter al punt de naixement
+56
View File
@@ -0,0 +1,56 @@
// entity_loader.cpp - Implementació del carregador d'entitats YAML
// © 2026 JailDesigner
#include "core/entities/entity_loader.hpp"
#include <cstdint>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "core/resources/resource_helper.hpp"
namespace Entities {
std::unordered_map<std::string, std::shared_ptr<fkyaml::node>> EntityLoader::cache;
auto EntityLoader::load(const std::string& name) -> std::shared_ptr<fkyaml::node> {
// Cache hit
auto it = cache.find(name);
if (it != cache.end()) {
std::cout << "[EntityLoader] Cache hit: " << name << '\n';
return it->second;
}
const std::string PATH = "entities/" + name + "/" + name + ".yaml";
std::vector<uint8_t> data = Resource::Helper::loadFile(PATH);
if (data.empty()) {
std::cerr << "[EntityLoader] Error: no s'ha pogut load " << PATH << '\n';
return nullptr;
}
try {
std::string yaml_content(data.begin(), data.end());
std::stringstream stream(yaml_content);
auto node = std::make_shared<fkyaml::node>(fkyaml::node::deserialize(stream));
std::cout << "[EntityLoader] Carregat: " << PATH << '\n';
cache[name] = node;
return node;
} catch (const std::exception& e) {
std::cerr << "[EntityLoader] Excepció parsejant " << PATH << ": " << e.what() << '\n';
return nullptr;
}
}
void EntityLoader::clearCache() {
std::cout << "[EntityLoader] Netejant caché (" << cache.size() << " entitats)" << '\n';
cache.clear();
}
auto EntityLoader::getCacheSize() -> size_t { return cache.size(); }
} // namespace Entities
+38
View File
@@ -0,0 +1,38 @@
// entity_loader.hpp - Carregador genèric de descriptors d'entitats en YAML
// © 2026 JailDesigner
//
// Cada entitat viu a `data/entities/<name>/<name>.yaml` (mateix patró que el
// projecte germà aee_arcade). Aquest loader resol el path, llegeix del
// resource pack via Resource::Helper, parseja amb fkyaml i cacheja el node
// per evitar relectures. Retorna nullptr en cas d'error (el caller decideix
// si abortar).
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include "external/fkyaml_node.hpp"
namespace Entities {
class EntityLoader {
public:
EntityLoader() = delete; // tot estàtic
// Carrega el descriptor d'una entitat per nom (ex. "player" →
// "entities/player/player.yaml"). Retorna nullptr si no es pot
// carregar o parsejar. Cachejat per nom.
static auto load(const std::string& name) -> std::shared_ptr<fkyaml::node>;
// Buidar caché (útil per debug/recàrrega).
static void clearCache();
[[nodiscard]] static auto getCacheSize() -> size_t;
private:
static std::unordered_map<std::string, std::shared_ptr<fkyaml::node>> cache;
};
} // namespace Entities
+2 -1
View File
@@ -53,7 +53,8 @@ namespace Graphics {
// Cache and return
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->getName()
<< ", " << shape->getNumPrimitives() << " primitives)" << '\n';
<< ", " << shape->getNumPrimitives() << " primitives, bounding_radius="
<< shape->getBoundingRadius() << ")" << '\n';
cache[filename] = shape;
return shape;
+7 -1
View File
@@ -47,7 +47,7 @@ namespace Graphics {
}
// Cargar símbolos
const std::string SYMBOLS[] = {".", ",", "-", ":", "!", "?"};
const std::string SYMBOLS[] = {".", ",", "-", ":", "!", "?", "/", "(", ")"};
for (const auto& sym : SYMBOLS) {
char c = sym[0];
std::string filename = getShapeFilename(c);
@@ -164,6 +164,12 @@ namespace Graphics {
return "font/char_exclamation.shp";
case '?':
return "font/char_question.shp";
case '/':
return "font/char_slash.shp";
case '(':
return "font/char_lparen.shp";
case ')':
return "font/char_rparen.shp";
case ' ':
return ""; // Espai es maneja sin load shape
+1 -1
View File
@@ -21,7 +21,7 @@ namespace Graphics {
// Renderizar string completo
// - text: cadena a renderizar (soporta: A-Z, a-z, 0-9, '.', ',', '-', ':',
// '!', '?', ' ')
// '!', '?', '/', '(', ')', ' ')
// - position: posición inicial (esquina superior izquierda)
// - scale: factor de scale (1.0 = 20×40 px por carácter)
// - spacing: espacio entre caracteres en píxeles (a scale 1.0)
+405
View File
@@ -0,0 +1,405 @@
// define_inputs.cpp - Implementacio de l'overlay modal de redefinicio
// © 2026 JailDesigner
#include "core/input/define_inputs.hpp"
#include <algorithm>
#include <format>
#include <string>
#include <vector>
#include "core/audio/audio.hpp"
#include "core/defaults/service_menu.hpp"
#include "core/input/input.hpp"
#include "core/locale/locale.hpp"
#include "core/types.hpp"
#include "game/config_yaml.hpp"
namespace {
constexpr float CANVAS_W = 1280.0F;
constexpr float CANVAS_H = 720.0F;
// Llindar de trigger per a edge-detect L2/R2 com a boto virtual.
constexpr Sint16 TRIGGER_THRESHOLD = 16384;
// Codis virtuals per als triggers (consistents amb input_types.cpp).
constexpr int TRIGGER_L2_VIRTUAL = 100;
constexpr int TRIGGER_R2_VIRTUAL = 101;
// Durada del missatge de confirmacio abans de tancar-se.
constexpr float COMPLETE_DISPLAY_S = 1.5F;
// Llindar dpad als axis sticks: no es captura per evitar conflicte amb el
// moviment LEFT/RIGHT/UP/DOWN (que es presuposen no redefinibles al mando).
constexpr Sint16 STICK_THRESHOLD = 16384;
// Crida pushRect amb un SDL_Color (les components s'escalen a [0..1]).
void fillRect(Rendering::Renderer* renderer, float x, float y, float w, float h, SDL_Color color) {
renderer->pushRect(x, y, w, h, static_cast<float>(color.r) / 255.0F, static_cast<float>(color.g) / 255.0F, static_cast<float>(color.b) / 255.0F, static_cast<float>(color.a) / 255.0F);
}
auto titleKey(System::DefineInputs::Mode mode, System::DefineInputs::Player player) -> std::string {
const bool IS_KB = (mode == System::DefineInputs::Mode::KEYBOARD);
const bool IS_P1 = (player == System::DefineInputs::Player::P1);
if (IS_KB && IS_P1) {
return "define.title_keyboard_p1";
}
if (IS_KB) {
return "define.title_keyboard_p2";
}
if (IS_P1) {
return "define.title_gamepad_p1";
}
return "define.title_gamepad_p2";
}
// Scancodes que MAI capturem com a binding (reservats per a navegacio o
// global hotkeys). Tornen true → handleEvent les deixa passar al pipeline
// global perque facin la seua feina (ESC obre el prompt d'eixida, F1-F12
// son hotkeys de sistema, RETURN/BACKSPACE/TAB son navegacio).
auto isReservedScancode(SDL_Scancode sc) -> bool {
if (sc == SDL_SCANCODE_ESCAPE) {
return true;
}
if (sc >= SDL_SCANCODE_F1 && sc <= SDL_SCANCODE_F12) {
return true;
}
if (sc == SDL_SCANCODE_RETURN || sc == SDL_SCANCODE_KP_ENTER) {
return true;
}
if (sc == SDL_SCANCODE_BACKSPACE || sc == SDL_SCANCODE_TAB) {
return true;
}
return false;
}
// Conversio sense pèrdua de SDL_Scancode → int per a comparacions
// homogenies dins de sequence_ (que guarda codis de tots dos modes).
auto scancodeToInt(SDL_Scancode sc) -> int {
return static_cast<int>(sc);
}
} // namespace
namespace System {
std::unique_ptr<DefineInputs> DefineInputs::instance;
void DefineInputs::init(Rendering::Renderer* renderer) {
if (!instance) {
instance = std::unique_ptr<DefineInputs>(new DefineInputs(renderer));
}
}
void DefineInputs::destroy() { instance.reset(); }
auto DefineInputs::get() -> DefineInputs* { return instance.get(); }
DefineInputs::DefineInputs(Rendering::Renderer* renderer)
: renderer_(renderer),
text_(renderer) {}
auto DefineInputs::isActive() const -> bool {
return phase_ != Phase::INACTIVE;
}
auto DefineInputs::begin(Mode mode, Player player) -> bool {
if (mode == Mode::GAMEPAD) {
// Requereix un pad assignat al jugador.
const auto* input = Input::get();
if (input == nullptr) {
return false;
}
const int IDX = (player == Player::P1) ? 0 : 1;
if (input->getPlayerGamepad(IDX) == nullptr) {
return false;
}
}
mode_ = mode;
player_ = player;
index_ = 0;
complete_timer_s_ = 0.0F;
l2_was_pressed_ = false;
r2_was_pressed_ = false;
buildSequence();
phase_ = Phase::CAPTURING;
return true;
}
void DefineInputs::cancel() {
phase_ = Phase::INACTIVE;
sequence_.clear();
index_ = 0;
complete_timer_s_ = 0.0F;
}
void DefineInputs::buildSequence() {
sequence_.clear();
if (mode_ == Mode::KEYBOARD) {
// Teclat: LEFT, RIGHT, FIRE (SHOOT), ACCELERATE (THRUST)
sequence_.push_back({.action_label_key = "define.action.left", .action = InputAction::LEFT, .captured = -1});
sequence_.push_back({.action_label_key = "define.action.right", .action = InputAction::RIGHT, .captured = -1});
sequence_.push_back({.action_label_key = "define.action.fire", .action = InputAction::SHOOT, .captured = -1});
sequence_.push_back({.action_label_key = "define.action.accelerate", .action = InputAction::THRUST, .captured = -1});
} else {
// Mando: FIRE, ACCELERATE, START, MENU
sequence_.push_back({.action_label_key = "define.action.fire", .action = InputAction::SHOOT, .captured = -1});
sequence_.push_back({.action_label_key = "define.action.accelerate", .action = InputAction::THRUST, .captured = -1});
sequence_.push_back({.action_label_key = "define.action.start", .action = InputAction::START, .captured = -1});
sequence_.push_back({.action_label_key = "define.action.menu", .action = InputAction::MENU, .captured = -1});
}
}
auto DefineInputs::isInUse(int code) const -> bool {
return std::ranges::any_of(sequence_, [code](const Step& s) {
return s.captured == code;
});
}
void DefineInputs::captureAndAdvance(int code) {
if (index_ >= sequence_.size()) {
return;
}
if (isInUse(code)) {
return; // Duplicat dins de la sessio: rebutgem silenciosament
}
sequence_[index_].captured = code;
++index_;
if (auto* audio = Audio::get(); audio != nullptr) {
audio->playSound(Defaults::ServiceMenu::ACCEPT_SOUND, Audio::Group::INTERFACE);
}
if (index_ >= sequence_.size()) {
persistAndComplete();
}
}
void DefineInputs::persistAndComplete() {
auto& cfg = (player_ == Player::P1)
? ConfigYaml::engine_config.player1
: ConfigYaml::engine_config.player2;
if (mode_ == Mode::KEYBOARD) {
for (const Step& s : sequence_) {
switch (s.action) {
case InputAction::LEFT:
cfg.keyboard.key_left = static_cast<SDL_Scancode>(s.captured);
break;
case InputAction::RIGHT:
cfg.keyboard.key_right = static_cast<SDL_Scancode>(s.captured);
break;
case InputAction::SHOOT:
cfg.keyboard.key_shoot = static_cast<SDL_Scancode>(s.captured);
break;
case InputAction::THRUST:
cfg.keyboard.key_thrust = static_cast<SDL_Scancode>(s.captured);
break;
default:
break; // START / MENU no es redefineixen al teclat
}
}
} else {
for (const Step& s : sequence_) {
switch (s.action) {
case InputAction::SHOOT:
cfg.gamepad.button_shoot = s.captured;
break;
case InputAction::THRUST:
cfg.gamepad.button_thrust = s.captured;
break;
case InputAction::START:
cfg.gamepad.button_start = s.captured;
break;
case InputAction::MENU:
cfg.gamepad.button_menu = s.captured;
break;
default:
break; // LEFT / RIGHT no es redefineixen al mando
}
}
}
// Aplicar canvis al runtime de l'Input i persistir a disc.
if (auto* input = Input::get(); input != nullptr) {
if (player_ == Player::P1) {
input->applyPlayer1Bindings(ConfigYaml::engine_config.player1);
} else {
input->applyPlayer2Bindings(ConfigYaml::engine_config.player2);
}
}
ConfigYaml::saveToFile();
phase_ = Phase::COMPLETE;
complete_timer_s_ = COMPLETE_DISPLAY_S;
}
void DefineInputs::update(float delta_time) {
if (phase_ != Phase::COMPLETE) {
return;
}
complete_timer_s_ -= delta_time;
if (complete_timer_s_ <= 0.0F) {
cancel();
}
}
void DefineInputs::processTrigger(int virtual_button, bool& was_pressed, bool now) {
if (now && !was_pressed) {
captureAndAdvance(virtual_button);
}
was_pressed = now;
}
auto DefineInputs::handleKeyboardEvent(const SDL_Event& event) -> bool {
if (event.type != SDL_EVENT_KEY_DOWN) {
return true; // Empassem la resta sense fer res
}
const SDL_Scancode SC = event.key.scancode;
if (isReservedScancode(SC)) {
// ESC, F1-F12, RETURN, BACKSPACE, TAB es deixen passar al pipeline
// global (ESC obre el prompt d'eixida; F1-F12 hotkeys, etc.).
return false;
}
captureAndAdvance(scancodeToInt(SC));
return true;
}
auto DefineInputs::handleGamepadEvent(const SDL_Event& event) -> bool {
// KEY_DOWN no es per al rebind de mando: deixem que el global el
// gestioni (ex. ESC → prompt d'eixida, F12 → tanca menu, etc.).
if (event.type == SDL_EVENT_KEY_DOWN) {
return false;
}
// Filtrar events al pad del jugador actiu.
const auto* input = Input::get();
if (input == nullptr) {
return true;
}
const int IDX = (player_ == Player::P1) ? 0 : 1;
auto pad = input->getPlayerGamepad(IDX);
if (!pad) {
return true;
}
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
if (event.gbutton.which != pad->instance_id) {
return true;
}
captureAndAdvance(static_cast<int>(event.gbutton.button));
return true;
}
if (event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION) {
if (event.gaxis.which != pad->instance_id) {
return true;
}
const auto AXIS = static_cast<SDL_GamepadAxis>(event.gaxis.axis);
const Sint16 VAL = event.gaxis.value;
if (AXIS == SDL_GAMEPAD_AXIS_LEFT_TRIGGER) {
processTrigger(TRIGGER_L2_VIRTUAL, l2_was_pressed_, VAL >= TRIGGER_THRESHOLD);
} else if (AXIS == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
processTrigger(TRIGGER_R2_VIRTUAL, r2_was_pressed_, VAL >= TRIGGER_THRESHOLD);
}
// Sticks LEFTX/LEFTY/RIGHTX/RIGHTY: ignorats (no son redefinibles).
(void)STICK_THRESHOLD;
return true;
}
return true;
}
auto DefineInputs::handleEvent(const SDL_Event& event) -> bool {
if (phase_ == Phase::INACTIVE) {
return false;
}
// SDL_EVENT_QUIT i WINDOW_CLOSE_REQUESTED han de poder tancar la
// finestra encara que el modal estiga obert; els passem al pipeline.
if (event.type == SDL_EVENT_QUIT ||
event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
return false;
}
if (phase_ == Phase::COMPLETE) {
// Mentre mostrem el missatge OK, empassem la resta d'events sense
// capturar perque l'usuari no puga avançar accions sense voler.
return true;
}
if (mode_ == Mode::KEYBOARD) {
return handleKeyboardEvent(event);
}
return handleGamepadEvent(event);
}
void DefineInputs::draw() const {
if (phase_ == Phase::INACTIVE) {
return;
}
using namespace Defaults::ServiceMenu;
// Caixa centrada, dimensions fixes (no depen del contingut a redefinir).
constexpr float BOX_W = 560.0F;
constexpr float BOX_H = 280.0F;
const float BOX_X = (CANVAS_W - BOX_W) * 0.5F;
const float BOX_Y = (CANVAS_H - BOX_H) * 0.5F;
// Fons + brackets als 4 cantons (estil HUD del menu de servei).
fillRect(renderer_, BOX_X, BOX_Y, BOX_W, BOX_H, BG_COLOR);
const auto T = static_cast<float>(CORNER_THICKNESS);
const auto AH = static_cast<float>(CORNER_ARM_H);
const auto AV = static_cast<float>(CORNER_ARM_V);
fillRect(renderer_, BOX_X, BOX_Y, AH, T, CORNER_COLOR);
fillRect(renderer_, BOX_X, BOX_Y, T, AV, CORNER_COLOR);
fillRect(renderer_, BOX_X + BOX_W - AH, BOX_Y, AH, T, CORNER_COLOR);
fillRect(renderer_, BOX_X + BOX_W - T, BOX_Y, T, AV, CORNER_COLOR);
fillRect(renderer_, BOX_X, BOX_Y + BOX_H - T, AH, T, CORNER_COLOR);
fillRect(renderer_, BOX_X, BOX_Y + BOX_H - AV, T, AV, CORNER_COLOR);
fillRect(renderer_, BOX_X + BOX_W - AH, BOX_Y + BOX_H - T, AH, T, CORNER_COLOR);
fillRect(renderer_, BOX_X + BOX_W - T, BOX_Y + BOX_H - AV, T, AV, CORNER_COLOR);
const std::string TITLE = Locale::get().text(titleKey(mode_, player_));
const float TITLE_W = Graphics::VectorText::getTextWidth(TITLE, TITLE_SCALE, TEXT_SPACING);
const float TITLE_X = BOX_X + ((BOX_W - TITLE_W) * 0.5F);
const float TITLE_Y = BOX_Y + 26.0F;
text_.render(TITLE, Vec2{.x = TITLE_X, .y = TITLE_Y}, TITLE_SCALE, TEXT_SPACING, 1.0F, TITLE_COLOR);
if (phase_ == Phase::COMPLETE) {
const std::string OK = Locale::get().text("define.complete");
constexpr float OK_SCALE = 0.7F;
const float OK_W = Graphics::VectorText::getTextWidth(OK, OK_SCALE, TEXT_SPACING);
const float OK_X = BOX_X + ((BOX_W - OK_W) * 0.5F);
const float OK_Y = BOX_Y + (BOX_H * 0.5F) - 10.0F;
constexpr SDL_Color OK_COLOR{.r = 120, .g = 255, .b = 140, .a = 255};
text_.render(OK, Vec2{.x = OK_X, .y = OK_Y}, OK_SCALE, TEXT_SPACING, 1.0F, OK_COLOR);
return;
}
// Instruccio (premeu tecla / boto) + accio actual + progres.
const std::string PROMPT = Locale::get().text(
mode_ == Mode::KEYBOARD ? "define.press_key" : "define.press_button");
const float PROMPT_W = Graphics::VectorText::getTextWidth(PROMPT, ITEM_SCALE, TEXT_SPACING);
const float PROMPT_X = BOX_X + ((BOX_W - PROMPT_W) * 0.5F);
const float PROMPT_Y = BOX_Y + 86.0F;
text_.render(PROMPT, Vec2{.x = PROMPT_X, .y = PROMPT_Y}, ITEM_SCALE, TEXT_SPACING, 1.0F, SUBTITLE_COLOR);
if (index_ < sequence_.size()) {
const std::string ACTION = Locale::get().text(sequence_[index_].action_label_key);
constexpr float ACTION_SCALE = 0.9F;
const float ACTION_W = Graphics::VectorText::getTextWidth(ACTION, ACTION_SCALE, TEXT_SPACING);
const float ACTION_X = BOX_X + ((BOX_W - ACTION_W) * 0.5F);
const float ACTION_Y = BOX_Y + 130.0F;
text_.render(ACTION, Vec2{.x = ACTION_X, .y = ACTION_Y}, ACTION_SCALE, TEXT_SPACING, 1.0F, CURSOR_COLOR);
}
const std::string PROGRESS = std::format("{}/{}", index_ + 1, sequence_.size());
constexpr float PROG_SCALE = 0.4F;
const float PROG_W = Graphics::VectorText::getTextWidth(PROGRESS, PROG_SCALE, TEXT_SPACING);
const float PROG_X = BOX_X + ((BOX_W - PROG_W) * 0.5F);
const float PROG_Y = BOX_Y + 200.0F;
text_.render(PROGRESS, Vec2{.x = PROG_X, .y = PROG_Y}, PROG_SCALE, TEXT_SPACING, 1.0F, LABEL_COLOR);
}
} // namespace System
+107
View File
@@ -0,0 +1,107 @@
// define_inputs.hpp - Overlay modal de redefinici de controls (singleton)
// © 2026 JailDesigner
//
// Sub-mòdul inspirat en aee_arcade/source/core/input/define_buttons. Quan el
// menú de servei dispara una acció "Redefinir tecles/botons P1/P2", aquest
// singleton pren el control: pinta una caixa central, captura events SDL i
// avança per una seqüència fixa d'accions, persistint les noves assignacions
// a config.yaml en acabar.
//
// Cicle de vida:
// 1. begin(mode, player) → construeix la seqüència (4 passos) i activa
// l'overlay. Per a GAMEPAD, retorna false si el jugador no té pad.
// 2. handleEvent() captura el següent event vàlid; ESC cancel·la sense
// desar; duplicats dins de la sessió es rebutgen silenciosament.
// 3. Quan la seqüència es completa, persistim a engine_config + saveToFile,
// reapliquem els bindings i mostrem un missatge "OK" durant 1.5 s
// abans d'auto-tancar-se.
//
// El routing d'events es fa des de GlobalEvents::handle: mentre isActive()
// retorna true, tots els events SDL es desvien aquí i no arriben al joc ni
// al menú de servei.
#pragma once
#include <SDL3/SDL.h>
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "core/graphics/vector_text.hpp"
#include "core/input/input_types.hpp"
#include "core/rendering/render_context.hpp"
namespace System {
class DefineInputs {
public:
enum class Mode : std::uint8_t { KEYBOARD,
GAMEPAD };
enum class Player : std::uint8_t { P1,
P2 };
static void init(Rendering::Renderer* renderer);
static void destroy();
[[nodiscard]] static auto get() -> DefineInputs*;
// Comença la sessió. Retorna false per a GAMEPAD si el jugador no té
// cap pad assignat (el caller hauria de notificar a l'usuari abans).
auto begin(Mode mode, Player player) -> bool;
void cancel();
[[nodiscard]] auto isActive() const -> bool;
void update(float delta_time);
void draw() const;
// Retorna true si l'event s'ha consumit (és a dir, mentre l'overlay
// és actiu sempre consumeix tot per evitar passages al joc o menú).
auto handleEvent(const SDL_Event& event) -> bool;
private:
explicit DefineInputs(Rendering::Renderer* renderer);
enum class Phase : std::uint8_t {
INACTIVE,
CAPTURING,
COMPLETE, // mostra missatge OK breu abans d'auto-cancel
};
struct Step {
std::string action_label_key; // p.ex. "define.action.left"
InputAction action; // mapeig a la struct PlayerBindings
int captured{-1}; // scancode o button code; -1 = sense capturar
};
void buildSequence();
[[nodiscard]] auto isInUse(int code) const -> bool;
void captureAndAdvance(int code);
void persistAndComplete();
// Handlers especialitzats segons mode_.
auto handleKeyboardEvent(const SDL_Event& event) -> bool;
auto handleGamepadEvent(const SDL_Event& event) -> bool;
// Edge-detect per als triggers L2/R2 com a botons virtuals.
void processTrigger(int virtual_button, bool& was_pressed, bool now);
Rendering::Renderer* renderer_;
Graphics::VectorText text_;
Phase phase_{Phase::INACTIVE};
Mode mode_{Mode::KEYBOARD};
Player player_{Player::P1};
std::vector<Step> sequence_;
std::size_t index_{0};
float complete_timer_s_{0.0F};
// Estat d'edge-detect dels triggers durant la sessió GAMEPAD.
bool l2_was_pressed_{false};
bool r2_was_pressed_{false};
static std::unique_ptr<DefineInputs> instance;
};
} // namespace System
+84 -18
View File
@@ -8,6 +8,10 @@
#include <unordered_map> // Para unordered_map, _Node_iterator, operator==, _Node_iterator_base, _Node_const_iterator
#include <utility> // Para move
#include "core/locale/locale.hpp"
#include "core/system/notifier.hpp"
#include "core/utils/string_utils.hpp"
// Singleton
Input* Input::instance = nullptr;
@@ -373,9 +377,25 @@ void Input::update() {
// --- MANDOS ---
for (const auto& gamepad : gamepads_) {
// LEFT i RIGHT NO son redefinibles al mando (assumits dpad o stick).
// Llegim el left stick X i el fusionem amb l'estat del dpad: qualsevol
// de les dos fonts activa l'accio. Llindar AXIS_THRESHOLD (30000).
const Sint16 STICK_X = SDL_GetGamepadAxis(gamepad->pad, SDL_GAMEPAD_AXIS_LEFTX);
const bool STICK_LEFT = STICK_X < -AXIS_THRESHOLD;
const bool STICK_RIGHT = STICK_X > AXIS_THRESHOLD;
for (auto& binding : gamepad->bindings) {
bool button_is_down_now = static_cast<int>(SDL_GetGamepadButton(gamepad->pad, static_cast<SDL_GamepadButton>(binding.second.button))) != 0;
// Per a LEFT/RIGHT, fer un OR amb el stick X. La resta d'accions
// (THRUST/SHOOT/START/MENU) ignoren el stick aqui — si es vol
// dispar amb trigger L2/R2 cal binding amb codi 100/101.
if (binding.first == Action::LEFT) {
button_is_down_now = button_is_down_now || STICK_LEFT;
} else if (binding.first == Action::RIGHT) {
button_is_down_now = button_is_down_now || STICK_RIGHT;
}
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo
binding.second.just_pressed = button_is_down_now && !binding.second.is_held;
binding.second.is_held = button_is_down_now;
@@ -407,6 +427,16 @@ auto Input::addGamepad(int device_index) -> std::string {
auto name = gamepad->name;
std::cout << "Gamepad connected (" << name << ")" << '\n';
gamepads_.push_back(std::move(gamepad));
// Toast a pantalla. Pot ser nullptr durant discoverGamepads() inicial
// (l'Input::init() es crida abans que el Director instanciï el Notifier).
if (auto* notifier = System::Notifier::get(); notifier != nullptr) {
notifier->notifyInfo(localeSubstitute(
Locale::get().text("notification.gamepad_connected"),
"{name}",
Utils::toUpperAscii(name)));
}
return name + " CONNECTED";
}
@@ -419,6 +449,14 @@ auto Input::removeGamepad(SDL_JoystickID id) -> std::string {
std::string name = (*it)->name;
std::cout << "Gamepad disconnected (" << name << ")" << '\n';
gamepads_.erase(it);
if (auto* notifier = System::Notifier::get(); notifier != nullptr) {
notifier->notifyInfo(localeSubstitute(
Locale::get().text("notification.gamepad_disconnected"),
"{name}",
Utils::toUpperAscii(name)));
}
return name + " DISCONNECTED";
}
std::cerr << "No se encontró el gamepad con ID " << id << '\n';
@@ -465,6 +503,33 @@ auto Input::findAvailableGamepadByName(const std::string& gamepad_name) -> std::
// ========== MÉTODOS ESPECÍFICOS POR JUGADOR (ORNI) ==========
// Cerca el gamepad assignat a un jugador. Prioritat path > name. Si els
// dos camps venen buits o no n'hi ha cap match retornem nullptr (sense
// mando explicit). L'autoassignacio inicial es resol al boot.
auto Input::resolvePlayerGamepad(const Config::PlayerBindings& bindings) -> std::shared_ptr<Gamepad> {
if (gamepads_.empty()) {
return nullptr;
}
if (!bindings.gamepad_path.empty()) {
for (const auto& pad : gamepads_) {
if (pad && pad->path == bindings.gamepad_path) {
return pad;
}
}
}
if (!bindings.gamepad_name.empty()) {
for (const auto& pad : gamepads_) {
if (pad && pad->name == bindings.gamepad_name) {
return pad;
}
}
}
return nullptr;
}
// Aplica configuración de controles del player 1
void Input::applyPlayer1Bindings(const Config::PlayerBindings& bindings) {
// 1. Aplicar bindings de teclado (NO usar bindKey, llenar mapa específico)
@@ -474,15 +539,8 @@ void Input::applyPlayer1Bindings(const Config::PlayerBindings& bindings) {
player1_keyboard_bindings_[Action::SHOOT].scancode = bindings.keyboard.key_shoot;
player1_keyboard_bindings_[Action::START].scancode = bindings.keyboard.key_start;
// 2. Encontrar gamepad por nombre (o usar primer gamepad como fallback)
std::shared_ptr<Gamepad> gamepad = nullptr;
if (bindings.gamepad_name.empty()) {
// Fallback: usar primer gamepad disponible
gamepad = (!gamepads_.empty()) ? gamepads_[0] : nullptr;
} else {
// Buscar por nombre
gamepad = findAvailableGamepadByName(bindings.gamepad_name);
}
// 2. Resoldre gamepad per path/name
std::shared_ptr<Gamepad> gamepad = resolvePlayerGamepad(bindings);
if (!gamepad) {
player1_gamepad_ = nullptr;
@@ -494,6 +552,8 @@ void Input::applyPlayer1Bindings(const Config::PlayerBindings& bindings) {
gamepad->bindings[Action::RIGHT].button = bindings.gamepad.button_right;
gamepad->bindings[Action::THRUST].button = bindings.gamepad.button_thrust;
gamepad->bindings[Action::SHOOT].button = bindings.gamepad.button_shoot;
gamepad->bindings[Action::START].button = bindings.gamepad.button_start;
gamepad->bindings[Action::MENU].button = bindings.gamepad.button_menu;
// 4. Cachear referencia
player1_gamepad_ = gamepad;
@@ -508,15 +568,8 @@ void Input::applyPlayer2Bindings(const Config::PlayerBindings& bindings) {
player2_keyboard_bindings_[Action::SHOOT].scancode = bindings.keyboard.key_shoot;
player2_keyboard_bindings_[Action::START].scancode = bindings.keyboard.key_start;
// 2. Encontrar gamepad por nombre (o usar segundo gamepad como fallback)
std::shared_ptr<Gamepad> gamepad = nullptr;
if (bindings.gamepad_name.empty()) {
// Fallback: usar segundo gamepad disponible
gamepad = (gamepads_.size() > 1) ? gamepads_[1] : nullptr;
} else {
// Buscar por nombre
gamepad = findAvailableGamepadByName(bindings.gamepad_name);
}
// 2. Resoldre gamepad per path/name
std::shared_ptr<Gamepad> gamepad = resolvePlayerGamepad(bindings);
if (!gamepad) {
player2_gamepad_ = nullptr;
@@ -528,6 +581,8 @@ void Input::applyPlayer2Bindings(const Config::PlayerBindings& bindings) {
gamepad->bindings[Action::RIGHT].button = bindings.gamepad.button_right;
gamepad->bindings[Action::THRUST].button = bindings.gamepad.button_thrust;
gamepad->bindings[Action::SHOOT].button = bindings.gamepad.button_shoot;
gamepad->bindings[Action::START].button = bindings.gamepad.button_start;
gamepad->bindings[Action::MENU].button = bindings.gamepad.button_menu;
// 4. Cachear referencia
player2_gamepad_ = gamepad;
@@ -555,6 +610,17 @@ auto Input::checkActionPlayer1(Action action, bool repeat) -> bool {
return keyboard_active || gamepad_active;
}
// Retorna el pad assignat (0=P1, 1=P2). Pot ser nullptr.
auto Input::getPlayerGamepad(int player_index) const -> std::shared_ptr<Input::Gamepad> {
if (player_index == 0) {
return player1_gamepad_;
}
if (player_index == 1) {
return player2_gamepad_;
}
return nullptr;
}
// Consulta de input para player 2
auto Input::checkActionPlayer2(Action action, bool repeat) -> bool {
// Comprobar teclado con el mapa específico de P2
+8 -1
View File
@@ -62,7 +62,9 @@ class Input {
{Action::LEFT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_DPAD_LEFT)}},
{Action::RIGHT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)}},
{Action::THRUST, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_WEST)}},
{Action::SHOOT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_SOUTH)}}} {}
{Action::SHOOT, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_SOUTH)}},
{Action::START, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_START)}},
{Action::MENU, ButtonState{.button = static_cast<int>(SDL_GAMEPAD_BUTTON_BACK)}}} {}
~Gamepad() {
if (pad != nullptr) {
@@ -107,6 +109,10 @@ class Input {
auto checkActionPlayer1(Action action, bool repeat = true) -> bool;
auto checkActionPlayer2(Action action, bool repeat = true) -> bool;
// Accés al gamepad assignat per jugador (0=P1, 1=P2). nullptr si no n'hi
// ha cap d'assignat o connectat. Usat per la UI de redefinició de botons.
[[nodiscard]] auto getPlayerGamepad(int player_index) const -> std::shared_ptr<Gamepad>;
// Check if any player pressed any action from a list
auto checkAnyPlayerAction(const std::span<const InputAction>& actions, bool repeat = DO_NOT_ALLOW_REPEAT) -> bool;
@@ -142,6 +148,7 @@ class Input {
auto removeGamepad(SDL_JoystickID id) -> std::string;
void addGamepadMappingsFromFile();
void discoverGamepads();
auto resolvePlayerGamepad(const Config::PlayerBindings& bindings) -> std::shared_ptr<Gamepad>;
// --- Variables miembro ---
static Input* instance; // Instancia única del singleton
+4
View File
@@ -6,6 +6,8 @@ const std::unordered_map<InputAction, std::string> ACTION_TO_STRING = {
{InputAction::RIGHT, "RIGHT"},
{InputAction::THRUST, "THRUST"},
{InputAction::SHOOT, "SHOOT"},
{InputAction::START, "START"},
{InputAction::MENU, "MENU"},
{InputAction::WINDOW_INC_ZOOM, "WINDOW_INC_ZOOM"},
{InputAction::WINDOW_DEC_ZOOM, "WINDOW_DEC_ZOOM"},
{InputAction::TOGGLE_FULLSCREEN, "TOGGLE_FULLSCREEN"},
@@ -18,6 +20,8 @@ const std::unordered_map<std::string, InputAction> STRING_TO_ACTION = {
{"RIGHT", InputAction::RIGHT},
{"THRUST", InputAction::THRUST},
{"SHOOT", InputAction::SHOOT},
{"START", InputAction::START},
{"MENU", InputAction::MENU},
{"WINDOW_INC_ZOOM", InputAction::WINDOW_INC_ZOOM},
{"WINDOW_DEC_ZOOM", InputAction::WINDOW_DEC_ZOOM},
{"TOGGLE_FULLSCREEN", InputAction::TOGGLE_FULLSCREEN},
+1
View File
@@ -15,6 +15,7 @@ enum class InputAction : std::uint8_t { // Acciones de entrada posibles en el j
THRUST, // Acelerar
SHOOT, // Disparar
START, // Empezar match
MENU, // Abrir/cerrar menu de servicio (equivalent a F12)
// Inputs de sistema (globales)
WINDOW_INC_ZOOM, // F2
@@ -5,6 +5,7 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_gpu.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstring>
@@ -390,6 +391,10 @@ namespace Rendering::GPU {
color_target.store_op = SDL_GPU_STOREOP_STORE;
render_pass_ = SDL_BeginGPURenderPass(cmd_buffer_, &color_target, 1, nullptr);
// L'scissor és per render pass: en reobrir cal restaurar-lo des del top
// de la pila si pushClip/popClip s'han usat mid-frame.
applyCurrentScissor();
SDL_BindGPUGraphicsPipeline(render_pass_, line_pipeline_.get());
// UBO de líneas usa el tamaño lógico (también del offscreen).
@@ -415,6 +420,11 @@ namespace Rendering::GPU {
SDL_ReleaseGPUBuffer(dev, vbo);
SDL_ReleaseGPUBuffer(dev, ibo);
SDL_ReleaseGPUTransferBuffer(dev, tbo);
// Buidem el batch perquè pushClip/popClip puguin emetre seccions
// separades dins el mateix frame sense re-enviar geometria.
vertices_.clear();
indices_.clear();
}
void GpuFrameRenderer::bloomPass() {
@@ -603,6 +613,51 @@ namespace Rendering::GPU {
SDL_DrawGPUPrimitives(render_pass_, 3, 1, 0, 0);
}
void GpuFrameRenderer::pushClip(int logical_x, int logical_y, int logical_w, int logical_h) {
// Convertim coordenades lògiques (espai del joc, 1280×720) a píxels
// físics del offscreen (render_w_ × render_h_). Si l'usuari hi treballa
// amb upscale (p.ex. 1920×1080), l'scissor escala proporcionalment.
const float SX = render_w_ / logical_w_;
const float SY = render_h_ / logical_h_;
SDL_Rect rect{
.x = static_cast<int>(static_cast<float>(logical_x) * SX),
.y = static_cast<int>(static_cast<float>(logical_y) * SY),
.w = std::max(0, static_cast<int>(static_cast<float>(logical_w) * SX)),
.h = std::max(0, static_cast<int>(static_cast<float>(logical_h) * SY)),
};
// Emetem tot el batch acumulat *abans* d'activar l'scissor perquè quedi
// dibuixat sense retallar.
flushBatch();
clip_stack_.push_back(rect);
applyCurrentScissor();
}
void GpuFrameRenderer::popClip() {
// Emetem el batch que s'ha acumulat *dins* del clip actiu.
flushBatch();
if (!clip_stack_.empty()) {
clip_stack_.pop_back();
}
applyCurrentScissor();
}
void GpuFrameRenderer::applyCurrentScissor() {
if (render_pass_ == nullptr) {
return;
}
SDL_Rect rect{};
if (clip_stack_.empty()) {
// Sense clips: scissor cobreix tot el offscreen.
rect.x = 0;
rect.y = 0;
rect.w = static_cast<int>(render_w_);
rect.h = static_cast<int>(render_h_);
} else {
rect = clip_stack_.back();
}
SDL_SetGPUScissor(render_pass_, &rect);
}
void GpuFrameRenderer::endFrame() {
if (cmd_buffer_ == nullptr) {
return;
@@ -94,6 +94,15 @@ namespace Rendering::GPU {
// d'UI (notificacions, panels).
void pushRect(float x, float y, float w, float h, float r, float g, float b, float a);
// Clipping rectangular per a UI (scissor a SDL_GPU). pushClip/popClip
// forcen un flush intermedi del batch i activen/restauren l'scissor del
// pase actiu. Coordenades en píxels lògics del joc (1280×720); es
// converteixen a píxels físics del offscreen automàticament. Stack
// d'scissors per a clips niats. Quan la pila queda buida, l'scissor
// torna a cobrir el target sencer.
void pushClip(int logical_x, int logical_y, int logical_w, int logical_h);
void popClip();
// endFrame: flush del batch de líneas → composite postpro → submit + presenta.
void endFrame();
@@ -168,6 +177,10 @@ namespace Rendering::GPU {
std::vector<LineVertex> vertices_;
std::vector<uint16_t> indices_;
// Pila d'scissors actius en píxels físics del offscreen. Buida = sense
// clip (full target). Cada push/pop fa un flushBatch i reaplica scissor.
std::vector<SDL_Rect> clip_stack_;
// Estado del frame en curso.
SDL_GPUCommandBuffer* cmd_buffer_{nullptr};
SDL_GPUTexture* swapchain_texture_{nullptr};
@@ -190,6 +203,7 @@ namespace Rendering::GPU {
void bloomPass(); // pre-composite: H + V passes sobre les bloom textures
void compositePass();
void applyFinalViewport();
void applyCurrentScissor(); // re-aplica el top de clip_stack_ al render_pass_
};
} // namespace Rendering::GPU
+20
View File
@@ -385,6 +385,26 @@ void SDLManager::toggleAntialias() {
}
}
void SDLManager::setRenderResolution(int w, int h) {
if (!Defaults::Rendering::isValidRenderResolution(w, h)) {
std::cerr << "[SDLManager] Resolucio no valida (" << w << "x" << h
<< "), ignorant.\n";
return;
}
if (w == cfg_->rendering.render_width && h == cfg_->rendering.render_height) {
return; // ja era l'actual
}
if (!gpu_renderer_.resizeRenderTarget(static_cast<float>(w), static_cast<float>(h))) {
std::cerr << "[SDLManager] resizeRenderTarget ha fallat.\n";
return;
}
cfg_->rendering.render_width = w;
cfg_->rendering.render_height = h;
if (on_persist_) {
on_persist_();
}
}
void SDLManager::togglePostFx() {
const bool NEW_STATE = !gpu_renderer_.isPostFxEnabled();
gpu_renderer_.setPostFxEnabled(NEW_STATE);
+12 -6
View File
@@ -30,12 +30,16 @@ class SDLManager {
auto operator=(const SDLManager&) -> SDLManager& = delete;
// [NUEVO] Gestió de finestra dinàmica
void increaseWindowSize(); // F2: +100px
void decreaseWindowSize(); // F1: -100px
void toggleFullscreen(); // F3
void toggleVSync(); // F4
void toggleAntialias(); // F5
void togglePostFx(); // F6
void increaseWindowSize(); // F2: +100px
void decreaseWindowSize(); // F1: -100px
void toggleFullscreen(); // F3
void toggleVSync(); // F4
void toggleAntialias(); // F5
void togglePostFx(); // F6
// Canvia la resolució del render target offscreen (recrea la textura).
// Cal cridar-lo fora d'un frame (event phase, no draw phase). Si el
// valor no es un preset valid o ja es l'actual, es no-op.
void setRenderResolution(int w, int h);
auto handleWindowEvent(const SDL_Event& event) -> bool; // Per a SDL_EVENT_WINDOW_RESIZED
// Funciones principals (renderizado).
@@ -47,6 +51,8 @@ class SDLManager {
// Getters
auto getRenderer() -> Rendering::Renderer* { return &gpu_renderer_; }
[[nodiscard]] auto getScaleFactor() const -> float { return zoom_factor_; }
[[nodiscard]] auto isFullscreen() const -> bool { return is_fullscreen_; }
[[nodiscard]] auto isPostFxEnabled() const -> bool { return gpu_renderer_.isPostFxEnabled(); }
// [NUEVO] Actualitzar context de renderizado (factor de scale global)
void updateRenderingContext() const;
+61 -56
View File
@@ -9,72 +9,77 @@
namespace Resource::Helper {
// Inicialitzar el sistema de recursos
auto initializeResourceSystem(const std::string& pack_file, bool fallback) -> bool {
return Loader::get().initialize(pack_file, fallback);
}
// Inicialitzar el sistema de recursos
auto initializeResourceSystem(const std::string& pack_file, bool fallback) -> bool {
return Loader::get().initialize(pack_file, fallback);
}
// Carregar un file
auto loadFile(const std::string& filepath) -> std::vector<uint8_t> {
// Normalitzar la ruta
std::string normalized = normalizePath(filepath);
// Carregar un file
auto loadFile(const std::string& filepath) -> std::vector<uint8_t> {
// Normalitzar la ruta
std::string normalized = normalizePath(filepath);
// Carregar del sistema de recursos
return Loader::get().loadResource(normalized);
}
// Carregar del sistema de recursos
return Loader::get().loadResource(normalized);
}
// Comprovar si existeix un file
auto fileExists(const std::string& filepath) -> bool {
std::string normalized = normalizePath(filepath);
return Loader::get().resourceExists(normalized);
}
// Llistar recursos amb un prefix donat
auto listResources(const std::string& prefix) -> std::vector<std::string> {
return Loader::get().listResources(prefix);
}
// Obtenir ruta normalitzada per al paquet
// Elimina prefixos "data/", rutes absolutes, etc.
auto getPackPath(const std::string& asset_path) -> std::string {
std::string path = asset_path;
// Comprovar si existeix un file
auto fileExists(const std::string& filepath) -> bool {
std::string normalized = normalizePath(filepath);
return Loader::get().resourceExists(normalized);
}
// Eliminar rutes absolutes (detectar / o C:\ al principi)
if (!path.empty() && path[0] == '/') {
// Buscar "data/" i agafar el que ve después
size_t data_pos = path.find("/data/");
if (data_pos != std::string::npos) {
path = path.substr(data_pos + 6); // Saltar "/data/"
// Obtenir ruta normalitzada per al paquet
// Elimina prefixos "data/", rutes absolutes, etc.
auto getPackPath(const std::string& asset_path) -> std::string {
std::string path = asset_path;
// Eliminar rutes absolutes (detectar / o C:\ al principi)
if (!path.empty() && path[0] == '/') {
// Buscar "data/" i agafar el que ve después
size_t data_pos = path.find("/data/");
if (data_pos != std::string::npos) {
path = path.substr(data_pos + 6); // Saltar "/data/"
}
}
// Eliminar "./" i "../" del principi
while (path.starts_with("./")) {
path = path.substr(2);
}
while (path.starts_with("../")) {
path = path.substr(3);
}
// Eliminar "data/" del principi
if (path.starts_with("data/")) {
path = path.substr(5);
}
// Eliminar "Resources/" (macOS bundles)
if (path.starts_with("Resources/")) {
path = path.substr(10);
}
// Convertir barres invertides a normals
std::ranges::replace(path, '\\', '/');
return path;
}
// Eliminar "./" i "../" del principi
while (path.starts_with("./")) {
path = path.substr(2);
}
while (path.starts_with("../")) {
path = path.substr(3);
// Normalitzar ruta (alias de getPackPath)
auto normalizePath(const std::string& path) -> std::string {
return getPackPath(path);
}
// Eliminar "data/" del principi
if (path.starts_with("data/")) {
path = path.substr(5);
// Comprovar si hay paquet carregat
auto isPackLoaded() -> bool {
return Loader::get().isPackLoaded();
}
// Eliminar "Resources/" (macOS bundles)
if (path.starts_with("Resources/")) {
path = path.substr(10);
}
// Convertir barres invertides a normals
std::ranges::replace(path, '\\', '/');
return path;
}
// Normalitzar ruta (alias de getPackPath)
auto normalizePath(const std::string& path) -> std::string {
return getPackPath(path);
}
// Comprovar si hay paquet carregat
auto isPackLoaded() -> bool {
return Loader::get().isPackLoaded();
}
} // namespace Resource::Helper
+13 -10
View File
@@ -10,18 +10,21 @@
namespace Resource::Helper {
// Inicialización del sistema
auto initializeResourceSystem(const std::string& pack_file, bool fallback) -> bool;
// Inicialización del sistema
auto initializeResourceSystem(const std::string& pack_file, bool fallback) -> bool;
// Càrrega de archivos
auto loadFile(const std::string& filepath) -> std::vector<uint8_t>;
auto fileExists(const std::string& filepath) -> bool;
// Càrrega de archivos
auto loadFile(const std::string& filepath) -> std::vector<uint8_t>;
auto fileExists(const std::string& filepath) -> bool;
// Normalització de rutes
auto getPackPath(const std::string& asset_path) -> std::string;
auto normalizePath(const std::string& path) -> std::string;
// Llistat de recursos disponibles amb un prefix (ex. "shapes/", "sounds/").
auto listResources(const std::string& prefix) -> std::vector<std::string>;
// Estat
auto isPackLoaded() -> bool;
// Normalització de rutes
auto getPackPath(const std::string& asset_path) -> std::string;
auto normalizePath(const std::string& path) -> std::string;
// Estat
auto isPackLoaded() -> bool;
} // namespace Resource::Helper
+145 -108
View File
@@ -3,141 +3,178 @@
#include "resource_loader.hpp"
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace Resource {
// Singleton
auto Loader::get() -> Loader& {
static Loader instance_;
return instance_;
}
// Inicialitzar el sistema de recursos
auto Loader::initialize(const std::string& pack_file, bool enable_fallback) -> bool {
fallback_enabled_ = enable_fallback;
// Intentar load el paquet
pack_ = std::make_unique<Pack>();
if (!pack_->loadPack(pack_file)) {
if (!fallback_enabled_) {
std::cerr << "[ResourceLoader] ERROR FATAL: No es pot load " << pack_file
<< " y el fallback está desactivat\n";
return false;
}
std::cout << "[ResourceLoader] Paquet no trobat, usant fallback al sistema de archivos\n";
pack_.reset(); // No hay paquet
return true;
// Singleton
auto Loader::get() -> Loader& {
static Loader instance_;
return instance_;
}
std::cout << "[ResourceLoader] Paquet carregat: " << pack_file << "\n";
return true;
}
// Inicialitzar el sistema de recursos
auto Loader::initialize(const std::string& pack_file, bool enable_fallback) -> bool {
fallback_enabled_ = enable_fallback;
// Carregar un recurs
auto Loader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
// Intentar load del paquet primer
if (pack_) {
if (pack_->hasResource(filename)) {
auto data = pack_->getResource(filename);
if (!data.empty()) {
return data;
// Intentar load el paquet
pack_ = std::make_unique<Pack>();
if (!pack_->loadPack(pack_file)) {
if (!fallback_enabled_) {
std::cerr << "[ResourceLoader] ERROR FATAL: No es pot load " << pack_file
<< " y el fallback está desactivat\n";
return false;
}
std::cerr << "[ResourceLoader] Advertència: recurs buit al paquet: " << filename
<< "\n";
std::cout << "[ResourceLoader] Paquet no trobat, usant fallback al sistema de archivos\n";
pack_.reset(); // No hay paquet
return true;
}
// Si no está al paquet y no hay fallback, falla
if (!fallback_enabled_) {
std::cerr << "[ResourceLoader] ERROR: Recurs no trobat al paquet i fallback desactivat: "
<< filename << "\n";
return {};
}
}
// Fallback al sistema de archivos
if (fallback_enabled_) {
return loadFromFilesystem(filename);
}
return {};
}
// Comprovar si existeix un recurs
auto Loader::resourceExists(const std::string& filename) -> bool {
// Comprovar al paquet
if (pack_ && pack_->hasResource(filename)) {
std::cout << "[ResourceLoader] Paquet carregat: " << pack_file << "\n";
return true;
}
// Comprovar al sistema de archivos si está activat el fallback
if (fallback_enabled_) {
std::string fullpath = base_path_.empty() ? "data/" + filename : base_path_ + "/data/" + filename;
return std::filesystem::exists(fullpath);
// Carregar un recurs
auto Loader::loadResource(const std::string& filename) -> std::vector<uint8_t> {
// Intentar load del paquet primer
if (pack_) {
if (pack_->hasResource(filename)) {
auto data = pack_->getResource(filename);
if (!data.empty()) {
return data;
}
std::cerr << "[ResourceLoader] Advertència: recurs buit al paquet: " << filename
<< "\n";
}
// Si no está al paquet y no hay fallback, falla
if (!fallback_enabled_) {
std::cerr << "[ResourceLoader] ERROR: Recurs no trobat al paquet i fallback desactivat: "
<< filename << "\n";
return {};
}
}
// Fallback al sistema de archivos
if (fallback_enabled_) {
return loadFromFilesystem(filename);
}
return {};
}
return false;
}
auto Loader::listResources(const std::string& prefix) -> std::vector<std::string> {
std::vector<std::string> result;
if (pack_) {
for (const auto& path : pack_->getResourceList()) {
if (path.starts_with(prefix)) {
result.push_back(path);
}
}
return result;
}
if (!fallback_enabled_) {
return result;
}
std::string root = base_path_.empty() ? "data/" + prefix : base_path_ + "/data/" + prefix;
if (!std::filesystem::exists(root)) {
return result;
}
for (const auto& entry : std::filesystem::recursive_directory_iterator(root)) {
if (!entry.is_regular_file()) {
continue;
}
std::string full = entry.path().generic_string();
if (auto pos = full.find("/data/"); pos != std::string::npos) {
result.push_back(full.substr(pos + 6));
} else if (full.starts_with("data/")) {
result.push_back(full.substr(5));
}
}
std::ranges::sort(result);
return result;
}
// Comprovar si existeix un recurs
auto Loader::resourceExists(const std::string& filename) -> bool {
// Comprovar al paquet
if (pack_ && pack_->hasResource(filename)) {
return true;
}
// Comprovar al sistema de archivos si está activat el fallback
if (fallback_enabled_) {
std::string fullpath = base_path_.empty() ? "data/" + filename : base_path_ + "/data/" + filename;
return std::filesystem::exists(fullpath);
}
// Validar el paquet
auto Loader::validatePack() -> bool {
if (!pack_) {
std::cerr << "[ResourceLoader] Advertència: no hay paquet carregat per validar\n";
return false;
}
return pack_->validatePack();
}
// Validar el paquet
auto Loader::validatePack() -> bool {
if (!pack_) {
std::cerr << "[ResourceLoader] Advertència: no hay paquet carregat per validar\n";
return false;
}
// Comprovar si hay paquet carregat
auto Loader::isPackLoaded() const -> bool {
return pack_ != nullptr;
}
// Establir la ruta base
void Loader::setBasePath(const std::string& path) {
base_path_ = path;
std::cout << "[ResourceLoader] Ruta base establerta: " << base_path_ << "\n";
}
// Obtenir la ruta base
auto Loader::getBasePath() const -> const std::string& {
return base_path_;
}
// Carregar des del sistema de archivos (fallback)
auto Loader::loadFromFilesystem(const std::string& filename) -> std::vector<uint8_t> {
// The filename is already normalized (e.g., "shapes/logo/letra_j.shp")
// We need to prepend base_path + "data/"
std::string fullpath;
if (base_path_.empty()) {
fullpath = "data/" + filename;
} else {
fullpath = base_path_ + "/data/" + filename;
return pack_->validatePack();
}
std::ifstream file(fullpath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "[ResourceLoader] Error: no es pot obrir " << fullpath << "\n";
return {};
// Comprovar si hay paquet carregat
auto Loader::isPackLoaded() const -> bool {
return pack_ != nullptr;
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(file_size);
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
std::cerr << "[ResourceLoader] Error: no es pot llegir " << fullpath << "\n";
return {};
// Establir la ruta base
void Loader::setBasePath(const std::string& path) {
base_path_ = path;
std::cout << "[ResourceLoader] Ruta base establerta: " << base_path_ << "\n";
}
std::cout << "[ResourceLoader] Carregat des del sistema de archivos: " << fullpath << "\n";
return data;
}
// Obtenir la ruta base
auto Loader::getBasePath() const -> const std::string& {
return base_path_;
}
// Carregar des del sistema de archivos (fallback)
auto Loader::loadFromFilesystem(const std::string& filename) -> std::vector<uint8_t> {
// The filename is already normalized (e.g., "shapes/logo/letra_j.shp")
// We need to prepend base_path + "data/"
std::string fullpath;
if (base_path_.empty()) {
fullpath = "data/" + filename;
} else {
fullpath = base_path_ + "/data/" + filename;
}
std::ifstream file(fullpath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "[ResourceLoader] Error: no es pot obrir " << fullpath << "\n";
return {};
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(file_size);
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
std::cerr << "[ResourceLoader] Error: no es pot llegir " << fullpath << "\n";
return {};
}
std::cout << "[ResourceLoader] Carregat des del sistema de archivos: " << fullpath << "\n";
return data;
}
} // namespace Resource
+10 -5
View File
@@ -12,9 +12,9 @@
namespace Resource {
// Singleton per gestionar la càrrega de recursos
class Loader {
public:
// Singleton per gestionar la càrrega de recursos
class Loader {
public:
// Singleton
static auto get() -> Loader&;
@@ -25,6 +25,11 @@ class Loader {
auto loadResource(const std::string& filename) -> std::vector<uint8_t>;
auto resourceExists(const std::string& filename) -> bool;
// Llistat de recursos amb prefix (ex. "shapes/", "sounds/"). Si hi ha
// pack, retorna els fitxers del pack filtrats; si no, escaneja el
// sistema de fitxers recursivament a `data/<prefix>`.
auto listResources(const std::string& prefix) -> std::vector<std::string>;
// Validació
auto validatePack() -> bool;
[[nodiscard]] auto isPackLoaded() const -> bool;
@@ -37,7 +42,7 @@ class Loader {
Loader(const Loader&) = delete;
auto operator=(const Loader&) -> Loader& = delete;
private:
private:
Loader() = default;
~Loader() = default;
@@ -48,6 +53,6 @@ class Loader {
// Funciones auxiliars
auto loadFromFilesystem(const std::string& filename) -> std::vector<uint8_t>;
};
};
} // namespace Resource
+238 -251
View File
@@ -10,300 +10,287 @@
namespace Resource {
// Calcular checksum CRC32 simplificat
auto Pack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
uint32_t checksum = 0x12345678;
for (unsigned char byte : data) {
checksum = ((checksum << 5) + checksum) + byte;
}
return checksum;
}
// Encriptació XOR (simètrica)
void Pack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
if (key.empty()) {
return;
}
for (size_t i = 0; i < data.size(); ++i) {
data[i] ^= key[i % key.length()];
}
}
void Pack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
// XOR es simètric
encryptData(data, key);
}
// Llegir file complet a memòria
auto Pack::readFile(const std::string& filepath) -> std::vector<uint8_t> {
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "[ResourcePack] Error: no es pot obrir " << filepath << '\n';
return {};
// Calcular checksum CRC32 simplificat
auto Pack::calculateChecksum(const std::vector<uint8_t>& data) -> uint32_t {
uint32_t checksum = 0x12345678;
for (unsigned char byte : data) {
checksum = ((checksum << 5) + checksum) + byte;
}
return checksum;
}
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(file_size);
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
std::cerr << "[ResourcePack] Error: no es pot llegir " << filepath << '\n';
return {};
// Encriptació XOR (simètrica)
void Pack::encryptData(std::vector<uint8_t>& data, const std::string& key) {
if (key.empty()) {
return;
}
for (size_t i = 0; i < data.size(); ++i) {
data[i] ^= key[i % key.length()];
}
}
return data;
}
// Añadir un file individual al paquet
auto Pack::addFile(const std::string& filepath, const std::string& pack_name) -> bool {
auto file_data = readFile(filepath);
if (file_data.empty()) {
return false;
void Pack::decryptData(std::vector<uint8_t>& data, const std::string& key) {
// XOR es simètric
encryptData(data, key);
}
ResourceEntry entry{
.filename = pack_name,
.offset = data_.size(),
.size = file_data.size(),
.checksum = calculateChecksum(file_data)};
// Añadir dades al bloc de dades
data_.insert(data_.end(), file_data.begin(), file_data.end());
resources_[pack_name] = entry;
std::cout << "[ResourcePack] Añadido: " << pack_name << " (" << file_data.size()
<< " bytes)\n";
return true;
}
// Añadir todos los archivos de un directori recursivament
auto Pack::addDirectory(const std::string& dir_path,
const std::string& base_path) -> bool {
namespace fs = std::filesystem;
if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) {
std::cerr << "[ResourcePack] Error: directori no trobat: " << dir_path << '\n';
return false;
}
std::string current_base = base_path.empty() ? "" : base_path + "/";
for (const auto& entry : fs::recursive_directory_iterator(dir_path)) {
if (!entry.is_regular_file()) {
continue;
// Llegir file complet a memòria
auto Pack::readFile(const std::string& filepath) -> std::vector<uint8_t> {
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "[ResourcePack] Error: no es pot obrir " << filepath << '\n';
return {};
}
std::string full_path = entry.path().string();
std::string relative_path = entry.path().lexically_relative(dir_path).string();
std::streamsize file_size = file.tellg();
file.seekg(0, std::ios::beg);
// Convertir barres invertides a normals (Windows)
std::ranges::replace(relative_path, '\\', '/');
// Saltar archivos de desenvolupament
if (relative_path.find(".world") != std::string::npos ||
relative_path.find(".tsx") != std::string::npos ||
relative_path.find(".DS_Store") != std::string::npos ||
relative_path.find(".git") != std::string::npos) {
std::cout << "[ResourcePack] Saltant: " << relative_path << '\n';
continue;
std::vector<uint8_t> data(file_size);
if (!file.read(reinterpret_cast<char*>(data.data()), file_size)) {
std::cerr << "[ResourcePack] Error: no es pot llegir " << filepath << '\n';
return {};
}
std::string pack_name = current_base + relative_path;
addFile(full_path, pack_name);
return data;
}
return true;
}
// Añadir un file individual al paquet
auto Pack::addFile(const std::string& filepath, const std::string& pack_name) -> bool {
auto file_data = readFile(filepath);
if (file_data.empty()) {
return false;
}
// Guardar paquet a disc
auto Pack::savePack(const std::string& pack_file) -> bool {
std::ofstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "[ResourcePack] Error: no es pot crear " << pack_file << '\n';
return false;
ResourceEntry entry{
.filename = pack_name,
.offset = data_.size(),
.size = file_data.size(),
.checksum = calculateChecksum(file_data)};
// Añadir dades al bloc de dades
data_.insert(data_.end(), file_data.begin(), file_data.end());
resources_[pack_name] = entry;
return true;
}
// Escriure capçalera
file.write(MAGIC_HEADER, 4);
file.write(reinterpret_cast<const char*>(&VERSION), sizeof(VERSION));
// Añadir todos los archivos de un directori recursivament
auto Pack::addDirectory(const std::string& dir_path,
const std::string& base_path) -> bool {
namespace fs = std::filesystem;
// Escriure nombre de recursos
auto resource_count = static_cast<uint32_t>(resources_.size());
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
if (!fs::exists(dir_path) || !fs::is_directory(dir_path)) {
std::cerr << "[ResourcePack] Error: directori no trobat: " << dir_path << '\n';
return false;
}
// Escriure metadades de recursos
for (const auto& [name, entry] : resources_) {
// Nom del file
auto name_len = static_cast<uint32_t>(entry.filename.length());
file.write(reinterpret_cast<const char*>(&name_len), sizeof(name_len));
file.write(entry.filename.c_str(), name_len);
std::string current_base = base_path.empty() ? "" : base_path + "/";
// Offset, mida, checksum
file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
for (const auto& entry : fs::recursive_directory_iterator(dir_path)) {
if (!entry.is_regular_file()) {
continue;
}
std::string full_path = entry.path().string();
std::string relative_path = entry.path().lexically_relative(dir_path).string();
// Convertir barres invertides a normals (Windows)
std::ranges::replace(relative_path, '\\', '/');
// Saltar archivos de desenvolupament
if (relative_path.find(".world") != std::string::npos ||
relative_path.find(".tsx") != std::string::npos ||
relative_path.find(".DS_Store") != std::string::npos ||
relative_path.find(".git") != std::string::npos) {
continue;
}
std::string pack_name = current_base + relative_path;
addFile(full_path, pack_name);
}
return true;
}
// Encriptar dades
std::vector<uint8_t> encrypted_data = data_;
encryptData(encrypted_data, DEFAULT_ENCRYPT_KEY);
// Guardar paquet a disc
auto Pack::savePack(const std::string& pack_file) -> bool {
std::ofstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "[ResourcePack] Error: no es pot crear " << pack_file << '\n';
return false;
}
// Escriure mida de dades y dades encriptades
auto data_size = static_cast<uint64_t>(encrypted_data.size());
file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
file.write(reinterpret_cast<const char*>(encrypted_data.data()), encrypted_data.size());
// Escriure capçalera
file.write(MAGIC_HEADER, 4);
file.write(reinterpret_cast<const char*>(&VERSION), sizeof(VERSION));
std::cout << "[ResourcePack] Guardat: " << pack_file << " (" << resources_.size()
<< " recursos, " << data_size << " bytes)\n";
// Escriure nombre de recursos
auto resource_count = static_cast<uint32_t>(resources_.size());
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
return true;
}
// Escriure metadades de recursos
for (const auto& [name, entry] : resources_) {
// Nom del file
auto name_len = static_cast<uint32_t>(entry.filename.length());
file.write(reinterpret_cast<const char*>(&name_len), sizeof(name_len));
file.write(entry.filename.c_str(), name_len);
// Carregar paquet desde disc
auto Pack::loadPack(const std::string& pack_file) -> bool {
std::ifstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "[ResourcePack] Error: no es pot obrir " << pack_file << '\n';
return false;
// Offset, mida, checksum
file.write(reinterpret_cast<const char*>(&entry.offset), sizeof(entry.offset));
file.write(reinterpret_cast<const char*>(&entry.size), sizeof(entry.size));
file.write(reinterpret_cast<const char*>(&entry.checksum), sizeof(entry.checksum));
}
// Encriptar dades
std::vector<uint8_t> encrypted_data = data_;
encryptData(encrypted_data, DEFAULT_ENCRYPT_KEY);
// Escriure mida de dades y dades encriptades
auto data_size = static_cast<uint64_t>(encrypted_data.size());
file.write(reinterpret_cast<const char*>(&data_size), sizeof(data_size));
file.write(reinterpret_cast<const char*>(encrypted_data.data()), encrypted_data.size());
return true;
}
// Llegir capçalera
char magic[4];
file.read(magic, 4);
if (std::string(magic, 4) != MAGIC_HEADER) {
std::cerr << "[ResourcePack] Error: capçalera invàlida (esperava " << MAGIC_HEADER
<< ")\n";
return false;
// Carregar paquet desde disc
auto Pack::loadPack(const std::string& pack_file) -> bool {
std::ifstream file(pack_file, std::ios::binary);
if (!file) {
std::cerr << "[ResourcePack] Error: no es pot obrir " << pack_file << '\n';
return false;
}
// Llegir capçalera
char magic[4];
file.read(magic, 4);
if (std::string(magic, 4) != MAGIC_HEADER) {
std::cerr << "[ResourcePack] Error: capçalera invàlida (esperava " << MAGIC_HEADER
<< ")\n";
return false;
}
uint32_t version;
file.read(reinterpret_cast<char*>(&version), sizeof(version));
if (version != VERSION) {
std::cerr << "[ResourcePack] Error: versión incompatible (esperava " << VERSION
<< ", trobat " << version << ")\n";
return false;
}
// Llegir nombre de recursos
uint32_t resource_count;
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
// Llegir metadades de recursos
resources_.clear();
for (uint32_t i = 0; i < resource_count; ++i) {
// Nom del file
uint32_t name_len;
file.read(reinterpret_cast<char*>(&name_len), sizeof(name_len));
std::string filename(name_len, '\0');
file.read(filename.data(), name_len);
// Offset, mida, checksum
ResourceEntry entry;
entry.filename = filename;
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));
resources_[filename] = entry;
}
// Llegir dades encriptades
uint64_t data_size;
file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));
data_.resize(data_size);
file.read(reinterpret_cast<char*>(data_.data()), data_size);
// Desencriptar
decryptData(data_, DEFAULT_ENCRYPT_KEY);
return true;
}
uint32_t version;
file.read(reinterpret_cast<char*>(&version), sizeof(version));
if (version != VERSION) {
std::cerr << "[ResourcePack] Error: versión incompatible (esperava " << VERSION
<< ", trobat " << version << ")\n";
return false;
}
// Obtenir un recurs del paquet
auto Pack::getResource(const std::string& filename) -> std::vector<uint8_t> {
auto it = resources_.find(filename);
if (it == resources_.end()) {
std::cerr << "[ResourcePack] Error: recurs no trobat: " << filename << '\n';
return {};
}
// Llegir nombre de recursos
uint32_t resource_count;
file.read(reinterpret_cast<char*>(&resource_count), sizeof(resource_count));
const auto& entry = it->second;
// Llegir metadades de recursos
resources_.clear();
for (uint32_t i = 0; i < resource_count; ++i) {
// Nom del file
uint32_t name_len;
file.read(reinterpret_cast<char*>(&name_len), sizeof(name_len));
std::string filename(name_len, '\0');
file.read(filename.data(), name_len);
// Offset, mida, checksum
ResourceEntry entry;
entry.filename = filename;
file.read(reinterpret_cast<char*>(&entry.offset), sizeof(entry.offset));
file.read(reinterpret_cast<char*>(&entry.size), sizeof(entry.size));
file.read(reinterpret_cast<char*>(&entry.checksum), sizeof(entry.checksum));
resources_[filename] = entry;
}
// Llegir dades encriptades
uint64_t data_size;
file.read(reinterpret_cast<char*>(&data_size), sizeof(data_size));
data_.resize(data_size);
file.read(reinterpret_cast<char*>(data_.data()), data_size);
// Desencriptar
decryptData(data_, DEFAULT_ENCRYPT_KEY);
std::cout << "[ResourcePack] Carregat: " << pack_file << " (" << resources_.size()
<< " recursos)\n";
return true;
}
// Obtenir un recurs del paquet
auto Pack::getResource(const std::string& filename) -> std::vector<uint8_t> {
auto it = resources_.find(filename);
if (it == resources_.end()) {
std::cerr << "[ResourcePack] Error: recurs no trobat: " << filename << '\n';
return {};
}
const auto& entry = it->second;
// Extreure dades
if (entry.offset + entry.size > data_.size()) {
std::cerr << "[ResourcePack] Error: offset/mida invàlid per " << filename << '\n';
return {};
}
std::vector<uint8_t> resource_data(data_.begin() + entry.offset,
data_.begin() + entry.offset + entry.size);
// Verificar checksum
uint32_t computed_checksum = calculateChecksum(resource_data);
if (computed_checksum != entry.checksum) {
std::cerr << "[ResourcePack] ADVERTÈNCIA: checksum invàlid per " << filename
<< " (esperat " << entry.checksum << ", calculat " << computed_checksum
<< ")\n";
// No falla, pero adverteix
}
return resource_data;
}
// Comprovar si existeix un recurs
auto Pack::hasResource(const std::string& filename) const -> bool {
return resources_.contains(filename);
}
// Obtenir list de todos los recursos
auto Pack::getResourceList() const -> std::vector<std::string> {
std::vector<std::string> list;
list.reserve(resources_.size());
for (const auto& [name, entry] : resources_) {
list.push_back(name);
}
std::ranges::sort(list);
return list;
}
// Validar integritat del paquet
auto Pack::validatePack() const -> bool {
bool valid = true;
for (const auto& [name, entry] : resources_) {
// Verificar offset i mida
// Extreure dades
if (entry.offset + entry.size > data_.size()) {
std::cerr << "[ResourcePack] Error de validació: " << name
<< " té offset/mida invàlid\n";
valid = false;
continue;
std::cerr << "[ResourcePack] Error: offset/mida invàlid per " << filename << '\n';
return {};
}
// Extreure i verificar checksum
std::vector<uint8_t> resource_data(data_.begin() + entry.offset,
data_.begin() + entry.offset + entry.size);
// Verificar checksum
uint32_t computed_checksum = calculateChecksum(resource_data);
if (computed_checksum != entry.checksum) {
std::cerr << "[ResourcePack] Error de validació: " << name
<< " té checksum invàlid\n";
valid = false;
std::cerr << "[ResourcePack] ADVERTÈNCIA: checksum invàlid per " << filename
<< " (esperat " << entry.checksum << ", calculat " << computed_checksum
<< ")\n";
// No falla, pero adverteix
}
return resource_data;
}
if (valid) {
std::cout << "[ResourcePack] Validació OK (" << resources_.size() << " recursos)\n";
// Comprovar si existeix un recurs
auto Pack::hasResource(const std::string& filename) const -> bool {
return resources_.contains(filename);
}
return valid;
}
// Obtenir list de todos los recursos
auto Pack::getResourceList() const -> std::vector<std::string> {
std::vector<std::string> list;
list.reserve(resources_.size());
for (const auto& [name, entry] : resources_) {
list.push_back(name);
}
std::ranges::sort(list);
return list;
}
// Validar integritat del paquet
auto Pack::validatePack() const -> bool {
bool valid = true;
for (const auto& [name, entry] : resources_) {
// Verificar offset i mida
if (entry.offset + entry.size > data_.size()) {
std::cerr << "[ResourcePack] Error de validació: " << name
<< " té offset/mida invàlid\n";
valid = false;
continue;
}
// Extreure i verificar checksum
std::vector<uint8_t> resource_data(data_.begin() + entry.offset,
data_.begin() + entry.offset + entry.size);
uint32_t computed_checksum = calculateChecksum(resource_data);
if (computed_checksum != entry.checksum) {
std::cerr << "[ResourcePack] Error de validació: " << name
<< " té checksum invàlid\n";
valid = false;
}
}
return valid;
}
} // namespace Resource
+37 -4
View File
@@ -2,20 +2,33 @@
#include "core/system/debug_overlay.hpp"
#include <SDL3/SDL.h>
#include <cctype>
#include <cmath>
#include <string>
#include "core/defaults.hpp"
#include "core/rendering/gpu/gpu_frame_renderer.hpp"
#include "core/types.hpp"
namespace System {
namespace {
namespace Cfg = Defaults::Hud::DebugOverlay;
auto toUpperAscii(std::string s) -> std::string {
for (char& c : s) {
c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
}
return s;
}
} // namespace
DebugOverlay::DebugOverlay(Rendering::Renderer* renderer,
const Config::RenderingConfig& rendering_cfg)
: text_(renderer),
renderer_(renderer),
rendering_cfg_(&rendering_cfg) {}
void DebugOverlay::update(float delta_time) {
@@ -23,7 +36,7 @@ namespace System {
fps_frame_count_++;
if (fps_accumulator_ >= Cfg::FPS_UPDATE_INTERVAL) {
fps_display_ = static_cast<int>(fps_frame_count_ / fps_accumulator_);
fps_display_ = static_cast<int>(std::lround(static_cast<float>(fps_frame_count_) / fps_accumulator_));
fps_frame_count_ = 0;
fps_accumulator_ = 0.0F;
}
@@ -35,23 +48,43 @@ namespace System {
}
const std::string FPS_TEXT = "FPS: " + std::to_string(fps_display_);
const std::string RES_TEXT = "RES: " + std::to_string(rendering_cfg_->render_width) + "X" + std::to_string(rendering_cfg_->render_height);
const char* driver_raw = SDL_GetGPUDeviceDriver(renderer_->device().get());
const std::string DRIVER_TEXT = "DRIVER: " + toUpperAscii(driver_raw != nullptr ? driver_raw : "?");
const std::string VSYNC_TEXT = std::string("VSYNC: ") + (rendering_cfg_->vsync == 1 ? "ON" : "OFF");
const std::string AA_TEXT = std::string("AA: ") + (rendering_cfg_->antialias == 1 ? "ON" : "OFF");
float y = Cfg::Y_FPS;
text_.render(FPS_TEXT,
Vec2{.x = Cfg::X, .y = Cfg::Y_FPS},
Vec2{.x = Cfg::X, .y = y},
Cfg::FPS_SCALE,
Cfg::TEXT_SPACING,
Cfg::BRIGHTNESS,
Cfg::COLOR);
y += Cfg::FPS_LINE_HEIGHT;
text_.render(RES_TEXT,
Vec2{.x = Cfg::X, .y = y},
Cfg::TEXT_SCALE,
Cfg::TEXT_SPACING,
Cfg::BRIGHTNESS,
Cfg::COLOR);
y += Cfg::LINE_HEIGHT;
text_.render(DRIVER_TEXT,
Vec2{.x = Cfg::X, .y = y},
Cfg::TEXT_SCALE,
Cfg::TEXT_SPACING,
Cfg::BRIGHTNESS,
Cfg::COLOR);
y += Cfg::LINE_HEIGHT;
text_.render(VSYNC_TEXT,
Vec2{.x = Cfg::X, .y = Cfg::Y_FPS + Cfg::LINE_HEIGHT},
Vec2{.x = Cfg::X, .y = y},
Cfg::TEXT_SCALE,
Cfg::TEXT_SPACING,
Cfg::BRIGHTNESS,
Cfg::COLOR);
y += Cfg::LINE_HEIGHT;
text_.render(AA_TEXT,
Vec2{.x = Cfg::X, .y = Cfg::Y_FPS + (2.0F * Cfg::LINE_HEIGHT)},
Vec2{.x = Cfg::X, .y = y},
Cfg::TEXT_SCALE,
Cfg::TEXT_SPACING,
Cfg::BRIGHTNESS,
+1
View File
@@ -31,6 +31,7 @@ namespace System {
private:
Graphics::VectorText text_;
Rendering::Renderer* renderer_;
const Config::RenderingConfig* rendering_cfg_;
bool visible_{false};
+72 -10
View File
@@ -11,8 +11,9 @@
#include "core/audio/audio.hpp"
#include "core/audio/audio_adapter.hpp"
#include "core/defaults/audio.hpp"
#include "core/defaults/window.hpp"
#include "core/graphics/shape_loader.hpp"
#include "core/input/define_inputs.hpp"
#include "core/input/input.hpp"
#include "core/input/mouse.hpp"
#include "core/locale/locale.hpp"
@@ -20,6 +21,7 @@
#include "core/resources/resource_helper.hpp"
#include "core/resources/resource_loader.hpp"
#include "core/system/notifier.hpp"
#include "core/system/service_menu.hpp"
#include "core/utils/path_utils.hpp"
#include "debug_overlay.hpp"
#include "game/config_yaml.hpp"
@@ -107,6 +109,34 @@ Director::Director(int argc, char* argv[])
// Inicialitzar sistema de input
Input::init("data/gamecontrollerdb.txt");
// Autoassignacio de primer arranque: si cap dels dos jugadors te mando
// assignat al config, repartim els que hi haja detectats (P1 = pad 0,
// P2 = pad 1 si existeix) i ho persistim. Aixo nomes dispara amb tots
// dos buits perque un "SENSE MANDO" explicit ha de sobreviure entre
// arrancades.
{
auto& p1 = cfg_->player1;
auto& p2 = cfg_->player2;
const bool BOTH_EMPTY = p1.gamepad_name.empty() && p1.gamepad_path.empty() && p2.gamepad_name.empty() && p2.gamepad_path.empty();
if (BOTH_EMPTY) {
const auto& pads = Input::get()->getGamepads();
bool changed = false;
if (!pads.empty() && pads[0]) {
p1.gamepad_name = pads[0]->name;
p1.gamepad_path = pads[0]->path;
changed = true;
}
if (pads.size() > 1 && pads[1]) {
p2.gamepad_name = pads[1]->name;
p2.gamepad_path = pads[1]->path;
changed = true;
}
if (changed) {
ConfigYaml::saveToFile();
}
}
}
// Aplicar configuración de controls dels jugadors
Input::get()->applyPlayer1Bindings(cfg_->player1);
Input::get()->applyPlayer2Bindings(cfg_->player2);
@@ -137,20 +167,31 @@ Director::Director(int argc, char* argv[])
}
const Audio::Config AUDIO_CONFIG{
.enabled = Defaults::Audio::ENABLED,
.volume = Defaults::Audio::VOLUME,
.music_enabled = Defaults::Audio::MUSIC_ENABLED,
.music_volume = Defaults::Audio::MUSIC_VOLUME,
.sound_enabled = Defaults::Audio::SOUND_ENABLED,
.sound_volume = Defaults::Audio::SOUND_VOLUME,
.enabled = cfg_->audio.enabled,
.volume = cfg_->audio.volume,
.music_enabled = cfg_->audio.music_enabled,
.music_volume = cfg_->audio.music_volume,
.sound_enabled = cfg_->audio.sound_enabled,
.sound_volume = cfg_->audio.sound_volume,
};
Audio::init(AUDIO_CONFIG);
Audio::get()->applySettings(AUDIO_CONFIG);
AudioResource::getMusic("title.ogg");
AudioResource::getMusic("game.ogg");
// Precàrrega blocant de tots els recursos al boot per evitar hits d'I/O i
// de decodificació en transicions (TITLE → GAME, primera explosió, etc.).
// Mateix patró que aee_arcade: iterem `listResources` i forcem la càrrega
// al cache de cada subsistema.
for (const auto& path : Resource::Helper::listResources("music/")) {
AudioResource::getMusic(path.substr(std::string_view{"music/"}.size()));
}
for (const auto& path : Resource::Helper::listResources("sounds/")) {
AudioResource::getSound(path.substr(std::string_view{"sounds/"}.size()));
}
for (const auto& path : Resource::Helper::listResources("shapes/")) {
Graphics::ShapeLoader::load(path.substr(std::string_view{"shapes/"}.size()));
}
if (cfg_->console) {
std::cout << "Música precacheada\n";
std::cout << "Recursos precachejats (música, sons, shapes)\n";
}
context_ = std::make_unique<SceneContext>();
@@ -165,6 +206,8 @@ Director::Director(int argc, char* argv[])
cfg_->rendering);
System::Notifier::init(sdl_->getRenderer());
System::ServiceMenu::init(sdl_->getRenderer(), sdl_.get(), debug_overlay_.get());
System::DefineInputs::init(sdl_->getRenderer());
last_ticks_ms_ = SDL_GetTicks();
}
@@ -179,6 +222,8 @@ Director::~Director() {
// l'hem de cridar nosaltres.
current_scene_.reset();
debug_overlay_.reset();
System::DefineInputs::destroy();
System::ServiceMenu::destroy();
System::Notifier::destroy();
context_.reset();
sdl_.reset();
@@ -359,6 +404,12 @@ auto Director::iterate() -> SDL_AppResult {
if (auto* notifier = System::Notifier::get(); notifier != nullptr) {
notifier->update(delta_time);
}
if (auto* menu = System::ServiceMenu::get(); menu != nullptr) {
menu->update(delta_time);
}
if (auto* di = System::DefineInputs::get(); di != nullptr) {
di->update(delta_time);
}
Audio::update();
// Si la swapchain no està disponible (finestra minimitzada, etc.),
@@ -372,6 +423,17 @@ auto Director::iterate() -> SDL_AppResult {
if (const auto* notifier = System::Notifier::get(); notifier != nullptr) {
notifier->draw(); // toast: per damunt de tot
}
// Mentre l'overlay de redefinicio esta actiu, amaguem el menu de servei
// (encara queda "open" per a absorbir events un cop el modal s'auto-tanqui,
// pero no es pinta per no confondre's visualment amb el modal).
const auto* di = System::DefineInputs::get();
const bool DEFINE_ACTIVE = (di != nullptr) && di->isActive();
if (const auto* menu = System::ServiceMenu::get(); menu != nullptr && !DEFINE_ACTIVE) {
menu->draw(); // service menu: per damunt fins i tot dels toasts
}
if (di != nullptr) {
di->draw(); // overlay de rebind: per damunt de tot
}
sdl_->present();
return SDL_APP_CONTINUE;
}
+106 -1
View File
@@ -5,11 +5,13 @@
#include <iostream>
#include "core/input/define_inputs.hpp"
#include "core/input/input.hpp"
#include "core/input/mouse.hpp"
#include "core/locale/locale.hpp"
#include "core/rendering/sdl_manager.hpp"
#include "core/system/notifier.hpp"
#include "core/system/service_menu.hpp"
#include "game/config_yaml.hpp"
#include "scene_context.hpp"
@@ -19,6 +21,81 @@ using SceneType = SceneContext::SceneType;
namespace GlobalEvents {
namespace {
// Reenvia events al menu de servei si esta obert. Accepta:
// - KEY_DOWN (excepte F1-F12 i ESC, que sempre passen com a globals)
// - GAMEPAD_BUTTON_DOWN (per navegacio amb dpad + FIRE/ACCELERATE)
// - GAMEPAD_AXIS_MOTION (per navegacio amb stick)
// Retorna true si l'event s'ha entregat al menu.
auto forwardToServiceMenu(const SDL_Event& event) -> bool {
auto* menu = System::ServiceMenu::get();
if (menu == nullptr || !menu->isOpen()) {
return false;
}
if (event.type == SDL_EVENT_KEY_DOWN) {
const SDL_Scancode SC = event.key.scancode;
const bool PASSTHROUGH = (SC == SDL_SCANCODE_ESCAPE) ||
(SC >= SDL_SCANCODE_F1 && SC <= SDL_SCANCODE_F12);
if (PASSTHROUGH) {
return false;
}
menu->handleEvent(event);
return true;
}
if (event.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN ||
event.type == SDL_EVENT_GAMEPAD_AXIS_MOTION) {
menu->handleEvent(event);
return true;
}
return false;
}
// Engoleix els events que DefineInputs vol consumir mentre l'overlay
// es actiu. Els events que el modul torna a passar (QUIT, ESC) cauen
// cap al pipeline normal i poden tancar la finestra o obrir el prompt
// d'eixida sense haver de completar la sequencia.
auto consumeIfDefineActive(const SDL_Event& event) -> bool {
auto* di = System::DefineInputs::get();
if (di == nullptr || !di->isActive()) {
return false;
}
return di->handleEvent(event);
}
// Botó MENU al mando d'algun jugador → alterna el menú de servei
// (mateix comportament que F12 al teclat). Retorna true si l'event és
// un GAMEPAD_BUTTON_DOWN consumit.
auto handleGamepadMenuButton(const SDL_Event& event) -> bool {
if (event.type != SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
return false;
}
auto* input = Input::get();
if (input == nullptr) {
return false;
}
auto match_player = [&](int player_index) {
auto pad = input->getPlayerGamepad(player_index);
if (!pad || pad->instance_id != event.gbutton.which) {
return false;
}
auto it = pad->bindings.find(InputAction::MENU);
if (it == pad->bindings.end()) {
return false;
}
return it->second.button == static_cast<int>(event.gbutton.button);
};
if (!match_player(0) && !match_player(1)) {
return false;
}
if (auto* menu = System::ServiceMenu::get(); menu != nullptr) {
menu->toggle();
}
return true;
}
} // namespace
auto handle(const SDL_Event& event, SDLManager& sdl, SceneContext& context) -> bool {
// 1. Permitir que Input procese el evento (para hotplug de gamepads)
auto event_msg = Input::get()->handleEvent(event);
@@ -26,6 +103,12 @@ namespace GlobalEvents {
std::cout << "[Input] " << event_msg << '\n';
}
// 1b. Si l'overlay de redefinicio esta actiu, engoleix tots els events
// (cap arriba al joc, al menu de servei ni als hotkeys F1-F12).
if (consumeIfDefineActive(event)) {
return true;
}
// 2. Procesar SDL_EVENT_QUIT directamente (no es input de juego)
if (event.type == SDL_EVENT_QUIT) {
context.setNextScene(SceneType::EXIT);
@@ -36,7 +119,20 @@ namespace GlobalEvents {
// 3. Gestió del ratolí (auto-ocultar)
Mouse::handleEvent(event);
// 4. Procesar acciones globales directamente desde eventos SDL
// 3b. Botó MENU al mando (equivalent a F12)
if (handleGamepadMenuButton(event)) {
return true;
}
// 4. Service Menu (F12): consumeix tot KEY_DOWN excepte tecles de
// funció (F1-F12) i ESC, que continuen sent globals (zoom, fullscreen,
// vsync, AA, postfx, locale, exit prompt). Aixi el menu captura
// ENTER/BACKSPACE/UP/DOWN/LEFT/RIGHT i lletres mentre esta obert.
if (forwardToServiceMenu(event)) {
return true;
}
// 5. Procesar acciones globales directamente desde eventos SDL
// (NO usar Input::checkAction() para evitar desfase de timing)
if (event.type == SDL_EVENT_KEY_DOWN) {
switch (event.key.scancode) {
@@ -84,6 +180,15 @@ namespace GlobalEvents {
return true;
}
case SDL_SCANCODE_F12: {
// Toggle del menu de servei. Sempre passa com a global
// (alterna obert/tancat des de qualsevol escena).
if (auto* menu = System::ServiceMenu::get(); menu != nullptr) {
menu->toggle();
}
return true;
}
case SDL_SCANCODE_ESCAPE: {
// Doble pulsació per confirmar sortida: la primera ESC
// dispara un toast d'avís; només si aquest toast concret
+61
View File
@@ -0,0 +1,61 @@
// relaunch.cpp - Implementacio del reinici en calent
// © 2026 JailDesigner
#include "core/system/relaunch.hpp"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <iostream>
#ifdef _WIN32
#include <process.h> // _execv
#else
#include <unistd.h> // execv
#endif
namespace {
// Estat global (process-scope). Aquesta TU es la unica que gestiona el
// reinici, aixi que els static interns no s'escapen.
char** g_argv = nullptr;
bool g_requested = false;
} // namespace
namespace System::Relaunch {
void setArgv(int /*argc*/, char** argv) {
g_argv = argv;
}
void request() {
g_requested = true;
}
auto isRequested() -> bool {
return g_requested;
}
void execIfRequested() {
#ifdef __EMSCRIPTEN__
// Al navegador el reinici real seria location.reload(); aqui no fem res.
return;
#else
if (!g_requested || g_argv == nullptr || g_argv[0] == nullptr) {
return;
}
std::cout << "[Relaunch] Reiniciant " << g_argv[0] << "...\n";
#ifdef _WIN32
_execv(g_argv[0], g_argv);
#else
execv(g_argv[0], g_argv);
#endif
// Si arribem aqui, execv ha fallat. Tots els subsistemes ja estan
// destruits; sortim amb error i el shell rebra el codi.
std::cerr << "[Relaunch] Ha fallat: " << std::strerror(errno) << '\n';
std::exit(EXIT_FAILURE);
#endif
}
} // namespace System::Relaunch
+33
View File
@@ -0,0 +1,33 @@
// relaunch.hpp - Reinici en calent del proces (execv)
// © 2026 JailDesigner
//
// Helper desacoblat per a permetre que el menu de servei demani un reinici
// sense conèixer Director ni main.cpp. Patro:
//
// main() → Relaunch::setArgv(argc, argv) (a l'arrencada)
// ServiceMenu → Relaunch::request() (en activar REINICIAR)
// main() → Relaunch::execIfRequested() (a SDL_AppQuit)
//
// L'execv() reemplaca el proces actual: si torna, ha fallat. A EMSCRIPTEN
// no es pot reiniciar; isRequested() seguira dient true pero execIfRequested
// sera no-op.
#pragma once
namespace System::Relaunch {
// Emmagatzema l'argv original. Cal cridar-ho una vegada des de main.
void setArgv(int argc, char** argv);
// Demana un reinici (no actua immediatament; nomes marca el flag).
void request();
// Consulta del flag.
[[nodiscard]] auto isRequested() -> bool;
// Si hi ha reinici demanat i tenim argv valid, fa execv. En cas d'exit
// no torna. Si execv falla, registra l'error i torna; el caller hauria
// de sortir normalment.
void execIfRequested();
} // namespace System::Relaunch
File diff suppressed because it is too large Load Diff
+179
View File
@@ -0,0 +1,179 @@
// service_menu.hpp - Menu de servei (singleton)
// © 2026 JailDesigner
//
// Overlay de configuracio global accessible amb F12 des de qualsevol escena
// (LOGO, TITLE, GAME). Captura tots els KEY_DOWN excepte F1-F12 i ESC, que
// continuen arribant a GlobalEvents. Mentre esta obert, GameScene::update()
// fa early return per pausar el joc; LOGO i TITLE continuen renderitzant-se
// sota el menu.
//
// Arquitectura inspirada en aee_arcade service_menu.{hpp,cpp}: pila de
// pagines amb cursor, animacio open/close amb easing easeOutQuad i clipping
// del contingut mentre la caixa creix/decreix.
//
// API singleton equivalent a Notifier: init() al startup amb un renderer,
// get() retorna el punter, destroy() al teardown.
#pragma once
#include <SDL3/SDL.h>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include "core/graphics/vector_text.hpp"
#include "core/rendering/render_context.hpp"
class SDLManager;
namespace System {
class DebugOverlay;
class ServiceMenu {
public:
// Tipus d'item de menu. En aquesta iteracio nomes s'usen SUBMENU i
// LABEL; la resta queden reservats per a iteracions futures (toggles
// de vsync/zoom, picker d'idioma, restart, exit...).
enum class Kind : std::uint8_t {
LABEL, // No interactiu, nomes es dibuixa
TOGGLE, // bool flip — reservat
CYCLE, // index amb modul — reservat
INT_RANGE, // step ± — reservat
SUBMENU, // pushPage en activar — usat
ACTION // call al lambda en activar — reservat
};
struct Item {
Kind kind = Kind::LABEL;
std::string label_key; // Clau de locale (s'ignora si label_text no esta buit)
std::string label_text; // Text literal (no locale). Util per a labels que no necessiten traduccio (resolucions, etc.)
bool selectable = true;
// SUBMENU / ACTION: callback en ENTER / RIGHT.
std::function<void()> on_activate;
// TOGGLE / CYCLE / INT_RANGE: text del valor actual (renderitzat a la dreta).
std::function<std::string()> get_value_text;
// TOGGLE / CYCLE / INT_RANGE: callback amb +1 (RIGHT/ENTER) o -1 (LEFT).
std::function<void(int)> on_change;
};
struct Page {
std::string title_key;
// Subtitol opcional, renderitzat sota el titol amb tipografia mes
// petita i color apagat. Es una funcio perque pot ser dinamic
// (versio+hash, etc.). Si esta buit, no es renderitza.
std::function<std::string()> subtitle_provider;
std::vector<Item> items;
std::size_t cursor = 0;
};
// Inicialitza el singleton amb el renderer global, l'SDLManager (video
// toggles: fullscreen, vsync, AA, postfx, zoom) i el DebugOverlay
// (toggle del HUD de debug a OPCIONS). Tots propietat del Director.
static void init(Rendering::Renderer* renderer, SDLManager* sdl, DebugOverlay* debug_overlay);
static void destroy();
[[nodiscard]] static auto get() -> ServiceMenu*;
// F12: alterna obrir/tancar amb animacio.
void toggle();
[[nodiscard]] auto isOpen() const -> bool;
void update(float delta_time);
void draw() const;
// Processa events de navegacio. Retorna true si l'event s'ha consumit.
// Accepta:
// - SDL_EVENT_KEY_DOWN: UP/DOWN/ENTER/RIGHT/LEFT/BACKSPACE.
// - SDL_EVENT_GAMEPAD_BUTTON_DOWN: DPAD per nav, FIRE = ENTER,
// ACCELERATE = BACK. La resta de botons s'ignoren.
// - SDL_EVENT_GAMEPAD_AXIS_MOTION: stick X/Y amb edge-detect.
auto handleEvent(const SDL_Event& event) -> bool;
private:
ServiceMenu(Rendering::Renderer* renderer, SDLManager* sdl, DebugOverlay* debug_overlay);
// Sub-handlers de handleEvent. Privats, no son part de l'API publica.
auto handleKeyDown(const SDL_Event& event) -> bool;
auto handleGamepadButton(const SDL_Event& event) -> bool;
auto handleGamepadAxis(const SDL_Event& event) -> bool;
// Helpers per a cada eix; permeten que handleGamepadAxis es quedi
// com a dispatcher i no bote el llindar de complexitat.
void processStickX(Sint16 val);
void processStickY(Sint16 val);
void processTriggerEdge(SDL_JoystickID which, Sint16 val, int virtual_button, bool& held);
void buildRootPage();
[[nodiscard]] auto buildVideoPage() -> Page;
[[nodiscard]] auto buildResolutionPage() const -> Page;
[[nodiscard]] static auto buildAudioPage() -> Page;
[[nodiscard]] auto buildOptionsPage() const -> Page;
[[nodiscard]] auto buildSystemPage() -> Page;
[[nodiscard]] auto buildControlsPage() -> Page;
// Llista de mandos detectats per a un jugador. Cada item assigna el
// pad triat (amb swap automatic si l'altre jugador ja el tenia) i
// tanca la picker amb popPage. L'ultim item es "SENSE MANDO" per a
// desasignar.
[[nodiscard]] auto buildPadPickerPage(int player_index) -> Page;
// Pagina de confirmacio "ESTAS SEGUR? NO/SI". on_yes s'executa si
// l'usuari selecciona SI; el cursor per defecte apunta a NO.
void pushConfirmPage(const std::string& title_key, std::function<void()> on_yes);
void pushPage(Page page);
void popPage();
void moveCursor(int direction);
void activateCurrent();
// RIGHT (direction=+1) / LEFT (direction=-1). Per a TOGGLE/CYCLE/INT_RANGE
// crida on_change. Per a SUBMENU/ACTION nomes +1 (entra/activa).
void changeValue(int direction);
// Alçada objectiu de la caixa per a la pagina superior (sense animacio).
[[nodiscard]] auto computeTargetHeight() const -> float;
// Ample objectiu de la caixa per a la pagina superior (sense animacio).
// Pren com a base BOX_WIDTH_MIN i s'eixampla si algun text no hi cap.
[[nodiscard]] auto computeTargetWidth() const -> float;
// Y (top) de l'item index dins una caixa col·locada a box_y. Si la
// pagina te subtitol, els items es desplacen cap avall.
[[nodiscard]] static auto computeItemTopY(float box_y, std::size_t index, bool has_subtitle) -> float;
Rendering::Renderer* renderer_;
SDLManager* sdl_;
DebugOverlay* debug_overlay_;
Graphics::VectorText text_;
std::vector<Page> stack_;
bool open_ = false;
bool closing_ = false;
float open_anim_ = 0.0F; // 0..1 raw (sense easing)
float animated_h_ = 0.0F; // Alçada animada amb smoothing exponencial
float animated_w_ = 0.0F; // Ample animat (eixampla segons contingut)
// Estat del highlight (rectangle del cursor). Es lerpa cap a l'item
// actiu amb ease-out exponencial; quan el cursor "salta" (open o
// push/pop de pagina), s'enganxa directament al nou objectiu.
float highlight_y_ = 0.0F;
float highlight_h_ = 0.0F;
bool highlight_snap_ = true;
// Edge-detect de stick analogic per a navegacio. Una sola activacio
// per direccio: cal tornar a centre (sota el llindar) per disparar
// una altra. Compartit entre tots els pads — qualsevol jugador pot
// navegar el menu.
bool stick_left_held_ = false;
bool stick_right_held_ = false;
bool stick_up_held_ = false;
bool stick_down_held_ = false;
// Edge-detect dels triggers L2/R2 com a botons virtuals. SDL3 no
// emet button events per als triggers; els llegim com a axis i
// sintetitzem una pulsacio quan creuen el llindar.
bool trigger_l2_held_ = false;
bool trigger_r2_held_ = false;
static std::unique_ptr<ServiceMenu> instance;
};
} // namespace System
+23
View File
@@ -0,0 +1,23 @@
// string_utils.hpp - Utilitats genèriques de cadenes
// © 2026 JailDesigner
//
// VectorText només admet ASCII en majúscules; les notificacions, el menú
// de servei i l'overlay de rebind passen els textos dinàmics per aquest
// helper abans de pintar-los.
#pragma once
#include <cctype>
#include <string>
namespace Utils {
inline auto toUpperAscii(const std::string& s) -> std::string {
std::string result = s;
for (char& c : result) {
c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
}
return result;
}
} // namespace Utils
+68 -4
View File
@@ -5,6 +5,7 @@
#include <string>
#include <unordered_map>
#include "core/defaults/audio.hpp"
#include "core/defaults/rendering.hpp"
#include "core/defaults/window.hpp"
#include "external/fkyaml_node.hpp"
@@ -17,6 +18,7 @@ namespace ConfigYaml {
// Permeten escriure window.width en lloc d'engine_config.window.width.
Config::WindowConfig& window = engine_config.window;
Config::RenderingConfig& rendering = engine_config.rendering;
Config::AudioConfig& audio = engine_config.audio;
Config::PlayerBindings& player1 = engine_config.player1;
Config::PlayerBindings& player2 = engine_config.player2;
bool& console = engine_config.console;
@@ -209,6 +211,14 @@ namespace ConfigYaml {
rendering.render_width = Defaults::Rendering::RENDER_WIDTH_DEFAULT;
rendering.render_height = Defaults::Rendering::RENDER_HEIGHT_DEFAULT;
// Audio
audio.enabled = Defaults::Audio::ENABLED;
audio.volume = Defaults::Audio::VOLUME;
audio.music_enabled = Defaults::Audio::MUSIC_ENABLED;
audio.music_volume = Defaults::Audio::MUSIC_VOLUME;
audio.sound_enabled = Defaults::Audio::SOUND_ENABLED;
audio.sound_volume = Defaults::Audio::SOUND_VOLUME;
// Idioma
locale = "ca";
@@ -307,6 +317,22 @@ namespace ConfigYaml {
}
}
static void loadAudioConfigFromYaml(const fkyaml::node& yaml) {
if (!yaml.contains("audio")) {
return;
}
const auto& aud = yaml["audio"];
auto in_unit_range = [](float v) { return v >= 0.0F && v <= 1.0F; };
readField(aud, "enabled", audio.enabled, Defaults::Audio::ENABLED);
readField(aud, "volume", audio.volume, Defaults::Audio::VOLUME, in_unit_range);
readField(aud, "music_enabled", audio.music_enabled, Defaults::Audio::MUSIC_ENABLED);
readField(aud, "music_volume", audio.music_volume, Defaults::Audio::MUSIC_VOLUME, in_unit_range);
readField(aud, "sound_enabled", audio.sound_enabled, Defaults::Audio::SOUND_ENABLED);
readField(aud, "sound_volume", audio.sound_volume, Defaults::Audio::SOUND_VOLUME, in_unit_range);
}
// Carregar controls del player 1 desde YAML
static void loadPlayer1ControlsFromYaml(const fkyaml::node& yaml) {
if (!yaml.contains("player1")) {
@@ -347,12 +373,21 @@ namespace ConfigYaml {
if (gp.contains("button_shoot")) {
player1.gamepad.button_shoot = stringToButton(gp["button_shoot"].get_value<std::string>());
}
if (gp.contains("button_start")) {
player1.gamepad.button_start = stringToButton(gp["button_start"].get_value<std::string>());
}
if (gp.contains("button_menu")) {
player1.gamepad.button_menu = stringToButton(gp["button_menu"].get_value<std::string>());
}
}
// Carregar nom del gamepad
// Carregar nom i path del gamepad assignat
if (p1.contains("gamepad_name")) {
player1.gamepad_name = p1["gamepad_name"].get_value<std::string>();
}
if (p1.contains("gamepad_path")) {
player1.gamepad_path = p1["gamepad_path"].get_value<std::string>();
}
}
// Carregar controls del player 2 desde YAML
@@ -395,12 +430,21 @@ namespace ConfigYaml {
if (gp.contains("button_shoot")) {
player2.gamepad.button_shoot = stringToButton(gp["button_shoot"].get_value<std::string>());
}
if (gp.contains("button_start")) {
player2.gamepad.button_start = stringToButton(gp["button_start"].get_value<std::string>());
}
if (gp.contains("button_menu")) {
player2.gamepad.button_menu = stringToButton(gp["button_menu"].get_value<std::string>());
}
}
// Carregar nom del gamepad
// Carregar nom i path del gamepad assignat
if (p2.contains("gamepad_name")) {
player2.gamepad_name = p2["gamepad_name"].get_value<std::string>();
}
if (p2.contains("gamepad_path")) {
player2.gamepad_path = p2["gamepad_path"].get_value<std::string>();
}
}
// Carregar configuración des del file YAML
@@ -447,6 +491,7 @@ namespace ConfigYaml {
// Carregar seccions
loadWindowConfigFromYaml(yaml);
loadRenderingConfigFromYaml(yaml);
loadAudioConfigFromYaml(yaml);
loadPlayer1ControlsFromYaml(yaml);
loadPlayer2ControlsFromYaml(yaml);
@@ -479,6 +524,17 @@ namespace ConfigYaml {
}
}
static void saveAudioConfigToYaml(std::ofstream& file) {
file << "# AUDIO\n";
file << "audio:\n";
file << " enabled: " << (audio.enabled ? "true" : "false") << " # ON/OFF general\n";
file << " volume: " << audio.volume << " # Master 0.0-1.0\n";
file << " music_enabled: " << (audio.music_enabled ? "true" : "false") << "\n";
file << " music_volume: " << audio.music_volume << " # 0.0-1.0\n";
file << " sound_enabled: " << (audio.sound_enabled ? "true" : "false") << "\n";
file << " sound_volume: " << audio.sound_volume << " # 0.0-1.0\n\n";
}
// Guardar controls del player 1 a YAML
static void savePlayer1ControlsToYaml(std::ofstream& file) {
file << "# CONTROLS JUGADOR 1\n";
@@ -493,7 +549,10 @@ namespace ConfigYaml {
file << " button_right: " << buttonToString(player1.gamepad.button_right) << "\n";
file << " button_thrust: " << buttonToString(player1.gamepad.button_thrust) << "\n";
file << " button_shoot: " << buttonToString(player1.gamepad.button_shoot) << "\n";
file << " gamepad_name: \"" << player1.gamepad_name << "\" # Buit = primer disponible\n\n";
file << " button_start: " << buttonToString(player1.gamepad.button_start) << "\n";
file << " button_menu: " << buttonToString(player1.gamepad.button_menu) << "\n";
file << " gamepad_name: \"" << player1.gamepad_name << "\" # Buit = primer disponible\n";
file << " gamepad_path: \"" << player1.gamepad_path << "\" # Prioritari sobre name\n\n";
}
// Guardar controls del player 2 a YAML
@@ -510,7 +569,10 @@ namespace ConfigYaml {
file << " button_right: " << buttonToString(player2.gamepad.button_right) << "\n";
file << " button_thrust: " << buttonToString(player2.gamepad.button_thrust) << "\n";
file << " button_shoot: " << buttonToString(player2.gamepad.button_shoot) << "\n";
file << " gamepad_name: \"" << player2.gamepad_name << "\" # Buit = segon disponible\n\n";
file << " button_start: " << buttonToString(player2.gamepad.button_start) << "\n";
file << " button_menu: " << buttonToString(player2.gamepad.button_menu) << "\n";
file << " gamepad_name: \"" << player2.gamepad_name << "\" # Buit = segon disponible\n";
file << " gamepad_path: \"" << player2.gamepad_path << "\" # Prioritari sobre name\n\n";
}
// Guardar configuración al file YAML
@@ -549,6 +611,8 @@ namespace ConfigYaml {
file << "# IDIOMA\n";
file << "locale: " << locale << " # ca | en\n\n";
saveAudioConfigToYaml(file);
// Guardar controls de jugadors
savePlayer1ControlsToYaml(file);
savePlayer2ControlsToYaml(file);
+1
View File
@@ -28,6 +28,7 @@ namespace ConfigYaml {
.key_start = SDL_SCANCODE_2,
},
.gamepad_name = "",
.gamepad_path = "",
},
};
+24 -34
View File
@@ -14,38 +14,39 @@
#include "core/rendering/shape_renderer.hpp"
#include "core/types.hpp"
#include "game/constants.hpp"
#include "game/entities/bullet_config.hpp"
#include "game/entities/bullet_registry.hpp"
Bullet::Bullet(Rendering::Renderer* renderer)
: Entity(renderer) {
// Brightness específico para balas
: Entity(renderer),
config_(&BulletRegistry::get()) {
brightness_ = Defaults::Brightness::BALA;
// Configuración del cuerpo físico.
// Las balas son cinemáticas: no colisionan con otros bodies ni paredes.
// El gameplay (GameScene) gestiona los hits con check_collision y la
// salida del PLAYAREA. Por eso radius=0 en el world (no participa en
// resolveBodyCollisions ni resolveBoundsCollisions).
body_.setMass(0.5F); // Ligera (no afecta a nadie, pero por consistencia)
body_.radius = 0.0F; // Sin colisión física (cinemática pura)
body_.restitution = 0.0F; // Irrelevante (no rebota)
body_.linear_damping = 0.0F; // Sin fricción (movimiento rectilíneo uniforme)
body_.angular_damping = 0.0F;
// Cinemàtiques pures: no col·lisionen al PhysicsWorld (body_.radius = 0).
// El gameplay (GameScene) gestiona els hits via checkCollisionSwept i la
// sortida del PLAYAREA.
body_.setMass(config_->physics.mass);
body_.radius = 0.0F;
body_.restitution = config_->physics.restitution;
body_.linear_damping = config_->physics.linear_damping;
body_.angular_damping = config_->physics.angular_damping;
// Cargar shape compartida desde archivo
shape_ = Graphics::ShapeLoader::load("bullet.shp");
shape_ = Graphics::ShapeLoader::load(config_->shape.path);
if (!shape_ || !shape_->isValid()) {
std::cerr << "[Bullet] Error: no s'ha pogut load bullet.shp" << '\n';
std::cerr << "[Bullet] Error: no s'ha pogut carregar " << config_->shape.path << '\n';
}
// Radi de col·lisió derivat del cercle circumscrit de la shape.
const float BOUNDING = (shape_ != nullptr) ? shape_->getBoundingRadius() : 0.0F;
collision_radius_ = BOUNDING * config_->shape.scale * config_->shape.collision_factor;
}
void Bullet::init() {
// Inicialment inactiva
is_active_ = false;
center_ = {.x = 0.0F, .y = 0.0F};
prev_position_ = {.x = 0.0F, .y = 0.0F};
angle_ = 0.0F;
// Reset del cuerpo físico
body_.position = Vec2{};
body_.velocity = Vec2{};
body_.angle = 0.0F;
@@ -53,29 +54,24 @@ void Bullet::init() {
body_.clearAccumulators();
}
void Bullet::fire(const Vec2& position, float angle, uint8_t owner_id) {
// Activar bullet
void Bullet::fire(const Vec2& position, float angle, uint8_t owner_id, float bullet_speed) {
is_active_ = true;
// Almacenar propietario (0=P1, 1=P2)
owner_id_ = owner_id;
// Posición y orientación iniciales = ship
center_ = position;
prev_position_ = position; // Al spawn no hi ha moviment encara: swept degenera a punt-cercle
prev_position_ = position; // spawn: swept degenera a punt-cercle
angle_ = angle;
// Sincronizar el body físico: posición + velocidad cartesiana
// angle - PI/2 porque angle=0 apunta hacia arriba (eje Y negativo SDL)
// Sincronizar el body físic: posició + velocitat cartesiana.
// angle - PI/2 perquè angle=0 apunta cap amunt (eje Y negatiu SDL).
body_.position = position;
body_.angle = angle;
const float DIR_X = std::cos(angle - (Constants::PI / 2.0F));
const float DIR_Y = std::sin(angle - (Constants::PI / 2.0F));
body_.velocity = Vec2{.x = DIR_X * Defaults::Game::BULLET_SPEED, .y = DIR_Y * Defaults::Game::BULLET_SPEED};
body_.velocity = Vec2{.x = DIR_X * bullet_speed, .y = DIR_Y * bullet_speed};
body_.angular_velocity = 0.0F;
body_.clearAccumulators();
// Reproducir sonido de disparo láser
Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME);
}
@@ -87,24 +83,18 @@ void Bullet::update(float /*delta_time*/) {
}
void Bullet::postUpdate(float /*delta_time*/) {
// Captura la posició al final del frame anterior abans de sobreescriure center_;
// així el sistema de col·lisions pot fer swept (segment-vs-cercle) entre prev_position_
// i la nova center_, evitant tunneling a velocitats altes.
prev_position_ = center_;
center_ = body_.position;
// angle_ no cambia (las balas no rotan visualmente).
}
void Bullet::desactivar() {
is_active_ = false;
// Detener el cuerpo físico para que no acumule deriva mientras inactiva.
body_.velocity = Vec2{};
body_.angular_velocity = 0.0F;
}
void Bullet::draw() const {
if (is_active_ && shape_) {
// Les bales roten segons l'angle de trayectòria (estático tras disparo)
Rendering::renderShape(renderer_, shape_, center_, angle_, 1.0F, 1.0F, brightness_, Defaults::Palette::BULLET);
Rendering::renderShape(renderer_, shape_, center_, angle_, 1.0F, 1.0F, brightness_, config_->colors.normal);
}
}
+13 -11
View File
@@ -6,10 +6,12 @@
#include <cstdint>
#include "core/defaults.hpp"
#include "core/entities/entity.hpp"
#include "core/types.hpp"
// Forward declaration — la definició completa s'inclou només al .cpp.
struct BulletConfig;
class Bullet : public Entities::Entity {
public:
Bullet()
@@ -17,7 +19,7 @@ class Bullet : public Entities::Entity {
explicit Bullet(Rendering::Renderer* renderer);
void init() override;
void fire(const Vec2& position, float angle, uint8_t owner_id);
void fire(const Vec2& position, float angle, uint8_t owner_id, float bullet_speed);
void update(float delta_time) override;
void postUpdate(float delta_time) override;
void draw() const override;
@@ -25,25 +27,25 @@ class Bullet : public Entities::Entity {
// Override: Interfaz de Entity
[[nodiscard]] auto isActive() const -> bool override { return is_active_; }
// Override: Interfaz de colisión (gameplay-level: PLAYAREA bounds-check)
[[nodiscard]] auto getCollisionRadius() const -> float override {
return Defaults::Entities::BULLET_RADIUS;
}
// Override: Interfaz de colisión (radi derivat al ctor des del shape).
[[nodiscard]] auto getCollisionRadius() const -> float override { return collision_radius_; }
[[nodiscard]] auto isCollidable() const -> bool override {
return is_active_;
}
// Configuració associada (vàlida des del ctor — la bala l'agafa del BulletRegistry).
[[nodiscard]] auto getConfig() const -> const BulletConfig& { return *config_; }
// Getters (API pública sin cambios)
[[nodiscard]] auto getOwnerId() const -> uint8_t { return owner_id_; }
// Posició al final del frame anterior, per a CCD segment-vs-cercle.
[[nodiscard]] auto getPrevPosition() const -> const Vec2& { return prev_position_; }
void desactivar();
private:
// Miembros específicos de Bullet (heredados: renderer_, shape_, center_, angle_, brightness_, body_).
// Inicializados en la declaración para que tanto el ctor por defecto como el que toma renderer
// dejen el objeto en estado coherente (proyectil inactivo, sin owner).
const BulletConfig* config_{nullptr}; // apunta al BulletRegistry; vàlid post-ctor
float collision_radius_{0.0F}; // derivat: shape.bounding_radius × scale × collision_factor
bool is_active_{false};
uint8_t owner_id_{0}; // 0=P1, 1=P2
Vec2 prev_position_{}; // Posició al final del frame anterior (per a swept collision)
Vec2 prev_position_{}; // posició al final del frame anterior (swept CCD)
};
+80
View File
@@ -0,0 +1,80 @@
// bullet_config.cpp - Implementació del parser de BulletConfig
// © 2026 JailDesigner
#include "game/entities/bullet_config.hpp"
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
namespace {
auto parseColor(const fkyaml::node& node, SDL_Color& out) -> bool {
if (!node.is_sequence() || node.size() != 3) {
return false;
}
const auto R = node[0].get_value<uint32_t>();
const auto G = node[1].get_value<uint32_t>();
const auto B = node[2].get_value<uint32_t>();
out = SDL_Color{
.r = static_cast<uint8_t>(R),
.g = static_cast<uint8_t>(G),
.b = static_cast<uint8_t>(B),
.a = 255};
return true;
}
auto parseShape(const fkyaml::node& node, BulletConfig::ShapeCfg& out) -> bool {
if (!node.contains("shape") || !node["shape"].contains("path")) {
std::cerr << "[BulletConfig] Error: falta 'shape.path'\n";
return false;
}
const auto& s = node["shape"];
out.path = s["path"].get_value<std::string>();
out.scale = s.contains("scale") ? s["scale"].get_value<float>() : 1.0F;
out.collision_factor = s.contains("collision_factor")
? s["collision_factor"].get_value<float>()
: 1.0F;
return true;
}
auto parsePhysics(const fkyaml::node& node, BulletConfig::PhysicsCfg& out) -> bool {
if (!node.contains("physics")) {
std::cerr << "[BulletConfig] Error: falta 'physics'\n";
return false;
}
const auto& p = node["physics"];
out.mass = p["mass"].get_value<float>();
out.restitution = p["restitution"].get_value<float>();
out.linear_damping = p["linear_damping"].get_value<float>();
out.angular_damping = p["angular_damping"].get_value<float>();
out.impact_momentum_factor = p["impact_momentum_factor"].get_value<float>();
return true;
}
auto parseColors(const fkyaml::node& node, BulletConfig::ColorsCfg& out) -> bool {
if (!node.contains("colors") || !parseColor(node["colors"]["normal"], out.normal)) {
std::cerr << "[BulletConfig] Error: 'colors.normal' no és [r,g,b]\n";
return false;
}
return true;
}
} // namespace
auto BulletConfig::fromYaml(const fkyaml::node& node) -> std::optional<BulletConfig> {
try {
BulletConfig cfg;
cfg.name = node.contains("name") ? node["name"].get_value<std::string>() : "bullet";
if (!parseShape(node, cfg.shape)) { return std::nullopt; }
if (!parsePhysics(node, cfg.physics)) { return std::nullopt; }
if (!parseColors(node, cfg.colors)) { return std::nullopt; }
return cfg;
} catch (const std::exception& e) {
std::cerr << "[BulletConfig] Excepció parsejant: " << e.what() << '\n';
return std::nullopt;
}
}
+41
View File
@@ -0,0 +1,41 @@
// bullet_config.hpp - Configuració de la bala carregada des de YAML
// © 2026 JailDesigner
//
// Paral·lel a Player/EnemyConfig. Una sola instància a tot el joc (per ara);
// es comparteix entre totes les bales actives via BulletRegistry.
#pragma once
#include <SDL3/SDL.h>
#include <optional>
#include <string>
#include "external/fkyaml_node.hpp"
struct BulletConfig {
struct ShapeCfg {
std::string path;
float scale;
float collision_factor;
};
struct PhysicsCfg {
float mass;
float restitution;
float linear_damping;
float angular_damping;
float impact_momentum_factor; // factor de transferència bullet→enemic
};
struct ColorsCfg {
SDL_Color normal;
};
std::string name;
ShapeCfg shape;
PhysicsCfg physics;
ColorsCfg colors;
static auto fromYaml(const fkyaml::node& node) -> std::optional<BulletConfig>;
};
+37
View File
@@ -0,0 +1,37 @@
// bullet_registry.cpp - Implementació del registre de bala
// © 2026 JailDesigner
#include "game/entities/bullet_registry.hpp"
#include <cstdlib>
#include <iostream>
#include "core/entities/entity_loader.hpp"
BulletConfig BulletRegistry::config;
bool BulletRegistry::loaded = false;
auto BulletRegistry::load() -> bool {
auto yaml = Entities::EntityLoader::load("bullet");
if (!yaml) {
std::cerr << "[BulletRegistry] Error: no s'ha pogut carregar bullet.yaml\n";
return false;
}
auto cfg = BulletConfig::fromYaml(*yaml);
if (!cfg) {
std::cerr << "[BulletRegistry] Error: format invàlid a bullet.yaml\n";
return false;
}
config = *cfg;
loaded = true;
std::cout << "[BulletRegistry] Configuració de bala carregada.\n";
return true;
}
auto BulletRegistry::get() -> const BulletConfig& {
if (!loaded) {
std::cerr << "[BulletRegistry] FATAL: get() abans de load()\n";
std::exit(EXIT_FAILURE);
}
return config;
}
+26
View File
@@ -0,0 +1,26 @@
// bullet_registry.hpp - Registre estàtic de la configuració de la bala
// © 2026 JailDesigner
//
// Una única instància per a tota la sessió. Es manté el patró registry
// (paral·lel a EnemyRegistry) tot i ser una sola entitat: si el dia de demà
// hi ha més tipus de bala (laser/plasma/etc.) només cal estendre-ho.
#pragma once
#include "game/entities/bullet_config.hpp"
class BulletRegistry {
public:
BulletRegistry() = delete;
// Carrega data/entities/bullet/bullet.yaml. Retorna false si falla.
static auto load() -> bool;
// Accés a la configuració. Avorta amb log fatal si load() no s'ha cridat
// o ha fallat.
static auto get() -> const BulletConfig&;
private:
static BulletConfig config;
static bool loaded;
};
+88 -185
View File
@@ -14,6 +14,8 @@
#include "core/rendering/shape_renderer.hpp"
#include "core/types.hpp"
#include "game/constants.hpp"
#include "game/entities/enemy_config.hpp"
#include "game/entities/enemy_registry.hpp"
namespace {
@@ -27,99 +29,72 @@ namespace {
}
// Recupera el "ángulo equivalente" de un body en movimiento (para zigzag).
// Si está parado, devuelve 0.
auto velocityToAngle(const Vec2& velocity) -> float {
if (velocity.lengthSquared() < 0.0001F) {
return 0.0F;
}
// El movimiento (vx, vy) corresponde a angle - PI/2; invertimos.
return std::atan2(velocity.y, velocity.x) + (Constants::PI / 2.0F);
}
// Random float [0..1).
auto randFloat01() -> float {
return static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
}
// Random float [min..max).
auto randRange(float min, float max) -> float {
return min + (randFloat01() * (max - min));
}
} // namespace
Enemy::Enemy(Rendering::Renderer* renderer)
: Entity(renderer),
tracking_strength_(Defaults::Enemies::Square::TRACKING_STRENGTH) {
: Entity(renderer) {
brightness_ = Defaults::Brightness::ENEMIC;
// Configuración del cuerpo físico — defaults para enemy genérico.
// init() ajusta velocidad y masa según el tipo (Pentagon/Quadrat/Molinillo).
body_.setMass(Defaults::Enemies::Body::DEFAULT_MASS);
body_.radius = 0.0F; // 0 hasta spawn (no colisiona inactivo)
body_.restitution = Defaults::Enemies::Body::RESTITUTION;
body_.linear_damping = Defaults::Enemies::Body::LINEAR_DAMPING;
body_.angular_damping = Defaults::Enemies::Body::ANGULAR_DAMPING;
// Body queda amb defaults inocus (radius=0 = no col·lisiona) fins
// que init() apliqui la configuració del tipus carregada via Registry.
body_.radius = 0.0F;
}
void Enemy::init(EnemyType type, const Vec2* ship_pos) {
type_ = type;
config_ = &EnemyRegistry::get(type);
const EnemyConfig& cfg = *config_;
const char* shape_file = nullptr;
float base_speed = 0.0F;
float rotation_delta_min = 0.0F;
float rotation_delta_max = 0.0F;
float type_mass = Defaults::Enemies::Body::DEFAULT_MASS;
switch (type_) {
case EnemyType::PENTAGON:
shape_file = Defaults::Enemies::Pentagon::SHAPE_FILE;
base_speed = Defaults::Enemies::Pentagon::SPEED;
rotation_delta_min = Defaults::Enemies::Pentagon::ROTATION_DELTA_MIN;
rotation_delta_max = Defaults::Enemies::Pentagon::ROTATION_DELTA_MAX;
type_mass = Defaults::Enemies::Pentagon::MASS;
break;
case EnemyType::SQUARE:
shape_file = Defaults::Enemies::Square::SHAPE_FILE;
base_speed = Defaults::Enemies::Square::SPEED;
rotation_delta_min = Defaults::Enemies::Square::ROTATION_DELTA_MIN;
rotation_delta_max = Defaults::Enemies::Square::ROTATION_DELTA_MAX;
type_mass = Defaults::Enemies::Square::MASS;
tracking_timer_ = 0.0F;
break;
case EnemyType::PINWHEEL:
shape_file = Defaults::Enemies::Pinwheel::SHAPE_FILE;
base_speed = Defaults::Enemies::Pinwheel::SPEED;
rotation_delta_min = Defaults::Enemies::Pinwheel::ROTATION_DELTA_MIN;
rotation_delta_max = Defaults::Enemies::Pinwheel::ROTATION_DELTA_MAX;
type_mass = Defaults::Enemies::Pinwheel::MASS;
break;
default:
std::cerr << "[Enemy] Error: tipo desconocido ("
<< static_cast<int>(type_) << "), usando PENTAGON\n";
shape_file = Defaults::Enemies::Pentagon::SHAPE_FILE;
base_speed = Defaults::Enemies::Pentagon::SPEED;
rotation_delta_min = Defaults::Enemies::Pentagon::ROTATION_DELTA_MIN;
rotation_delta_max = Defaults::Enemies::Pentagon::ROTATION_DELTA_MAX;
break;
if (type_ == EnemyType::SQUARE) {
tracking_timer_ = 0.0F;
tracking_strength_ = cfg.behavior.tracking_strength;
}
body_.setMass(type_mass);
body_.radius = Defaults::Entities::ENEMY_RADIUS;
// Cargar shape
shape_ = Graphics::ShapeLoader::load(shape_file);
shape_ = Graphics::ShapeLoader::load(cfg.shape.path);
if (!shape_ || !shape_->isValid()) {
std::cerr << "[Enemy] Error: no se ha podido cargar " << shape_file << '\n';
std::cerr << "[Enemy] Error: no se ha podido cargar " << cfg.shape.path << '\n';
}
// Posición aleatoria con comprobación de seguridad
// Radi de col·lisió derivat del cercle circumscrit de la shape.
const float BOUNDING = (shape_ != nullptr) ? shape_->getBoundingRadius() : 0.0F;
collision_radius_ = BOUNDING * cfg.shape.scale * cfg.shape.collision_factor;
body_.setMass(cfg.physics.mass);
body_.radius = collision_radius_;
body_.restitution = cfg.physics.restitution;
body_.linear_damping = cfg.physics.linear_damping;
body_.angular_damping = cfg.physics.angular_damping;
// Posició aleatòria amb comprovació de safety_distance.
float min_x;
float max_x;
float min_y;
float max_y;
Constants::getSafePlayAreaBounds(Defaults::Entities::ENEMY_RADIUS, min_x, max_x, min_y, max_y);
Constants::getSafePlayAreaBounds(collision_radius_, min_x, max_x, min_y, max_y);
if (ship_pos != nullptr) {
bool found_safe_position = false;
for (int attempt = 0; attempt < Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS; attempt++) {
float candidate_x;
float candidate_y;
if (attemptSafeSpawn(*ship_pos, candidate_x, candidate_y)) {
if (attemptSafeSpawn(*ship_pos, collision_radius_, cfg.spawn.safety_distance, candidate_x, candidate_y)) {
center_.x = candidate_x;
center_.y = candidate_y;
found_safe_position = true;
@@ -141,32 +116,26 @@ void Enemy::init(EnemyType type, const Vec2* ship_pos) {
center_.y = static_cast<float>((std::rand() % RANGE_Y) + static_cast<int>(min_y));
}
// Dirección inicial aleatoria, velocidad escalar según tipo
const float ANGLE_INICIAL = (std::rand() % 360) * Constants::PI / 180.0F;
setVelocityFromAngle(ANGLE_INICIAL, base_speed);
const float ANGLE_INICIAL = static_cast<float>(std::rand() % 360) * Constants::PI / 180.0F;
setVelocityFromAngle(ANGLE_INICIAL, cfg.physics.speed);
// Sincronizar body_ con posición inicial
body_.position = center_;
body_.angle = 0.0F;
body_.angular_velocity = 0.0F;
body_.clearAccumulators();
// Rotación visual aleatoria (independiente del body)
const float ROTATION_DELTA_RANGE = rotation_delta_max - rotation_delta_min;
rotation_delta_ = rotation_delta_min + ((static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) * ROTATION_DELTA_RANGE);
// Rotació visual aleatòria dins del rang del tipus
rotation_delta_ = randRange(cfg.physics.rotation_delta_min, cfg.physics.rotation_delta_max);
rotation_ = 0.0F;
// Estado de animación
animation_ = EnemyAnimation();
animation_.rotation_delta_base = rotation_delta_;
animation_.rotation_delta_target = rotation_delta_;
animation_.rotation_delta_t = 1.0F;
// Invulnerabilidad post-spawn
invulnerability_timer_ = Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
brightness_ = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START;
invulnerability_timer_ = cfg.spawn.invulnerability_duration;
brightness_ = cfg.spawn.invulnerability_brightness_start;
// Timer para próximo cambio de dirección (Pentagon)
direction_change_timer_ = 0.0F;
is_active_ = true;
@@ -177,8 +146,6 @@ void Enemy::update(float delta_time) {
return;
}
// Decremento de timer "herido"; al cruzar 0 marca expiración para que el
// system layer dispare la explosión diferida.
wound_expired_this_frame_ = false;
if (wounded_timer_ > 0.0F) {
wounded_timer_ -= delta_time;
@@ -188,25 +155,24 @@ void Enemy::update(float delta_time) {
}
}
// Decremento de invulnerabilidad + LERP de brightness
if (invulnerability_timer_ > 0.0F) {
invulnerability_timer_ -= delta_time;
invulnerability_timer_ = std::max(invulnerability_timer_, 0.0F);
const float T_INV = invulnerability_timer_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
const float T_INV = invulnerability_timer_ / config_->spawn.invulnerability_duration;
const float T = 1.0F - T_INV;
const float SMOOTH_T = T * T * (3.0F - (2.0F * T));
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START;
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_END;
const float START = config_->spawn.invulnerability_brightness_start;
const float END = config_->spawn.invulnerability_brightness_end;
brightness_ = START + ((END - START) * SMOOTH_T);
}
// Comportamiento por tipo (ajusta body_.velocity, NO mueve posición).
// Skip cuando está herido: el enemy és un "cos mort" inert, només
// respon a la inèrcia del impulse rebut i a les col·lisions físiques.
if (!isWounded()) {
switch (type_) {
case EnemyType::PENTAGON:
case EnemyType::STAR:
// STAR reusa el zigzag esquivador de Pentagon. Si en el futur
// vol comportament propi, separa-li el cas.
behaviorPentagon(delta_time);
break;
case EnemyType::SQUARE:
@@ -218,15 +184,12 @@ void Enemy::update(float delta_time) {
}
}
// Animaciones (palpitación + rotación acelerada)
updateAnimation(delta_time);
// Rotación visual (decoración, no afecta movimiento)
rotation_ += rotation_delta_ * delta_time;
}
void Enemy::postUpdate(float /*delta_time*/) {
// Sincronizar mirror tras la integración del world.
if (is_active_) {
center_ = body_.position;
}
@@ -236,27 +199,14 @@ void Enemy::draw() const {
if (!is_active_ || !shape_) {
return;
}
const float SCALE = computeCurrentScale();
SDL_Color color{};
switch (type_) {
case EnemyType::PENTAGON:
color = Defaults::Palette::PENTAGON;
break;
case EnemyType::SQUARE:
color = Defaults::Palette::SQUARE;
break;
case EnemyType::PINWHEEL:
color = Defaults::Palette::PINWHEEL;
break;
}
const float SCALE = config_->shape.scale * computeCurrentScale();
SDL_Color color = config_->colors.normal;
// Parpadeo dorado mientras está herido: alterna color de tipo ↔ dorado
// a Wounded::BLINK_HZ usando el timer (fmod sobre el periodo).
if (wounded_timer_ > 0.0F) {
const float CYCLE = 1.0F / Defaults::Enemies::Wounded::BLINK_HZ;
const float CYCLE = 1.0F / config_->wounded.blink_hz;
const float T = std::fmod(wounded_timer_, CYCLE);
if (T < (CYCLE / 2.0F)) {
color = Defaults::Palette::WOUNDED;
color = config_->colors.wounded;
}
}
@@ -267,17 +217,15 @@ void Enemy::destroy() {
is_active_ = false;
body_.velocity = Vec2{};
body_.angular_velocity = 0.0F;
body_.radius = 0.0F; // No colisiona mientras está inactivo
body_.radius = 0.0F;
wounded_timer_ = 0.0F;
wound_expired_this_frame_ = false;
last_hit_by_ = 0xFF;
}
void Enemy::hurt(uint8_t shooter_id) {
wounded_timer_ = Defaults::Enemies::Wounded::DURATION;
wounded_timer_ = config_->wounded.duration;
last_hit_by_ = shooter_id;
// El so HIT ara el reprodueix la bala quan es trenca en debris
// (Systems::Collision::breakBullet), no l'enemic en entrar a HURT.
}
void Enemy::applyImpulse(const Vec2& impulse) {
@@ -285,13 +233,11 @@ void Enemy::applyImpulse(const Vec2& impulse) {
}
void Enemy::setVelocity(float speed) {
// Mantener la dirección actual del body, cambiar solo la magnitud.
const float CURRENT_SPEED = body_.velocity.length();
if (CURRENT_SPEED > 0.0F) {
body_.velocity = body_.velocity * (speed / CURRENT_SPEED);
} else {
// Sin dirección actual: usar ángulo aleatorio
const float A = (std::rand() % 360) * Constants::PI / 180.0F;
const float A = static_cast<float>(std::rand() % 360) * Constants::PI / 180.0F;
setVelocityFromAngle(A, speed);
}
}
@@ -300,19 +246,14 @@ void Enemy::setVelocityFromAngle(float angle_movement, float speed) {
body_.velocity = angleToDirection(angle_movement) * speed;
}
// PENTAGON: zigzag esquivador. Cambios de dirección periódicos (probabilísticos)
// en lugar de detectar paredes; el rebote contra muros lo hace PhysicsWorld
// con restitution=1.0.
// PENTAGON: zigzag esquivador. Canvis de direcció periòdics (probabilístics)
// en lloc de detectar parets; el rebot contra murs el fa PhysicsWorld.
void Enemy::behaviorPentagon(float delta_time) {
direction_change_timer_ += delta_time;
// Probabilidad de zigzag por segundo (calibrada para sensación equivalente
// a la versión vieja que disparaba en cada toque de pared).
const float RAND_VAL = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
if (RAND_VAL < Defaults::Enemies::Pentagon::ZIGZAG_PROB_PER_SECOND * delta_time) {
if (randFloat01() < config_->behavior.zigzag_prob_per_second * delta_time) {
const float CURRENT_ANGLE = velocityToAngle(body_.velocity);
const float DELTA = (static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) *
Defaults::Enemies::Pentagon::ANGLE_CHANGE_MAX;
const float DELTA = randFloat01() * config_->behavior.angle_change_max;
const float NEW_ANGLE = CURRENT_ANGLE + ((std::rand() % 2 == 0) ? DELTA : -DELTA);
const float SPEED = body_.velocity.length();
setVelocityFromAngle(NEW_ANGLE, SPEED);
@@ -320,12 +261,11 @@ void Enemy::behaviorPentagon(float delta_time) {
}
}
// SQUARE: tracking discreto cada TRACKING_INTERVAL. Ajusta dirección
// hacia el ship mezclando con tracking_strength_.
// SQUARE: tracking discret cap a la nau cada N segons.
void Enemy::behaviorSquare(float delta_time) {
tracking_timer_ += delta_time;
if (tracking_timer_ >= Defaults::Enemies::Square::TRACKING_INTERVAL && ship_position_ != nullptr) {
if (tracking_timer_ >= config_->behavior.tracking_interval && ship_position_ != nullptr) {
tracking_timer_ = 0.0F;
const Vec2 TO_SHIP = *ship_position_ - center_;
@@ -335,11 +275,9 @@ void Enemy::behaviorSquare(float delta_time) {
const float SPEED = body_.velocity.length();
const Vec2 DESIRED_VEL = DESIRED_DIR * SPEED;
// Mezcla LERP: velocidad actual con la deseada según tracking_strength_.
body_.velocity = (body_.velocity * (1.0F - tracking_strength_)) +
(DESIRED_VEL * tracking_strength_);
// Renormalizar a la velocidad escalar original
const float NEW_SPEED = body_.velocity.length();
if (NEW_SPEED > 0.0F) {
body_.velocity = body_.velocity * (SPEED / NEW_SPEED);
@@ -348,21 +286,17 @@ void Enemy::behaviorSquare(float delta_time) {
}
}
// PINWHEEL: movimiento recto + boost de rotación visual cerca del ship.
// Sin tracking — solo cambios de dirección raros (igual que Pentagon pero
// con probabilidad mucho menor).
// PINWHEEL: movement rectilini + boost de rotació visual prop del ship.
void Enemy::behaviorPinwheel(float /*delta_time*/) {
// Boost de rotación visual por proximidad al ship
if (ship_position_ != nullptr) {
const Vec2 TO_SHIP = *ship_position_ - center_;
const float DIST = TO_SHIP.length();
if (DIST < Defaults::Enemies::Pinwheel::PROXIMITY_DISTANCE) {
rotation_delta_ = animation_.rotation_delta_base * Defaults::Enemies::Pinwheel::ROTATION_DELTA_PROXIMITY_MULTIPLIER;
if (DIST < config_->behavior.proximity_distance) {
rotation_delta_ = animation_.rotation_delta_base * config_->behavior.rotation_proximity_multiplier;
} else {
rotation_delta_ = animation_.rotation_delta_base;
}
}
// Movimiento lineal puro: el world se encarga de integrar y rebotar.
}
void Enemy::updateAnimation(float delta_time) {
@@ -371,38 +305,26 @@ void Enemy::updateAnimation(float delta_time) {
}
void Enemy::updatePulse(float delta_time) {
const auto& cfg = config_->animation.pulse;
if (animation_.pulse_active) {
animation_.pulse_phase += 2.0F * Constants::PI * animation_.pulse_frequency * delta_time;
animation_.pulse_time_remaining -= delta_time;
if (animation_.pulse_time_remaining <= 0.0F) {
animation_.pulse_active = false;
}
} else {
const float RAND_VAL = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
const float TRIGGER_PROB = Defaults::Enemies::Animation::PULSE_TRIGGER_PROB * delta_time;
if (RAND_VAL < TRIGGER_PROB) {
animation_.pulse_active = true;
animation_.pulse_phase = 0.0F;
const float FREQ_RANGE = Defaults::Enemies::Animation::PULSE_FREQ_MAX -
Defaults::Enemies::Animation::PULSE_FREQ_MIN;
animation_.pulse_frequency = Defaults::Enemies::Animation::PULSE_FREQ_MIN +
((static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) * FREQ_RANGE);
const float AMP_RANGE = Defaults::Enemies::Animation::PULSE_AMPLITUD_MAX -
Defaults::Enemies::Animation::PULSE_AMPLITUD_MIN;
animation_.pulse_amplitude = Defaults::Enemies::Animation::PULSE_AMPLITUD_MIN +
((static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) * AMP_RANGE);
const float DUR_RANGE = Defaults::Enemies::Animation::PULSE_DURATION_MAX -
Defaults::Enemies::Animation::PULSE_DURATION_MIN;
animation_.pulse_time_remaining = Defaults::Enemies::Animation::PULSE_DURATION_MIN +
((static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) * DUR_RANGE);
}
return;
}
if (randFloat01() < cfg.trigger_prob_per_second * delta_time) {
animation_.pulse_active = true;
animation_.pulse_phase = 0.0F;
animation_.pulse_frequency = randRange(cfg.frequency_min, cfg.frequency_max);
animation_.pulse_amplitude = randRange(cfg.amplitude_min, cfg.amplitude_max);
animation_.pulse_time_remaining = randRange(cfg.duration_min, cfg.duration_max);
}
}
void Enemy::updateRotationAcceleration(float delta_time) {
const auto& cfg = config_->animation.rotation_accel;
if (animation_.rotation_delta_t < 1.0F) {
animation_.rotation_delta_t += delta_time / animation_.rotation_delta_duration;
if (animation_.rotation_delta_t >= 1.0F) {
@@ -416,34 +338,24 @@ void Enemy::updateRotationAcceleration(float delta_time) {
const float TARGET = animation_.rotation_delta_target;
rotation_delta_ = INITIAL + ((TARGET - INITIAL) * SMOOTH_T);
}
} else {
const float RAND_VAL = static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX);
const float TRIGGER_PROB = Defaults::Enemies::Animation::ROTATION_ACCEL_TRIGGER_PROB * delta_time;
if (RAND_VAL < TRIGGER_PROB) {
animation_.rotation_delta_t = 0.0F;
const float MULT_RANGE = Defaults::Enemies::Animation::ROTATION_ACCEL_MULTIPLIER_MAX -
Defaults::Enemies::Animation::ROTATION_ACCEL_MULTIPLIER_MIN;
const float MULTIPLIER = Defaults::Enemies::Animation::ROTATION_ACCEL_MULTIPLIER_MIN +
((static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) * MULT_RANGE);
animation_.rotation_delta_target = animation_.rotation_delta_base * MULTIPLIER;
const float DUR_RANGE = Defaults::Enemies::Animation::ROTATION_ACCEL_DURATION_MAX -
Defaults::Enemies::Animation::ROTATION_ACCEL_DURATION_MIN;
animation_.rotation_delta_duration = Defaults::Enemies::Animation::ROTATION_ACCEL_DURATION_MIN +
((static_cast<float>(std::rand()) / static_cast<float>(RAND_MAX)) * DUR_RANGE);
}
return;
}
if (randFloat01() < cfg.trigger_prob_per_second * delta_time) {
animation_.rotation_delta_t = 0.0F;
const float MULTIPLIER = randRange(cfg.multiplier_min, cfg.multiplier_max);
animation_.rotation_delta_target = animation_.rotation_delta_base * MULTIPLIER;
animation_.rotation_delta_duration = randRange(cfg.duration_min, cfg.duration_max);
}
}
auto Enemy::computeCurrentScale() const -> float {
float scale = 1.0F;
if (invulnerability_timer_ > 0.0F) {
const float T_INV = invulnerability_timer_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
const float T_INV = invulnerability_timer_ / config_->spawn.invulnerability_duration;
const float T = 1.0F - T_INV;
const float SMOOTH_T = T * T * (3.0F - (2.0F * T));
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_START;
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_END;
const float START = config_->spawn.invulnerability_scale_start;
const float END = config_->spawn.invulnerability_scale_end;
scale = START + ((END - START) * SMOOTH_T);
} else if (animation_.pulse_active) {
scale += animation_.pulse_amplitude * std::sin(animation_.pulse_phase);
@@ -452,16 +364,7 @@ auto Enemy::computeCurrentScale() const -> float {
}
auto Enemy::getBaseVelocity() const -> float {
switch (type_) {
case EnemyType::PENTAGON:
return Defaults::Enemies::Pentagon::SPEED;
case EnemyType::SQUARE:
return Defaults::Enemies::Square::SPEED;
case EnemyType::PINWHEEL:
return Defaults::Enemies::Pinwheel::SPEED;
default:
return Defaults::Enemies::Pentagon::SPEED;
}
return EnemyRegistry::get(type_).physics.speed;
}
auto Enemy::getBaseRotation() const -> float {
@@ -474,12 +377,12 @@ void Enemy::setTrackingStrength(float strength) {
}
}
auto Enemy::attemptSafeSpawn(const Vec2& ship_pos, float& out_x, float& out_y) -> bool {
auto Enemy::attemptSafeSpawn(const Vec2& ship_pos, float collision_radius, float safety_distance, float& out_x, float& out_y) -> bool {
float min_x;
float max_x;
float min_y;
float max_y;
Constants::getSafePlayAreaBounds(Defaults::Entities::ENEMY_RADIUS, min_x, max_x, min_y, max_y);
Constants::getSafePlayAreaBounds(collision_radius, min_x, max_x, min_y, max_y);
const int RANGE_X = static_cast<int>(max_x - min_x);
const int RANGE_Y = static_cast<int>(max_y - min_y);
@@ -489,5 +392,5 @@ auto Enemy::attemptSafeSpawn(const Vec2& ship_pos, float& out_x, float& out_y) -
const float DX = out_x - ship_pos.x;
const float DY = out_y - ship_pos.y;
const float DISTANCE = std::sqrt((DX * DX) + (DY * DY));
return DISTANCE >= Defaults::Enemies::Spawn::SAFETY_DISTANCE;
return DISTANCE >= safety_distance;
}
+28 -19
View File
@@ -6,7 +6,6 @@
#include <cstdint>
#include "core/defaults.hpp"
#include "core/entities/entity.hpp"
#include "core/types.hpp"
@@ -14,9 +13,13 @@
enum class EnemyType : uint8_t {
PENTAGON = 0, // Pentágono esquivador (zigzag)
SQUARE = 1, // Square perseguidor (tracks ship)
PINWHEEL = 2 // Molinillo agresivo (rápido, girando)
PINWHEEL = 2, // Molinillo agresivo (rápido, girando)
STAR = 3 // Estrella de 5 puntes (clone visual de Pentagon, comportament zigzag)
};
// Forward declaration — EnemyConfig viu a enemy_config.hpp i s'inclou només a enemy.cpp.
struct EnemyConfig;
// Estado de animación (palpitación + rotación acelerada)
struct EnemyAnimation {
// Palpitación (efecto respiración)
@@ -48,10 +51,8 @@ class Enemy : public Entities::Entity {
// Override: Interfaz de Entity
[[nodiscard]] auto isActive() const -> bool override { return is_active_; }
// Override: Interfaz de colisión
[[nodiscard]] auto getCollisionRadius() const -> float override {
return Defaults::Entities::ENEMY_RADIUS;
}
// Override: Interfaz de colisión. El radi ve del config carregat per tipus.
[[nodiscard]] auto getCollisionRadius() const -> float override { return collision_radius_; }
// Mentre fa spawn (invulnerable) segueix col·lisionant: les bales el
// poden abatre i el cos físic rebota amb la nau. El damage a la nau
// segueix filtrat per `isInvulnerable()` al detectShipEnemy.
@@ -65,6 +66,9 @@ class Enemy : public Entities::Entity {
// Getters
[[nodiscard]] auto getRotationDelta() const -> float { return rotation_delta_; }
[[nodiscard]] auto getVelocityVector() const -> Vec2 { return body_.velocity; }
// Configuració activa (carregada al darrer init()). Vàlida mentre l'enemic
// ha estat inicialitzat almenys un cop; abans és nullptr.
[[nodiscard]] auto getConfig() const -> const EnemyConfig& { return *config_; }
// Set ship position reference for tracking behavior
void setShipPosition(const Vec2* ship_pos) { ship_position_ = ship_pos; }
@@ -101,29 +105,34 @@ class Enemy : public Entities::Entity {
void applyImpulse(const Vec2& impulse);
private:
// Miembros específicos (heredados: renderer_, shape_, center_, angle_, brightness_, body_).
// Inicializados en la declaración: el ctor por defecto deja al enemy en estado "inactivo
// como pentágono", coherente con lo que harán init() o el ctor con renderer al activarlo.
float rotation_delta_{0.0F}; // Velocidad angular visual (rad/s) — solo decoración, separada de body_.angular_velocity
float rotation_{0.0F}; // Rotación visual acumulada (no afecta movimiento)
// Configuració carregada per tipus (apunta a una entrada de EnemyRegistry).
// nullptr abans del primer init(); per això getConfig() només és vàlid post-init.
const EnemyConfig* config_{nullptr};
// Cache local del radi (per evitar dereferenciar config_ a getCollisionRadius);
// s'actualitza a init() segons el tipus.
float collision_radius_{0.0F};
float rotation_delta_{0.0F}; // Velocidad angular visual (rad/s)
float rotation_{0.0F}; // Rotación visual acumulada
bool is_active_{false};
EnemyType type_{EnemyType::PENTAGON};
EnemyAnimation animation_;
// Comportamiento type-specific
float tracking_timer_{0.0F}; // Quadrat: tiempo desde último update de dirección
const Vec2* ship_position_{nullptr}; // Puntero a posición de la nave (para tracking)
float tracking_strength_{0.0F}; // Quadrat: intensidad de tracking (0.0-1.5), default 0.5
float direction_change_timer_{0.0F}; // Pentagon: tiempo para próximo cambio de dirección
float tracking_timer_{0.0F};
const Vec2* ship_position_{nullptr};
float tracking_strength_{0.0F};
float direction_change_timer_{0.0F};
// Invulnerabilidad post-spawn
float invulnerability_timer_{0.0F};
// Estado "herido": timer cuenta atrás; al cruzar 0 se marca expiración.
// Estado "herido"
float wounded_timer_{0.0F};
bool wound_expired_this_frame_{false};
uint8_t last_hit_by_{0xFF}; // 0xFF = sin atribución
uint8_t last_hit_by_{0xFF};
// Métodos privados
void updateAnimation(float delta_time);
@@ -133,8 +142,8 @@ class Enemy : public Entities::Entity {
void behaviorSquare(float delta_time);
void behaviorPinwheel(float delta_time);
[[nodiscard]] auto computeCurrentScale() const -> float;
// Estático: solo opera sobre ship_pos pasado; no consulta estado del enemy.
static auto attemptSafeSpawn(const Vec2& ship_pos, float& out_x, float& out_y) -> bool;
// Static: passa els paràmetres com a args per no acoblar a *this.
static auto attemptSafeSpawn(const Vec2& ship_pos, float collision_radius, float safety_distance, float& out_x, float& out_y) -> bool;
// Helper: setear body_.velocity desde un ángulo y magnitud.
// angle_movement=0 apunta hacia arriba (eje Y negativo SDL).
+277
View File
@@ -0,0 +1,277 @@
// enemy_config.cpp - Implementació del parser de EnemyConfig
// © 2026 JailDesigner
#include "game/entities/enemy_config.hpp"
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
namespace {
auto parseColor(const fkyaml::node& node, SDL_Color& out) -> bool {
if (!node.is_sequence() || node.size() != 3) {
return false;
}
const auto R = node[0].get_value<uint32_t>();
const auto G = node[1].get_value<uint32_t>();
const auto B = node[2].get_value<uint32_t>();
out = SDL_Color{
.r = static_cast<uint8_t>(R),
.g = static_cast<uint8_t>(G),
.b = static_cast<uint8_t>(B),
.a = 255};
return true;
}
auto aiTypeFromString(const std::string& s) -> std::optional<EnemyType> {
if (s == "pentagon") { return EnemyType::PENTAGON; }
if (s == "square") { return EnemyType::SQUARE; }
if (s == "pinwheel") { return EnemyType::PINWHEEL; }
if (s == "star") { return EnemyType::STAR; }
return std::nullopt;
}
// Cada parseXxx valida + omple la sub-struct corresponent. Retornen false
// amb log si falta un camp requerit. Separar-los baixa la complexitat
// cognitiva del fromYaml() principal.
auto parseAiType(const fkyaml::node& node, EnemyType expected, const std::string& name, EnemyType& out) -> bool {
if (!node.contains("ai_type")) {
std::cerr << "[EnemyConfig] Error: falta 'ai_type' a " << name << '\n';
return false;
}
const auto AI_STR = node["ai_type"].get_value<std::string>();
const auto PARSED = aiTypeFromString(AI_STR);
if (!PARSED) {
std::cerr << "[EnemyConfig] Error: ai_type desconegut '" << AI_STR << "' a " << name << '\n';
return false;
}
if (*PARSED != expected) {
std::cerr << "[EnemyConfig] Error: ai_type '" << AI_STR
<< "' no coincideix amb el tipus esperat (per directori) a " << name << '\n';
return false;
}
out = *PARSED;
return true;
}
auto parseShape(const fkyaml::node& node, const std::string& name, EnemyConfig::ShapeCfg& out) -> bool {
if (!node.contains("shape") || !node["shape"].contains("path")) {
std::cerr << "[EnemyConfig] Error: falta 'shape.path' a " << name << '\n';
return false;
}
const auto& shape = node["shape"];
out.path = shape["path"].get_value<std::string>();
out.scale = shape.contains("scale") ? shape["scale"].get_value<float>() : 1.0F;
out.collision_factor = shape.contains("collision_factor")
? shape["collision_factor"].get_value<float>()
: 1.0F;
return true;
}
auto parsePhysics(const fkyaml::node& node, const std::string& name, EnemyConfig::PhysicsCfg& out) -> bool {
if (!node.contains("physics")) {
std::cerr << "[EnemyConfig] Error: falta 'physics' a " << name << '\n';
return false;
}
const auto& p = node["physics"];
out.mass = p["mass"].get_value<float>();
out.speed = p["speed"].get_value<float>();
out.rotation_delta_min = p["rotation_delta_min"].get_value<float>();
out.rotation_delta_max = p["rotation_delta_max"].get_value<float>();
out.restitution = p["restitution"].get_value<float>();
out.linear_damping = p["linear_damping"].get_value<float>();
out.angular_damping = p["angular_damping"].get_value<float>();
return true;
}
auto parseAnimation(const fkyaml::node& node, const std::string& name, EnemyConfig::AnimationCfg& out) -> bool {
if (!node.contains("animation") ||
!node["animation"].contains("pulse") ||
!node["animation"].contains("rotation_accel")) {
std::cerr << "[EnemyConfig] Error: falta 'animation.pulse' o 'animation.rotation_accel' a " << name << '\n';
return false;
}
const auto& p = node["animation"]["pulse"];
out.pulse.trigger_prob_per_second = p["trigger_prob_per_second"].get_value<float>();
out.pulse.duration_min = p["duration_min"].get_value<float>();
out.pulse.duration_max = p["duration_max"].get_value<float>();
out.pulse.amplitude_min = p["amplitude_min"].get_value<float>();
out.pulse.amplitude_max = p["amplitude_max"].get_value<float>();
out.pulse.frequency_min = p["frequency_min"].get_value<float>();
out.pulse.frequency_max = p["frequency_max"].get_value<float>();
const auto& r = node["animation"]["rotation_accel"];
out.rotation_accel.trigger_prob_per_second = r["trigger_prob_per_second"].get_value<float>();
out.rotation_accel.duration_min = r["duration_min"].get_value<float>();
out.rotation_accel.duration_max = r["duration_max"].get_value<float>();
out.rotation_accel.multiplier_min = r["multiplier_min"].get_value<float>();
out.rotation_accel.multiplier_max = r["multiplier_max"].get_value<float>();
return true;
}
auto parseWounded(const fkyaml::node& node, const std::string& name, EnemyConfig::WoundedCfg& out) -> bool {
if (!node.contains("wounded")) {
std::cerr << "[EnemyConfig] Error: falta 'wounded' a " << name << '\n';
return false;
}
const auto& w = node["wounded"];
out.duration = w["duration"].get_value<float>();
out.blink_hz = w["blink_hz"].get_value<float>();
return true;
}
auto parseSpawn(const fkyaml::node& node, const std::string& name, EnemyConfig::SpawnCfg& out) -> bool {
if (!node.contains("spawn")) {
std::cerr << "[EnemyConfig] Error: falta 'spawn' a " << name << '\n';
return false;
}
const auto& s = node["spawn"];
out.invulnerability_duration = s["invulnerability_duration"].get_value<float>();
out.invulnerability_brightness_start = s["invulnerability_brightness_start"].get_value<float>();
out.invulnerability_brightness_end = s["invulnerability_brightness_end"].get_value<float>();
out.invulnerability_scale_start = s["invulnerability_scale_start"].get_value<float>();
out.invulnerability_scale_end = s["invulnerability_scale_end"].get_value<float>();
out.safety_distance = s["safety_distance"].get_value<float>();
return true;
}
// Tots els camps de behavior són opcionals; només l'AI corresponent els consumeix.
void parseBehavior(const fkyaml::node& node, EnemyConfig::BehaviorCfg& out) {
if (!node.contains("behavior")) {
return;
}
const auto& b = node["behavior"];
const auto READ_OPT = [&b](const char* key, float& dst) {
if (b.contains(key)) {
dst = b[key].get_value<float>();
}
};
READ_OPT("zigzag_prob_per_second", out.zigzag_prob_per_second);
READ_OPT("angle_change_max", out.angle_change_max);
READ_OPT("tracking_strength", out.tracking_strength);
READ_OPT("tracking_interval", out.tracking_interval);
READ_OPT("rotation_proximity_multiplier", out.rotation_proximity_multiplier);
READ_OPT("proximity_distance", out.proximity_distance);
}
auto parseColors(const fkyaml::node& node, const std::string& name, EnemyConfig::ColorsCfg& out) -> bool {
if (!node.contains("colors") ||
!parseColor(node["colors"]["normal"], out.normal) ||
!parseColor(node["colors"]["wounded"], out.wounded)) {
std::cerr << "[EnemyConfig] Error: 'colors.normal' / 'colors.wounded' no són [r,g,b] a "
<< name << '\n';
return false;
}
return true;
}
auto parseScore(const fkyaml::node& node, const std::string& name, int& out) -> bool {
if (!node.contains("score")) {
std::cerr << "[EnemyConfig] Error: falta 'score' a " << name << '\n';
return false;
}
out = node["score"].get_value<int>();
return true;
}
auto actionTypeFromString(const std::string& s) -> std::optional<EnemyActionType> {
if (s == "set_hurt") { return EnemyActionType::SET_HURT; }
if (s == "destroy") { return EnemyActionType::DESTROY; }
if (s == "add_score") { return EnemyActionType::ADD_SCORE; }
if (s == "create_debris") { return EnemyActionType::CREATE_DEBRIS; }
if (s == "create_fireworks") { return EnemyActionType::CREATE_FIREWORKS; }
if (s == "apply_impulse") { return EnemyActionType::APPLY_IMPULSE; }
return std::nullopt;
}
auto parseActionList(const fkyaml::node& list_node, const std::string& enemy_name, const char* event_name, std::vector<EnemyAction>& out) -> bool {
if (!list_node.is_sequence()) {
std::cerr << "[EnemyConfig] Error: '" << event_name << "' ha de ser una llista a "
<< enemy_name << '\n';
return false;
}
for (const auto& item : list_node) {
if (!item.contains("action")) {
std::cerr << "[EnemyConfig] Error: entrada sense 'action' a " << event_name
<< " (" << enemy_name << ")\n";
return false;
}
const auto STR = item["action"].get_value<std::string>();
const auto PARSED = actionTypeFromString(STR);
if (!PARSED) {
std::cerr << "[EnemyConfig] Error: acció desconeguda '" << STR << "' a "
<< event_name << " (" << enemy_name << ")\n";
return false;
}
out.push_back({*PARSED});
}
return true;
}
// Defaults: replica el flux hardcoded actual (set_hurt → destroy → score+debris+fireworks).
void fillLegacyDefaults(EnemyEventConfig& events) {
events.on_hit = {{EnemyActionType::SET_HURT}};
events.on_hurt_end = {{EnemyActionType::DESTROY}};
events.on_destroy = {
{EnemyActionType::ADD_SCORE},
{EnemyActionType::CREATE_DEBRIS},
{EnemyActionType::CREATE_FIREWORKS},
};
}
auto parseEvents(const fkyaml::node& node, const std::string& name, EnemyEventConfig& out) -> bool {
if (!node.contains("events")) {
fillLegacyDefaults(out);
return true;
}
const auto& e = node["events"];
if (e.contains("on_hit") && !parseActionList(e["on_hit"], name, "on_hit", out.on_hit)) {
return false;
}
if (e.contains("on_hurt_end") &&
!parseActionList(e["on_hurt_end"], name, "on_hurt_end", out.on_hurt_end)) {
return false;
}
if (e.contains("on_destroy") &&
!parseActionList(e["on_destroy"], name, "on_destroy", out.on_destroy)) {
return false;
}
// Validació: destroy no pot aparèixer dins on_destroy (recursió infinita).
for (const auto& a : out.on_destroy) {
if (a.type == EnemyActionType::DESTROY) {
std::cerr << "[EnemyConfig] Error: 'destroy' no pot aparèixer dins 'on_destroy' a "
<< name << " (recursió infinita)\n";
return false;
}
}
return true;
}
} // namespace
auto EnemyConfig::fromYaml(const fkyaml::node& node, EnemyType expected_ai_type)
-> std::optional<EnemyConfig> {
try {
EnemyConfig cfg;
cfg.name = node.contains("name") ? node["name"].get_value<std::string>() : "enemy";
if (!parseAiType(node, expected_ai_type, cfg.name, cfg.ai_type)) { return std::nullopt; }
if (!parseShape(node, cfg.name, cfg.shape)) { return std::nullopt; }
if (!parsePhysics(node, cfg.name, cfg.physics)) { return std::nullopt; }
parseBehavior(node, cfg.behavior);
if (!parseAnimation(node, cfg.name, cfg.animation)) { return std::nullopt; }
if (!parseWounded(node, cfg.name, cfg.wounded)) { return std::nullopt; }
if (!parseSpawn(node, cfg.name, cfg.spawn)) { return std::nullopt; }
if (!parseColors(node, cfg.name, cfg.colors)) { return std::nullopt; }
if (!parseScore(node, cfg.name, cfg.score)) { return std::nullopt; }
if (!parseEvents(node, cfg.name, cfg.events)) { return std::nullopt; }
return cfg;
} catch (const std::exception& e) {
std::cerr << "[EnemyConfig] Excepció parsejant: " << e.what() << '\n';
return std::nullopt;
}
}
+108
View File
@@ -0,0 +1,108 @@
// enemy_config.hpp - Configuració d'un tipus d'enemic carregada des de YAML
// © 2026 JailDesigner
//
// Una instància per tipus (Pentagon/Square/Pinwheel), carregada un cop al
// startup per EnemyRegistry i compartida entre totes les instàncies d'aquell
// tipus. Estructura paral·lela a PlayerConfig.
#pragma once
#include <SDL3/SDL.h>
#include <optional>
#include <string>
#include "external/fkyaml_node.hpp"
#include "game/entities/enemy.hpp" // EnemyType
#include "game/entities/enemy_event.hpp"
struct EnemyConfig {
struct ShapeCfg {
std::string path;
float scale; // multiplicador visual + hitbox sobre la mida nativa del .shp
float collision_factor; // ajust opcional del hitbox respecte el cercle circumscrit (default 1.0)
};
struct PhysicsCfg {
float mass;
float speed;
float rotation_delta_min;
float rotation_delta_max;
float restitution; // rebot contra parets (1.0 = perfectament elàstic)
float linear_damping; // fricció lineal (s^-1)
float angular_damping;
};
// Camps específics de cada AI. Els no aplicables a un tipus queden a 0.0F
// i no s'usen — el dispatch viu a Enemy::behaviorXxx.
struct BehaviorCfg {
// Pentagon
float zigzag_prob_per_second{0.0F};
float angle_change_max{0.0F};
// Square
float tracking_strength{0.0F};
float tracking_interval{0.0F};
// Pinwheel
float rotation_proximity_multiplier{0.0F};
float proximity_distance{0.0F};
};
// Animacions decoratives. Compartides estructuralment entre tots els tipus
// però amb valors propis per personalitzar la "personalitat" visual de cada un.
struct AnimationCfg {
struct PulseCfg {
float trigger_prob_per_second; // probabilitat per segon d'iniciar un pulse
float duration_min;
float duration_max;
float amplitude_min; // amplitud d'escala (±)
float amplitude_max;
float frequency_min; // Hz
float frequency_max;
};
struct RotationAccelCfg {
float trigger_prob_per_second;
float duration_min; // segons de transició al nou speed
float duration_max;
float multiplier_min; // multiplicador sobre rotation_delta_base
float multiplier_max;
};
PulseCfg pulse;
RotationAccelCfg rotation_accel;
};
struct WoundedCfg {
float duration; // segons en estat ferit abans d'explotar
float blink_hz; // freqüència del parpelleig color normal ↔ wounded
};
struct SpawnCfg {
float invulnerability_duration; // segons d'invulnerabilitat post-spawn
float invulnerability_brightness_start; // brightness inicial (corba LERP)
float invulnerability_brightness_end; // brightness final
float invulnerability_scale_start; // escala inicial (corba LERP, 0 = invisible)
float invulnerability_scale_end; // escala final (1 = mida nativa)
float safety_distance; // px mínim respecte al player al spawn
};
struct ColorsCfg {
SDL_Color normal;
SDL_Color wounded;
};
std::string name;
EnemyType ai_type;
ShapeCfg shape;
PhysicsCfg physics;
BehaviorCfg behavior;
AnimationCfg animation;
WoundedCfg wounded;
SpawnCfg spawn;
ColorsCfg colors;
int score;
EnemyEventConfig events;
// Parseja un descriptor d'enemic. expected_ai_type valida que ai_type del
// YAML coincideix amb el tipus que el caller espera (segons el directori).
static auto fromYaml(const fkyaml::node& node, EnemyType expected_ai_type)
-> std::optional<EnemyConfig>;
};
+48
View File
@@ -0,0 +1,48 @@
// enemy_event.hpp - Sistema declaratiu d'events i accions per a enemics
// © 2026 JailDesigner
//
// Cada enemic descriu al seu YAML què passa quan rep un event (on_hit,
// on_hurt_end, on_destroy) com a llista d'accions. El motor només dispatcha;
// el comportament viu a les dades.
#pragma once
#include <cstdint>
#include <vector>
enum class EnemyEventType : uint8_t {
ON_HIT, // Impactat per una bala
ON_HURT_END, // Timer wounded ha expirat aquest frame
ON_DESTROY, // L'acció destroy s'està executant (efectes col·laterals)
};
enum class EnemyActionType : uint8_t {
SET_HURT, // Entra estat wounded (o destrueix si ja era wounded)
DESTROY, // Dispara on_destroy + desactiva físicament
ADD_SCORE, // Suma config.score al shooter + floating score
CREATE_DEBRIS, // Explosió de debris amb herència de velocitat
CREATE_FIREWORKS, // Burst radial de firework
APPLY_IMPULSE, // Aplica l'impuls de la bala impactant
};
struct EnemyAction {
EnemyActionType type;
};
struct EnemyEventConfig {
std::vector<EnemyAction> on_hit;
std::vector<EnemyAction> on_hurt_end;
std::vector<EnemyAction> on_destroy;
[[nodiscard]] auto getActions(EnemyEventType event) const -> const std::vector<EnemyAction>& {
switch (event) {
case EnemyEventType::ON_HIT:
return on_hit;
case EnemyEventType::ON_HURT_END:
return on_hurt_end;
case EnemyEventType::ON_DESTROY:
return on_destroy;
}
return on_hit; // unreachable
}
};
+66
View File
@@ -0,0 +1,66 @@
// enemy_registry.cpp - Implementació del registre estàtic d'enemics
// © 2026 JailDesigner
#include "game/entities/enemy_registry.hpp"
#include <cstdlib>
#include <iostream>
#include <string>
#include "core/entities/entity_loader.hpp"
EnemyConfig EnemyRegistry::pentagon_config;
EnemyConfig EnemyRegistry::square_config;
EnemyConfig EnemyRegistry::pinwheel_config;
EnemyConfig EnemyRegistry::star_config;
bool EnemyRegistry::loaded = false;
namespace {
auto loadOne(const std::string& name, EnemyType expected_type, EnemyConfig& out) -> bool {
auto yaml = Entities::EntityLoader::load(name);
if (!yaml) {
std::cerr << "[EnemyRegistry] Error: no s'ha pogut carregar " << name << ".yaml\n";
return false;
}
auto cfg = EnemyConfig::fromYaml(*yaml, expected_type);
if (!cfg) {
std::cerr << "[EnemyRegistry] Error: format invàlid a " << name << ".yaml\n";
return false;
}
out = *cfg;
return true;
}
} // namespace
auto EnemyRegistry::loadAll() -> bool {
const bool OK = loadOne("pentagon", EnemyType::PENTAGON, pentagon_config) &&
loadOne("square", EnemyType::SQUARE, square_config) &&
loadOne("pinwheel", EnemyType::PINWHEEL, pinwheel_config) &&
loadOne("star", EnemyType::STAR, star_config);
loaded = OK;
if (OK) {
std::cout << "[EnemyRegistry] 4 configuracions d'enemic carregades.\n";
}
return OK;
}
auto EnemyRegistry::get(EnemyType type) -> const EnemyConfig& {
if (!loaded) {
std::cerr << "[EnemyRegistry] FATAL: get() abans de loadAll()\n";
std::exit(EXIT_FAILURE);
}
switch (type) {
case EnemyType::PENTAGON:
return pentagon_config;
case EnemyType::SQUARE:
return square_config;
case EnemyType::PINWHEEL:
return pinwheel_config;
case EnemyType::STAR:
return star_config;
}
std::cerr << "[EnemyRegistry] FATAL: tipus desconegut\n";
std::exit(EXIT_FAILURE);
}
+31
View File
@@ -0,0 +1,31 @@
// enemy_registry.hpp - Registre estàtic de configuracions d'enemics per tipus
// © 2026 JailDesigner
//
// Carrega els 3 fitxers YAML (pentagon, square, pinwheel) un cop al startup
// i exposa el lookup per EnemyType. Pensat per a ser invocat des de
// GameScene; si la càrrega falla, el caller decideix avortar.
#pragma once
#include "game/entities/enemy.hpp"
#include "game/entities/enemy_config.hpp"
class EnemyRegistry {
public:
EnemyRegistry() = delete; // tot estàtic
// Carrega els 3 descriptors. Retorna true si tots tres s'han carregat
// i parsejat correctament. Cridar abans del primer get().
static auto loadAll() -> bool;
// Lookup. Cal haver cridat loadAll() abans. Si el tipus no s'ha carregat
// (loadAll fallida o no cridada), avorta amb log fatal.
static auto get(EnemyType type) -> const EnemyConfig&;
private:
static EnemyConfig pentagon_config;
static EnemyConfig square_config;
static EnemyConfig pinwheel_config;
static EnemyConfig star_config;
static bool loaded;
};
+111
View File
@@ -0,0 +1,111 @@
// player_config.cpp - Implementació del parser de PlayerConfig
// © 2026 JailDesigner
#include "game/entities/player_config.hpp"
#include <cstdint>
#include <exception>
#include <iostream>
#include <string>
namespace {
// Helper: extreu un SDL_Color d'una seqüència de 3 enters [r, g, b] del YAML.
// Retorna true si el format és vàlid.
auto parseColor(const fkyaml::node& node, SDL_Color& out) -> bool {
if (!node.is_sequence() || node.size() != 3) {
return false;
}
const auto R = node[0].get_value<uint32_t>();
const auto G = node[1].get_value<uint32_t>();
const auto B = node[2].get_value<uint32_t>();
out = SDL_Color{
.r = static_cast<uint8_t>(R),
.g = static_cast<uint8_t>(G),
.b = static_cast<uint8_t>(B),
.a = 255};
return true;
}
} // namespace
auto PlayerConfig::fromYaml(const fkyaml::node& node) -> std::optional<PlayerConfig> {
try {
PlayerConfig cfg;
cfg.name = node.contains("name") ? node["name"].get_value<std::string>() : "player";
// shape
if (!node.contains("shape") || !node["shape"].contains("path")) {
std::cerr << "[PlayerConfig] Error: falta 'shape.path'" << '\n';
return std::nullopt;
}
const auto& shape = node["shape"];
cfg.shape.path = shape["path"].get_value<std::string>();
cfg.shape.scale = shape.contains("scale") ? shape["scale"].get_value<float>() : 1.0F;
cfg.shape.collision_factor = shape.contains("collision_factor")
? shape["collision_factor"].get_value<float>()
: 1.0F;
// physics
if (!node.contains("physics")) {
std::cerr << "[PlayerConfig] Error: falta 'physics'" << '\n';
return std::nullopt;
}
const auto& physics = node["physics"];
cfg.physics.mass = physics["mass"].get_value<float>();
cfg.physics.restitution = physics["restitution"].get_value<float>();
cfg.physics.linear_damping = physics["linear_damping"].get_value<float>();
cfg.physics.angular_damping = physics["angular_damping"].get_value<float>();
cfg.physics.rotation_speed = physics["rotation_speed"].get_value<float>();
cfg.physics.acceleration = physics["acceleration"].get_value<float>();
cfg.physics.max_velocity = physics["max_velocity"].get_value<float>();
cfg.physics.death_impact_factor = physics["death_impact_factor"].get_value<float>();
// invulnerability
if (!node.contains("invulnerability")) {
std::cerr << "[PlayerConfig] Error: falta 'invulnerability'" << '\n';
return std::nullopt;
}
const auto& invul = node["invulnerability"];
cfg.invulnerability.duration = invul["duration"].get_value<float>();
cfg.invulnerability.blink_visible = invul["blink_visible"].get_value<float>();
cfg.invulnerability.blink_invisible = invul["blink_invisible"].get_value<float>();
// hurt
if (!node.contains("hurt")) {
std::cerr << "[PlayerConfig] Error: falta 'hurt'" << '\n';
return std::nullopt;
}
cfg.hurt.duration = node["hurt"]["duration"].get_value<float>();
cfg.hurt.blink_hz = node["hurt"]["blink_hz"].get_value<float>();
// visual_thrust
if (!node.contains("visual_thrust")) {
std::cerr << "[PlayerConfig] Error: falta 'visual_thrust'" << '\n';
return std::nullopt;
}
cfg.visual_thrust.push_divisor = node["visual_thrust"]["push_divisor"].get_value<float>();
cfg.visual_thrust.scale_divisor = node["visual_thrust"]["scale_divisor"].get_value<float>();
// colors
if (!node.contains("colors") ||
!parseColor(node["colors"]["normal"], cfg.colors.normal) ||
!parseColor(node["colors"]["hurt"], cfg.colors.hurt)) {
std::cerr << "[PlayerConfig] Error: 'colors.normal' / 'colors.hurt' no són seqüències [r,g,b]" << '\n';
return std::nullopt;
}
// weapon
if (!node.contains("weapon")) {
std::cerr << "[PlayerConfig] Error: falta 'weapon'" << '\n';
return std::nullopt;
}
cfg.weapon.bullet_speed = node["weapon"]["bullet_speed"].get_value<float>();
return cfg;
} catch (const std::exception& e) {
std::cerr << "[PlayerConfig] Excepció parsejant: " << e.what() << '\n';
return std::nullopt;
}
}
+73
View File
@@ -0,0 +1,73 @@
// player_config.hpp - Configuració de la nau del player carregada des de YAML
// © 2026 JailDesigner
//
// POD struct amb sub-structs per organitzar els paràmetres del jugador
// (física, invulnerabilitat, hurt, empenta visual, colors, weapon). Es
// construeix a partir d'un node fkyaml carregat per EntityLoader.
#pragma once
#include <SDL3/SDL.h>
#include <optional>
#include <string>
#include "external/fkyaml_node.hpp"
struct PlayerConfig {
struct ShapeCfg {
std::string path;
float scale; // multiplicador visual + hitbox sobre la mida nativa del .shp
float collision_factor; // ajust opcional del hitbox respecte el cercle circumscrit (default 1.0)
};
struct PhysicsCfg {
float mass;
float restitution;
float linear_damping;
float angular_damping;
float rotation_speed; // rad/s
float acceleration; // px/s^2 multiplicat per la massa
float max_velocity; // px/s (clamp post-integració)
float death_impact_factor; // [0..1] moment transferit a l'enemic al morir
};
struct InvulnerabilityCfg {
float duration;
float blink_visible;
float blink_invisible;
};
struct HurtCfg {
float duration;
float blink_hz;
};
struct VisualThrustCfg {
float push_divisor;
float scale_divisor;
};
struct ColorsCfg {
SDL_Color normal;
SDL_Color hurt;
};
struct WeaponCfg {
float bullet_speed;
};
std::string name;
ShapeCfg shape;
PhysicsCfg physics;
InvulnerabilityCfg invulnerability;
HurtCfg hurt;
VisualThrustCfg visual_thrust;
ColorsCfg colors;
WeaponCfg weapon;
// Construeix un PlayerConfig a partir del node YAML. Retorna std::nullopt
// si falten camps requerits o el format no és vàlid (el caller decideix
// si abortar).
static auto fromYaml(const fkyaml::node& node) -> std::optional<PlayerConfig>;
};
+35 -48
View File
@@ -9,6 +9,7 @@
#include <cmath>
#include <cstdint>
#include <iostream>
#include <utility>
#include "core/audio/audio.hpp"
#include "core/defaults.hpp"
@@ -20,27 +21,30 @@
#include "core/types.hpp"
#include "game/constants.hpp"
Ship::Ship(Rendering::Renderer* renderer, const char* shape_file)
: Entity(renderer) {
// Brightness específico para naves
Ship::Ship(Rendering::Renderer* renderer, PlayerConfig config, const char* shape_override)
: Entity(renderer),
config_(std::move(config)) {
brightness_ = Defaults::Brightness::NAU;
// Configuración del cuerpo físico
body_.setMass(Defaults::Ship::MASS);
body_.radius = Defaults::Entities::SHIP_RADIUS;
body_.restitution = Defaults::Ship::RESTITUTION;
body_.linear_damping = Defaults::Ship::LINEAR_DAMPING;
body_.angular_damping = Defaults::Ship::ANGULAR_DAMPING;
// Cargar shape compartida desde archivo
shape_ = Graphics::ShapeLoader::load(shape_file);
// El shape pot venir del YAML o ser overridden (ex: P2 amb "ship2.shp").
const std::string SHAPE_PATH = (shape_override != nullptr) ? shape_override : config_.shape.path;
shape_ = Graphics::ShapeLoader::load(SHAPE_PATH);
if (!shape_ || !shape_->isValid()) {
std::cerr << "[Ship] Error: no se ha podido cargar " << shape_file << '\n';
std::cerr << "[Ship] Error: no se ha podido cargar " << SHAPE_PATH << '\n';
}
// Radi de col·lisió derivat del cercle circumscrit de la shape * scale * collision_factor.
const float BOUNDING = (shape_ != nullptr) ? shape_->getBoundingRadius() : 0.0F;
collision_radius_ = BOUNDING * config_.shape.scale * config_.shape.collision_factor;
body_.setMass(config_.physics.mass);
body_.radius = collision_radius_;
body_.restitution = config_.physics.restitution;
body_.linear_damping = config_.physics.linear_damping;
body_.angular_damping = config_.physics.angular_damping;
}
void Ship::init(const Vec2* spawn_point, bool activar_invulnerabilitat) {
// Posición inicial
if (spawn_point != nullptr) {
center_ = *spawn_point;
} else {
@@ -50,34 +54,27 @@ void Ship::init(const Vec2* spawn_point, bool activar_invulnerabilitat) {
center_ = {.x = center_x, .y = center_y};
}
// Reset orientación
angle_ = 0.0F;
// Sincronizar cuerpo físico con la posición/orientación inicial
body_.position = center_;
body_.angle = angle_;
body_.velocity = Vec2{};
body_.angular_velocity = 0.0F;
body_.clearAccumulators();
// Activar invulnerabilidad solo si es respawn
invulnerable_timer_ = activar_invulnerabilitat ? Defaults::Ship::INVULNERABILITY_DURATION : 0.0F;
invulnerable_timer_ = activar_invulnerabilitat ? config_.invulnerability.duration : 0.0F;
is_hit_ = false;
hurt_timer_ = 0.0F;
touching_enemy_prev_frame_ = false;
}
void Ship::processInput(float delta_time, uint8_t player_id) {
// Solo procesa input si la nave está viva
if (is_hit_) {
return;
}
auto* input = Input::get();
// Rotación: control directo del ángulo (no física, no inercial).
// Se actualiza también body_.angle para que el dibujado tras
// postUpdate refleje el cambio inmediatamente.
const bool ROTATE_RIGHT = (player_id == 0)
? input->checkActionPlayer1(InputAction::RIGHT, Input::ALLOW_REPEAT)
: input->checkActionPlayer2(InputAction::RIGHT, Input::ALLOW_REPEAT);
@@ -89,10 +86,10 @@ void Ship::processInput(float delta_time, uint8_t player_id) {
: input->checkActionPlayer2(InputAction::THRUST, Input::ALLOW_REPEAT);
if (ROTATE_RIGHT) {
body_.angle += Defaults::Physics::ROTATION_SPEED * delta_time;
body_.angle += config_.physics.rotation_speed * delta_time;
}
if (ROTATE_LEFT) {
body_.angle -= Defaults::Physics::ROTATION_SPEED * delta_time;
body_.angle -= config_.physics.rotation_speed * delta_time;
}
// Thrust: fuerza vectorial en la dirección de la nariz.
@@ -100,44 +97,36 @@ void Ship::processInput(float delta_time, uint8_t player_id) {
if (THRUST) {
const float DIR_X = std::cos(body_.angle - (Constants::PI / 2.0F));
const float DIR_Y = std::sin(body_.angle - (Constants::PI / 2.0F));
// Fuerza = masa * aceleración: 10 kg * 400 px/s² = 4000 (unidades arcade)
const float MAGNITUDE = body_.mass * Defaults::Physics::ACCELERATION;
const float MAGNITUDE = body_.mass * config_.physics.acceleration;
body_.applyForce(Vec2{.x = DIR_X * MAGNITUDE, .y = DIR_Y * MAGNITUDE});
}
}
void Ship::update(float delta_time) {
// Solo update si la nave está viva
if (is_hit_) {
return;
}
// Decrementar timer de invulnerabilidad
if (invulnerable_timer_ > 0.0F) {
invulnerable_timer_ -= delta_time;
invulnerable_timer_ = std::max(invulnerable_timer_, 0.0F);
}
// Decrementar timer d'estat HURT (a 0 → torna a normal sense efecte extern)
if (hurt_timer_ > 0.0F) {
hurt_timer_ -= delta_time;
hurt_timer_ = std::max(hurt_timer_, 0.0F);
}
// El movimiento real lo hace PhysicsWorld::update().
// Aquí solo lógica de estado.
// Cap de velocidad: el thrust acumula fuerza sin límite; limitamos
// la magnitud de body_.velocity tras aplicar fuerzas para preservar
// el feel arcade del MAX_VELOCITY original.
const float CURRENT_SPEED = body_.velocity.length();
if (CURRENT_SPEED > Defaults::Physics::MAX_VELOCITY) {
body_.velocity = body_.velocity * (Defaults::Physics::MAX_VELOCITY / CURRENT_SPEED);
if (CURRENT_SPEED > config_.physics.max_velocity) {
body_.velocity = body_.velocity * (config_.physics.max_velocity / CURRENT_SPEED);
}
}
void Ship::postUpdate(float /*delta_time*/) {
// Sincronizar mirror desde body_ tras la integración del world.
center_ = body_.position;
angle_ = body_.angle;
}
@@ -147,11 +136,10 @@ void Ship::draw() const {
return;
}
// Parpadeo si invulnerable
if (isInvulnerable()) {
const float BLINK_CYCLE = Defaults::Ship::BLINK_VISIBLE_TIME + Defaults::Ship::BLINK_INVISIBLE_TIME;
const float BLINK_CYCLE = config_.invulnerability.blink_visible + config_.invulnerability.blink_invisible;
const float TIME_IN_CYCLE = std::fmod(invulnerable_timer_, BLINK_CYCLE);
if (TIME_IN_CYCLE < Defaults::Ship::BLINK_INVISIBLE_TIME) {
if (TIME_IN_CYCLE < config_.invulnerability.blink_invisible) {
return;
}
}
@@ -160,20 +148,19 @@ void Ship::draw() const {
return;
}
// Efecto visual de empuje: escala proporcional a la velocidad.
// 0..200 px/s → escala 1.0..1.5 (manteniendo la sensación del Pascal original).
// Efecte visual d'empenta (modulador sobre l'escala base del YAML).
const float SPEED = getSpeed();
const float VISUAL_PUSH = SPEED / Defaults::Ship::VISUAL_PUSH_DIVISOR;
const float SCALE = 1.0F + (VISUAL_PUSH / Defaults::Ship::VISUAL_SCALE_DIVISOR);
const float VISUAL_PUSH = SPEED / config_.visual_thrust.push_divisor;
const float THRUST_MODULATOR = 1.0F + (VISUAL_PUSH / config_.visual_thrust.scale_divisor);
const float SCALE = config_.shape.scale * THRUST_MODULATOR;
// Parpelleig daurat mentre està ferida: alterna color normal ↔ color hurt
// a Hurt::BLINK_HZ (mateixa estètica que el wounded dels enemics).
SDL_Color color = color_normal_;
// Parpelleig daurat mentre està ferida: alterna color normal ↔ color hurt.
SDL_Color color = config_.colors.normal;
if (hurt_timer_ > 0.0F) {
const float CYCLE = 1.0F / Defaults::Ship::Hurt::BLINK_HZ;
const float CYCLE = 1.0F / config_.hurt.blink_hz;
const float T = std::fmod(hurt_timer_, CYCLE);
if (T < (CYCLE / 2.0F)) {
color = color_hurt_;
color = config_.colors.hurt;
}
}
@@ -181,6 +168,6 @@ void Ship::draw() const {
}
void Ship::hurt() {
hurt_timer_ = Defaults::Ship::Hurt::DURATION;
hurt_timer_ = config_.hurt.duration;
Audio::get()->playSound(Defaults::Sound::HURT, Audio::Group::GAME);
}
+18 -16
View File
@@ -6,15 +6,18 @@
#include <cstdint>
#include "core/defaults.hpp"
#include "core/entities/entity.hpp"
#include "core/types.hpp"
#include "game/entities/player_config.hpp"
class Ship : public Entities::Entity {
public:
Ship()
: Entity(nullptr) {}
explicit Ship(Rendering::Renderer* renderer, const char* shape_file = "ship.shp");
// shape_override: si no és nullptr, substitueix config.shape.path
// (utilitzat per donar al P2 un model visual diferent compartint la
// mateixa configuració del player).
explicit Ship(Rendering::Renderer* renderer, PlayerConfig config, const char* shape_override = nullptr);
void init() override { init(nullptr, false); }
void init(const Vec2* spawn_point, bool activar_invulnerabilitat = false);
@@ -26,20 +29,18 @@ class Ship : public Entities::Entity {
// Override: Interfaz de Entity
[[nodiscard]] auto isActive() const -> bool override { return !is_hit_; }
// Override: Interfaz de colisión
[[nodiscard]] auto getCollisionRadius() const -> float override {
return Defaults::Entities::SHIP_RADIUS;
}
// Override: Interfaz de colisión. Derivat al ctor del bounding_radius del
// shape carregat × scale × collision_factor.
[[nodiscard]] auto getCollisionRadius() const -> float override { return collision_radius_; }
[[nodiscard]] auto isCollidable() const -> bool override {
return !is_hit_ && invulnerable_timer_ <= 0.0F;
}
// Getters
[[nodiscard]] auto isInvulnerable() const -> bool { return invulnerable_timer_ > 0.0F; }
// Velocidad como vector cartesiano (ahora viene directa del body_).
[[nodiscard]] auto getVelocityVector() const -> Vec2 { return body_.velocity; }
// Velocidad escalar (utilidad para draw y debugging).
[[nodiscard]] auto getSpeed() const -> float { return body_.velocity.length(); }
[[nodiscard]] auto getConfig() const -> const PlayerConfig& { return config_; }
// Setters
void setCenter(const Vec2& nou_centre) {
@@ -65,17 +66,18 @@ class Ship : public Entities::Entity {
void setTouchingEnemyPrevFrame(bool touching) { touching_enemy_prev_frame_ = touching; }
private:
// Miembros específicos de Ship (heredados: renderer_, shape_, center_, angle_, brightness_, body_).
// Inicializados en la declaración: el ctor por defecto deja la nave "viva y sin invulnerabilidad",
// que es el estado coherente al que llevan tanto init() como el ctor con renderer.
// Configuració carregada des de YAML. Default-init zero permet el ctor
// per defecte (necessari per a `std::array<Ship, N>`); s'omple via
// copy/move-assignment quan GameScene crea la nau real.
PlayerConfig config_{};
// Radi de col·lisió derivat: shape.bounding_radius × shape.scale × shape.collision_factor.
float collision_radius_{0.0F};
bool is_hit_{false};
float invulnerable_timer_{0.0F}; // 0.0f = vulnerable, >0.0f = invulnerable
// Colors de la nau (propietats, prep per migració a YAML).
SDL_Color color_normal_{Defaults::Palette::SHIP};
SDL_Color color_hurt_{Defaults::Palette::WOUNDED};
// >0 → estat HURT (parpelleig color_normal_ ↔ color_hurt_).
// >0 → estat HURT (parpelleig color normal ↔ color hurt).
float hurt_timer_{0.0F};
// Edge-trigger: true si el frame anterior la nau ja estava en contacte amb un enemic.
+44 -5
View File
@@ -10,9 +10,14 @@
#include <iostream>
#include "core/audio/audio.hpp"
#include "core/entities/entity_loader.hpp"
#include "core/input/input.hpp"
#include "core/locale/locale.hpp"
#include "core/system/scene_context.hpp"
#include "core/system/service_menu.hpp"
#include "game/entities/bullet_registry.hpp"
#include "game/entities/enemy_registry.hpp"
#include "game/entities/player_config.hpp"
#include "game/stage_system/stage_loader.hpp"
#include "game/systems/collision_system.hpp"
#include "game/systems/continue_system.hpp"
@@ -48,9 +53,36 @@ GameScene::GameScene(SDLManager& sdl, SceneContext& context)
auto option = context_.consumeOption();
(void)option; // Suprimir warning de variable no usada
// Inicialitzar naves con renderer (P1=ship.shp, P2=ship2.shp)
ships_[0] = Ship(sdl.getRenderer(), "ship.shp"); // Jugador 1: nave estàndar
ships_[1] = Ship(sdl.getRenderer(), "ship2.shp"); // Jugador 2: interceptor con ales
// Carregar la configuració del player des de YAML. Sense fallback: si
// falla, abortem (la nau no és construïble sense paràmetres).
auto player_yaml = Entities::EntityLoader::load("player");
if (!player_yaml) {
std::cerr << "[GameScene] FATAL: no s'ha pogut carregar data/entities/player/player.yaml\n";
std::exit(EXIT_FAILURE);
}
auto player_config = PlayerConfig::fromYaml(*player_yaml);
if (!player_config) {
std::cerr << "[GameScene] FATAL: player.yaml mal format\n";
std::exit(EXIT_FAILURE);
}
// Carregar les configuracions dels 3 enemics. Sense fallback: si falla,
// abortem (els enemics no es poden construir sense els seus paràmetres).
if (!EnemyRegistry::loadAll()) {
std::cerr << "[GameScene] FATAL: no s'han pogut carregar els enemics YAML\n";
std::exit(EXIT_FAILURE);
}
// Carregar la configuració de la bala. Cal abans de construir el pool de
// bullets, ja que cada Bullet llegeix el registry al seu ctor.
if (!BulletRegistry::load()) {
std::cerr << "[GameScene] FATAL: no s'ha pogut carregar bullet.yaml\n";
std::exit(EXIT_FAILURE);
}
// Inicialitzar naves: P1 amb el shape del YAML, P2 amb override visual.
ships_[0] = Ship(sdl.getRenderer(), *player_config); // Jugador 1: nau estàndard
ships_[1] = Ship(sdl.getRenderer(), *player_config, "ship2.shp"); // Jugador 2: interceptor amb ales
// Inicialitzar balas con renderer
std::ranges::fill(bullets_, Bullet(sdl.getRenderer()));
@@ -182,6 +214,13 @@ void GameScene::handleEvent(const SDL_Event& event) {
}
void GameScene::update(float delta_time) {
// Pausa global: mentre el menu de servei esta obert, congelem la lògica
// de joc. El draw() segueix executant-se per a mantenir l'escena visible
// sota el menu.
if (const auto* menu = System::ServiceMenu::get(); menu != nullptr && menu->isOpen()) {
return;
}
// Orquestador delgado: cada paso vive en su propia función para
// mantener update() legible y reducir complejidad cognitiva.
stepPhysics(delta_time);
@@ -750,7 +789,7 @@ void GameScene::tocado(uint8_t player_id) {
0.0F, // sense herència angular
0.0F, // sin herencia visual
Defaults::Sound::EXPLOSION2,
Defaults::Palette::SHIP,
ships_[player_id].getConfig().colors.normal,
Defaults::Physics::Debris::ENEMY_LIFETIME,
Defaults::Physics::Debris::ENEMY_FRICTION,
Defaults::Physics::Debris::ENEMY_SEGMENT_MULTIPLIER);
@@ -936,7 +975,7 @@ void GameScene::fireBullet(uint8_t player_id) {
const int START_IDX = player_id * SLOTS_PER_PLAYER;
for (int i = START_IDX; i < START_IDX + SLOTS_PER_PLAYER; i++) {
if (!bullets_[i].isActive()) {
bullets_[i].fire(fire_position, ship_angle, player_id);
bullets_[i].fire(fire_position, ship_angle, player_id, ships_[player_id].getConfig().weapon.bullet_speed);
break;
}
}
+16 -2
View File
@@ -13,11 +13,13 @@
#include "core/audio/audio.hpp"
#include "core/defaults.hpp"
#include "core/graphics/shape_loader.hpp"
#include "core/input/define_inputs.hpp"
#include "core/input/input.hpp"
#include "core/locale/locale.hpp"
#include "core/math/easing.hpp"
#include "core/rendering/shape_renderer.hpp"
#include "core/system/scene_context.hpp"
#include "core/system/service_menu.hpp"
#include "project.h"
using SceneManager::SceneContext;
@@ -324,8 +326,20 @@ void TitleScene::update(float delta_time) {
break;
}
handleSkipInput();
handleStartInput();
// Les animacions segueixen pero els inputs es bloquegen mentre el menu
// de servei o l'overlay de redefinicio estiguin actius: en cas contrari,
// SDL_GetKeyboardState i SDL_GetGamepadButton segueixen veient les tecles
// pulsades i podrien disparar handleSkipInput/handleStartInput sense
// intencio. Mateixa logica: per a GameScene tota la pausa es global,
// pero a TitleScene nomes guardem els polls d'input.
const auto* menu = System::ServiceMenu::get();
const auto* di = System::DefineInputs::get();
const bool INPUT_BLOCKED = (menu != nullptr && menu->isOpen()) ||
(di != nullptr && di->isActive());
if (!INPUT_BLOCKED) {
handleSkipInput();
handleStartInput();
}
}
void TitleScene::updateStarfieldFadeInState(float delta_time) {
@@ -136,7 +136,10 @@ namespace StageSystem {
if (rand_val < config_->distribucio.pentagon + config_->distribucio.cuadrado) {
return EnemyType::SQUARE;
}
return EnemyType::PINWHEEL;
if (rand_val < config_->distribucio.pentagon + config_->distribucio.cuadrado + config_->distribucio.molinillo) {
return EnemyType::PINWHEEL;
}
return EnemyType::STAR;
}
void SpawnController::spawnEnemy(Enemy& enemy, EnemyType type, const Vec2* ship_pos) {
+2 -1
View File
@@ -28,6 +28,7 @@ namespace StageSystem {
uint8_t pentagon; // 0-100
uint8_t cuadrado; // 0-100
uint8_t molinillo; // 0-100
uint8_t star{0}; // 0-100 (opcional al YAML; default 0 per compat amb stages antics)
// Suma ha de ser 100, validat en StageLoader
};
@@ -59,7 +60,7 @@ namespace StageSystem {
// el tipo; basta con confirmar que no es 0 (sentinela "sin asignar").
return stage_id >= 1 &&
total_enemies > 0 && total_enemies <= 200 &&
distribucio.pentagon + distribucio.cuadrado + distribucio.molinillo == 100;
distribucio.pentagon + distribucio.cuadrado + distribucio.molinillo + distribucio.star == 100;
}
};
+204 -202
View File
@@ -19,241 +19,243 @@
namespace StageSystem {
auto StageLoader::load(const std::string& path) -> std::unique_ptr<StageSystemConfig> {
try {
// Normalize path: "data/stages/stages.yaml" → "stages/stages.yaml"
std::string normalized = path;
if (normalized.starts_with("data/")) {
normalized = normalized.substr(5);
}
auto StageLoader::load(const std::string& path) -> std::unique_ptr<StageSystemConfig> {
try {
// Normalize path: "data/stages/stages.yaml" → "stages/stages.yaml"
std::string normalized = path;
if (normalized.starts_with("data/")) {
normalized = normalized.substr(5);
}
// Load from resource system
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
if (data.empty()) {
std::cerr << "[StageLoader] Error: no es pot load " << normalized << '\n';
return nullptr;
}
// Convert to string
std::string yaml_content(data.begin(), data.end());
std::stringstream stream(yaml_content);
// Parse YAML
fkyaml::node yaml = fkyaml::node::deserialize(stream);
auto config = std::make_unique<StageSystemConfig>();
// Parse metadata
if (!yaml.contains("metadata")) {
std::cerr << "[StageLoader] Error: falta camp 'metadata'" << '\n';
return nullptr;
}
if (!parseMetadata(yaml["metadata"], config->metadata)) {
return nullptr;
}
// Parse stages
if (!yaml.contains("stages")) {
std::cerr << "[StageLoader] Error: falta camp 'stages'" << '\n';
return nullptr;
}
if (!yaml["stages"].is_sequence()) {
std::cerr << "[StageLoader] Error: 'stages' ha de ser una list" << '\n';
return nullptr;
}
for (const auto& stage_yaml : yaml["stages"]) {
StageConfig stage;
if (!parseStage(stage_yaml, stage)) {
// Load from resource system
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
if (data.empty()) {
std::cerr << "[StageLoader] Error: no es pot load " << normalized << '\n';
return nullptr;
}
config->stages.push_back(stage);
}
// Validar configuración
if (!validateConfig(*config)) {
// Convert to string
std::string yaml_content(data.begin(), data.end());
std::stringstream stream(yaml_content);
// Parse YAML
fkyaml::node yaml = fkyaml::node::deserialize(stream);
auto config = std::make_unique<StageSystemConfig>();
// Parse metadata
if (!yaml.contains("metadata")) {
std::cerr << "[StageLoader] Error: falta camp 'metadata'" << '\n';
return nullptr;
}
if (!parseMetadata(yaml["metadata"], config->metadata)) {
return nullptr;
}
// Parse stages
if (!yaml.contains("stages")) {
std::cerr << "[StageLoader] Error: falta camp 'stages'" << '\n';
return nullptr;
}
if (!yaml["stages"].is_sequence()) {
std::cerr << "[StageLoader] Error: 'stages' ha de ser una list" << '\n';
return nullptr;
}
for (const auto& stage_yaml : yaml["stages"]) {
StageConfig stage;
if (!parseStage(stage_yaml, stage)) {
return nullptr;
}
config->stages.push_back(stage);
}
// Validar configuración
if (!validateConfig(*config)) {
return nullptr;
}
std::cout << "[StageLoader] Carregats " << config->stages.size()
<< " stages correctament" << '\n';
return config;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Excepció: " << e.what() << '\n';
return nullptr;
}
std::cout << "[StageLoader] Carregats " << config->stages.size()
<< " stages correctament" << '\n';
return config;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Excepció: " << e.what() << '\n';
return nullptr;
}
}
auto StageLoader::parseMetadata(const fkyaml::node& yaml, MetadataStages& meta) -> bool {
try {
if (!yaml.contains("version") || !yaml.contains("total_stages")) {
std::cerr << "[StageLoader] Error: metadata incompleta" << '\n';
auto StageLoader::parseMetadata(const fkyaml::node& yaml, MetadataStages& meta) -> bool {
try {
if (!yaml.contains("version") || !yaml.contains("total_stages")) {
std::cerr << "[StageLoader] Error: metadata incompleta" << '\n';
return false;
}
meta.version = yaml["version"].get_value<std::string>();
meta.total_stages = yaml["total_stages"].get_value<uint8_t>();
meta.descripcio = yaml.contains("description")
? yaml["description"].get_value<std::string>()
: "";
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing metadata: " << e.what() << '\n';
return false;
}
meta.version = yaml["version"].get_value<std::string>();
meta.total_stages = yaml["total_stages"].get_value<uint8_t>();
meta.descripcio = yaml.contains("description")
? yaml["description"].get_value<std::string>()
: "";
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing metadata: " << e.what() << '\n';
return false;
}
}
auto StageLoader::parseStage(const fkyaml::node& yaml, StageConfig& stage) -> bool {
try {
if (!yaml.contains("stage_id") || !yaml.contains("total_enemies") ||
!yaml.contains("spawn_config") || !yaml.contains("enemy_distribution") ||
!yaml.contains("difficulty_multipliers")) {
std::cerr << "[StageLoader] Error: stage incompleta" << '\n';
auto StageLoader::parseStage(const fkyaml::node& yaml, StageConfig& stage) -> bool {
try {
if (!yaml.contains("stage_id") || !yaml.contains("total_enemies") ||
!yaml.contains("spawn_config") || !yaml.contains("enemy_distribution") ||
!yaml.contains("difficulty_multipliers")) {
std::cerr << "[StageLoader] Error: stage incompleta" << '\n';
return false;
}
stage.stage_id = yaml["stage_id"].get_value<uint8_t>();
stage.total_enemies = yaml["total_enemies"].get_value<uint8_t>();
if (!parseSpawnConfig(yaml["spawn_config"], stage.config_spawn)) {
return false;
}
if (!parseDistribution(yaml["enemy_distribution"], stage.distribucio)) {
return false;
}
if (!parseMultipliers(yaml["difficulty_multipliers"], stage.multiplicadors)) {
return false;
}
if (!stage.isValid()) {
std::cerr << "[StageLoader] Error: stage " << static_cast<int>(stage.stage_id)
<< " no es vàlid" << '\n';
return false;
}
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing stage: " << e.what() << '\n';
return false;
}
stage.stage_id = yaml["stage_id"].get_value<uint8_t>();
stage.total_enemies = yaml["total_enemies"].get_value<uint8_t>();
if (!parseSpawnConfig(yaml["spawn_config"], stage.config_spawn)) {
return false;
}
if (!parseDistribution(yaml["enemy_distribution"], stage.distribucio)) {
return false;
}
if (!parseMultipliers(yaml["difficulty_multipliers"], stage.multiplicadors)) {
return false;
}
if (!stage.isValid()) {
std::cerr << "[StageLoader] Error: stage " << static_cast<int>(stage.stage_id)
<< " no es vàlid" << '\n';
return false;
}
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing stage: " << e.what() << '\n';
return false;
}
}
auto StageLoader::parseSpawnConfig(const fkyaml::node& yaml, ConfigSpawn& config) -> bool {
try {
if (!yaml.contains("mode") || !yaml.contains("initial_delay") ||
!yaml.contains("spawn_interval")) {
std::cerr << "[StageLoader] Error: spawn_config incompleta" << '\n';
auto StageLoader::parseSpawnConfig(const fkyaml::node& yaml, ConfigSpawn& config) -> bool {
try {
if (!yaml.contains("mode") || !yaml.contains("initial_delay") ||
!yaml.contains("spawn_interval")) {
std::cerr << "[StageLoader] Error: spawn_config incompleta" << '\n';
return false;
}
auto mode_str = yaml["mode"].get_value<std::string>();
config.mode = parseSpawnMode(mode_str);
config.delay_inicial = yaml["initial_delay"].get_value<float>();
config.interval_spawn = yaml["spawn_interval"].get_value<float>();
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing spawn_config: " << e.what() << '\n';
return false;
}
auto mode_str = yaml["mode"].get_value<std::string>();
config.mode = parseSpawnMode(mode_str);
config.delay_inicial = yaml["initial_delay"].get_value<float>();
config.interval_spawn = yaml["spawn_interval"].get_value<float>();
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing spawn_config: " << e.what() << '\n';
return false;
}
}
auto StageLoader::parseDistribution(const fkyaml::node& yaml, DistribucioEnemics& dist) -> bool {
try {
if (!yaml.contains("pentagon") || !yaml.contains("cuadrado") ||
!yaml.contains("molinillo")) {
std::cerr << "[StageLoader] Error: enemy_distribution incompleta" << '\n';
auto StageLoader::parseDistribution(const fkyaml::node& yaml, DistribucioEnemics& dist) -> bool {
try {
if (!yaml.contains("pentagon") || !yaml.contains("cuadrado") ||
!yaml.contains("molinillo")) {
std::cerr << "[StageLoader] Error: enemy_distribution incompleta" << '\n';
return false;
}
dist.pentagon = yaml["pentagon"].get_value<uint8_t>();
dist.cuadrado = yaml["cuadrado"].get_value<uint8_t>();
dist.molinillo = yaml["molinillo"].get_value<uint8_t>();
// 'star' és opcional per compatibilitat amb stages antics (default 0).
dist.star = yaml.contains("star") ? yaml["star"].get_value<uint8_t>() : 0;
// Validar que suma 100
int sum = dist.pentagon + dist.cuadrado + dist.molinillo + dist.star;
if (sum != 100) {
std::cerr << "[StageLoader] Error: distribució no suma 100 (suma=" << sum << ")" << '\n';
return false;
}
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing distribution: " << e.what() << '\n';
return false;
}
dist.pentagon = yaml["pentagon"].get_value<uint8_t>();
dist.cuadrado = yaml["cuadrado"].get_value<uint8_t>();
dist.molinillo = yaml["molinillo"].get_value<uint8_t>();
// Validar que suma 100
int sum = dist.pentagon + dist.cuadrado + dist.molinillo;
if (sum != 100) {
std::cerr << "[StageLoader] Error: distribució no suma 100 (suma=" << sum << ")" << '\n';
return false;
}
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing distribution: " << e.what() << '\n';
return false;
}
}
auto StageLoader::parseMultipliers(const fkyaml::node& yaml, MultiplicadorsDificultat& mult) -> bool {
try {
if (!yaml.contains("speed_multiplier") || !yaml.contains("rotation_multiplier") ||
!yaml.contains("tracking_strength")) {
std::cerr << "[StageLoader] Error: difficulty_multipliers incompleta" << '\n';
auto StageLoader::parseMultipliers(const fkyaml::node& yaml, MultiplicadorsDificultat& mult) -> bool {
try {
if (!yaml.contains("speed_multiplier") || !yaml.contains("rotation_multiplier") ||
!yaml.contains("tracking_strength")) {
std::cerr << "[StageLoader] Error: difficulty_multipliers incompleta" << '\n';
return false;
}
mult.velocity = yaml["speed_multiplier"].get_value<float>();
mult.rotation = yaml["rotation_multiplier"].get_value<float>();
mult.tracking_strength = yaml["tracking_strength"].get_value<float>();
// Validar rangs raonables
if (mult.velocity < 0.1F || mult.velocity > 5.0F) {
std::cerr << "[StageLoader] Warning: speed_multiplier fuera de rang (0.1-5.0)" << '\n';
}
if (mult.rotation < 0.1F || mult.rotation > 5.0F) {
std::cerr << "[StageLoader] Warning: rotation_multiplier fuera de rang (0.1-5.0)" << '\n';
}
if (mult.tracking_strength < 0.0F || mult.tracking_strength > 2.0F) {
std::cerr << "[StageLoader] Warning: tracking_strength fuera de rang (0.0-2.0)" << '\n';
}
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing multipliers: " << e.what() << '\n';
return false;
}
mult.velocity = yaml["speed_multiplier"].get_value<float>();
mult.rotation = yaml["rotation_multiplier"].get_value<float>();
mult.tracking_strength = yaml["tracking_strength"].get_value<float>();
// Validar rangs raonables
if (mult.velocity < 0.1F || mult.velocity > 5.0F) {
std::cerr << "[StageLoader] Warning: speed_multiplier fuera de rang (0.1-5.0)" << '\n';
}
if (mult.rotation < 0.1F || mult.rotation > 5.0F) {
std::cerr << "[StageLoader] Warning: rotation_multiplier fuera de rang (0.1-5.0)" << '\n';
}
if (mult.tracking_strength < 0.0F || mult.tracking_strength > 2.0F) {
std::cerr << "[StageLoader] Warning: tracking_strength fuera de rang (0.0-2.0)" << '\n';
}
return true;
} catch (const std::exception& e) {
std::cerr << "[StageLoader] Error parsing multipliers: " << e.what() << '\n';
return false;
}
}
auto StageLoader::parseSpawnMode(const std::string& mode_str) -> ModeSpawn {
if (mode_str == "progressive") {
auto StageLoader::parseSpawnMode(const std::string& mode_str) -> ModeSpawn {
if (mode_str == "progressive") {
return ModeSpawn::PROGRESSIVE;
}
if (mode_str == "immediate") {
return ModeSpawn::IMMEDIATE;
}
if (mode_str == "wave") {
return ModeSpawn::WAVE;
}
std::cerr << "[StageLoader] Warning: mode de spawn desconegut '" << mode_str
<< "', usant PROGRESSIVE" << '\n';
return ModeSpawn::PROGRESSIVE;
}
if (mode_str == "immediate") {
return ModeSpawn::IMMEDIATE;
}
if (mode_str == "wave") {
return ModeSpawn::WAVE;
}
std::cerr << "[StageLoader] Warning: mode de spawn desconegut '" << mode_str
<< "', usant PROGRESSIVE" << '\n';
return ModeSpawn::PROGRESSIVE;
}
auto StageLoader::validateConfig(const StageSystemConfig& config) -> bool {
if (config.stages.empty()) {
std::cerr << "[StageLoader] Error: sin stage carregat" << '\n';
return false;
}
if (config.stages.size() != config.metadata.total_stages) {
std::cerr << "[StageLoader] Warning: nombre de stages (" << config.stages.size()
<< ") no coincideix con metadata.total_stages ("
<< static_cast<int>(config.metadata.total_stages) << ")" << '\n';
}
// Validar stage_id consecutius
for (size_t i = 0; i < config.stages.size(); i++) {
if (config.stages[i].stage_id != i + 1) {
std::cerr << "[StageLoader] Error: stage_id no consecutius (esperat "
<< i + 1 << ", trobat " << static_cast<int>(config.stages[i].stage_id)
<< ")" << '\n';
auto StageLoader::validateConfig(const StageSystemConfig& config) -> bool {
if (config.stages.empty()) {
std::cerr << "[StageLoader] Error: sin stage carregat" << '\n';
return false;
}
if (config.stages.size() != config.metadata.total_stages) {
std::cerr << "[StageLoader] Warning: nombre de stages (" << config.stages.size()
<< ") no coincideix con metadata.total_stages ("
<< static_cast<int>(config.metadata.total_stages) << ")" << '\n';
}
// Validar stage_id consecutius
for (size_t i = 0; i < config.stages.size(); i++) {
if (config.stages[i].stage_id != i + 1) {
std::cerr << "[StageLoader] Error: stage_id no consecutius (esperat "
<< i + 1 << ", trobat " << static_cast<int>(config.stages[i].stage_id)
<< ")" << '\n';
return false;
}
}
return true;
}
return true;
}
} // namespace StageSystem
+14 -102
View File
@@ -8,86 +8,12 @@
#include "core/physics/collision.hpp"
#include "core/types.hpp"
#include "game/constants.hpp"
#include "game/entities/bullet_config.hpp"
#include "game/systems/enemy_event_dispatcher.hpp"
namespace Systems::Collision {
namespace {
constexpr uint8_t NO_SHOOTER = 0xFF;
// Lookup tabla puntos / color por tipo de enemy (mantiene la lógica
// anterior pero centralizada para reutilizar entre paths).
auto scoreForType(EnemyType type) -> int {
switch (type) {
case EnemyType::PENTAGON:
return Defaults::Enemies::Scoring::PENTAGON_SCORE;
case EnemyType::SQUARE:
return Defaults::Enemies::Scoring::SQUARE_SCORE;
case EnemyType::PINWHEEL:
return Defaults::Enemies::Scoring::PINWHEEL_SCORE;
}
return 0;
}
auto colorForType(EnemyType type) -> SDL_Color {
switch (type) {
case EnemyType::PENTAGON:
return Defaults::Palette::PENTAGON;
case EnemyType::SQUARE:
return Defaults::Palette::SQUARE;
case EnemyType::PINWHEEL:
return Defaults::Palette::PINWHEEL;
}
return SDL_Color{};
}
// Mata al enemy con explosión: floating score, debris con velocity heredada,
// sonido. Si shooter_id ≠ NO_SHOOTER, suma puntos a ese jugador.
// CRUCIAL: leer velocity/datos ANTES de destruir() (que zera la velocity).
void explodeNow(Context& ctx, Enemy& enemy, uint8_t shooter_id) {
const Vec2 ENEMY_POS = enemy.getCenter();
const Vec2 ENEMY_VEL = enemy.getVelocityVector();
const float BRIGHTNESS = enemy.getBrightness();
const auto SHAPE = enemy.getShape();
const EnemyType TYPE = enemy.getType();
const int POINTS = scoreForType(TYPE);
const SDL_Color COLOR = colorForType(TYPE);
if (shooter_id != NO_SHOOTER) {
ctx.score_per_player[shooter_id] += POINTS;
}
ctx.floating_score_manager.crear(POINTS, ENEMY_POS);
enemy.destroy();
constexpr float SPEED_EXPLOSIO = 80.0F; // px/s (explosión suave)
const Vec2 INHERITED_VEL = ENEMY_VEL * Defaults::Physics::Debris::ENEMY_VELOCITY_INHERITANCE;
ctx.debris_manager.explode(
SHAPE,
ENEMY_POS,
0.0F, // angle (rotación interna del enemy)
1.0F, // escala
SPEED_EXPLOSIO,
BRIGHTNESS,
INHERITED_VEL,
0.0F, // sense herència angular: evita que els 5 trossos curvin en bloc
0.0F, // sin herencia visual
Defaults::Sound::EXPLOSION,
COLOR,
Defaults::Physics::Debris::ENEMY_LIFETIME,
Defaults::Physics::Debris::ENEMY_FRICTION,
Defaults::Physics::Debris::ENEMY_SEGMENT_MULTIPLIER);
// Firework burst radial des del centro de l'enemic (efecte adicional al debris).
// Línia blanca + halo daurat (WOUNDED) per a feel d'espurnes.
ctx.firework_manager.spawn(ENEMY_POS,
Defaults::FX::Firework::DEFAULT_COLOR,
Defaults::FX::Firework::SPEED,
Defaults::FX::Firework::N_POINTS,
Defaults::FX::Firework::INITIAL_BRIGHTNESS,
/*glow=*/true,
Defaults::Palette::WOUNDED);
}
// Trenca una bala en debris (8 fragments de l'octàgon) + so HIT + desactiva.
// S'invoca des de qualsevol desactivació de bala (impacte amb enemic, amb jugador,
@@ -105,7 +31,7 @@ namespace Systems::Collision {
0.0F, // sense velocity angular heretada
0.0F, // sense rotació visual heretada
Defaults::Sound::HIT,
Defaults::Palette::BULLET,
bullet.getConfig().colors.normal,
Defaults::Physics::Debris::TEMPS_VIDA,
Defaults::Physics::Debris::ACCELERACIO,
1); // sense duplicat de segments
@@ -121,31 +47,16 @@ namespace Systems::Collision {
continue;
}
for (auto& enemy : ctx.enemies) {
if (!Physics::checkCollisionSwept(bullet.getPrevPosition(), bullet.getCenter(), Defaults::Entities::BULLET_RADIUS, enemy, AMPLIFIER)) {
if (!Physics::checkCollisionSwept(bullet.getPrevPosition(), bullet.getCenter(), bullet.getCollisionRadius(), enemy, AMPLIFIER)) {
continue;
}
// *** COLISIÓN bullet → enemy ***
// Empuje físico cuasi-realista: el impulse és el moment de la bala
// (m·v) multiplicat pel factor de transferència. Direcció = vector
// velocity de la bala (cap a on viatjava).
const Vec2 IMPULSE = bullet.getBody().velocity *
(bullet.getBody().mass * Defaults::Physics::Bullet::IMPACT_MOMENTUM_FACTOR);
enemy.applyImpulse(IMPULSE);
const uint8_t SHOOTER = bullet.getOwnerId();
if (enemy.isWounded()) {
// Segundo impacto sobre enemy ya herido → muerte instantánea,
// puntos al nuevo shooter.
explodeNow(ctx, enemy, SHOOTER);
} else {
// Primer impacto → entra en estado herido (explosión diferida).
enemy.hurt(SHOOTER);
}
// La cadena d'efectes (impulse, hurt, destroy, debris, score...) viu
// al YAML de l'enemic via la secció `events:`. Aquí només dispatchem.
Systems::EnemyEvents::dispatchEvent(ctx, enemy, EnemyEventType::ON_HIT, bullet.getOwnerId(), &bullet);
breakBullet(ctx.debris_manager, bullet);
break; // Una bala impacta a un enemy y muere
break;
}
}
}
@@ -156,7 +67,7 @@ namespace Systems::Collision {
continue;
}
enemy.consumeWoundExpired();
explodeNow(ctx, enemy, enemy.getLastHitBy());
Systems::EnemyEvents::dispatchEvent(ctx, enemy, EnemyEventType::ON_HURT_END, enemy.getLastHitBy());
}
}
@@ -224,7 +135,8 @@ namespace Systems::Collision {
// Segon impacte durant HURT → mort. Aplica un impuls afegit
// perquè l'enemic surti disparat (feedback visible).
const Vec2 SHIP_VEL = ctx.ships[i].getVelocityVector();
const Vec2 IMPULSE = SHIP_VEL * (Defaults::Ship::MASS * Defaults::Physics::Ship::DEATH_IMPACT_MOMENTUM_FACTOR);
const float DEATH_FACTOR = ctx.ships[i].getConfig().physics.death_impact_factor;
const Vec2 IMPULSE = SHIP_VEL * (ctx.ships[i].getBody().mass * DEATH_FACTOR);
touched_enemy->applyImpulse(IMPULSE);
ctx.on_player_hit(i);
} else {
@@ -268,7 +180,7 @@ namespace Systems::Collision {
continue;
}
if (!Physics::checkCollisionSwept(bullet.getPrevPosition(), bullet.getCenter(), Defaults::Entities::BULLET_RADIUS, ctx.ships[player_id], AMPLIFIER)) {
if (!Physics::checkCollisionSwept(bullet.getPrevPosition(), bullet.getCenter(), bullet.getCollisionRadius(), ctx.ships[player_id], AMPLIFIER)) {
continue;
}
@@ -277,7 +189,7 @@ namespace Systems::Collision {
// de la bala a la nau ABANS de on_player_hit perquè tocado()
// captura la velocitat per als debris (si no, queden quiets).
const Vec2 BULLET_IMPULSE = bullet.getBody().velocity *
(bullet.getBody().mass * Defaults::Physics::Bullet::IMPACT_MOMENTUM_FACTOR);
(bullet.getBody().mass * bullet.getConfig().physics.impact_momentum_factor);
ctx.ships[player_id].getBody().applyImpulse(BULLET_IMPULSE);
ctx.on_player_hit(player_id);
ctx.lives_per_player[BULLET_OWNER]++;
@@ -304,12 +216,12 @@ namespace Systems::Collision {
float min_y;
float max_y;
Constants::getPlayAreaBounds(min_x, max_x, min_y, max_y);
constexpr float R = Defaults::Entities::BULLET_RADIUS;
for (auto& bullet : bullets) {
if (!bullet.isActive()) {
continue;
}
const float R = bullet.getCollisionRadius();
const Vec2& pos = bullet.getCenter();
if (pos.x < min_x + R || pos.x > max_x - R ||
pos.y < min_y + R || pos.y > max_y - R) {
@@ -0,0 +1,101 @@
// enemy_event_dispatcher.cpp - Implementació del dispatcher d'events d'enemic
// © 2026 JailDesigner
#include "game/systems/enemy_event_dispatcher.hpp"
#include <cstdint>
#include "core/defaults.hpp"
#include "core/types.hpp"
#include "game/entities/bullet.hpp"
#include "game/entities/bullet_config.hpp"
#include "game/entities/enemy_config.hpp"
namespace Systems::EnemyEvents {
namespace {
constexpr uint8_t NO_SHOOTER = 0xFF;
void doAddScore(Systems::Collision::Context& ctx, const Enemy& enemy, uint8_t shooter) {
const int POINTS = enemy.getConfig().score;
if (shooter != NO_SHOOTER) {
ctx.score_per_player[shooter] += POINTS;
}
ctx.floating_score_manager.crear(POINTS, enemy.getCenter());
}
void doCreateDebris(Systems::Collision::Context& ctx, const Enemy& enemy) {
constexpr float SPEED_EXPLOSIO = 80.0F;
const Vec2 INHERITED_VEL = enemy.getVelocityVector() *
Defaults::Physics::Debris::ENEMY_VELOCITY_INHERITANCE;
ctx.debris_manager.explode(
enemy.getShape(),
enemy.getCenter(),
0.0F,
1.0F,
SPEED_EXPLOSIO,
enemy.getBrightness(),
INHERITED_VEL,
0.0F,
0.0F,
Defaults::Sound::EXPLOSION,
enemy.getConfig().colors.normal,
Defaults::Physics::Debris::ENEMY_LIFETIME,
Defaults::Physics::Debris::ENEMY_FRICTION,
Defaults::Physics::Debris::ENEMY_SEGMENT_MULTIPLIER);
}
void doCreateFireworks(Systems::Collision::Context& ctx, const Enemy& enemy) {
ctx.firework_manager.spawn(enemy.getCenter(),
Defaults::FX::Firework::DEFAULT_COLOR,
Defaults::FX::Firework::SPEED,
Defaults::FX::Firework::N_POINTS,
Defaults::FX::Firework::INITIAL_BRIGHTNESS,
/*glow=*/true,
enemy.getConfig().colors.wounded);
}
void doApplyImpulse(Enemy& enemy, const Bullet* bullet) {
if (bullet == nullptr) {
return;
}
const Vec2 IMPULSE = bullet->getBody().velocity *
(bullet->getBody().mass * bullet->getConfig().physics.impact_momentum_factor);
enemy.applyImpulse(IMPULSE);
}
} // namespace
void dispatchEvent(Systems::Collision::Context& ctx, Enemy& enemy, EnemyEventType event, uint8_t shooter_id, const Bullet* bullet) {
const auto& actions = enemy.getConfig().events.getActions(event);
for (const auto& action : actions) {
switch (action.type) {
case EnemyActionType::SET_HURT:
if (enemy.isWounded()) {
// Segon hit sobre wounded → mort immediata (regla 2-hits).
dispatchEvent(ctx, enemy, EnemyEventType::ON_DESTROY, shooter_id, bullet);
enemy.destroy();
} else {
enemy.hurt(shooter_id);
}
break;
case EnemyActionType::DESTROY:
dispatchEvent(ctx, enemy, EnemyEventType::ON_DESTROY, shooter_id, bullet);
enemy.destroy();
break;
case EnemyActionType::ADD_SCORE:
doAddScore(ctx, enemy, shooter_id);
break;
case EnemyActionType::CREATE_DEBRIS:
doCreateDebris(ctx, enemy);
break;
case EnemyActionType::CREATE_FIREWORKS:
doCreateFireworks(ctx, enemy);
break;
case EnemyActionType::APPLY_IMPULSE:
doApplyImpulse(enemy, bullet);
break;
}
}
}
} // namespace Systems::EnemyEvents
@@ -0,0 +1,23 @@
// enemy_event_dispatcher.hpp - Executa les accions YAML d'un event d'enemic
// © 2026 JailDesigner
//
// Mira la llista d'EnemyAction associada a l'event al config de l'enemic i les
// executa una per una. L'acció DESTROY dispara recursivament ON_DESTROY abans
// de desactivar físicament l'enemic (el parser garanteix que ON_DESTROY no
// conté DESTROY, evitant recursió infinita).
#pragma once
#include <cstdint>
#include "game/entities/enemy_event.hpp"
#include "game/systems/collision_system.hpp"
namespace Systems::EnemyEvents {
// shooter_id: id del jugador que ha disparat (0xFF = sense atribució).
// bullet: punter opcional a la bala que ha causat l'event (usat per APPLY_IMPULSE);
// nullptr per a events no derivats d'una bala (on_hurt_end).
void dispatchEvent(Systems::Collision::Context& ctx, Enemy& enemy, EnemyEventType event, uint8_t shooter_id, const Bullet* bullet = nullptr);
} // namespace Systems::EnemyEvents
+8
View File
@@ -13,8 +13,12 @@
#include <memory>
#include "core/system/director.hpp"
#include "core/system/relaunch.hpp"
auto SDL_AppInit(void** appstate, int argc, char* argv[]) -> SDL_AppResult {
// Desem argv perquè el menu de servei pugui demanar un reinici en calent
// (execv) sense haver de conèixer Director.
System::Relaunch::setArgv(argc, argv);
auto director = std::make_unique<Director>(argc, argv);
*appstate = director.release();
return SDL_APP_CONTINUE;
@@ -33,4 +37,8 @@ auto SDL_AppIterate(void* appstate) -> SDL_AppResult {
void SDL_AppQuit(void* appstate, SDL_AppResult /*result*/) {
// Reabsorbim la propietat: el destructor del Director allibera tot.
std::unique_ptr<Director> director(static_cast<Director*>(appstate));
director.reset();
// Si el menu va demanar reinici, fem execv ara que tot esta net. En cas
// d'exit no torna; si falla, l'aplicacio surt amb codi d'error.
System::Relaunch::execIfRequested();
}
+72 -65
View File
@@ -1,92 +1,99 @@
// pack_resources.cpp - Utilitat per crear paquets de recursos
// © 2026 JailDesigner
#include "../../source/core/resources/resource_pack.hpp"
#include <filesystem>
#include <iostream>
#include <string>
void print_usage(const char* program_name) {
std::cout << "Ús: " << program_name << " [opcions] [directori_entrada] [fitxer_sortida]\n";
std::cout << "\nOpcions:\n";
std::cout << " --list <fitxer> Llistar contingut d'un paquet\n";
std::cout << "\nExemples:\n";
std::cout << " " << program_name << " data resources.pack\n";
std::cout << " " << program_name << " --list resources.pack\n";
std::cout << "\nSi no s'especifiquen arguments, empaqueta 'data/' a 'resources.pack'\n";
}
#include "core/resources/resource_pack.hpp"
#include "project.h"
namespace {
void showHelp() {
std::cout << Project::LONG_NAME << " - Resource Packer\n";
std::cout << "==============================================\n";
std::cout << "Usage: pack_resources [options] [input_dir] [output_file]\n\n";
std::cout << "Options:\n";
std::cout << " --help Show this help message\n";
std::cout << " --list List contents of an existing pack file\n\n";
std::cout << "Arguments:\n";
std::cout << " input_dir Directory to pack (default: data)\n";
std::cout << " output_file Pack file name (default: resources.pack)\n";
}
void listPackContents(const std::string& pack_file) {
Resource::Pack pack;
if (!pack.loadPack(pack_file)) {
std::cerr << "Error: cannot open pack file: " << pack_file << '\n';
return;
}
auto resources = pack.getResourceList();
std::cout << "Pack file: " << pack_file << '\n';
std::cout << "Resources: " << resources.size() << '\n';
for (const auto& r : resources) { std::cout << " " << r << '\n'; }
}
} // namespace
int main(int argc, char* argv[]) {
std::string input_dir = "data";
std::string data_dir = "data";
std::string output_file = "resources.pack";
bool list_mode = false;
bool data_dir_set = false;
// Processar arguments
if (argc == 2 && std::string(argv[1]) == "--help") {
print_usage(argv[0]);
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--help" || arg == "-h") {
showHelp();
return 0;
}
if (arg == "--list") {
list_mode = true;
if (i + 1 < argc) { output_file = argv[++i]; }
continue;
}
if (!arg.empty() && arg[0] != '-') {
if (!data_dir_set) {
data_dir = arg;
data_dir_set = true;
} else {
output_file = arg;
}
}
}
if (list_mode) {
listPackContents(output_file);
return 0;
}
// Mode --list
if (argc == 3 && std::string(argv[1]) == "--list") {
Resource::Pack pack;
if (!pack.loadPack(argv[2])) {
std::cerr << "ERROR: No es pot carregar " << argv[2] << "\n";
return 1;
}
std::cout << Project::LONG_NAME << " - Resource Packer\n";
std::cout << "==============================================\n";
std::cout << "Input directory: " << data_dir << '\n';
std::cout << "Output file: " << output_file << '\n';
std::cout << "Contingut de " << argv[2] << ":\n";
auto resources = pack.getResourceList();
std::cout << "Total: " << resources.size() << " recursos\n\n";
for (const auto& name : resources) {
std::cout << " " << name << "\n";
}
return 0;
}
// Mode empaquetar
if (argc >= 3) {
input_dir = argv[1];
output_file = argv[2];
}
// Verificar que existeix el directori
if (!std::filesystem::exists(input_dir)) {
std::cerr << "ERROR: Directori no trobat: " << input_dir << "\n";
if (!std::filesystem::exists(data_dir)) {
std::cerr << "Error: input directory does not exist: " << data_dir << '\n';
return 1;
}
if (!std::filesystem::is_directory(input_dir)) {
std::cerr << "ERROR: " << input_dir << " no és un directori\n";
return 1;
}
// Crear paquet
std::cout << "Creant paquet de recursos...\n";
std::cout << " Entrada: " << input_dir << "\n";
std::cout << " Sortida: " << output_file << "\n\n";
Resource::Pack pack;
if (!pack.addDirectory(input_dir)) {
std::cerr << "ERROR: No s'ha pogut afegir el directori\n";
std::cout << "Scanning and packing resources...\n";
if (!pack.addDirectory(data_dir)) {
std::cerr << "Error: failed to add directory to pack\n";
return 1;
}
std::cout << "Found " << pack.getResourceList().size() << " resources\n";
std::cout << "Saving pack file...\n";
if (!pack.savePack(output_file)) {
std::cerr << "ERROR: No s'ha pogut guardar el paquet\n";
std::cerr << "Error: failed to save pack file\n";
return 1;
}
// Resum
auto resources = pack.getResourceList();
std::cout << "\n";
std::cout << "✓ Paquet creat amb èxit!\n";
std::cout << " Recursos: " << resources.size() << "\n";
// Mostrar mida del fitxer
auto file_size = std::filesystem::file_size(output_file);
std::cout << " Mida: " << (file_size / 1024) << " KB\n";
auto file_size = std::filesystem::file_size(std::filesystem::path(output_file));
std::cout << "Pack file created: " << output_file << " ("
<< (static_cast<double>(file_size) / 1024.0 / 1024.0) << " MB)\n";
return 0;
}