Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 64ab08973c | |||
| 94a7a38cdd | |||
| 76165e4345 | |||
| 767a1f6af8 | |||
| 20ca024100 | |||
| 3c3857c1b2 | |||
| 523342fed9 | |||
| 217ca58b1a | |||
| ec6565bf71 | |||
| cd7f06f3a1 | |||
| 8886873ed5 | |||
| a41e696b69 | |||
| 4b7cbd88bb | |||
| 789cbbc593 | |||
| 1dd87c0707 | |||
| 330044e10f | |||
| f8c5207d5c | |||
| 2caaa29124 | |||
| cdc4d07394 | |||
| 1023cde1be | |||
| a3aeed4b7c | |||
| 3b0354da54 | |||
| 622ccd22bc | |||
| 1441134aea |
10
.gitignore
vendored
10
.gitignore
vendored
@@ -17,6 +17,16 @@ asteroids
|
|||||||
*.exe
|
*.exe
|
||||||
*.out
|
*.out
|
||||||
*.app
|
*.app
|
||||||
|
tools/pack_resources/pack_resources
|
||||||
|
tools/pack_resources/pack_resources.exe
|
||||||
|
|
||||||
|
# Releases
|
||||||
|
*.zip
|
||||||
|
*.tar.gz
|
||||||
|
*.dmg
|
||||||
|
|
||||||
|
# Generated resources
|
||||||
|
resources.pack
|
||||||
|
|
||||||
# Compiled Object files
|
# Compiled Object files
|
||||||
*.o
|
*.o
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# CMakeLists.txt
|
# CMakeLists.txt
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(orni VERSION 0.3.1)
|
project(orni VERSION 0.5.0)
|
||||||
|
|
||||||
# Info del proyecto
|
# Info del proyecto
|
||||||
set(PROJECT_LONG_NAME "Orni Attack")
|
set(PROJECT_LONG_NAME "Orni Attack")
|
||||||
@@ -73,6 +73,11 @@ target_compile_options(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:-O2 -ffunctio
|
|||||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)
|
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:DEBUG>:_DEBUG>)
|
||||||
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:RELEASE_BUILD>)
|
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:RELEASE>:RELEASE_BUILD>)
|
||||||
|
|
||||||
|
# Definir MACOS_BUNDLE si es un bundle de macOS
|
||||||
|
if(APPLE AND MACOSX_BUNDLE)
|
||||||
|
target_compile_definitions(${PROJECT_NAME} PRIVATE MACOS_BUNDLE)
|
||||||
|
endif()
|
||||||
|
|
||||||
# Configuración específica para cada plataforma
|
# Configuración específica para cada plataforma
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
|
target_compile_definitions(${PROJECT_NAME} PRIVATE WINDOWS_BUILD)
|
||||||
|
|||||||
41
Makefile
41
Makefile
@@ -58,6 +58,25 @@ else
|
|||||||
UNAME_S := $(shell uname -s)
|
UNAME_S := $(shell uname -s)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# PACKING TOOL
|
||||||
|
# ==============================================================================
|
||||||
|
PACK_TOOL := tools/pack_resources/pack_resources
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# DEFAULT GOAL
|
||||||
|
# ==============================================================================
|
||||||
|
.DEFAULT_GOAL := all
|
||||||
|
|
||||||
|
.PHONY: pack_tool resources.pack
|
||||||
|
|
||||||
|
pack_tool:
|
||||||
|
@$(MAKE) -C tools/pack_resources
|
||||||
|
|
||||||
|
resources.pack: pack_tool
|
||||||
|
@echo "Creating resources.pack..."
|
||||||
|
@./$(PACK_TOOL) data resources.pack
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# TARGETS
|
# TARGETS
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
@@ -67,8 +86,8 @@ endif
|
|||||||
# BUILD TARGETS (delegate to CMake)
|
# BUILD TARGETS (delegate to CMake)
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
|
||||||
# Default target: build with CMake
|
# Default target: build with CMake + resources
|
||||||
all: $(TARGET_FILE)
|
all: resources.pack $(TARGET_FILE)
|
||||||
|
|
||||||
$(TARGET_FILE):
|
$(TARGET_FILE):
|
||||||
@cmake -B build -DCMAKE_BUILD_TYPE=Release
|
@cmake -B build -DCMAKE_BUILD_TYPE=Release
|
||||||
@@ -76,10 +95,10 @@ $(TARGET_FILE):
|
|||||||
@echo "Build successful: $(TARGET_FILE)"
|
@echo "Build successful: $(TARGET_FILE)"
|
||||||
|
|
||||||
# Debug build
|
# Debug build
|
||||||
debug:
|
debug: resources.pack
|
||||||
@cmake -B build -DCMAKE_BUILD_TYPE=Debug
|
@cmake -B build -DCMAKE_BUILD_TYPE=Debug
|
||||||
@cmake --build build
|
@cmake --build build
|
||||||
@echo "Debug build successful: $(TARGET_FILE)_debug"
|
@echo "Debug build successful: $(TARGET_FILE)"
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# RELEASE PACKAGING TARGETS
|
# RELEASE PACKAGING TARGETS
|
||||||
@@ -87,7 +106,7 @@ debug:
|
|||||||
|
|
||||||
# macOS Release (Apple Silicon)
|
# macOS Release (Apple Silicon)
|
||||||
.PHONY: macos_release
|
.PHONY: macos_release
|
||||||
macos_release:
|
macos_release: pack_tool resources.pack
|
||||||
@echo "Creating macOS release - Version: $(VERSION)"
|
@echo "Creating macOS release - Version: $(VERSION)"
|
||||||
|
|
||||||
# Check/install create-dmg
|
# Check/install create-dmg
|
||||||
@@ -104,8 +123,8 @@ macos_release:
|
|||||||
@$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
|
@$(MKDIR) "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources"
|
||||||
@$(MKDIR) Frameworks
|
@$(MKDIR) Frameworks
|
||||||
|
|
||||||
# Copy resources
|
# Copy resources.pack to Resources
|
||||||
@cp -r resources "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources/"
|
@cp resources.pack "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Resources/"
|
||||||
@ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework"
|
@ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework "$(RELEASE_FOLDER)/$(APP_NAME).app/Contents/Frameworks/SDL3.framework"
|
||||||
@ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework Frameworks/SDL3.framework
|
@ditto release/frameworks/SDL3.xcframework/macos-arm64_x86_64/SDL3.framework Frameworks/SDL3.framework
|
||||||
|
|
||||||
@@ -150,6 +169,8 @@ macos_release:
|
|||||||
--icon-size 96 \
|
--icon-size 96 \
|
||||||
--text-size 12 \
|
--text-size 12 \
|
||||||
--icon "$(APP_NAME).app" 278 102 \
|
--icon "$(APP_NAME).app" 278 102 \
|
||||||
|
--icon "LICENSE" 441 102 \
|
||||||
|
--icon "README.md" 604 102 \
|
||||||
--app-drop-link 115 102 \
|
--app-drop-link 115 102 \
|
||||||
--hide-extension "$(APP_NAME).app" \
|
--hide-extension "$(APP_NAME).app" \
|
||||||
"$(MACOS_ARM_RELEASE)" \
|
"$(MACOS_ARM_RELEASE)" \
|
||||||
@@ -313,12 +334,14 @@ else
|
|||||||
@$(RMFILE) $(TARGET_FILE) $(TARGET_FILE)_debug
|
@$(RMFILE) $(TARGET_FILE) $(TARGET_FILE)_debug
|
||||||
@$(RMDIR) build $(RELEASE_FOLDER)
|
@$(RMDIR) build $(RELEASE_FOLDER)
|
||||||
@$(RMFILE) *.dmg *.zip *.tar.gz 2>/dev/null || true
|
@$(RMFILE) *.dmg *.zip *.tar.gz 2>/dev/null || true
|
||||||
|
@$(RMFILE) resources.pack 2>/dev/null || true
|
||||||
|
@$(MAKE) -C tools/pack_resources clean 2>/dev/null || true
|
||||||
endif
|
endif
|
||||||
@echo "Clean complete"
|
@echo "Clean complete"
|
||||||
|
|
||||||
# Backup to remote server
|
# Backup to remote server
|
||||||
backup:
|
backup:
|
||||||
@echo "Backing up project to maverick:/home/sergio/git-backup/asteroids..."
|
@echo "Backing up project to maverick:/home/sergio/git-backup/orni..."
|
||||||
rsync -a --delete \
|
rsync -a --delete \
|
||||||
--exclude='build/' \
|
--exclude='build/' \
|
||||||
--exclude='*.o' \
|
--exclude='*.o' \
|
||||||
@@ -326,7 +349,7 @@ backup:
|
|||||||
--exclude='orni' \
|
--exclude='orni' \
|
||||||
--exclude='orni_debug' \
|
--exclude='orni_debug' \
|
||||||
--exclude='*_release/' \
|
--exclude='*_release/' \
|
||||||
$(DIR_ROOT) maverick:/home/sergio/git-backup/asteroids/
|
$(DIR_ROOT) maverick:/home/sergio/git-backup/orni/
|
||||||
@echo "Backup completed successfully"
|
@echo "Backup completed successfully"
|
||||||
|
|
||||||
# Help target
|
# Help target
|
||||||
|
|||||||
BIN
data/music/game.ogg
Normal file
BIN
data/music/game.ogg
Normal file
Binary file not shown.
BIN
data/music/title.ogg
Normal file
BIN
data/music/title.ogg
Normal file
Binary file not shown.
@@ -6,14 +6,18 @@ name: bullet
|
|||||||
scale: 1.0
|
scale: 1.0
|
||||||
center: 0, 0
|
center: 0, 0
|
||||||
|
|
||||||
# Pentàgon petit radi=5 (1/4 del enemic)
|
# Cercle (octàgon regular radi=3)
|
||||||
# Pentàgon regular amb 72° entre punts
|
# 8 punts equidistants (45° entre ells) per aproximar un cercle
|
||||||
|
# Començant a angle=-90° (amunt), rotant sentit horari
|
||||||
#
|
#
|
||||||
# Conversió polar→cartesià (radi=5, SDL: Y creix cap avall):
|
# Conversió polar→cartesià (radi=3, SDL: Y creix cap avall):
|
||||||
# angle=-90°: (0.00, -5.00)
|
# angle=-90°: (0.00, -3.00)
|
||||||
# angle=-18°: (4.76, -1.55)
|
# angle=-45°: (2.12, -2.12)
|
||||||
# angle=54°: (2.94, 4.05)
|
# angle=0°: (3.00, 0.00)
|
||||||
# angle=126°: (-2.94, 4.05)
|
# angle=45°: (2.12, 2.12)
|
||||||
# angle=198°: (-4.76, -1.55)
|
# angle=90°: (0.00, 3.00)
|
||||||
|
# angle=135°: (-2.12, 2.12)
|
||||||
|
# angle=180°: (-3.00, 0.00)
|
||||||
|
# angle=225°: (-2.12, -2.12)
|
||||||
|
|
||||||
polyline: 0,-5 4.76,-1.55 2.94,4.05 -2.94,4.05 -4.76,-1.55 0,-5
|
polyline: 0,-3 2.12,-2.12 3,0 2.12,2.12 0,3 -2.12,2.12 -3,0 -2.12,-2.12 0,-3
|
||||||
|
|||||||
30
data/shapes/enemy_pinwheel.shp
Normal file
30
data/shapes/enemy_pinwheel.shp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# enemy_pinwheel.shp - ORNI enemic (molinillo de 4 triangles)
|
||||||
|
# © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
name: enemy_pinwheel
|
||||||
|
scale: 1.0
|
||||||
|
center: 0, 0
|
||||||
|
|
||||||
|
# Molinillo: 4 triangles, un en cada quadrant
|
||||||
|
# Cada triangle comparteix el centre (0,0) i té:
|
||||||
|
# - Un vèrtex en un eix (±20, 0) o (0, ±20)
|
||||||
|
# - Un vèrtex en la diagonal del quadrant (±14.14, ±14.14)
|
||||||
|
# - El tercer vèrtex al centre (0,0)
|
||||||
|
#
|
||||||
|
# Geometria:
|
||||||
|
# Triangle 1 (quadrant superior-dret): centre → eix dret → diagonal
|
||||||
|
# Triangle 2 (quadrant superior-esq): centre → eix superior → diagonal
|
||||||
|
# Triangle 3 (quadrant inferior-esq): centre → eix esquerre → diagonal
|
||||||
|
# Triangle 4 (quadrant inferior-dret): centre → eix inferior → diagonal
|
||||||
|
|
||||||
|
# Triangle 1: quadrant superior-dret
|
||||||
|
polyline: 0,0 20,0 14.14,-14.14 0,0
|
||||||
|
|
||||||
|
# Triangle 2: quadrant superior-esquerre
|
||||||
|
polyline: 0,0 0,-20 -14.14,-14.14 0,0
|
||||||
|
|
||||||
|
# Triangle 3: quadrant inferior-esquerre
|
||||||
|
polyline: 0,0 -20,0 -14.14,14.14 0,0
|
||||||
|
|
||||||
|
# Triangle 4: quadrant inferior-dret
|
||||||
|
polyline: 0,0 0,20 14.14,14.14 0,0
|
||||||
19
data/shapes/enemy_square.shp
Normal file
19
data/shapes/enemy_square.shp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# enemy_square.shp - ORNI enemic (quadrat regular)
|
||||||
|
# © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
name: enemy_square
|
||||||
|
scale: 1.0
|
||||||
|
center: 0, 0
|
||||||
|
|
||||||
|
# Quadrat regular radi=20 (circumscrit)
|
||||||
|
# 4 punts equidistants al voltant d'un cercle (90° entre ells)
|
||||||
|
# Començant a angle=-90° (amunt), rotant sentit horari
|
||||||
|
#
|
||||||
|
# Angles: -90°, 0°, 90°, 180°
|
||||||
|
# Conversió polar→cartesià (SDL: Y creix cap avall):
|
||||||
|
# angle=-90°: (0.00, -20.00)
|
||||||
|
# angle=0°: (20.00, 0.00)
|
||||||
|
# angle=90°: (0.00, 20.00)
|
||||||
|
# angle=180°: (-20.00, 0.00)
|
||||||
|
|
||||||
|
polyline: 0,-20 20,0 0,20 -20,0 0,-20
|
||||||
24
data/shapes/ship2.shp
Normal file
24
data/shapes/ship2.shp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# ship2.shp - Nau del jugador (triangle amb base còncava - punta de fletxa)
|
||||||
|
# © 1999 Visente i Sergi (versió Pascal)
|
||||||
|
# © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
name: ship2
|
||||||
|
scale: 1.0
|
||||||
|
center: 0, 0
|
||||||
|
|
||||||
|
# Triangle amb base còncava tipus "punta de fletxa"
|
||||||
|
# Punts originals (polar):
|
||||||
|
# p1: r=12, angle=270° (3π/2) → punta amunt
|
||||||
|
# p2: r=12, angle=45° (π/4) → base dreta-darrere
|
||||||
|
# p3: r=12, angle=135° (3π/4) → base esquerra-darrere
|
||||||
|
#
|
||||||
|
# MODIFICACIÓ: afegit p4 al mig de la base, desplaçat cap al centre
|
||||||
|
# p4: (0, 4) → punt central de la base, cap endins
|
||||||
|
#
|
||||||
|
# Conversió polar→cartesià (angle-90° perquè origen visual és amunt):
|
||||||
|
# p1: (0, -12) → punta
|
||||||
|
# p2: (8.49, 8.49) → base dreta
|
||||||
|
# p4: (0, 4) → base centre (cap endins)
|
||||||
|
# p3: (-8.49, 8.49) → base esquerra
|
||||||
|
|
||||||
|
polyline: 0,-12 8.49,8.49 0,4 -8.49,8.49 0,-12
|
||||||
10
data/shapes/title/letra_a.shp
Normal file
10
data/shapes/title/letra_a.shp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# letra_a.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_a
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,100.00 0.00,75.00 37.50,0.00 100.00,0.00 137.50,75.00 137.50,100.00 100.00,100.00 100.00,87.50 37.50,87.50 37.50,100.00 0.00,100.00
|
||||||
|
polyline: 62.50,25.00 50.00,50.00 50.00,62.50 87.50,62.50 87.50,50.00 75.00,25.00 62.50,25.00
|
||||||
9
data/shapes/title/letra_c.shp
Normal file
9
data/shapes/title/letra_c.shp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# letra_c.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_c
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 12.50,100.00 0.00,87.50 0.00,12.50 12.50,0.00 125.00,0.00 137.50,12.50 137.50,37.50 100.00,37.50 100.00,25.00 37.50,25.00 37.50,75.00 100.00,75.00 100.00,62.50 137.50,62.50 137.50,87.50 125.00,100.00 12.50,100.00
|
||||||
10
data/shapes/title/letra_exclamacion.shp
Normal file
10
data/shapes/title/letra_exclamacion.shp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# letra_exclamacion.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 37.51 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_exclamacion
|
||||||
|
scale: 1.0
|
||||||
|
center: 18.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,62.50 0.00,0.00 37.51,0.00 37.51,62.50 0.00,62.50
|
||||||
|
polyline: 0.00,100.00 0.00,75.00 37.51,75.00 37.51,100.00 0.00,100.00
|
||||||
9
data/shapes/title/letra_i.shp
Normal file
9
data/shapes/title/letra_i.shp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# letra_i.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 37.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_i
|
||||||
|
scale: 1.0
|
||||||
|
center: 18.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,0.00 37.50,0.00 37.50,100.00 0.00,100.00 0.00,0.00
|
||||||
9
data/shapes/title/letra_k.shp
Normal file
9
data/shapes/title/letra_k.shp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# letra_k.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_k
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,100.00 0.00,0.00 37.50,0.00 37.50,37.50 50.00,37.50 100.00,0.00 137.50,0.00 137.50,25.00 87.06,50.00 137.50,75.00 137.50,100.00 100.00,100.00 50.00,62.50 37.50,62.50 37.50,100.00 0.00,100.00
|
||||||
9
data/shapes/title/letra_n.shp
Normal file
9
data/shapes/title/letra_n.shp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# letra_n.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_n
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,100.00 0.00,0.00 50.00,0.00 100.00,50.00 100.00,0.00 137.50,0.00 137.50,100.00 87.50,100.00 37.50,50.00 37.50,100.00 0.00,100.00
|
||||||
10
data/shapes/title/letra_o.shp
Normal file
10
data/shapes/title/letra_o.shp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# letra_o.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_o
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 12.50,100.00 0.00,87.50 0.00,12.50 12.50,0.00 125.00,0.00 137.50,12.50 137.50,87.50 125.00,100.00 12.50,100.00
|
||||||
|
polyline: 100.00,25.00 37.50,25.00 37.50,75.00 100.00,75.00 100.00,25.00
|
||||||
10
data/shapes/title/letra_r.shp
Normal file
10
data/shapes/title/letra_r.shp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# letra_r.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_r
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,100.00 0.00,0.00 125.00,0.00 137.50,12.50 137.50,62.50 125.00,62.50 137.50,75.00 137.50,100.00 100.00,100.00 100.00,75.00 37.50,75.00 37.50,100.00 0.00,100.00
|
||||||
|
polyline: 37.50,50.00 100.00,50.00 100.00,25.00 37.50,25.00 37.50,50.00
|
||||||
9
data/shapes/title/letra_t.shp
Normal file
9
data/shapes/title/letra_t.shp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# letra_t.shp
|
||||||
|
# Generado automáticamente desde jailgames.svg
|
||||||
|
# Dimensiones: 137.50 x 100.00 px
|
||||||
|
|
||||||
|
name: letra_t
|
||||||
|
scale: 1.0
|
||||||
|
center: 68.75, 50.00
|
||||||
|
|
||||||
|
polyline: 0.00,25.00 0.00,0.00 137.50,0.00 137.50,25.00 87.50,25.00 87.50,100.00 50.00,100.00 50.00,25.00 0.00,25.00
|
||||||
BIN
data/sounds/good_job_commander.wav
Normal file
BIN
data/sounds/good_job_commander.wav
Normal file
Binary file not shown.
168
data/stages/stages.yaml
Normal file
168
data/stages/stages.yaml
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
# stages.yaml - Configuració de les 10 etapes d'Orni Attack
|
||||||
|
# © 2025 Orni Attack
|
||||||
|
|
||||||
|
metadata:
|
||||||
|
version: "1.0"
|
||||||
|
total_stages: 10
|
||||||
|
description: "Progressive difficulty curve from novice to expert"
|
||||||
|
|
||||||
|
stages:
|
||||||
|
# STAGE 1: Tutorial - Only pentagons, slow speed
|
||||||
|
- stage_id: 1
|
||||||
|
total_enemies: 5
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 2.0
|
||||||
|
spawn_interval: 3.0
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 100
|
||||||
|
quadrat: 0
|
||||||
|
molinillo: 0
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 0.7
|
||||||
|
rotation_multiplier: 0.8
|
||||||
|
tracking_strength: 0.0
|
||||||
|
|
||||||
|
# STAGE 2: Introduction to tracking enemies
|
||||||
|
- stage_id: 2
|
||||||
|
total_enemies: 7
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 1.5
|
||||||
|
spawn_interval: 2.5
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 70
|
||||||
|
quadrat: 30
|
||||||
|
molinillo: 0
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 0.85
|
||||||
|
rotation_multiplier: 0.9
|
||||||
|
tracking_strength: 0.3
|
||||||
|
|
||||||
|
# STAGE 3: All enemy types, normal speed
|
||||||
|
- stage_id: 3
|
||||||
|
total_enemies: 10
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 1.0
|
||||||
|
spawn_interval: 2.0
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 50
|
||||||
|
quadrat: 30
|
||||||
|
molinillo: 20
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.0
|
||||||
|
rotation_multiplier: 1.0
|
||||||
|
tracking_strength: 0.5
|
||||||
|
|
||||||
|
# STAGE 4: Increased count, faster enemies
|
||||||
|
- stage_id: 4
|
||||||
|
total_enemies: 12
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.8
|
||||||
|
spawn_interval: 1.8
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 40
|
||||||
|
quadrat: 35
|
||||||
|
molinillo: 25
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.1
|
||||||
|
rotation_multiplier: 1.15
|
||||||
|
tracking_strength: 0.6
|
||||||
|
|
||||||
|
# STAGE 5: Maximum count reached
|
||||||
|
- stage_id: 5
|
||||||
|
total_enemies: 15
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.5
|
||||||
|
spawn_interval: 1.5
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 35
|
||||||
|
quadrat: 35
|
||||||
|
molinillo: 30
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.2
|
||||||
|
rotation_multiplier: 1.25
|
||||||
|
tracking_strength: 0.7
|
||||||
|
|
||||||
|
# STAGE 6: Molinillo becomes dominant
|
||||||
|
- stage_id: 6
|
||||||
|
total_enemies: 15
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.3
|
||||||
|
spawn_interval: 1.3
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 30
|
||||||
|
quadrat: 30
|
||||||
|
molinillo: 40
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.3
|
||||||
|
rotation_multiplier: 1.4
|
||||||
|
tracking_strength: 0.8
|
||||||
|
|
||||||
|
# STAGE 7: High intensity, fast spawns
|
||||||
|
- stage_id: 7
|
||||||
|
total_enemies: 15
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.2
|
||||||
|
spawn_interval: 1.0
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 25
|
||||||
|
quadrat: 30
|
||||||
|
molinillo: 45
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.4
|
||||||
|
rotation_multiplier: 1.5
|
||||||
|
tracking_strength: 0.9
|
||||||
|
|
||||||
|
# STAGE 8: Expert level, 50% molinillos
|
||||||
|
- stage_id: 8
|
||||||
|
total_enemies: 15
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.1
|
||||||
|
spawn_interval: 0.8
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 20
|
||||||
|
quadrat: 30
|
||||||
|
molinillo: 50
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.5
|
||||||
|
rotation_multiplier: 1.6
|
||||||
|
tracking_strength: 1.0
|
||||||
|
|
||||||
|
# STAGE 9: Near-maximum difficulty
|
||||||
|
- stage_id: 9
|
||||||
|
total_enemies: 15
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.0
|
||||||
|
spawn_interval: 0.6
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 15
|
||||||
|
quadrat: 25
|
||||||
|
molinillo: 60
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.6
|
||||||
|
rotation_multiplier: 1.7
|
||||||
|
tracking_strength: 1.1
|
||||||
|
|
||||||
|
# STAGE 10: Final challenge, 70% molinillos
|
||||||
|
- stage_id: 10
|
||||||
|
total_enemies: 15
|
||||||
|
spawn_config:
|
||||||
|
mode: "progressive"
|
||||||
|
initial_delay: 0.0
|
||||||
|
spawn_interval: 0.5
|
||||||
|
enemy_distribution:
|
||||||
|
pentagon: 10
|
||||||
|
quadrat: 20
|
||||||
|
molinillo: 70
|
||||||
|
difficulty_multipliers:
|
||||||
|
speed_multiplier: 1.8
|
||||||
|
rotation_multiplier: 2.0
|
||||||
|
tracking_strength: 1.2
|
||||||
BIN
data/voices/good_job_commander.wav
Normal file
BIN
data/voices/good_job_commander.wav
Normal file
Binary file not shown.
@@ -1,15 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
||||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
|
||||||
<svg width="100%" height="100%" viewBox="0 0 7875 4016" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
|
|
||||||
<g transform="matrix(0.792849,0,0,0.792849,84.327,350.707)">
|
|
||||||
<path d="M896,1693L896,1531.23L1219.53,1531.23L1219.53,560.632L1543.07,560.632L1543.07,1531.23L1381.3,1531.23L1381.3,1693L896,1693Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M2028.37,1369.47L2028.37,1693L1704.83,1693L1704.83,722.399L1866.6,722.399L1866.6,560.632L2351.9,560.632L2351.9,722.399L2513.67,722.399L2513.67,1693L2190.14,1693L2190.14,1369.47L2028.37,1369.47ZM2028.37,722.399L2028.37,1207.7L2190.14,1207.7L2190.14,722.399L2028.37,722.399Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<rect x="2675.44" y="560.632" width="323.534" height="1132.37" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M3160.74,560.632L3484.27,560.632L3484.27,1531.23L3807.8,1531.23L3807.8,1693L3160.74,1693L3160.74,560.632Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M4131.34,560.632L4616.64,560.632L4616.64,722.399L4293.1,722.399L4293.1,1531.23L4454.87,1531.23L4454.87,1045.93L4778.4,1045.93L4778.4,1693L4131.34,1693L4131.34,1531.23L3969.57,1531.23L3969.57,722.399L4131.34,722.399L4131.34,560.632Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M5263.71,1369.47L5263.71,1693L4940.17,1693L4940.17,722.399L5101.94,722.399L5101.94,560.632L5587.24,560.632L5587.24,722.399L5749.01,722.399L5749.01,1693L5425.47,1693L5425.47,1369.47L5263.71,1369.47ZM5263.71,722.399L5263.71,1207.7L5425.47,1207.7L5425.47,722.399L5263.71,722.399Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M6719.61,1207.7L6557.84,1207.7L6557.84,1369.47L6396.07,1369.47L6396.07,1207.7L6234.31,1207.7L6234.31,1693L5910.77,1693L5910.77,560.632L6072.54,560.632L6072.54,722.399L6234.31,722.399L6234.31,884.166L6396.07,884.166L6396.07,1045.93L6557.84,1045.93L6557.84,884.166L6719.61,884.166L6719.61,722.399L6881.37,722.399L6881.37,560.632L7043.14,560.632L7043.14,1693L6719.61,1693L6719.61,1207.7Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M7851.98,884.166L7851.98,1045.93L7528.44,1045.93L7528.44,1531.23L8013.74,1531.23L8013.74,1693L7204.91,1693L7204.91,560.632L8013.74,560.632L8013.74,722.399L7528.44,722.399L7528.44,884.166L7851.98,884.166Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
<path d="M8175.51,1531.23L8499.04,1531.23L8499.04,1207.7L8337.28,1207.7L8337.28,1045.93L8175.51,1045.93L8175.51,722.399L8337.28,722.399L8337.28,560.632L8822.58,560.632L8822.58,722.399L8499.04,722.399L8499.04,1045.93L8660.81,1045.93L8660.81,1207.7L8822.58,1207.7L8822.58,1531.23L8660.81,1531.23L8660.81,1693L8175.51,1693L8175.51,1531.23Z" style="fill:none;fill-rule:nonzero;stroke:black;stroke-width:7.01px;"/>
|
|
||||||
</g>
|
|
||||||
</svg>
|
|
||||||
|
Before Width: | Height: | Size: 3.2 KiB |
Binary file not shown.
BIN
release/icon.ico
BIN
release/icon.ico
Binary file not shown.
|
Before Width: | Height: | Size: 122 KiB After Width: | Height: | Size: 111 KiB |
BIN
release/icon.png
BIN
release/icon.png
Binary file not shown.
|
Before Width: | Height: | Size: 402 KiB After Width: | Height: | Size: 174 KiB |
BIN
sample.png
BIN
sample.png
Binary file not shown.
|
Before Width: | Height: | Size: 36 KiB |
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "core/audio/audio_cache.hpp"
|
#include "core/audio/audio_cache.hpp"
|
||||||
|
|
||||||
|
#include "core/resources/resource_helper.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
// Inicialització de variables estàtiques
|
// Inicialització de variables estàtiques
|
||||||
@@ -19,17 +21,28 @@ JA_Sound_t* AudioCache::getSound(const std::string& name) {
|
|||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache miss - cargar archivo
|
// Normalize path: "laser_shoot.wav" → "sounds/laser_shoot.wav"
|
||||||
std::string fullpath = resolveSoundPath(name);
|
std::string normalized = name;
|
||||||
JA_Sound_t* sound = JA_LoadSound(fullpath.c_str());
|
if (normalized.find("sounds/") != 0 && normalized.find('/') == std::string::npos) {
|
||||||
|
normalized = "sounds/" + normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load from resource system
|
||||||
|
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||||
|
if (data.empty()) {
|
||||||
|
std::cerr << "[AudioCache] Error: no s'ha pogut carregar " << normalized << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load sound from memory
|
||||||
|
JA_Sound_t* sound = JA_LoadSound(data.data(), static_cast<uint32_t>(data.size()));
|
||||||
if (sound == nullptr) {
|
if (sound == nullptr) {
|
||||||
std::cerr << "[AudioCache] Error: no s'ha pogut carregar " << fullpath
|
std::cerr << "[AudioCache] Error: no s'ha pogut decodificar " << normalized
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "[AudioCache] Sound loaded: " << name << std::endl;
|
std::cout << "[AudioCache] Sound loaded: " << normalized << std::endl;
|
||||||
sounds_[name] = sound;
|
sounds_[name] = sound;
|
||||||
return sound;
|
return sound;
|
||||||
}
|
}
|
||||||
@@ -42,17 +55,28 @@ JA_Music_t* AudioCache::getMusic(const std::string& name) {
|
|||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache miss - cargar archivo
|
// Normalize path: "title.ogg" → "music/title.ogg"
|
||||||
std::string fullpath = resolveMusicPath(name);
|
std::string normalized = name;
|
||||||
JA_Music_t* music = JA_LoadMusic(fullpath.c_str());
|
if (normalized.find("music/") != 0 && normalized.find('/') == std::string::npos) {
|
||||||
|
normalized = "music/" + normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load from resource system
|
||||||
|
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||||
|
if (data.empty()) {
|
||||||
|
std::cerr << "[AudioCache] Error: no s'ha pogut carregar " << normalized << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load music from memory
|
||||||
|
JA_Music_t* music = JA_LoadMusic(data.data(), static_cast<uint32_t>(data.size()));
|
||||||
if (music == nullptr) {
|
if (music == nullptr) {
|
||||||
std::cerr << "[AudioCache] Error: no s'ha pogut carregar " << fullpath
|
std::cerr << "[AudioCache] Error: no s'ha pogut decodificar " << normalized
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "[AudioCache] Music loaded: " << name << std::endl;
|
std::cout << "[AudioCache] Music loaded: " << normalized << std::endl;
|
||||||
musics_[name] = music;
|
musics_[name] = music;
|
||||||
return music;
|
return music;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,9 +70,24 @@ constexpr int MAX_IPUNTS = 30;
|
|||||||
|
|
||||||
constexpr float SHIP_RADIUS = 12.0f;
|
constexpr float SHIP_RADIUS = 12.0f;
|
||||||
constexpr float ENEMY_RADIUS = 20.0f;
|
constexpr float ENEMY_RADIUS = 20.0f;
|
||||||
constexpr float BULLET_RADIUS = 5.0f;
|
constexpr float BULLET_RADIUS = 3.0f;
|
||||||
} // namespace Entities
|
} // namespace Entities
|
||||||
|
|
||||||
|
// Game rules (lives, respawn, game over)
|
||||||
|
namespace Game {
|
||||||
|
constexpr int STARTING_LIVES = 3; // Initial lives
|
||||||
|
constexpr float DEATH_DURATION = 3.0f; // Seconds of death animation
|
||||||
|
constexpr float GAME_OVER_DURATION = 5.0f; // Seconds to display game over
|
||||||
|
constexpr float COLLISION_SHIP_ENEMY_AMPLIFIER = 0.80f; // 80% hitbox (generous)
|
||||||
|
// Transición LEVEL_START (mensajes aleatorios PRE-level)
|
||||||
|
constexpr float LEVEL_START_DURATION = 3.0f; // Duración total
|
||||||
|
constexpr float LEVEL_START_TYPING_RATIO = 0.3f; // 30% escribiendo, 70% mostrando
|
||||||
|
|
||||||
|
// Transición LEVEL_COMPLETED (mensaje "GOOD JOB COMMANDER!")
|
||||||
|
constexpr float LEVEL_COMPLETED_DURATION = 3.0f; // Duración total
|
||||||
|
constexpr float LEVEL_COMPLETED_TYPING_RATIO = 0.0f; // 0.0 = sin typewriter (directo)
|
||||||
|
} // namespace Game
|
||||||
|
|
||||||
// Física (valores actuales del juego, sincronizados con joc_asteroides.cpp)
|
// Física (valores actuales del juego, sincronizados con joc_asteroides.cpp)
|
||||||
namespace Physics {
|
namespace Physics {
|
||||||
constexpr float ROTATION_SPEED = 3.14f; // rad/s (~180°/s)
|
constexpr float ROTATION_SPEED = 3.14f; // rad/s (~180°/s)
|
||||||
@@ -90,8 +105,19 @@ constexpr float VARIACIO_VELOCITAT = 40.0f; // ±variació aleatòria (px/s)
|
|||||||
constexpr float ACCELERACIO = -60.0f; // Fricció/desacceleració (px/s²)
|
constexpr float ACCELERACIO = -60.0f; // Fricció/desacceleració (px/s²)
|
||||||
constexpr float ROTACIO_MIN = 0.1f; // Rotació mínima (rad/s ~5.7°/s)
|
constexpr float ROTACIO_MIN = 0.1f; // Rotació mínima (rad/s ~5.7°/s)
|
||||||
constexpr float ROTACIO_MAX = 0.3f; // Rotació màxima (rad/s ~17.2°/s)
|
constexpr float ROTACIO_MAX = 0.3f; // Rotació màxima (rad/s ~17.2°/s)
|
||||||
constexpr float TEMPS_VIDA = 2.0f; // Duració màxima (segons)
|
constexpr float TEMPS_VIDA = 2.0f; // Duració màxima (segons) - enemy/bullet debris
|
||||||
|
constexpr float TEMPS_VIDA_NAU = 3.0f; // Ship debris lifetime (matches DEATH_DURATION)
|
||||||
constexpr float SHRINK_RATE = 0.5f; // Reducció de mida (factor/s)
|
constexpr float SHRINK_RATE = 0.5f; // Reducció de mida (factor/s)
|
||||||
|
|
||||||
|
// Herència de velocitat angular (trayectorias curvas)
|
||||||
|
constexpr float FACTOR_HERENCIA_MIN = 0.7f; // Mínimo 70% del drotacio heredat
|
||||||
|
constexpr float FACTOR_HERENCIA_MAX = 1.0f; // Màxim 100% del drotacio heredat
|
||||||
|
constexpr float FRICCIO_ANGULAR = 0.5f; // Desacceleració angular (rad/s²)
|
||||||
|
|
||||||
|
// Angular velocity cap for trajectory inheritance
|
||||||
|
// Excess above this threshold is converted to tangential linear velocity
|
||||||
|
// Prevents "vortex trap" problem with high-rotation enemies
|
||||||
|
constexpr float VELOCITAT_ROT_MAX = 1.5f; // rad/s (~86°/s)
|
||||||
} // namespace Debris
|
} // namespace Debris
|
||||||
} // namespace Physics
|
} // namespace Physics
|
||||||
|
|
||||||
@@ -129,7 +155,7 @@ namespace Brightness {
|
|||||||
// Brillantor estàtica per entitats de joc (0.0-1.0)
|
// Brillantor estàtica per entitats de joc (0.0-1.0)
|
||||||
constexpr float NAU = 1.0f; // Màxima visibilitat (jugador)
|
constexpr float NAU = 1.0f; // Màxima visibilitat (jugador)
|
||||||
constexpr float ENEMIC = 0.7f; // 30% més tènue (destaca menys)
|
constexpr float ENEMIC = 0.7f; // 30% més tènue (destaca menys)
|
||||||
constexpr float BALA = 0.9f; // Destacada però menys que nau
|
constexpr float BALA = 1.0f; // Brillo a tope (màxima visibilitat)
|
||||||
|
|
||||||
// Starfield: gradient segons distància al centre
|
// Starfield: gradient segons distància al centre
|
||||||
// distancia_centre: 0.0 (centre) → 1.0 (vora pantalla)
|
// distancia_centre: 0.0 (centre) → 1.0 (vora pantalla)
|
||||||
@@ -159,9 +185,98 @@ constexpr int FADE_DURATION_MS = 1000; // Fade out duration
|
|||||||
|
|
||||||
// Efectes de so (sons puntuals)
|
// Efectes de so (sons puntuals)
|
||||||
namespace Sound {
|
namespace Sound {
|
||||||
constexpr float VOLUME = 1.0F; // Volumen efectos
|
constexpr float VOLUME = 1.0F; // Volumen efectos
|
||||||
constexpr bool ENABLED = true; // Sonidos habilitados
|
constexpr bool ENABLED = true; // Sonidos habilitados
|
||||||
constexpr const char* EXPLOSION = "explosion.wav"; // Explosión
|
constexpr const char* EXPLOSION = "explosion.wav"; // Explosión
|
||||||
constexpr const char* LASER = "laser_shoot.wav"; // Disparo
|
constexpr const char* LASER = "laser_shoot.wav"; // Disparo
|
||||||
|
constexpr const char* GOOD_JOB_COMMANDER = "good_job_commander.wav"; // Voz: "Good job, commander"
|
||||||
} // namespace Sound
|
} // namespace Sound
|
||||||
|
|
||||||
|
// Enemy type configuration (tipus d'enemics)
|
||||||
|
namespace Enemies {
|
||||||
|
// Pentagon (esquivador - zigzag evasion)
|
||||||
|
namespace Pentagon {
|
||||||
|
constexpr float VELOCITAT = 35.0f; // px/s (slightly slower)
|
||||||
|
constexpr float CANVI_ANGLE_PROB = 0.20f; // 20% per wall hit (frequent zigzag)
|
||||||
|
constexpr float CANVI_ANGLE_MAX = 1.0f; // Max random angle change (rad)
|
||||||
|
constexpr float DROTACIO_MIN = 0.75f; // Min visual rotation (rad/s) [+50%]
|
||||||
|
constexpr float DROTACIO_MAX = 3.75f; // Max visual rotation (rad/s) [+50%]
|
||||||
|
constexpr const char* SHAPE_FILE = "enemy_pentagon.shp";
|
||||||
|
} // namespace Pentagon
|
||||||
|
|
||||||
|
// Quadrat (perseguidor - tracks player)
|
||||||
|
namespace Quadrat {
|
||||||
|
constexpr float VELOCITAT = 40.0f; // px/s (medium speed)
|
||||||
|
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 DROTACIO_MIN = 0.3f; // Slow rotation [+50%]
|
||||||
|
constexpr float DROTACIO_MAX = 1.5f; // [+50%]
|
||||||
|
constexpr const char* SHAPE_FILE = "enemy_square.shp";
|
||||||
|
} // namespace Quadrat
|
||||||
|
|
||||||
|
// Molinillo (agressiu - fast straight lines, proximity spin-up)
|
||||||
|
namespace Molinillo {
|
||||||
|
constexpr float VELOCITAT = 50.0f; // px/s (fastest)
|
||||||
|
constexpr float CANVI_ANGLE_PROB = 0.05f; // 5% per wall hit (rare direction change)
|
||||||
|
constexpr float CANVI_ANGLE_MAX = 0.3f; // Small angle adjustments
|
||||||
|
constexpr float DROTACIO_MIN = 3.0f; // Base rotation (rad/s) [+50%]
|
||||||
|
constexpr float DROTACIO_MAX = 6.0f; // [+50%]
|
||||||
|
constexpr float DROTACIO_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 Molinillo
|
||||||
|
|
||||||
|
// Animation parameters (shared)
|
||||||
|
namespace Animation {
|
||||||
|
// Palpitation
|
||||||
|
constexpr float PALPITACIO_TRIGGER_PROB = 0.01f; // 1% chance per second
|
||||||
|
constexpr float PALPITACIO_DURACIO_MIN = 1.0f; // Min duration (seconds)
|
||||||
|
constexpr float PALPITACIO_DURACIO_MAX = 3.0f; // Max duration (seconds)
|
||||||
|
constexpr float PALPITACIO_AMPLITUD_MIN = 0.08f; // Min scale variation
|
||||||
|
constexpr float PALPITACIO_AMPLITUD_MAX = 0.20f; // Max scale variation
|
||||||
|
constexpr float PALPITACIO_FREQ_MIN = 1.5f; // Min frequency (Hz)
|
||||||
|
constexpr float PALPITACIO_FREQ_MAX = 3.0f; // Max frequency (Hz)
|
||||||
|
|
||||||
|
// Rotation acceleration
|
||||||
|
constexpr float ROTACIO_ACCEL_TRIGGER_PROB = 0.02f; // 2% chance per second [4x more frequent]
|
||||||
|
constexpr float ROTACIO_ACCEL_DURACIO_MIN = 3.0f; // Min transition time
|
||||||
|
constexpr float ROTACIO_ACCEL_DURACIO_MAX = 8.0f; // Max transition time
|
||||||
|
constexpr float ROTACIO_ACCEL_MULTIPLIER_MIN = 0.3f; // Min speed multiplier [more dramatic]
|
||||||
|
constexpr float ROTACIO_ACCEL_MULTIPLIER_MAX = 4.0f; // Max speed multiplier [more dramatic]
|
||||||
|
} // namespace Animation
|
||||||
|
|
||||||
|
// 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ó per tipus d'enemic)
|
||||||
|
namespace Scoring {
|
||||||
|
constexpr int PENTAGON_SCORE = 100; // Pentàgon (esquivador, 35 px/s)
|
||||||
|
constexpr int QUADRAT_SCORE = 150; // Quadrat (perseguidor, 40 px/s)
|
||||||
|
constexpr int MOLINILLO_SCORE = 200; // Molinillo (agressiu, 50 px/s)
|
||||||
|
} // namespace Scoring
|
||||||
|
|
||||||
|
} // namespace Enemies
|
||||||
|
|
||||||
|
// Floating score numbers (números flotants de puntuació)
|
||||||
|
namespace FloatingScore {
|
||||||
|
constexpr float LIFETIME = 2.0f; // Duració màxima (segons)
|
||||||
|
constexpr float VELOCITY_Y = -30.0f; // Velocitat vertical (px/s, negatiu = amunt)
|
||||||
|
constexpr float VELOCITY_X = 0.0f; // Velocitat horizontal (px/s)
|
||||||
|
constexpr float SCALE = 0.75f; // Escala del text (0.75 = 75% del marcador)
|
||||||
|
constexpr float SPACING = 0.0f; // Espaiat entre caràcters
|
||||||
|
constexpr int MAX_CONCURRENT = 15; // Pool size (= MAX_ORNIS)
|
||||||
|
} // namespace FloatingScore
|
||||||
|
|
||||||
} // namespace Defaults
|
} // namespace Defaults
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ class Shape {
|
|||||||
// Carregar forma des de fitxer .shp
|
// Carregar forma des de fitxer .shp
|
||||||
bool carregar(const std::string& filepath);
|
bool carregar(const std::string& filepath);
|
||||||
|
|
||||||
|
// Parsejar forma des de buffer de memòria (per al sistema de recursos)
|
||||||
|
bool parsejar_fitxer(const std::string& contingut);
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
const std::vector<ShapePrimitive>& get_primitives() const {
|
const std::vector<ShapePrimitive>& get_primitives() const {
|
||||||
return primitives_;
|
return primitives_;
|
||||||
@@ -50,9 +53,6 @@ class Shape {
|
|||||||
float escala_defecte_; // Escala per defecte (normalment 1.0)
|
float escala_defecte_; // Escala per defecte (normalment 1.0)
|
||||||
std::string nom_; // Nom de la forma (per depuració)
|
std::string nom_; // Nom de la forma (per depuració)
|
||||||
|
|
||||||
// Parsejador del fitxer
|
|
||||||
bool parsejar_fitxer(const std::string& contingut);
|
|
||||||
|
|
||||||
// Helpers privats per parsejar
|
// Helpers privats per parsejar
|
||||||
std::string trim(const std::string& str) const;
|
std::string trim(const std::string& str) const;
|
||||||
bool starts_with(const std::string& str, const std::string& prefix) const;
|
bool starts_with(const std::string& str, const std::string& prefix) const;
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "core/graphics/shape_loader.hpp"
|
#include "core/graphics/shape_loader.hpp"
|
||||||
|
|
||||||
|
#include "core/resources/resource_helper.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace Graphics {
|
namespace Graphics {
|
||||||
@@ -19,28 +21,40 @@ std::shared_ptr<Shape> ShapeLoader::load(const std::string& filename) {
|
|||||||
return it->second; // Cache hit
|
return it->second; // Cache hit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve full path
|
// Normalize path: "ship.shp" → "shapes/ship.shp"
|
||||||
std::string fullpath = resolve_path(filename);
|
// "logo/letra_j.shp" → "shapes/logo/letra_j.shp"
|
||||||
|
std::string normalized = filename;
|
||||||
|
if (normalized.find("shapes/") != 0) {
|
||||||
|
// Doesn't start with "shapes/", so add it
|
||||||
|
normalized = "shapes/" + normalized;
|
||||||
|
}
|
||||||
|
|
||||||
// Create and load shape
|
// Load from resource system
|
||||||
|
std::vector<uint8_t> data = Resource::Helper::loadFile(normalized);
|
||||||
|
if (data.empty()) {
|
||||||
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut carregar " << normalized
|
||||||
|
<< std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert bytes to string and parse
|
||||||
|
std::string file_content(data.begin(), data.end());
|
||||||
auto shape = std::make_shared<Shape>();
|
auto shape = std::make_shared<Shape>();
|
||||||
if (!shape->carregar(fullpath)) {
|
if (!shape->parsejar_fitxer(file_content)) {
|
||||||
std::cerr << "[ShapeLoader] Error: no s'ha pogut carregar " << filename
|
std::cerr << "[ShapeLoader] Error: no s'ha pogut parsejar " << normalized
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify shape is valid
|
// Verify shape is valid
|
||||||
if (!shape->es_valida()) {
|
if (!shape->es_valida()) {
|
||||||
std::cerr << "[ShapeLoader] Error: forma invàlida " << filename
|
std::cerr << "[ShapeLoader] Error: forma invàlida " << normalized << std::endl;
|
||||||
<< std::endl;
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache and return
|
// Cache and return
|
||||||
std::cout << "[ShapeLoader] Carregat: " << filename << " ("
|
std::cout << "[ShapeLoader] Carregat: " << normalized << " (" << shape->get_nom()
|
||||||
<< shape->get_nom() << ", " << shape->get_num_primitives()
|
<< ", " << shape->get_num_primitives() << " primitives)" << std::endl;
|
||||||
<< " primitives)" << std::endl;
|
|
||||||
|
|
||||||
cache_[filename] = shape;
|
cache_[filename] = shape;
|
||||||
return shape;
|
return shape;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "core/defaults.hpp"
|
#include "core/defaults.hpp"
|
||||||
|
#include "core/graphics/shape_loader.hpp"
|
||||||
#include "core/rendering/shape_renderer.hpp"
|
#include "core/rendering/shape_renderer.hpp"
|
||||||
|
|
||||||
namespace Graphics {
|
namespace Graphics {
|
||||||
@@ -17,15 +18,15 @@ Starfield::Starfield(SDL_Renderer* renderer,
|
|||||||
const Punt& punt_fuga,
|
const Punt& punt_fuga,
|
||||||
const SDL_FRect& area,
|
const SDL_FRect& area,
|
||||||
int densitat)
|
int densitat)
|
||||||
: renderer_(renderer)
|
: renderer_(renderer),
|
||||||
, punt_fuga_(punt_fuga)
|
punt_fuga_(punt_fuga),
|
||||||
, area_(area)
|
area_(area),
|
||||||
, densitat_(densitat) {
|
densitat_(densitat) {
|
||||||
// Carregar forma d'estrella
|
// Carregar forma d'estrella amb ShapeLoader
|
||||||
shape_estrella_ = std::make_shared<Shape>("data/shapes/star.shp");
|
shape_estrella_ = ShapeLoader::load("star.shp");
|
||||||
|
|
||||||
if (!shape_estrella_->es_valida()) {
|
if (!shape_estrella_ || !shape_estrella_->es_valida()) {
|
||||||
std::cerr << "ERROR: No s'ha pogut carregar data/shapes/star.shp" << std::endl;
|
std::cerr << "ERROR: No s'ha pogut carregar star.shp" << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,9 +85,9 @@ void Starfield::inicialitzar_estrella(Estrella& estrella) {
|
|||||||
// Verificar si una estrella està fora de l'àrea
|
// Verificar si una estrella està fora de l'àrea
|
||||||
bool Starfield::fora_area(const Estrella& estrella) const {
|
bool Starfield::fora_area(const Estrella& estrella) const {
|
||||||
return (estrella.posicio.x < area_.x ||
|
return (estrella.posicio.x < area_.x ||
|
||||||
estrella.posicio.x > area_.x + area_.w ||
|
estrella.posicio.x > area_.x + area_.w ||
|
||||||
estrella.posicio.y < area_.y ||
|
estrella.posicio.y < area_.y ||
|
||||||
estrella.posicio.y > area_.y + area_.h);
|
estrella.posicio.y > area_.y + area_.h);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcular escala dinàmica segons distància del centre
|
// Calcular escala dinàmica segons distància del centre
|
||||||
@@ -96,16 +97,19 @@ float Starfield::calcular_escala(const Estrella& estrella) const {
|
|||||||
// Interpolació lineal basada en distància del centre
|
// Interpolació lineal basada en distància del centre
|
||||||
// distancia_centre: 0.0 (centre) → 1.0 (vora)
|
// distancia_centre: 0.0 (centre) → 1.0 (vora)
|
||||||
return capa.escala_min +
|
return capa.escala_min +
|
||||||
(capa.escala_max - capa.escala_min) * estrella.distancia_centre;
|
(capa.escala_max - capa.escala_min) * estrella.distancia_centre;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calcular brightness dinàmica segons distància del centre
|
// Calcular brightness dinàmica segons distància del centre
|
||||||
float Starfield::calcular_brightness(const Estrella& estrella) const {
|
float Starfield::calcular_brightness(const Estrella& estrella) const {
|
||||||
// Interpolació lineal: estrelles properes (vora) més brillants
|
// Interpolació lineal: estrelles properes (vora) més brillants
|
||||||
// distancia_centre: 0.0 (centre, llunyanes) → 1.0 (vora, properes)
|
// distancia_centre: 0.0 (centre, llunyanes) → 1.0 (vora, properes)
|
||||||
return Defaults::Brightness::STARFIELD_MIN +
|
float brightness_base = Defaults::Brightness::STARFIELD_MIN +
|
||||||
(Defaults::Brightness::STARFIELD_MAX - Defaults::Brightness::STARFIELD_MIN) *
|
(Defaults::Brightness::STARFIELD_MAX - Defaults::Brightness::STARFIELD_MIN) *
|
||||||
estrella.distancia_centre;
|
estrella.distancia_centre;
|
||||||
|
|
||||||
|
// Aplicar multiplicador i limitar a 1.0
|
||||||
|
return std::min(1.0f, brightness_base * multiplicador_brightness_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualitzar posicions de les estrelles
|
// Actualitzar posicions de les estrelles
|
||||||
@@ -135,6 +139,11 @@ void Starfield::actualitzar(float delta_time) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Establir multiplicador de brightness
|
||||||
|
void Starfield::set_brightness(float multiplier) {
|
||||||
|
multiplicador_brightness_ = std::max(0.0f, multiplier); // Evitar valors negatius
|
||||||
|
}
|
||||||
|
|
||||||
// Dibuixar totes les estrelles
|
// Dibuixar totes les estrelles
|
||||||
void Starfield::dibuixar() {
|
void Starfield::dibuixar() {
|
||||||
if (!shape_estrella_->es_valida()) {
|
if (!shape_estrella_->es_valida()) {
|
||||||
|
|||||||
@@ -15,66 +15,68 @@ namespace Graphics {
|
|||||||
|
|
||||||
// Configuració per cada capa de profunditat
|
// Configuració per cada capa de profunditat
|
||||||
struct CapaConfig {
|
struct CapaConfig {
|
||||||
float velocitat_base; // Velocitat base d'aquesta capa (px/s)
|
float velocitat_base; // Velocitat base d'aquesta capa (px/s)
|
||||||
float escala_min; // Escala mínima prop del centre
|
float escala_min; // Escala mínima prop del centre
|
||||||
float escala_max; // Escala màxima al límit de pantalla
|
float escala_max; // Escala màxima al límit de pantalla
|
||||||
int num_estrelles; // Nombre d'estrelles en aquesta capa
|
int num_estrelles; // Nombre d'estrelles en aquesta capa
|
||||||
};
|
};
|
||||||
|
|
||||||
// Classe Starfield - camp d'estrelles animat amb efecte de profunditat
|
// Classe Starfield - camp d'estrelles animat amb efecte de profunditat
|
||||||
class Starfield {
|
class Starfield {
|
||||||
public:
|
public:
|
||||||
// Constructor
|
// Constructor
|
||||||
// - renderer: SDL renderer
|
// - renderer: SDL renderer
|
||||||
// - punt_fuga: punt d'origen/fuga des d'on surten les estrelles
|
// - punt_fuga: punt d'origen/fuga des d'on surten les estrelles
|
||||||
// - area: rectangle on actuen les estrelles (SDL_FRect)
|
// - area: rectangle on actuen les estrelles (SDL_FRect)
|
||||||
// - densitat: nombre total d'estrelles (es divideix entre capes)
|
// - densitat: nombre total d'estrelles (es divideix entre capes)
|
||||||
Starfield(SDL_Renderer* renderer,
|
Starfield(SDL_Renderer* renderer,
|
||||||
const Punt& punt_fuga,
|
const Punt& punt_fuga,
|
||||||
const SDL_FRect& area,
|
const SDL_FRect& area,
|
||||||
int densitat = 150);
|
int densitat = 150);
|
||||||
|
|
||||||
// Actualitzar posicions de les estrelles
|
// Actualitzar posicions de les estrelles
|
||||||
void actualitzar(float delta_time);
|
void actualitzar(float delta_time);
|
||||||
|
|
||||||
// Dibuixar totes les estrelles
|
// Dibuixar totes les estrelles
|
||||||
void dibuixar();
|
void dibuixar();
|
||||||
|
|
||||||
// Setters per ajustar paràmetres en temps real
|
// Setters per ajustar paràmetres en temps real
|
||||||
void set_punt_fuga(const Punt& punt) { punt_fuga_ = punt; }
|
void set_punt_fuga(const Punt& punt) { punt_fuga_ = punt; }
|
||||||
|
void set_brightness(float multiplier);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Estructura interna per cada estrella
|
// Estructura interna per cada estrella
|
||||||
struct Estrella {
|
struct Estrella {
|
||||||
Punt posicio; // Posició actual
|
Punt posicio; // Posició actual
|
||||||
float angle; // Angle de moviment (radians)
|
float angle; // Angle de moviment (radians)
|
||||||
float distancia_centre; // Distància normalitzada del centre (0.0-1.0)
|
float distancia_centre; // Distància normalitzada del centre (0.0-1.0)
|
||||||
int capa; // Índex de capa (0=lluny, 1=mitjà, 2=prop)
|
int capa; // Índex de capa (0=lluny, 1=mitjà, 2=prop)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Inicialitzar una estrella (nova o regenerada)
|
// Inicialitzar una estrella (nova o regenerada)
|
||||||
void inicialitzar_estrella(Estrella& estrella);
|
void inicialitzar_estrella(Estrella& estrella);
|
||||||
|
|
||||||
// Verificar si una estrella està fora de l'àrea
|
// Verificar si una estrella està fora de l'àrea
|
||||||
bool fora_area(const Estrella& estrella) const;
|
bool fora_area(const Estrella& estrella) const;
|
||||||
|
|
||||||
// Calcular escala dinàmica segons distància del centre
|
// Calcular escala dinàmica segons distància del centre
|
||||||
float calcular_escala(const Estrella& estrella) const;
|
float calcular_escala(const Estrella& estrella) const;
|
||||||
|
|
||||||
// Calcular brightness dinàmica segons distància del centre
|
// Calcular brightness dinàmica segons distància del centre
|
||||||
float calcular_brightness(const Estrella& estrella) const;
|
float calcular_brightness(const Estrella& estrella) const;
|
||||||
|
|
||||||
// Dades
|
// Dades
|
||||||
std::vector<Estrella> estrelles_;
|
std::vector<Estrella> estrelles_;
|
||||||
std::vector<CapaConfig> capes_; // Configuració de les 3 capes
|
std::vector<CapaConfig> capes_; // Configuració de les 3 capes
|
||||||
std::shared_ptr<Shape> shape_estrella_;
|
std::shared_ptr<Shape> shape_estrella_;
|
||||||
SDL_Renderer* renderer_;
|
SDL_Renderer* renderer_;
|
||||||
|
|
||||||
// Configuració
|
// Configuració
|
||||||
Punt punt_fuga_; // Punt d'origen de les estrelles
|
Punt punt_fuga_; // Punt d'origen de les estrelles
|
||||||
SDL_FRect area_; // Àrea activa
|
SDL_FRect area_; // Àrea activa
|
||||||
float radi_max_; // Distància màxima del centre al límit de pantalla
|
float radi_max_; // Distància màxima del centre al límit de pantalla
|
||||||
int densitat_; // Nombre total d'estrelles
|
int densitat_; // Nombre total d'estrelles
|
||||||
|
float multiplicador_brightness_{1.0f}; // Multiplicador de brillantor (1.0 = default)
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Graphics
|
} // namespace Graphics
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ bool VectorText::is_supported(char c) const {
|
|||||||
return chars_.find(c) != chars_.end();
|
return chars_.find(c) != chars_.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VectorText::render(const std::string& text, const Punt& posicio, float escala, float spacing) {
|
void VectorText::render(const std::string& text, const Punt& posicio, float escala, float spacing, float brightness) {
|
||||||
if (!renderer_) {
|
if (!renderer_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -223,7 +223,7 @@ void VectorText::render(const std::string& text, const Punt& posicio, float esca
|
|||||||
// Ajustar Y para que posicio represente esquina superior izquierda
|
// Ajustar Y para que posicio represente esquina superior izquierda
|
||||||
// (render_shape espera el centro, así que sumamos la mitad de la altura)
|
// (render_shape espera el centro, así que sumamos la mitad de la altura)
|
||||||
Punt char_pos = {current_x, posicio.y + char_height_scaled / 2.0f};
|
Punt char_pos = {current_x, posicio.y + char_height_scaled / 2.0f};
|
||||||
Rendering::render_shape(renderer_, it->second, char_pos, 0.0f, escala, true);
|
Rendering::render_shape(renderer_, it->second, char_pos, 0.0f, escala, true, 1.0f, brightness);
|
||||||
|
|
||||||
// Avanzar posición
|
// Avanzar posición
|
||||||
current_x += char_width_scaled + spacing_scaled;
|
current_x += char_width_scaled + spacing_scaled;
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ class VectorText {
|
|||||||
// - posicio: posición inicial (esquina superior izquierda)
|
// - posicio: posición inicial (esquina superior izquierda)
|
||||||
// - escala: factor de escala (1.0 = 20×40 px por carácter)
|
// - escala: factor de escala (1.0 = 20×40 px por carácter)
|
||||||
// - spacing: espacio entre caracteres en píxeles (a escala 1.0)
|
// - spacing: espacio entre caracteres en píxeles (a escala 1.0)
|
||||||
void render(const std::string& text, const Punt& posicio, float escala = 1.0f, float spacing = 2.0f);
|
// - brightness: factor de brillantor (0.0-1.0, default 1.0 = màxima brillantor)
|
||||||
|
void render(const std::string& text, const Punt& posicio, float escala = 1.0f, float spacing = 2.0f, float brightness = 1.0f);
|
||||||
|
|
||||||
// Calcular ancho total de un string (útil para centrado)
|
// Calcular ancho total de un string (útil para centrado)
|
||||||
float get_text_width(const std::string& text, float escala = 1.0f, float spacing = 2.0f) const;
|
float get_text_width(const std::string& text, float escala = 1.0f, float spacing = 2.0f) const;
|
||||||
|
|||||||
@@ -58,8 +58,7 @@ bool linea(SDL_Renderer* renderer, int x1, int y1, int x2, int y2, bool dibuixar
|
|||||||
SDL_SetRenderDrawColor(renderer, color_final.r, color_final.g, color_final.b, 255);
|
SDL_SetRenderDrawColor(renderer, color_final.r, color_final.g, color_final.b, 255);
|
||||||
|
|
||||||
// Renderitzar amb coordenades físiques
|
// Renderitzar amb coordenades físiques
|
||||||
SDL_RenderLine(renderer, static_cast<float>(px1), static_cast<float>(py1),
|
SDL_RenderLine(renderer, static_cast<float>(px1), static_cast<float>(py1), static_cast<float>(px2), static_cast<float>(py2));
|
||||||
static_cast<float>(px2), static_cast<float>(py2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Algorisme de Bresenham original (conservat per a futura detecció de
|
// Algorisme de Bresenham original (conservat per a futura detecció de
|
||||||
|
|||||||
@@ -433,11 +433,13 @@ void SDLManager::updateFPS(float delta_time) {
|
|||||||
fps_accumulator_ = 0.0f;
|
fps_accumulator_ = 0.0f;
|
||||||
|
|
||||||
// Actualitzar títol de la finestra
|
// Actualitzar títol de la finestra
|
||||||
std::string title = std::format("{} v{} ({}) - {} FPS",
|
std::string vsync_state = (Options::rendering.vsync == 1) ? "ON" : "OFF";
|
||||||
|
std::string title = std::format("{} v{} ({}) - {} FPS - VSync: {}",
|
||||||
Project::LONG_NAME,
|
Project::LONG_NAME,
|
||||||
Project::VERSION,
|
Project::VERSION,
|
||||||
Project::COPYRIGHT,
|
Project::COPYRIGHT,
|
||||||
fps_display_);
|
fps_display_,
|
||||||
|
vsync_state);
|
||||||
|
|
||||||
if (finestra_) {
|
if (finestra_) {
|
||||||
SDL_SetWindowTitle(finestra_, title.c_str());
|
SDL_SetWindowTitle(finestra_, title.c_str());
|
||||||
@@ -462,6 +464,10 @@ void SDLManager::toggleVSync() {
|
|||||||
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
|
SDL_SetRenderVSync(renderer_, Options::rendering.vsync);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset FPS counter para evitar valores mixtos entre regímenes
|
||||||
|
fps_accumulator_ = 0.0f;
|
||||||
|
fps_frame_count_ = 0;
|
||||||
|
|
||||||
// Guardar configuració
|
// Guardar configuració
|
||||||
Options::saveToFile();
|
Options::saveToFile();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,7 @@
|
|||||||
class SDLManager {
|
class SDLManager {
|
||||||
public:
|
public:
|
||||||
SDLManager(); // Constructor per defecte (usa Defaults::)
|
SDLManager(); // Constructor per defecte (usa Defaults::)
|
||||||
SDLManager(int width, int height,
|
SDLManager(int width, int height, bool fullscreen); // Constructor amb configuració
|
||||||
bool fullscreen); // Constructor amb configuració
|
|
||||||
~SDLManager();
|
~SDLManager();
|
||||||
|
|
||||||
// No permetre còpia ni assignació
|
// No permetre còpia ni assignació
|
||||||
@@ -27,8 +26,7 @@ class SDLManager {
|
|||||||
void decreaseWindowSize(); // F1: -100px
|
void decreaseWindowSize(); // F1: -100px
|
||||||
void toggleFullscreen(); // F3
|
void toggleFullscreen(); // F3
|
||||||
void toggleVSync(); // F4
|
void toggleVSync(); // F4
|
||||||
bool
|
bool handleWindowEvent(const SDL_Event& event); // Per a SDL_EVENT_WINDOW_RESIZED
|
||||||
handleWindowEvent(const SDL_Event& event); // Per a SDL_EVENT_WINDOW_RESIZED
|
|
||||||
|
|
||||||
// Funcions principals (renderitzat)
|
// Funcions principals (renderitzat)
|
||||||
void neteja(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0);
|
void neteja(uint8_t r = 0, uint8_t g = 0, uint8_t b = 0);
|
||||||
|
|||||||
83
source/core/resources/resource_helper.cpp
Normal file
83
source/core/resources/resource_helper.cpp
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
// resource_helper.cpp - Implementació de funcions d'ajuda
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#include "resource_helper.hpp"
|
||||||
|
|
||||||
|
#include "resource_loader.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace Resource {
|
||||||
|
namespace Helper {
|
||||||
|
|
||||||
|
// Inicialitzar el sistema de recursos
|
||||||
|
bool initializeResourceSystem(const std::string& pack_file, bool fallback) {
|
||||||
|
return Loader::get().initialize(pack_file, fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carregar un fitxer
|
||||||
|
std::vector<uint8_t> loadFile(const std::string& filepath) {
|
||||||
|
// Normalitzar la ruta
|
||||||
|
std::string normalized = normalizePath(filepath);
|
||||||
|
|
||||||
|
// Carregar del sistema de recursos
|
||||||
|
return Loader::get().loadResource(normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprovar si existeix un fitxer
|
||||||
|
bool fileExists(const std::string& filepath) {
|
||||||
|
std::string normalized = normalizePath(filepath);
|
||||||
|
return Loader::get().resourceExists(normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtenir ruta normalitzada per al paquet
|
||||||
|
// Elimina prefixos "data/", rutes absolutes, etc.
|
||||||
|
std::string getPackPath(const std::string& asset_path) {
|
||||||
|
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 despré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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalitzar ruta (alias de getPackPath)
|
||||||
|
std::string normalizePath(const std::string& path) {
|
||||||
|
return getPackPath(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprovar si hi ha paquet carregat
|
||||||
|
bool isPackLoaded() {
|
||||||
|
return Loader::get().isPackLoaded();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Helper
|
||||||
|
} // namespace Resource
|
||||||
28
source/core/resources/resource_helper.hpp
Normal file
28
source/core/resources/resource_helper.hpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// resource_helper.hpp - Funcions d'ajuda per gestió de recursos
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
// API simplificada i normalització de rutes
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Resource {
|
||||||
|
namespace Helper {
|
||||||
|
|
||||||
|
// Inicialització del sistema
|
||||||
|
bool initializeResourceSystem(const std::string& pack_file, bool fallback);
|
||||||
|
|
||||||
|
// Càrrega de fitxers
|
||||||
|
std::vector<uint8_t> loadFile(const std::string& filepath);
|
||||||
|
bool fileExists(const std::string& filepath);
|
||||||
|
|
||||||
|
// Normalització de rutes
|
||||||
|
std::string getPackPath(const std::string& asset_path);
|
||||||
|
std::string normalizePath(const std::string& path);
|
||||||
|
|
||||||
|
// Estat
|
||||||
|
bool isPackLoaded();
|
||||||
|
|
||||||
|
} // namespace Helper
|
||||||
|
} // namespace Resource
|
||||||
143
source/core/resources/resource_loader.cpp
Normal file
143
source/core/resources/resource_loader.cpp
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
// resource_loader.cpp - Implementació del carregador de recursos
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#include "resource_loader.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace Resource {
|
||||||
|
|
||||||
|
// Singleton
|
||||||
|
Loader& Loader::get() {
|
||||||
|
static Loader instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inicialitzar el sistema de recursos
|
||||||
|
bool Loader::initialize(const std::string& pack_file, bool enable_fallback) {
|
||||||
|
fallback_enabled_ = enable_fallback;
|
||||||
|
|
||||||
|
// Intentar carregar el paquet
|
||||||
|
pack_ = std::make_unique<Pack>();
|
||||||
|
|
||||||
|
if (!pack_->loadPack(pack_file)) {
|
||||||
|
if (!fallback_enabled_) {
|
||||||
|
std::cerr << "[ResourceLoader] ERROR FATAL: No es pot carregar " << pack_file
|
||||||
|
<< " i el fallback està desactivat\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[ResourceLoader] Paquet no trobat, usant fallback al sistema de fitxers\n";
|
||||||
|
pack_.reset(); // No hi ha paquet
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[ResourceLoader] Paquet carregat: " << pack_file << "\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carregar un recurs
|
||||||
|
std::vector<uint8_t> Loader::loadResource(const std::string& filename) {
|
||||||
|
// Intentar carregar 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 i no hi ha fallback, falla
|
||||||
|
if (!fallback_enabled_) {
|
||||||
|
std::cerr << "[ResourceLoader] ERROR: Recurs no trobat al paquet i fallback desactivat: "
|
||||||
|
<< filename << "\n";
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback al sistema de fitxers
|
||||||
|
if (fallback_enabled_) {
|
||||||
|
return loadFromFilesystem(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprovar si existeix un recurs
|
||||||
|
bool Loader::resourceExists(const std::string& filename) {
|
||||||
|
// Comprovar al paquet
|
||||||
|
if (pack_ && pack_->hasResource(filename)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprovar al sistema de fitxers 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar el paquet
|
||||||
|
bool Loader::validatePack() {
|
||||||
|
if (!pack_) {
|
||||||
|
std::cerr << "[ResourceLoader] Advertència: no hi ha paquet carregat per validar\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pack_->validatePack();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprovar si hi ha paquet carregat
|
||||||
|
bool Loader::isPackLoaded() const {
|
||||||
|
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
|
||||||
|
std::string Loader::getBasePath() const {
|
||||||
|
return base_path_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carregar des del sistema de fitxers (fallback)
|
||||||
|
std::vector<uint8_t> Loader::loadFromFilesystem(const std::string& filename) {
|
||||||
|
// 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 fitxers: " << fullpath << "\n";
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Resource
|
||||||
53
source/core/resources/resource_loader.hpp
Normal file
53
source/core/resources/resource_loader.hpp
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
// resource_loader.hpp - Carregador de recursos (Singleton)
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
// Coordina càrrega des del paquet i/o sistema de fitxers
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "resource_pack.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Resource {
|
||||||
|
|
||||||
|
// Singleton per gestionar la càrrega de recursos
|
||||||
|
class Loader {
|
||||||
|
public:
|
||||||
|
// Singleton
|
||||||
|
static Loader& get();
|
||||||
|
|
||||||
|
// Inicialització
|
||||||
|
bool initialize(const std::string& pack_file, bool enable_fallback);
|
||||||
|
|
||||||
|
// Càrrega de recursos
|
||||||
|
std::vector<uint8_t> loadResource(const std::string& filename);
|
||||||
|
bool resourceExists(const std::string& filename);
|
||||||
|
|
||||||
|
// Validació
|
||||||
|
bool validatePack();
|
||||||
|
bool isPackLoaded() const;
|
||||||
|
|
||||||
|
// Estat
|
||||||
|
void setBasePath(const std::string& path);
|
||||||
|
std::string getBasePath() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Loader() = default;
|
||||||
|
~Loader() = default;
|
||||||
|
|
||||||
|
// No es pot copiar ni moure
|
||||||
|
Loader(const Loader&) = delete;
|
||||||
|
Loader& operator=(const Loader&) = delete;
|
||||||
|
|
||||||
|
// Dades
|
||||||
|
std::unique_ptr<Pack> pack_;
|
||||||
|
bool fallback_enabled_ = false;
|
||||||
|
std::string base_path_;
|
||||||
|
|
||||||
|
// Funcions auxiliars
|
||||||
|
std::vector<uint8_t> loadFromFilesystem(const std::string& filename);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Resource
|
||||||
309
source/core/resources/resource_pack.cpp
Normal file
309
source/core/resources/resource_pack.cpp
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
// resource_pack.cpp - Implementació del sistema d'empaquetament
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#include "resource_pack.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace Resource {
|
||||||
|
|
||||||
|
// Calcular checksum CRC32 simplificat
|
||||||
|
uint32_t Pack::calculateChecksum(const std::vector<uint8_t>& data) const {
|
||||||
|
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 és simètric
|
||||||
|
encryptData(data, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Llegir fitxer complet a memòria
|
||||||
|
std::vector<uint8_t> Pack::readFile(const std::string& filepath) {
|
||||||
|
std::ifstream file(filepath, std::ios::binary | std::ios::ate);
|
||||||
|
if (!file) {
|
||||||
|
std::cerr << "[ResourcePack] Error: no es pot obrir " << filepath << '\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 << "[ResourcePack] Error: no es pot llegir " << filepath << '\n';
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Afegir un fitxer individual al paquet
|
||||||
|
bool Pack::addFile(const std::string& filepath, const std::string& pack_name) {
|
||||||
|
auto file_data = readFile(filepath);
|
||||||
|
if (file_data.empty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ResourceEntry entry{
|
||||||
|
.filename = pack_name,
|
||||||
|
.offset = data_.size(),
|
||||||
|
.size = file_data.size(),
|
||||||
|
.checksum = calculateChecksum(file_data)};
|
||||||
|
|
||||||
|
// Afegir dades al bloc de dades
|
||||||
|
data_.insert(data_.end(), file_data.begin(), file_data.end());
|
||||||
|
|
||||||
|
resources_[pack_name] = entry;
|
||||||
|
|
||||||
|
std::cout << "[ResourcePack] Afegit: " << pack_name << " (" << file_data.size()
|
||||||
|
<< " bytes)\n";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Afegir tots els fitxers d'un directori recursivament
|
||||||
|
bool Pack::addDirectory(const std::string& dir_path,
|
||||||
|
const std::string& base_path) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 fitxers 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::string pack_name = current_base + relative_path;
|
||||||
|
addFile(full_path, pack_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Guardar paquet a disc
|
||||||
|
bool Pack::savePack(const std::string& pack_file) {
|
||||||
|
std::ofstream file(pack_file, std::ios::binary);
|
||||||
|
if (!file) {
|
||||||
|
std::cerr << "[ResourcePack] Error: no es pot crear " << pack_file << '\n';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Escriure capçalera
|
||||||
|
file.write(MAGIC_HEADER, 4);
|
||||||
|
file.write(reinterpret_cast<const char*>(&VERSION), sizeof(VERSION));
|
||||||
|
|
||||||
|
// Escriure nombre de recursos
|
||||||
|
auto resource_count = static_cast<uint32_t>(resources_.size());
|
||||||
|
file.write(reinterpret_cast<const char*>(&resource_count), sizeof(resource_count));
|
||||||
|
|
||||||
|
// Escriure metadades de recursos
|
||||||
|
for (const auto& [name, entry] : resources_) {
|
||||||
|
// Nom del fitxer
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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 i 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());
|
||||||
|
|
||||||
|
std::cout << "[ResourcePack] Guardat: " << pack_file << " (" << resources_.size()
|
||||||
|
<< " recursos, " << data_size << " bytes)\n";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carregar paquet des de disc
|
||||||
|
bool Pack::loadPack(const std::string& pack_file) {
|
||||||
|
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ó 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 fitxer
|
||||||
|
uint32_t name_len;
|
||||||
|
file.read(reinterpret_cast<char*>(&name_len), sizeof(name_len));
|
||||||
|
|
||||||
|
std::string filename(name_len, '\0');
|
||||||
|
file.read(&filename[0], 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
|
||||||
|
std::vector<uint8_t> Pack::getResource(const std::string& filename) {
|
||||||
|
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, però adverteix
|
||||||
|
}
|
||||||
|
|
||||||
|
return resource_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprovar si existeix un recurs
|
||||||
|
bool Pack::hasResource(const std::string& filename) const {
|
||||||
|
return resources_.find(filename) != resources_.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtenir llista de tots els recursos
|
||||||
|
std::vector<std::string> Pack::getResourceList() const {
|
||||||
|
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
|
||||||
|
bool Pack::validatePack() const {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid) {
|
||||||
|
std::cout << "[ResourcePack] Validació OK (" << resources_.size() << " recursos)\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Resource
|
||||||
67
source/core/resources/resource_pack.hpp
Normal file
67
source/core/resources/resource_pack.hpp
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
// resource_pack.hpp - Sistema d'empaquetament de recursos
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
// Basat en el sistema de "pollo" amb adaptacions per Orni Attack
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Resource {
|
||||||
|
|
||||||
|
// Capçalera del fitxer de paquet
|
||||||
|
struct PackHeader {
|
||||||
|
char magic[4]; // "ORNI"
|
||||||
|
uint32_t version; // Versió del format (1)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Entrada de recurs dins el paquet
|
||||||
|
struct ResourceEntry {
|
||||||
|
std::string filename; // Nom del recurs (amb barres normals)
|
||||||
|
uint64_t offset; // Posició dins el bloc de dades
|
||||||
|
uint64_t size; // Mida en bytes
|
||||||
|
uint32_t checksum; // Checksum CRC32 per verificació
|
||||||
|
};
|
||||||
|
|
||||||
|
// Classe principal per gestionar paquets de recursos
|
||||||
|
class Pack {
|
||||||
|
public:
|
||||||
|
Pack() = default;
|
||||||
|
~Pack() = default;
|
||||||
|
|
||||||
|
// Afegir fitxers al paquet
|
||||||
|
bool addFile(const std::string& filepath, const std::string& pack_name);
|
||||||
|
bool addDirectory(const std::string& dir_path, const std::string& base_path = "");
|
||||||
|
|
||||||
|
// Guardar i carregar paquets
|
||||||
|
bool savePack(const std::string& pack_file);
|
||||||
|
bool loadPack(const std::string& pack_file);
|
||||||
|
|
||||||
|
// Accés a recursos
|
||||||
|
std::vector<uint8_t> getResource(const std::string& filename);
|
||||||
|
bool hasResource(const std::string& filename) const;
|
||||||
|
std::vector<std::string> getResourceList() const;
|
||||||
|
|
||||||
|
// Validació
|
||||||
|
bool validatePack() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Constants
|
||||||
|
static constexpr const char* MAGIC_HEADER = "ORNI";
|
||||||
|
static constexpr uint32_t VERSION = 1;
|
||||||
|
static constexpr const char* DEFAULT_ENCRYPT_KEY = "ORNI_RESOURCES_2025";
|
||||||
|
|
||||||
|
// Dades del paquet
|
||||||
|
std::unordered_map<std::string, ResourceEntry> resources_;
|
||||||
|
std::vector<uint8_t> data_;
|
||||||
|
|
||||||
|
// Funcions auxiliars
|
||||||
|
std::vector<uint8_t> readFile(const std::string& filepath);
|
||||||
|
uint32_t calculateChecksum(const std::vector<uint8_t>& data) const;
|
||||||
|
void encryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||||
|
void decryptData(std::vector<uint8_t>& data, const std::string& key);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Resource
|
||||||
70
source/core/system/context_escenes.hpp
Normal file
70
source/core/system/context_escenes.hpp
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// context_escenes.hpp - Sistema de gestió d'escenes i context de transicions
|
||||||
|
// © 2025 Port a C++20
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace GestorEscenes {
|
||||||
|
|
||||||
|
// Context de transició entre escenes
|
||||||
|
// Conté l'escena destinació i opcions específiques per aquella escena
|
||||||
|
class ContextEscenes {
|
||||||
|
public:
|
||||||
|
// Tipus d'escena del joc
|
||||||
|
enum class Escena {
|
||||||
|
LOGO, // Pantalla d'inici (logo JAILGAMES)
|
||||||
|
TITOL, // Pantalla de títol amb menú
|
||||||
|
JOC, // Joc principal (Asteroids)
|
||||||
|
EIXIR // Sortir del programa
|
||||||
|
};
|
||||||
|
|
||||||
|
// Opcions específiques per a cada escena
|
||||||
|
enum class Opcio {
|
||||||
|
NONE, // Sense opcions especials (comportament per defecte)
|
||||||
|
JUMP_TO_TITLE_MAIN, // TITOL: Saltar directament a MAIN (starfield instantani)
|
||||||
|
// MODE_DEMO, // JOC: Mode demostració amb IA (futur)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Constructor inicial amb escena LOGO i sense opcions
|
||||||
|
ContextEscenes()
|
||||||
|
: escena_desti_(Escena::LOGO),
|
||||||
|
opcio_(Opcio::NONE) {}
|
||||||
|
|
||||||
|
// Canviar escena amb opció específica
|
||||||
|
void canviar_escena(Escena nova_escena, Opcio opcio = Opcio::NONE) {
|
||||||
|
escena_desti_ = nova_escena;
|
||||||
|
opcio_ = opcio;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consultar escena destinació
|
||||||
|
[[nodiscard]] auto escena_desti() const -> Escena {
|
||||||
|
return escena_desti_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consultar opció actual
|
||||||
|
[[nodiscard]] auto opcio() const -> Opcio {
|
||||||
|
return opcio_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consumir opció (retorna valor i reseteja a NONE)
|
||||||
|
// Utilitzar quan l'escena processa l'opció
|
||||||
|
[[nodiscard]] auto consumir_opcio() -> Opcio {
|
||||||
|
Opcio valor = opcio_;
|
||||||
|
opcio_ = Opcio::NONE;
|
||||||
|
return valor;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset opció a NONE (sense retornar valor)
|
||||||
|
void reset_opcio() {
|
||||||
|
opcio_ = Opcio::NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Escena escena_desti_; // Escena a la qual transicionar
|
||||||
|
Opcio opcio_; // Opció específica per l'escena
|
||||||
|
};
|
||||||
|
|
||||||
|
// Variable global inline per gestionar l'escena actual (backward compatibility)
|
||||||
|
// Sincronitzada amb context.escena_desti() pel Director
|
||||||
|
inline ContextEscenes::Escena actual = ContextEscenes::Escena::LOGO;
|
||||||
|
|
||||||
|
} // namespace GestorEscenes
|
||||||
@@ -7,14 +7,18 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "core/audio/audio.hpp"
|
||||||
|
#include "core/audio/audio_cache.hpp"
|
||||||
|
#include "core/defaults.hpp"
|
||||||
|
#include "core/rendering/sdl_manager.hpp"
|
||||||
|
#include "core/resources/resource_helper.hpp"
|
||||||
|
#include "core/resources/resource_loader.hpp"
|
||||||
|
#include "core/utils/path_utils.hpp"
|
||||||
#include "game/escenes/escena_joc.hpp"
|
#include "game/escenes/escena_joc.hpp"
|
||||||
#include "game/escenes/escena_logo.hpp"
|
#include "game/escenes/escena_logo.hpp"
|
||||||
#include "game/escenes/escena_titol.hpp"
|
#include "game/escenes/escena_titol.hpp"
|
||||||
#include "game/options.hpp"
|
#include "game/options.hpp"
|
||||||
#include "core/audio/audio.hpp"
|
#include "context_escenes.hpp"
|
||||||
#include "core/defaults.hpp"
|
|
||||||
#include "core/rendering/sdl_manager.hpp"
|
|
||||||
#include "gestor_escenes.hpp"
|
|
||||||
#include "project.h"
|
#include "project.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
@@ -22,6 +26,10 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Using declarations per simplificar el codi
|
||||||
|
using GestorEscenes::ContextEscenes;
|
||||||
|
using Escena = ContextEscenes::Escena;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
Director::Director(std::vector<std::string> const& args) {
|
Director::Director(std::vector<std::string> const& args) {
|
||||||
std::cout << "Orni Attack - Inici\n";
|
std::cout << "Orni Attack - Inici\n";
|
||||||
@@ -32,6 +40,44 @@ Director::Director(std::vector<std::string> const& args) {
|
|||||||
// Comprovar arguments del programa
|
// Comprovar arguments del programa
|
||||||
executable_path_ = checkProgramArguments(args);
|
executable_path_ = checkProgramArguments(args);
|
||||||
|
|
||||||
|
// Inicialitzar sistema de rutes
|
||||||
|
Utils::initializePathSystem(args[0].c_str());
|
||||||
|
|
||||||
|
// Obtenir ruta base dels recursos
|
||||||
|
std::string resource_base = Utils::getResourceBasePath();
|
||||||
|
|
||||||
|
// Inicialitzar sistema de recursos
|
||||||
|
#ifdef RELEASE_BUILD
|
||||||
|
// Mode release: paquet obligatori, sense fallback
|
||||||
|
std::string pack_path = resource_base + "/resources.pack";
|
||||||
|
if (!Resource::Helper::initializeResourceSystem(pack_path, false)) {
|
||||||
|
std::cerr << "ERROR FATAL: No es pot carregar " << pack_path << "\n";
|
||||||
|
std::cerr << "El joc no pot continuar sense els recursos.\n";
|
||||||
|
std::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar integritat del paquet
|
||||||
|
if (!Resource::Loader::get().validatePack()) {
|
||||||
|
std::cerr << "ERROR FATAL: El paquet de recursos està corromput\n";
|
||||||
|
std::exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Sistema de recursos inicialitzat (mode release)\n";
|
||||||
|
#else
|
||||||
|
// Mode desenvolupament: intentar paquet amb fallback a data/
|
||||||
|
std::string pack_path = resource_base + "/resources.pack";
|
||||||
|
Resource::Helper::initializeResourceSystem(pack_path, true);
|
||||||
|
|
||||||
|
if (Resource::Helper::isPackLoaded()) {
|
||||||
|
std::cout << "Sistema de recursos inicialitzat (mode dev amb paquet)\n";
|
||||||
|
} else {
|
||||||
|
std::cout << "Sistema de recursos inicialitzat (mode dev, fallback a data/)\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Establir ruta base per al fallback
|
||||||
|
Resource::Loader::get().setBasePath(resource_base);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Crear carpetes del sistema
|
// Crear carpetes del sistema
|
||||||
createSystemFolder("jailgames");
|
createSystemFolder("jailgames");
|
||||||
createSystemFolder(std::string("jailgames/") + Project::NAME);
|
createSystemFolder(std::string("jailgames/") + Project::NAME);
|
||||||
@@ -160,24 +206,45 @@ auto Director::run() -> int {
|
|||||||
|
|
||||||
// Inicialitzar sistema d'audio
|
// Inicialitzar sistema d'audio
|
||||||
Audio::init();
|
Audio::init();
|
||||||
|
Audio::get()->setMusicVolume(1.0);
|
||||||
|
Audio::get()->setSoundVolume(0.4);
|
||||||
|
|
||||||
|
// Precachejar música per evitar lag al començar
|
||||||
|
AudioCache::getMusic("title.ogg");
|
||||||
|
if (Options::console) {
|
||||||
|
std::cout << "Música precachejada: "
|
||||||
|
<< AudioCache::getMusicCacheSize() << " fitxers\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Crear context d'escenes
|
||||||
|
ContextEscenes context;
|
||||||
|
#ifdef _DEBUG
|
||||||
|
context.canviar_escena(Escena::JOC);
|
||||||
|
#else
|
||||||
|
context.canviar_escena(Escena::LOGO);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Bucle principal de gestió d'escenes
|
// Bucle principal de gestió d'escenes
|
||||||
while (GestorEscenes::actual != GestorEscenes::Escena::EIXIR) {
|
while (context.escena_desti() != Escena::EIXIR) {
|
||||||
switch (GestorEscenes::actual) {
|
// Sincronitzar GestorEscenes::actual amb context
|
||||||
case GestorEscenes::Escena::LOGO: {
|
// (altres sistemes encara poden llegir GestorEscenes::actual)
|
||||||
EscenaLogo logo(sdl);
|
GestorEscenes::actual = context.escena_desti();
|
||||||
|
|
||||||
|
switch (context.escena_desti()) {
|
||||||
|
case Escena::LOGO: {
|
||||||
|
EscenaLogo logo(sdl, context);
|
||||||
logo.executar();
|
logo.executar();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case GestorEscenes::Escena::TITOL: {
|
case Escena::TITOL: {
|
||||||
EscenaTitol titol(sdl);
|
EscenaTitol titol(sdl, context);
|
||||||
titol.executar();
|
titol.executar();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case GestorEscenes::Escena::JOC: {
|
case Escena::JOC: {
|
||||||
EscenaJoc joc(sdl);
|
EscenaJoc joc(sdl, context);
|
||||||
joc.executar();
|
joc.executar();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -187,5 +254,8 @@ auto Director::run() -> int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sincronitzar final amb GestorEscenes::actual
|
||||||
|
GestorEscenes::actual = Escena::EIXIR;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
// gestor_escenes.hpp - Sistema de gestió d'escenes
|
|
||||||
// Basat en el patró del projecte "pollo"
|
|
||||||
// © 2025 Port a C++20
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
namespace GestorEscenes {
|
|
||||||
enum class Escena {
|
|
||||||
LOGO, // Pantalla d'inici (2 segons negre)
|
|
||||||
TITOL, // Pantalla de títol amb menú
|
|
||||||
JOC, // Joc principal
|
|
||||||
EIXIR // Sortir del programa
|
|
||||||
};
|
|
||||||
|
|
||||||
// Variable global inline per gestionar l'escena actual
|
|
||||||
inline Escena actual = Escena::LOGO;
|
|
||||||
} // namespace GestorEscenes
|
|
||||||
@@ -3,13 +3,17 @@
|
|||||||
|
|
||||||
#include "global_events.hpp"
|
#include "global_events.hpp"
|
||||||
|
|
||||||
#include "core/rendering/sdl_manager.hpp"
|
|
||||||
#include "gestor_escenes.hpp"
|
|
||||||
#include "core/input/mouse.hpp"
|
#include "core/input/mouse.hpp"
|
||||||
|
#include "core/rendering/sdl_manager.hpp"
|
||||||
|
#include "context_escenes.hpp"
|
||||||
|
|
||||||
|
// Using declarations per simplificar el codi
|
||||||
|
using GestorEscenes::ContextEscenes;
|
||||||
|
using Escena = ContextEscenes::Escena;
|
||||||
|
|
||||||
namespace GlobalEvents {
|
namespace GlobalEvents {
|
||||||
|
|
||||||
bool handle(const SDL_Event& event, SDLManager& sdl) {
|
bool handle(const SDL_Event& event, SDLManager& sdl, ContextEscenes& context) {
|
||||||
// Tecles globals de finestra (F1/F2/F3)
|
// Tecles globals de finestra (F1/F2/F3)
|
||||||
if (event.type == SDL_EVENT_KEY_DOWN) {
|
if (event.type == SDL_EVENT_KEY_DOWN) {
|
||||||
switch (event.key.key) {
|
switch (event.key.key) {
|
||||||
@@ -26,7 +30,8 @@ bool handle(const SDL_Event& event, SDLManager& sdl) {
|
|||||||
sdl.toggleVSync();
|
sdl.toggleVSync();
|
||||||
return true;
|
return true;
|
||||||
case SDLK_ESCAPE:
|
case SDLK_ESCAPE:
|
||||||
GestorEscenes::actual = GestorEscenes::Escena::EIXIR;
|
context.canviar_escena(Escena::EIXIR);
|
||||||
|
GestorEscenes::actual = Escena::EIXIR;
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -35,7 +40,8 @@ bool handle(const SDL_Event& event, SDLManager& sdl) {
|
|||||||
|
|
||||||
// Tancar finestra
|
// Tancar finestra
|
||||||
if (event.type == SDL_EVENT_QUIT) {
|
if (event.type == SDL_EVENT_QUIT) {
|
||||||
GestorEscenes::actual = GestorEscenes::Escena::EIXIR;
|
context.canviar_escena(Escena::EIXIR);
|
||||||
|
GestorEscenes::actual = Escena::EIXIR;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,12 @@
|
|||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
// Forward declaration
|
// Forward declarations
|
||||||
class SDLManager;
|
class SDLManager;
|
||||||
|
namespace GestorEscenes { class ContextEscenes; }
|
||||||
|
|
||||||
namespace GlobalEvents {
|
namespace GlobalEvents {
|
||||||
// Processa events globals (F1/F2/F3/ESC/QUIT)
|
// Processa events globals (F1/F2/F3/ESC/QUIT)
|
||||||
// Retorna true si l'event ha estat processat i no cal seguir processant-lo
|
// Retorna true si l'event ha estat processat i no cal seguir processant-lo
|
||||||
bool handle(const SDL_Event& event, SDLManager& sdl);
|
bool handle(const SDL_Event& event, SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||||
} // namespace GlobalEvents
|
} // namespace GlobalEvents
|
||||||
|
|||||||
92
source/core/utils/path_utils.cpp
Normal file
92
source/core/utils/path_utils.cpp
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// path_utils.cpp - Implementació de utilitats de rutes
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#include "path_utils.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace Utils {
|
||||||
|
|
||||||
|
// Variables globals per guardar argv[0]
|
||||||
|
static std::string executable_path_;
|
||||||
|
static std::string executable_directory_;
|
||||||
|
|
||||||
|
// Inicialitzar el sistema de rutes amb argv[0]
|
||||||
|
void initializePathSystem(const char* argv0) {
|
||||||
|
if (!argv0) {
|
||||||
|
std::cerr << "[PathUtils] ADVERTÈNCIA: argv[0] és nullptr\n";
|
||||||
|
executable_path_ = "";
|
||||||
|
executable_directory_ = ".";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
executable_path_ = argv0;
|
||||||
|
|
||||||
|
// Extreure el directori
|
||||||
|
std::filesystem::path path(argv0);
|
||||||
|
executable_directory_ = path.parent_path().string();
|
||||||
|
|
||||||
|
if (executable_directory_.empty()) {
|
||||||
|
executable_directory_ = ".";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[PathUtils] Executable: " << executable_path_ << "\n";
|
||||||
|
std::cout << "[PathUtils] Directori: " << executable_directory_ << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtenir el directori de l'executable
|
||||||
|
std::string getExecutableDirectory() {
|
||||||
|
if (executable_directory_.empty()) {
|
||||||
|
std::cerr << "[PathUtils] ADVERTÈNCIA: Sistema de rutes no inicialitzat\n";
|
||||||
|
return ".";
|
||||||
|
}
|
||||||
|
return executable_directory_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detectar si estem dins un bundle de macOS
|
||||||
|
bool isMacOSBundle() {
|
||||||
|
#ifdef MACOS_BUNDLE
|
||||||
|
return true;
|
||||||
|
#else
|
||||||
|
// Detecció en temps d'execució
|
||||||
|
// Cercar ".app/Contents/MacOS" a la ruta de l'executable
|
||||||
|
std::string exe_dir = getExecutableDirectory();
|
||||||
|
return exe_dir.find(".app/Contents/MacOS") != std::string::npos;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtenir la ruta base dels recursos
|
||||||
|
std::string getResourceBasePath() {
|
||||||
|
std::string exe_dir = getExecutableDirectory();
|
||||||
|
|
||||||
|
if (isMacOSBundle()) {
|
||||||
|
// Bundle de macOS: recursos a ../Resources des de MacOS/
|
||||||
|
std::cout << "[PathUtils] Detectat bundle de macOS\n";
|
||||||
|
return exe_dir + "/../Resources";
|
||||||
|
} else {
|
||||||
|
// Executable normal: recursos al mateix directori
|
||||||
|
return exe_dir;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalitzar ruta (convertir barres, etc.)
|
||||||
|
std::string normalizePath(const std::string& path) {
|
||||||
|
std::string normalized = path;
|
||||||
|
|
||||||
|
// Convertir barres invertides a normals
|
||||||
|
std::ranges::replace(normalized, '\\', '/');
|
||||||
|
|
||||||
|
// Simplificar rutes amb filesystem
|
||||||
|
try {
|
||||||
|
std::filesystem::path fs_path(normalized);
|
||||||
|
normalized = fs_path.lexically_normal().string();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[PathUtils] Error normalitzant ruta: " << e.what() << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return normalized;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Utils
|
||||||
24
source/core/utils/path_utils.hpp
Normal file
24
source/core/utils/path_utils.hpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// path_utils.hpp - Utilitats de gestió de rutes
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
// Detecció de directoris i bundles multiplataforma
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace Utils {
|
||||||
|
|
||||||
|
// Inicialització amb argv[0]
|
||||||
|
void initializePathSystem(const char* argv0);
|
||||||
|
|
||||||
|
// Obtenció de rutes
|
||||||
|
std::string getExecutableDirectory();
|
||||||
|
std::string getResourceBasePath();
|
||||||
|
|
||||||
|
// Detecció de plataforma
|
||||||
|
bool isMacOSBundle();
|
||||||
|
|
||||||
|
// Normalització
|
||||||
|
std::string normalizePath(const std::string& path);
|
||||||
|
|
||||||
|
} // namespace Utils
|
||||||
@@ -18,8 +18,9 @@ struct Debris {
|
|||||||
float acceleracio; // Acceleració negativa (fricció) en px/s²
|
float acceleracio; // Acceleració negativa (fricció) en px/s²
|
||||||
|
|
||||||
// Rotació
|
// Rotació
|
||||||
float angle_rotacio; // Angle de rotació acumulat (radians)
|
float angle_rotacio; // Angle de rotació acumulat (radians)
|
||||||
float velocitat_rot; // Velocitat de rotació en rad/s
|
float velocitat_rot; // Velocitat de rotació de TRAYECTORIA (rad/s)
|
||||||
|
float velocitat_rot_visual; // Velocitat de rotació VISUAL del segment (rad/s)
|
||||||
|
|
||||||
// Estat de vida
|
// Estat de vida
|
||||||
float temps_vida; // Temps transcorregut (segons)
|
float temps_vida; // Temps transcorregut (segons)
|
||||||
@@ -28,6 +29,9 @@ struct Debris {
|
|||||||
|
|
||||||
// Shrinking (reducció de distància entre punts)
|
// Shrinking (reducció de distància entre punts)
|
||||||
float factor_shrink; // Factor de reducció per segon (0.0-1.0)
|
float factor_shrink; // Factor de reducció per segon (0.0-1.0)
|
||||||
|
|
||||||
|
// Rendering
|
||||||
|
float brightness; // Factor de brillantor (0.0-1.0, heretat de l'objecte original)
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Effects
|
} // namespace Effects
|
||||||
|
|||||||
@@ -47,7 +47,11 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
|||||||
const Punt& centre,
|
const Punt& centre,
|
||||||
float angle,
|
float angle,
|
||||||
float escala,
|
float escala,
|
||||||
float velocitat_base) {
|
float velocitat_base,
|
||||||
|
float brightness,
|
||||||
|
const Punt& velocitat_objecte,
|
||||||
|
float velocitat_angular,
|
||||||
|
float factor_herencia_visual) {
|
||||||
if (!shape || !shape->es_valida()) {
|
if (!shape || !shape->es_valida()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -94,29 +98,92 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
|||||||
debris->p1 = world_p1;
|
debris->p1 = world_p1;
|
||||||
debris->p2 = world_p2;
|
debris->p2 = world_p2;
|
||||||
|
|
||||||
// 4. Calcular direcció perpendicular
|
// 4. Calcular direcció d'explosió (radial, des del centre cap a fora)
|
||||||
Punt direccio = calcular_direccio_perpendicular(world_p1, world_p2);
|
Punt direccio = calcular_direccio_explosio(world_p1, world_p2, centre);
|
||||||
|
|
||||||
// 5. Velocitat inicial (base ± variació aleatòria)
|
// 5. Velocitat inicial (base ± variació aleatòria + velocitat heretada)
|
||||||
float speed =
|
float speed =
|
||||||
velocitat_base +
|
velocitat_base +
|
||||||
((std::rand() / static_cast<float>(RAND_MAX)) * 2.0f - 1.0f) *
|
((std::rand() / static_cast<float>(RAND_MAX)) * 2.0f - 1.0f) *
|
||||||
Defaults::Physics::Debris::VARIACIO_VELOCITAT;
|
Defaults::Physics::Debris::VARIACIO_VELOCITAT;
|
||||||
|
|
||||||
debris->velocitat.x = direccio.x * speed;
|
// Heredar velocitat de l'objecte original (suma vectorial)
|
||||||
debris->velocitat.y = direccio.y * speed;
|
debris->velocitat.x = direccio.x * speed + velocitat_objecte.x;
|
||||||
|
debris->velocitat.y = direccio.y * speed + velocitat_objecte.y;
|
||||||
debris->acceleracio = Defaults::Physics::Debris::ACCELERACIO;
|
debris->acceleracio = Defaults::Physics::Debris::ACCELERACIO;
|
||||||
|
|
||||||
// 6. Rotació lenta aleatòria
|
// 6. Herència de velocitat angular amb cap + conversió d'excés
|
||||||
debris->velocitat_rot =
|
|
||||||
Defaults::Physics::Debris::ROTACIO_MIN +
|
|
||||||
(std::rand() / static_cast<float>(RAND_MAX)) *
|
|
||||||
(Defaults::Physics::Debris::ROTACIO_MAX -
|
|
||||||
Defaults::Physics::Debris::ROTACIO_MIN);
|
|
||||||
|
|
||||||
// 50% probabilitat de rotació en sentit contrari
|
// 6a. Rotació de TRAYECTORIA amb cap + conversió tangencial
|
||||||
if (std::rand() % 2 == 0) {
|
if (std::abs(velocitat_angular) > 0.01f) {
|
||||||
debris->velocitat_rot = -debris->velocitat_rot;
|
// FASE 1: Aplicar herència i variació (igual que abans)
|
||||||
|
float factor_herencia =
|
||||||
|
Defaults::Physics::Debris::FACTOR_HERENCIA_MIN +
|
||||||
|
(std::rand() / static_cast<float>(RAND_MAX)) *
|
||||||
|
(Defaults::Physics::Debris::FACTOR_HERENCIA_MAX -
|
||||||
|
Defaults::Physics::Debris::FACTOR_HERENCIA_MIN);
|
||||||
|
|
||||||
|
float velocitat_ang_heretada = velocitat_angular * factor_herencia;
|
||||||
|
|
||||||
|
float variacio =
|
||||||
|
(std::rand() / static_cast<float>(RAND_MAX)) * 0.2f - 0.1f;
|
||||||
|
velocitat_ang_heretada *= (1.0f + variacio);
|
||||||
|
|
||||||
|
// FASE 2: Aplicar cap i calcular excés
|
||||||
|
constexpr float CAP = Defaults::Physics::Debris::VELOCITAT_ROT_MAX;
|
||||||
|
float abs_ang = std::abs(velocitat_ang_heretada);
|
||||||
|
float sign_ang = (velocitat_ang_heretada >= 0.0f) ? 1.0f : -1.0f;
|
||||||
|
|
||||||
|
if (abs_ang > CAP) {
|
||||||
|
// Excés: convertir a velocitat tangencial
|
||||||
|
float excess = abs_ang - CAP;
|
||||||
|
|
||||||
|
// Radi de la forma (enemics = 20 px)
|
||||||
|
float radius = 20.0f;
|
||||||
|
|
||||||
|
// Velocitat tangencial = ω_excés × radi
|
||||||
|
float v_tangential = excess * radius;
|
||||||
|
|
||||||
|
// Direcció tangencial: perpendicular a la radial (90° CCW)
|
||||||
|
// Si direccio = (dx, dy), tangent = (-dy, dx)
|
||||||
|
float tangent_x = -direccio.y;
|
||||||
|
float tangent_y = direccio.x;
|
||||||
|
|
||||||
|
// Afegir velocitat tangencial (suma vectorial)
|
||||||
|
debris->velocitat.x += tangent_x * v_tangential;
|
||||||
|
debris->velocitat.y += tangent_y * v_tangential;
|
||||||
|
|
||||||
|
// Aplicar cap a velocitat angular (preservar signe)
|
||||||
|
debris->velocitat_rot = sign_ang * CAP;
|
||||||
|
} else {
|
||||||
|
// Per sota del cap: comportament normal
|
||||||
|
debris->velocitat_rot = velocitat_ang_heretada;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debris->velocitat_rot = 0.0f; // Nave: sin curvas
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6b. Rotació VISUAL (proporcional según factor_herencia_visual)
|
||||||
|
if (factor_herencia_visual > 0.01f && std::abs(velocitat_angular) > 0.01f) {
|
||||||
|
// Heredar rotación visual con factor proporcional
|
||||||
|
debris->velocitat_rot_visual = debris->velocitat_rot * factor_herencia_visual;
|
||||||
|
|
||||||
|
// Variació aleatòria petita (±5%) per naturalitat
|
||||||
|
float variacio_visual =
|
||||||
|
(std::rand() / static_cast<float>(RAND_MAX)) * 0.1f - 0.05f;
|
||||||
|
debris->velocitat_rot_visual *= (1.0f + variacio_visual);
|
||||||
|
} else {
|
||||||
|
// Rotació visual aleatòria (factor = 0.0 o sin velocidad angular)
|
||||||
|
debris->velocitat_rot_visual =
|
||||||
|
Defaults::Physics::Debris::ROTACIO_MIN +
|
||||||
|
(std::rand() / static_cast<float>(RAND_MAX)) *
|
||||||
|
(Defaults::Physics::Debris::ROTACIO_MAX -
|
||||||
|
Defaults::Physics::Debris::ROTACIO_MIN);
|
||||||
|
|
||||||
|
// 50% probabilitat de rotació en sentit contrari
|
||||||
|
if (std::rand() % 2 == 0) {
|
||||||
|
debris->velocitat_rot_visual = -debris->velocitat_rot_visual;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debris->angle_rotacio = 0.0f;
|
debris->angle_rotacio = 0.0f;
|
||||||
@@ -126,7 +193,10 @@ void DebrisManager::explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
|||||||
debris->temps_max = Defaults::Physics::Debris::TEMPS_VIDA;
|
debris->temps_max = Defaults::Physics::Debris::TEMPS_VIDA;
|
||||||
debris->factor_shrink = Defaults::Physics::Debris::SHRINK_RATE;
|
debris->factor_shrink = Defaults::Physics::Debris::SHRINK_RATE;
|
||||||
|
|
||||||
// 8. Activar
|
// 8. Heredar brightness
|
||||||
|
debris->brightness = brightness;
|
||||||
|
|
||||||
|
// 9. Activar
|
||||||
debris->actiu = true;
|
debris->actiu = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,6 +239,35 @@ void DebrisManager::actualitzar(float delta_time) {
|
|||||||
debris.velocitat.y = 0.0f;
|
debris.velocitat.y = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2b. Rotar vector de velocitat (trayectoria curva)
|
||||||
|
if (std::abs(debris.velocitat_rot) > 0.01f) {
|
||||||
|
// Calcular angle de rotació aquest frame
|
||||||
|
float dangle = debris.velocitat_rot * delta_time;
|
||||||
|
|
||||||
|
// Rotar vector de velocitat usant matriu de rotació 2D
|
||||||
|
float vel_x_old = debris.velocitat.x;
|
||||||
|
float vel_y_old = debris.velocitat.y;
|
||||||
|
|
||||||
|
float cos_a = std::cos(dangle);
|
||||||
|
float sin_a = std::sin(dangle);
|
||||||
|
|
||||||
|
debris.velocitat.x = vel_x_old * cos_a - vel_y_old * sin_a;
|
||||||
|
debris.velocitat.y = vel_x_old * sin_a + vel_y_old * cos_a;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2c. Aplicar fricció angular (desacceleració gradual)
|
||||||
|
if (std::abs(debris.velocitat_rot) > 0.01f) {
|
||||||
|
float sign = (debris.velocitat_rot > 0) ? 1.0f : -1.0f;
|
||||||
|
float reduccion =
|
||||||
|
Defaults::Physics::Debris::FRICCIO_ANGULAR * delta_time;
|
||||||
|
debris.velocitat_rot -= sign * reduccion;
|
||||||
|
|
||||||
|
// Evitar canvi de signe (no pot passar de CW a CCW)
|
||||||
|
if ((debris.velocitat_rot > 0) != (sign > 0)) {
|
||||||
|
debris.velocitat_rot = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 3. Calcular centre del segment
|
// 3. Calcular centre del segment
|
||||||
Punt centre = {(debris.p1.x + debris.p2.x) / 2.0f,
|
Punt centre = {(debris.p1.x + debris.p2.x) / 2.0f,
|
||||||
(debris.p1.y + debris.p2.y) / 2.0f};
|
(debris.p1.y + debris.p2.y) / 2.0f};
|
||||||
@@ -177,8 +276,8 @@ void DebrisManager::actualitzar(float delta_time) {
|
|||||||
centre.x += debris.velocitat.x * delta_time;
|
centre.x += debris.velocitat.x * delta_time;
|
||||||
centre.y += debris.velocitat.y * delta_time;
|
centre.y += debris.velocitat.y * delta_time;
|
||||||
|
|
||||||
// 5. Actualitzar rotació
|
// 5. Actualitzar rotació VISUAL
|
||||||
debris.angle_rotacio += debris.velocitat_rot * delta_time;
|
debris.angle_rotacio += debris.velocitat_rot_visual * delta_time;
|
||||||
|
|
||||||
// 6. Aplicar shrinking (reducció de distància entre punts)
|
// 6. Aplicar shrinking (reducció de distància entre punts)
|
||||||
float shrink_factor =
|
float shrink_factor =
|
||||||
@@ -206,8 +305,14 @@ void DebrisManager::dibuixar() const {
|
|||||||
if (!debris.actiu)
|
if (!debris.actiu)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Dibuixar segment de línia
|
// Dibuixar segment de línia amb brightness heretat
|
||||||
Rendering::linea(renderer_, static_cast<int>(debris.p1.x), static_cast<int>(debris.p1.y), static_cast<int>(debris.p2.x), static_cast<int>(debris.p2.y), true);
|
Rendering::linea(renderer_,
|
||||||
|
static_cast<int>(debris.p1.x),
|
||||||
|
static_cast<int>(debris.p1.y),
|
||||||
|
static_cast<int>(debris.p2.x),
|
||||||
|
static_cast<int>(debris.p2.y),
|
||||||
|
true,
|
||||||
|
debris.brightness);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,16 +325,22 @@ Debris* DebrisManager::trobar_slot_lliure() {
|
|||||||
return nullptr; // Pool ple
|
return nullptr; // Pool ple
|
||||||
}
|
}
|
||||||
|
|
||||||
Punt DebrisManager::calcular_direccio_perpendicular(const Punt& p1,
|
Punt DebrisManager::calcular_direccio_explosio(const Punt& p1,
|
||||||
const Punt& p2) const {
|
const Punt& p2,
|
||||||
// 1. Calcular vector de la línia (p1 → p2)
|
const Punt& centre_objecte) const {
|
||||||
float dx = p2.x - p1.x;
|
// 1. Calcular centre del segment
|
||||||
float dy = p2.y - p1.y;
|
float centro_seg_x = (p1.x + p2.x) / 2.0f;
|
||||||
|
float centro_seg_y = (p1.y + p2.y) / 2.0f;
|
||||||
|
|
||||||
// 2. Normalitzar (obtenir vector unitari)
|
// 2. Calcular vector des del centre de l'objecte cap al centre del segment
|
||||||
|
// Això garanteix que la direcció sempre apunte cap a fora (direcció radial)
|
||||||
|
float dx = centro_seg_x - centre_objecte.x;
|
||||||
|
float dy = centro_seg_y - centre_objecte.y;
|
||||||
|
|
||||||
|
// 3. Normalitzar (obtenir vector unitari)
|
||||||
float length = std::sqrt(dx * dx + dy * dy);
|
float length = std::sqrt(dx * dx + dy * dy);
|
||||||
if (length < 0.001f) {
|
if (length < 0.001f) {
|
||||||
// Línia degenerada, retornar direcció aleatòria
|
// Segment al centre (cas extrem molt improbable), retornar direcció aleatòria
|
||||||
float angle_rand =
|
float angle_rand =
|
||||||
(std::rand() / static_cast<float>(RAND_MAX)) * 2.0f * Defaults::Math::PI;
|
(std::rand() / static_cast<float>(RAND_MAX)) * 2.0f * Defaults::Math::PI;
|
||||||
return {std::cos(angle_rand), std::sin(angle_rand)};
|
return {std::cos(angle_rand), std::sin(angle_rand)};
|
||||||
@@ -238,26 +349,15 @@ Punt DebrisManager::calcular_direccio_perpendicular(const Punt& p1,
|
|||||||
dx /= length;
|
dx /= length;
|
||||||
dy /= length;
|
dy /= length;
|
||||||
|
|
||||||
// 3. Rotar 90° (perpendicular)
|
// 4. Afegir variació aleatòria petita (±15°) per varietat visual
|
||||||
// Rotació 90° sentit antihorari: (x,y) → (-y, x)
|
|
||||||
float perp_x = -dy;
|
|
||||||
float perp_y = dx;
|
|
||||||
|
|
||||||
// 4. Afegir variació aleatòria petita (±15°)
|
|
||||||
float angle_variacio =
|
float angle_variacio =
|
||||||
((std::rand() % 30) - 15) * Defaults::Math::PI / 180.0f;
|
((std::rand() % 30) - 15) * Defaults::Math::PI / 180.0f;
|
||||||
|
|
||||||
float cos_v = std::cos(angle_variacio);
|
float cos_v = std::cos(angle_variacio);
|
||||||
float sin_v = std::sin(angle_variacio);
|
float sin_v = std::sin(angle_variacio);
|
||||||
|
|
||||||
float final_x = perp_x * cos_v - perp_y * sin_v;
|
float final_x = dx * cos_v - dy * sin_v;
|
||||||
float final_y = perp_x * sin_v + perp_y * cos_v;
|
float final_y = dx * sin_v + dy * cos_v;
|
||||||
|
|
||||||
// 5. Afegir ± direcció aleatòria (50% probabilitat d'invertir)
|
|
||||||
if (std::rand() % 2 == 0) {
|
|
||||||
final_x = -final_x;
|
|
||||||
final_y = -final_y;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {final_x, final_y};
|
return {final_x, final_y};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,11 +25,19 @@ class DebrisManager {
|
|||||||
// - angle: orientació de l'objecte (radians)
|
// - angle: orientació de l'objecte (radians)
|
||||||
// - escala: escala de l'objecte (1.0 = normal)
|
// - escala: escala de l'objecte (1.0 = normal)
|
||||||
// - velocitat_base: velocitat inicial dels fragments (px/s)
|
// - velocitat_base: velocitat inicial dels fragments (px/s)
|
||||||
|
// - brightness: factor de brillantor heretat (0.0-1.0, per defecte 1.0)
|
||||||
|
// - velocitat_objecte: velocitat de l'objecte que explota (px/s, per defecte 0)
|
||||||
|
// - velocitat_angular: velocitat angular heretada (rad/s, per defecte 0)
|
||||||
|
// - factor_herencia_visual: factor de herència rotació visual (0.0-1.0, per defecte 0.0)
|
||||||
void explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
void explotar(const std::shared_ptr<Graphics::Shape>& shape,
|
||||||
const Punt& centre,
|
const Punt& centre,
|
||||||
float angle,
|
float angle,
|
||||||
float escala,
|
float escala,
|
||||||
float velocitat_base);
|
float velocitat_base,
|
||||||
|
float brightness = 1.0f,
|
||||||
|
const Punt& velocitat_objecte = {0.0f, 0.0f},
|
||||||
|
float velocitat_angular = 0.0f,
|
||||||
|
float factor_herencia_visual = 0.0f);
|
||||||
|
|
||||||
// Actualitzar tots els fragments actius
|
// Actualitzar tots els fragments actius
|
||||||
void actualitzar(float delta_time);
|
void actualitzar(float delta_time);
|
||||||
@@ -56,8 +64,8 @@ class DebrisManager {
|
|||||||
// Trobar primer slot inactiu
|
// Trobar primer slot inactiu
|
||||||
Debris* trobar_slot_lliure();
|
Debris* trobar_slot_lliure();
|
||||||
|
|
||||||
// Calcular direcció perpendicular a un segment
|
// Calcular direcció d'explosió (radial, des del centre cap al segment)
|
||||||
Punt calcular_direccio_perpendicular(const Punt& p1, const Punt& p2) const;
|
Punt calcular_direccio_explosio(const Punt& p1, const Punt& p2, const Punt& centre_objecte) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Effects
|
} // namespace Effects
|
||||||
|
|||||||
99
source/game/effects/gestor_puntuacio_flotant.cpp
Normal file
99
source/game/effects/gestor_puntuacio_flotant.cpp
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
// gestor_puntuacio_flotant.cpp - Implementació del gestor de números flotants
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#include "gestor_puntuacio_flotant.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace Effects {
|
||||||
|
|
||||||
|
GestorPuntuacioFlotant::GestorPuntuacioFlotant(SDL_Renderer* renderer)
|
||||||
|
: renderer_(renderer), text_(renderer) {
|
||||||
|
// Inicialitzar tots els slots com inactius
|
||||||
|
for (auto& pf : pool_) {
|
||||||
|
pf.actiu = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GestorPuntuacioFlotant::crear(int punts, const Punt& posicio) {
|
||||||
|
// 1. Trobar slot lliure
|
||||||
|
PuntuacioFlotant* pf = trobar_slot_lliure();
|
||||||
|
if (!pf)
|
||||||
|
return; // Pool ple (improbable)
|
||||||
|
|
||||||
|
// 2. Inicialitzar puntuació flotant
|
||||||
|
pf->text = std::to_string(punts);
|
||||||
|
pf->posicio = posicio;
|
||||||
|
pf->velocitat = {Defaults::FloatingScore::VELOCITY_X,
|
||||||
|
Defaults::FloatingScore::VELOCITY_Y};
|
||||||
|
pf->temps_vida = 0.0f;
|
||||||
|
pf->temps_max = Defaults::FloatingScore::LIFETIME;
|
||||||
|
pf->brightness = 1.0f;
|
||||||
|
pf->actiu = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GestorPuntuacioFlotant::actualitzar(float delta_time) {
|
||||||
|
for (auto& pf : pool_) {
|
||||||
|
if (!pf.actiu)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// 1. Actualitzar posició (deriva cap amunt)
|
||||||
|
pf.posicio.x += pf.velocitat.x * delta_time;
|
||||||
|
pf.posicio.y += pf.velocitat.y * delta_time;
|
||||||
|
|
||||||
|
// 2. Actualitzar temps de vida
|
||||||
|
pf.temps_vida += delta_time;
|
||||||
|
|
||||||
|
// 3. Calcular brightness (fade lineal)
|
||||||
|
float progress = pf.temps_vida / pf.temps_max; // 0.0 → 1.0
|
||||||
|
pf.brightness = 1.0f - progress; // 1.0 → 0.0
|
||||||
|
|
||||||
|
// 4. Desactivar quan acaba el temps
|
||||||
|
if (pf.temps_vida >= pf.temps_max) {
|
||||||
|
pf.actiu = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GestorPuntuacioFlotant::dibuixar() {
|
||||||
|
for (const auto& pf : pool_) {
|
||||||
|
if (!pf.actiu)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// 1. Calcular dimensions del text per centrar-lo
|
||||||
|
constexpr float escala = Defaults::FloatingScore::SCALE;
|
||||||
|
constexpr float spacing = Defaults::FloatingScore::SPACING;
|
||||||
|
float text_width = text_.get_text_width(pf.text, escala, spacing);
|
||||||
|
|
||||||
|
// 2. Centrar text sobre la posició
|
||||||
|
Punt render_pos = {pf.posicio.x - text_width / 2.0f, pf.posicio.y};
|
||||||
|
|
||||||
|
// 3. Renderitzar amb brightness (fade)
|
||||||
|
text_.render(pf.text, render_pos, escala, spacing, pf.brightness);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GestorPuntuacioFlotant::reiniciar() {
|
||||||
|
for (auto& pf : pool_) {
|
||||||
|
pf.actiu = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int GestorPuntuacioFlotant::get_num_actius() const {
|
||||||
|
int count = 0;
|
||||||
|
for (const auto& pf : pool_) {
|
||||||
|
if (pf.actiu)
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
PuntuacioFlotant* GestorPuntuacioFlotant::trobar_slot_lliure() {
|
||||||
|
for (auto& pf : pool_) {
|
||||||
|
if (!pf.actiu)
|
||||||
|
return &pf;
|
||||||
|
}
|
||||||
|
return nullptr; // Pool ple
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Effects
|
||||||
54
source/game/effects/gestor_puntuacio_flotant.hpp
Normal file
54
source/game/effects/gestor_puntuacio_flotant.hpp
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
// gestor_puntuacio_flotant.hpp - Gestor de números de puntuació flotants
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#include "core/defaults.hpp"
|
||||||
|
#include "core/graphics/vector_text.hpp"
|
||||||
|
#include "core/types.hpp"
|
||||||
|
#include "puntuacio_flotant.hpp"
|
||||||
|
|
||||||
|
namespace Effects {
|
||||||
|
|
||||||
|
// Gestor de números de puntuació flotants
|
||||||
|
// Manté un pool de PuntuacioFlotant i gestiona el seu cicle de vida
|
||||||
|
class GestorPuntuacioFlotant {
|
||||||
|
public:
|
||||||
|
explicit GestorPuntuacioFlotant(SDL_Renderer* renderer);
|
||||||
|
|
||||||
|
// Crear número flotant
|
||||||
|
// - punts: valor numèric (100, 150, 200)
|
||||||
|
// - posicio: on apareix (normalment centre d'enemic destruït)
|
||||||
|
void crear(int punts, const Punt& posicio);
|
||||||
|
|
||||||
|
// Actualitzar tots els números actius
|
||||||
|
void actualitzar(float delta_time);
|
||||||
|
|
||||||
|
// Dibuixar tots els números actius
|
||||||
|
void dibuixar();
|
||||||
|
|
||||||
|
// Reiniciar tots (neteja)
|
||||||
|
void reiniciar();
|
||||||
|
|
||||||
|
// Obtenir número actius (debug)
|
||||||
|
int get_num_actius() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
SDL_Renderer* renderer_;
|
||||||
|
Graphics::VectorText text_; // Sistema de text vectorial
|
||||||
|
|
||||||
|
// Pool de números flotants (màxim concurrent)
|
||||||
|
// Màxim 15 enemics simultanis = màxim 15 números
|
||||||
|
static constexpr int MAX_PUNTUACIONS =
|
||||||
|
Defaults::FloatingScore::MAX_CONCURRENT;
|
||||||
|
std::array<PuntuacioFlotant, MAX_PUNTUACIONS> pool_;
|
||||||
|
|
||||||
|
// Trobar primer slot inactiu
|
||||||
|
PuntuacioFlotant* trobar_slot_lliure();
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Effects
|
||||||
33
source/game/effects/puntuacio_flotant.hpp
Normal file
33
source/game/effects/puntuacio_flotant.hpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// puntuacio_flotant.hpp - Número de puntuació que apareix i desapareix
|
||||||
|
// © 2025 Port a C++20 amb SDL3
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "core/types.hpp"
|
||||||
|
|
||||||
|
namespace Effects {
|
||||||
|
|
||||||
|
// PuntuacioFlotant: text animat que mostra punts guanyats
|
||||||
|
// S'activa quan es destrueix un enemic i s'esvaeix després d'un temps
|
||||||
|
struct PuntuacioFlotant {
|
||||||
|
// Text a mostrar (e.g., "100", "150", "200")
|
||||||
|
std::string text;
|
||||||
|
|
||||||
|
// Posició actual (coordenades mundials)
|
||||||
|
Punt posicio;
|
||||||
|
|
||||||
|
// Animació de moviment
|
||||||
|
Punt velocitat; // px/s (normalment cap amunt: {0.0f, -30.0f})
|
||||||
|
|
||||||
|
// Animació de fade
|
||||||
|
float temps_vida; // Temps transcorregut (segons)
|
||||||
|
float temps_max; // Temps de vida màxim (segons)
|
||||||
|
float brightness; // Brillantor calculada (0.0-1.0)
|
||||||
|
|
||||||
|
// Estat
|
||||||
|
bool actiu;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace Effects
|
||||||
@@ -67,8 +67,8 @@ void Bala::actualitzar(float delta_time) {
|
|||||||
void Bala::dibuixar() const {
|
void Bala::dibuixar() const {
|
||||||
if (esta_ && forma_) {
|
if (esta_ && forma_) {
|
||||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
// [NUEVO] Usar render_shape en lloc de rota_pol
|
||||||
// Les bales no roten visualment (angle sempre 0.0f)
|
// Les bales roten segons l'angle de trajectòria
|
||||||
Rendering::render_shape(renderer_, forma_, centre_, 0.0f, 1.0f, true, 1.0f, brightness_);
|
Rendering::render_shape(renderer_, forma_, centre_, angle_, 1.0f, true, 1.0f, brightness_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,25 +21,55 @@ Enemic::Enemic(SDL_Renderer* renderer)
|
|||||||
drotacio_(0.0f),
|
drotacio_(0.0f),
|
||||||
rotacio_(0.0f),
|
rotacio_(0.0f),
|
||||||
esta_(false),
|
esta_(false),
|
||||||
brightness_(Defaults::Brightness::ENEMIC) {
|
brightness_(Defaults::Brightness::ENEMIC),
|
||||||
// [NUEVO] Carregar forma compartida des de fitxer
|
tipus_(TipusEnemic::PENTAGON),
|
||||||
forma_ = Graphics::ShapeLoader::load("enemy_pentagon.shp");
|
tracking_timer_(0.0f),
|
||||||
|
ship_position_(nullptr),
|
||||||
if (!forma_ || !forma_->es_valida()) {
|
tracking_strength_(0.5f), // Default tracking strength
|
||||||
std::cerr << "[Enemic] Error: no s'ha pogut carregar enemy_pentagon.shp"
|
timer_invulnerabilitat_(0.0f) { // Start vulnerable
|
||||||
<< std::endl;
|
// [NUEVO] Forma es carrega a inicialitzar() segons el tipus
|
||||||
}
|
// Constructor no carrega forma per permetre tipus diferents
|
||||||
}
|
}
|
||||||
|
|
||||||
void Enemic::inicialitzar() {
|
void Enemic::inicialitzar(TipusEnemic tipus, const Punt* ship_pos) {
|
||||||
// Inicialitzar enemic (pentàgon)
|
// Guardar tipus
|
||||||
// Copiat de joc_asteroides.cpp línies 41-54
|
tipus_ = tipus;
|
||||||
|
|
||||||
// [NUEVO] Ja no cal crear_poligon_regular - la geometria es carrega del
|
// Carregar forma segons el tipus
|
||||||
// fitxer Només inicialitzem l'estat de la instància
|
const char* shape_file;
|
||||||
|
float drotacio_min, drotacio_max;
|
||||||
|
|
||||||
// Posició aleatòria dins de l'àrea de joc
|
switch (tipus_) {
|
||||||
// Calcular rangs segurs amb radi de l'enemic
|
case TipusEnemic::PENTAGON:
|
||||||
|
shape_file = Defaults::Enemies::Pentagon::SHAPE_FILE;
|
||||||
|
velocitat_ = Defaults::Enemies::Pentagon::VELOCITAT;
|
||||||
|
drotacio_min = Defaults::Enemies::Pentagon::DROTACIO_MIN;
|
||||||
|
drotacio_max = Defaults::Enemies::Pentagon::DROTACIO_MAX;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TipusEnemic::QUADRAT:
|
||||||
|
shape_file = Defaults::Enemies::Quadrat::SHAPE_FILE;
|
||||||
|
velocitat_ = Defaults::Enemies::Quadrat::VELOCITAT;
|
||||||
|
drotacio_min = Defaults::Enemies::Quadrat::DROTACIO_MIN;
|
||||||
|
drotacio_max = Defaults::Enemies::Quadrat::DROTACIO_MAX;
|
||||||
|
tracking_timer_ = 0.0f;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case TipusEnemic::MOLINILLO:
|
||||||
|
shape_file = Defaults::Enemies::Molinillo::SHAPE_FILE;
|
||||||
|
velocitat_ = Defaults::Enemies::Molinillo::VELOCITAT;
|
||||||
|
drotacio_min = Defaults::Enemies::Molinillo::DROTACIO_MIN;
|
||||||
|
drotacio_max = Defaults::Enemies::Molinillo::DROTACIO_MAX;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Carregar forma
|
||||||
|
forma_ = Graphics::ShapeLoader::load(shape_file);
|
||||||
|
if (!forma_ || !forma_->es_valida()) {
|
||||||
|
std::cerr << "[Enemic] Error: no s'ha pogut carregar " << shape_file << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// [MODIFIED] Posició aleatòria amb comprovació de seguretat
|
||||||
float min_x, max_x, min_y, max_y;
|
float min_x, max_x, min_y, max_y;
|
||||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||||
min_x,
|
min_x,
|
||||||
@@ -47,32 +77,87 @@ void Enemic::inicialitzar() {
|
|||||||
min_y,
|
min_y,
|
||||||
max_y);
|
max_y);
|
||||||
|
|
||||||
// Spawn aleatori dins dels límits segurs
|
if (ship_pos != nullptr) {
|
||||||
int range_x = static_cast<int>(max_x - min_x);
|
// [NEW] Safe spawn: attempt to find position away from ship
|
||||||
int range_y = static_cast<int>(max_y - min_y);
|
bool found_safe_position = false;
|
||||||
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
|
||||||
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
for (int attempt = 0; attempt < Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS; attempt++) {
|
||||||
|
float candidate_x, candidate_y;
|
||||||
|
|
||||||
|
if (intent_spawn_safe(*ship_pos, candidate_x, candidate_y)) {
|
||||||
|
centre_.x = candidate_x;
|
||||||
|
centre_.y = candidate_y;
|
||||||
|
found_safe_position = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found_safe_position) {
|
||||||
|
// Fallback: spawn anywhere (user's preference)
|
||||||
|
int range_x = static_cast<int>(max_x - min_x);
|
||||||
|
int range_y = static_cast<int>(max_y - min_y);
|
||||||
|
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||||
|
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||||
|
|
||||||
|
std::cout << "[Enemic] Advertència: spawn sense zona segura després de "
|
||||||
|
<< Defaults::Enemies::Spawn::MAX_SPAWN_ATTEMPTS << " intents" << std::endl;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// [EXISTING] No ship position: spawn anywhere (backward compatibility)
|
||||||
|
int range_x = static_cast<int>(max_x - min_x);
|
||||||
|
int range_y = static_cast<int>(max_y - min_y);
|
||||||
|
centre_.x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||||
|
centre_.y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||||
|
}
|
||||||
|
|
||||||
// Angle aleatori de moviment
|
// Angle aleatori de moviment
|
||||||
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
angle_ = (std::rand() % 360) * Constants::PI / 180.0f;
|
||||||
|
|
||||||
// Velocitat (2 px/frame original * 20 FPS = 40 px/s)
|
// Rotació visual aleatòria (rad/s) dins del rang del tipus
|
||||||
velocitat_ = 40.0f;
|
float drotacio_range = drotacio_max - drotacio_min;
|
||||||
|
drotacio_ = drotacio_min + (static_cast<float>(std::rand()) / RAND_MAX) * drotacio_range;
|
||||||
// Rotació visual aleatòria (rad/s)
|
|
||||||
// Original Pascal: random * 0.1 rad/frame * 20 FPS ≈ 2 rad/s
|
|
||||||
drotacio_ = (static_cast<float>(std::rand()) / RAND_MAX) * 2.0f;
|
|
||||||
rotacio_ = 0.0f;
|
rotacio_ = 0.0f;
|
||||||
|
|
||||||
|
// Inicialitzar estat d'animació
|
||||||
|
animacio_ = AnimacioEnemic(); // Reset to defaults
|
||||||
|
animacio_.drotacio_base = drotacio_;
|
||||||
|
animacio_.drotacio_objetivo = drotacio_;
|
||||||
|
animacio_.drotacio_t = 1.0f; // Start without interpolating
|
||||||
|
|
||||||
|
// [NEW] Inicialitzar invulnerabilitat
|
||||||
|
timer_invulnerabilitat_ = Defaults::Enemies::Spawn::INVULNERABILITY_DURATION; // 3.0s
|
||||||
|
brightness_ = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START; // 0.3f
|
||||||
|
|
||||||
// Activar
|
// Activar
|
||||||
esta_ = true;
|
esta_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Enemic::actualitzar(float delta_time) {
|
void Enemic::actualitzar(float delta_time) {
|
||||||
if (esta_) {
|
if (esta_) {
|
||||||
|
// [NEW] Update invulnerability timer and brightness
|
||||||
|
if (timer_invulnerabilitat_ > 0.0f) {
|
||||||
|
timer_invulnerabilitat_ -= delta_time;
|
||||||
|
|
||||||
|
if (timer_invulnerabilitat_ < 0.0f) {
|
||||||
|
timer_invulnerabilitat_ = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Update brightness with LERP during invulnerability
|
||||||
|
float t_inv = timer_invulnerabilitat_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
|
||||||
|
float t = 1.0f - t_inv; // 0.0 → 1.0
|
||||||
|
float smooth_t = t * t * (3.0f - 2.0f * t); // smoothstep
|
||||||
|
|
||||||
|
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_START;
|
||||||
|
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_BRIGHTNESS_END;
|
||||||
|
brightness_ = START + (END - START) * smooth_t;
|
||||||
|
}
|
||||||
|
|
||||||
// Moviment autònom
|
// Moviment autònom
|
||||||
mou(delta_time);
|
mou(delta_time);
|
||||||
|
|
||||||
|
// Actualitzar animacions (palpitació, rotació accelerada)
|
||||||
|
actualitzar_animacio(delta_time);
|
||||||
|
|
||||||
// Rotació visual (time-based: drotacio_ està en rad/s)
|
// Rotació visual (time-based: drotacio_ està en rad/s)
|
||||||
rotacio_ += drotacio_ * delta_time;
|
rotacio_ += drotacio_ * delta_time;
|
||||||
}
|
}
|
||||||
@@ -80,21 +165,33 @@ void Enemic::actualitzar(float delta_time) {
|
|||||||
|
|
||||||
void Enemic::dibuixar() const {
|
void Enemic::dibuixar() const {
|
||||||
if (esta_ && forma_) {
|
if (esta_ && forma_) {
|
||||||
// [NUEVO] Usar render_shape en lloc de rota_pol
|
// Calculate animated scale (includes invulnerability LERP)
|
||||||
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, 1.0f, true, 1.0f, brightness_);
|
float escala = calcular_escala_actual();
|
||||||
|
|
||||||
|
// brightness_ is already updated in actualitzar()
|
||||||
|
Rendering::render_shape(renderer_, forma_, centre_, rotacio_, escala, true, 1.0f, brightness_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Enemic::mou(float delta_time) {
|
void Enemic::mou(float delta_time) {
|
||||||
// Moviment autònom d'ORNI (enemic pentàgon)
|
// Dispatcher: crida el comportament específic segons el tipus
|
||||||
// Basat EXACTAMENT en el codi Pascal original: ASTEROID.PAS lines 279-293
|
switch (tipus_) {
|
||||||
// Copiat EXACTAMENT de joc_asteroides.cpp línies 348-394
|
case TipusEnemic::PENTAGON:
|
||||||
//
|
comportament_pentagon(delta_time);
|
||||||
// IMPORTANT: El Pascal original NO té canvi aleatori continu!
|
break;
|
||||||
// Només ajusta l'angle quan toca una paret.
|
case TipusEnemic::QUADRAT:
|
||||||
|
comportament_quadrat(delta_time);
|
||||||
|
break;
|
||||||
|
case TipusEnemic::MOLINILLO:
|
||||||
|
comportament_molinillo(delta_time);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::comportament_pentagon(float delta_time) {
|
||||||
|
// Pentagon: zigzag esquivador (frequent direction changes)
|
||||||
|
// Similar a comportament original però amb probabilitat més alta
|
||||||
|
|
||||||
// Calcular nova posició PROPUESTA (time-based, però lògica Pascal)
|
|
||||||
// velocitat_ ja està en px/s (40 px/s), multiplicar per delta_time
|
|
||||||
float velocitat_efectiva = velocitat_ * delta_time;
|
float velocitat_efectiva = velocitat_ * delta_time;
|
||||||
|
|
||||||
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
// Calcular desplaçament (angle-PI/2 perquè angle=0 apunta amunt)
|
||||||
@@ -104,7 +201,7 @@ void Enemic::mou(float delta_time) {
|
|||||||
float new_y = centre_.y + dy;
|
float new_y = centre_.y + dy;
|
||||||
float new_x = centre_.x + dx;
|
float new_x = centre_.x + dx;
|
||||||
|
|
||||||
// Obtenir límits segurs compensant el radi de l'enemic
|
// Obtenir límits segurs
|
||||||
float min_x, max_x, min_y, max_y;
|
float min_x, max_x, min_y, max_y;
|
||||||
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||||
min_x,
|
min_x,
|
||||||
@@ -112,33 +209,306 @@ void Enemic::mou(float delta_time) {
|
|||||||
min_y,
|
min_y,
|
||||||
max_y);
|
max_y);
|
||||||
|
|
||||||
// Lògica Pascal: Actualitza Y si dins, sinó ajusta angle aleatòriament
|
// Zigzag: canvi d'angle més freqüent en tocar límits
|
||||||
// if (dy>marge_dalt) and (dy<marge_baix) then orni.centre.y:=round(Dy)
|
|
||||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
|
||||||
// CORRECCIÓ: Usar inequalitats inclusives (>= i <=) per evitar fugides
|
|
||||||
if (new_y >= min_y && new_y <= max_y) {
|
if (new_y >= min_y && new_y <= max_y) {
|
||||||
centre_.y = new_y;
|
centre_.y = new_y;
|
||||||
} else {
|
} else {
|
||||||
// Pequeño ajuste aleatorio: (random(256)/512)*(random(3)-1)
|
// Probabilitat més alta de canvi d'angle
|
||||||
// random(256) = 0..255, /512 = 0..0.498
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Pentagon::CANVI_ANGLE_PROB) {
|
||||||
// random(3) = 0,1,2, -1 = -1,0,1
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||||
// Resultado: ±0.5 rad aprox
|
Defaults::Enemies::Pentagon::CANVI_ANGLE_MAX;
|
||||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||||
int rand2 = (std::rand() % 3) - 1; // -1, 0, o 1
|
}
|
||||||
angle_ += rand1 * static_cast<float>(rand2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lògica Pascal: Actualitza X si dins, sinó ajusta angle aleatòriament
|
|
||||||
// if (dx>marge_esq) and (dx<marge_dret) then orni.centre.x:=round(Dx)
|
|
||||||
// else orni.angle:=orni.angle+(random(256)/512)*(random(3)-1);
|
|
||||||
// CORRECCIÓ: Usar inequalitats inclusives (>= i <=) per evitar fugides
|
|
||||||
if (new_x >= min_x && new_x <= max_x) {
|
if (new_x >= min_x && new_x <= max_x) {
|
||||||
centre_.x = new_x;
|
centre_.x = new_x;
|
||||||
} else {
|
} else {
|
||||||
float rand1 = (static_cast<float>(std::rand() % 256) / 512.0f);
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Pentagon::CANVI_ANGLE_PROB) {
|
||||||
int rand2 = (std::rand() % 3) - 1;
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||||
angle_ += rand1 * static_cast<float>(rand2);
|
Defaults::Enemies::Pentagon::CANVI_ANGLE_MAX;
|
||||||
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::comportament_quadrat(float delta_time) {
|
||||||
|
// Quadrat: perseguidor (tracks player position)
|
||||||
|
|
||||||
|
// Update tracking timer
|
||||||
|
tracking_timer_ += delta_time;
|
||||||
|
|
||||||
|
// Periodically update angle toward ship
|
||||||
|
if (tracking_timer_ >= Defaults::Enemies::Quadrat::TRACKING_INTERVAL) {
|
||||||
|
tracking_timer_ = 0.0f;
|
||||||
|
|
||||||
|
if (ship_position_) {
|
||||||
|
// Calculate angle to ship
|
||||||
|
float dx = ship_position_->x - centre_.x;
|
||||||
|
float dy = ship_position_->y - centre_.y;
|
||||||
|
float target_angle = std::atan2(dy, dx) + Constants::PI / 2.0f;
|
||||||
|
|
||||||
|
// Interpolate toward target angle
|
||||||
|
float angle_diff = target_angle - angle_;
|
||||||
|
|
||||||
|
// Normalize angle difference to [-π, π]
|
||||||
|
while (angle_diff > Constants::PI) angle_diff -= 2.0f * Constants::PI;
|
||||||
|
while (angle_diff < -Constants::PI) angle_diff += 2.0f * Constants::PI;
|
||||||
|
|
||||||
|
// Apply tracking strength (uses member variable, defaults to 0.5)
|
||||||
|
angle_ += angle_diff * tracking_strength_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nota: La rotació visual (rotacio_ += drotacio_) ja es fa a actualitzar()
|
// Move in current direction
|
||||||
|
float velocitat_efectiva = velocitat_ * delta_time;
|
||||||
|
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||||
|
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||||
|
|
||||||
|
float new_y = centre_.y + dy;
|
||||||
|
float new_x = centre_.x + dx;
|
||||||
|
|
||||||
|
// Obtenir límits segurs
|
||||||
|
float min_x, max_x, min_y, max_y;
|
||||||
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||||
|
min_x,
|
||||||
|
max_x,
|
||||||
|
min_y,
|
||||||
|
max_y);
|
||||||
|
|
||||||
|
// Bounce on walls (simple reflection)
|
||||||
|
if (new_y >= min_y && new_y <= max_y) {
|
||||||
|
centre_.y = new_y;
|
||||||
|
} else {
|
||||||
|
angle_ = -angle_; // Vertical reflection
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_x >= min_x && new_x <= max_x) {
|
||||||
|
centre_.x = new_x;
|
||||||
|
} else {
|
||||||
|
angle_ = Constants::PI - angle_; // Horizontal reflection
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::comportament_molinillo(float delta_time) {
|
||||||
|
// Molinillo: agressiu (fast, straight lines, proximity spin-up)
|
||||||
|
|
||||||
|
// Check proximity to ship for spin-up effect
|
||||||
|
if (ship_position_) {
|
||||||
|
float dx = ship_position_->x - centre_.x;
|
||||||
|
float dy = ship_position_->y - centre_.y;
|
||||||
|
float distance = std::sqrt(dx * dx + dy * dy);
|
||||||
|
|
||||||
|
if (distance < Defaults::Enemies::Molinillo::PROXIMITY_DISTANCE) {
|
||||||
|
// Temporarily boost rotation speed when near ship
|
||||||
|
float boost = Defaults::Enemies::Molinillo::DROTACIO_PROXIMITY_MULTIPLIER;
|
||||||
|
drotacio_ = animacio_.drotacio_base * boost;
|
||||||
|
} else {
|
||||||
|
// Normal rotation speed
|
||||||
|
drotacio_ = animacio_.drotacio_base;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fast straight-line movement
|
||||||
|
float velocitat_efectiva = velocitat_ * delta_time;
|
||||||
|
float dy = velocitat_efectiva * std::sin(angle_ - Constants::PI / 2.0f);
|
||||||
|
float dx = velocitat_efectiva * std::cos(angle_ - Constants::PI / 2.0f);
|
||||||
|
|
||||||
|
float new_y = centre_.y + dy;
|
||||||
|
float new_x = centre_.x + dx;
|
||||||
|
|
||||||
|
// Obtenir límits segurs
|
||||||
|
float min_x, max_x, min_y, max_y;
|
||||||
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||||
|
min_x,
|
||||||
|
max_x,
|
||||||
|
min_y,
|
||||||
|
max_y);
|
||||||
|
|
||||||
|
// Rare angle changes on wall hits
|
||||||
|
if (new_y >= min_y && new_y <= max_y) {
|
||||||
|
centre_.y = new_y;
|
||||||
|
} else {
|
||||||
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Molinillo::CANVI_ANGLE_PROB) {
|
||||||
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||||
|
Defaults::Enemies::Molinillo::CANVI_ANGLE_MAX;
|
||||||
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_x >= min_x && new_x <= max_x) {
|
||||||
|
centre_.x = new_x;
|
||||||
|
} else {
|
||||||
|
if (static_cast<float>(std::rand()) / RAND_MAX < Defaults::Enemies::Molinillo::CANVI_ANGLE_PROB) {
|
||||||
|
float rand_angle = (static_cast<float>(std::rand()) / RAND_MAX) *
|
||||||
|
Defaults::Enemies::Molinillo::CANVI_ANGLE_MAX;
|
||||||
|
angle_ += (std::rand() % 2 == 0) ? rand_angle : -rand_angle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::actualitzar_animacio(float delta_time) {
|
||||||
|
actualitzar_palpitacio(delta_time);
|
||||||
|
actualitzar_rotacio_accelerada(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::actualitzar_palpitacio(float delta_time) {
|
||||||
|
if (animacio_.palpitacio_activa) {
|
||||||
|
// Advance phase (2π * frequency * dt)
|
||||||
|
animacio_.palpitacio_fase += 2.0f * Constants::PI * animacio_.palpitacio_frequencia * delta_time;
|
||||||
|
|
||||||
|
// Decrement timer
|
||||||
|
animacio_.palpitacio_temps_restant -= delta_time;
|
||||||
|
|
||||||
|
// Deactivate when timer expires
|
||||||
|
if (animacio_.palpitacio_temps_restant <= 0.0f) {
|
||||||
|
animacio_.palpitacio_activa = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Random trigger (probability per second)
|
||||||
|
float rand_val = static_cast<float>(std::rand()) / RAND_MAX;
|
||||||
|
float trigger_prob = Defaults::Enemies::Animation::PALPITACIO_TRIGGER_PROB * delta_time;
|
||||||
|
|
||||||
|
if (rand_val < trigger_prob) {
|
||||||
|
// Activate palpitation
|
||||||
|
animacio_.palpitacio_activa = true;
|
||||||
|
animacio_.palpitacio_fase = 0.0f;
|
||||||
|
|
||||||
|
// Randomize parameters
|
||||||
|
float freq_range = Defaults::Enemies::Animation::PALPITACIO_FREQ_MAX -
|
||||||
|
Defaults::Enemies::Animation::PALPITACIO_FREQ_MIN;
|
||||||
|
animacio_.palpitacio_frequencia = Defaults::Enemies::Animation::PALPITACIO_FREQ_MIN +
|
||||||
|
(static_cast<float>(std::rand()) / RAND_MAX) * freq_range;
|
||||||
|
|
||||||
|
float amp_range = Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MAX -
|
||||||
|
Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MIN;
|
||||||
|
animacio_.palpitacio_amplitud = Defaults::Enemies::Animation::PALPITACIO_AMPLITUD_MIN +
|
||||||
|
(static_cast<float>(std::rand()) / RAND_MAX) * amp_range;
|
||||||
|
|
||||||
|
float dur_range = Defaults::Enemies::Animation::PALPITACIO_DURACIO_MAX -
|
||||||
|
Defaults::Enemies::Animation::PALPITACIO_DURACIO_MIN;
|
||||||
|
animacio_.palpitacio_temps_restant = Defaults::Enemies::Animation::PALPITACIO_DURACIO_MIN +
|
||||||
|
(static_cast<float>(std::rand()) / RAND_MAX) * dur_range;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::actualitzar_rotacio_accelerada(float delta_time) {
|
||||||
|
if (animacio_.drotacio_t < 1.0f) {
|
||||||
|
// Transitioning to new target
|
||||||
|
animacio_.drotacio_t += delta_time / animacio_.drotacio_duracio;
|
||||||
|
|
||||||
|
if (animacio_.drotacio_t >= 1.0f) {
|
||||||
|
animacio_.drotacio_t = 1.0f;
|
||||||
|
animacio_.drotacio_base = animacio_.drotacio_objetivo; // Reached target
|
||||||
|
drotacio_ = animacio_.drotacio_base;
|
||||||
|
} else {
|
||||||
|
// Smoothstep interpolation: t² * (3 - 2t)
|
||||||
|
float t = animacio_.drotacio_t;
|
||||||
|
float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||||
|
|
||||||
|
// Interpolate between base and target
|
||||||
|
float initial = animacio_.drotacio_base;
|
||||||
|
float target = animacio_.drotacio_objetivo;
|
||||||
|
drotacio_ = initial + (target - initial) * smooth_t;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Random trigger for new acceleration
|
||||||
|
float rand_val = static_cast<float>(std::rand()) / RAND_MAX;
|
||||||
|
float trigger_prob = Defaults::Enemies::Animation::ROTACIO_ACCEL_TRIGGER_PROB * delta_time;
|
||||||
|
|
||||||
|
if (rand_val < trigger_prob) {
|
||||||
|
// Start new transition
|
||||||
|
animacio_.drotacio_t = 0.0f;
|
||||||
|
|
||||||
|
// Randomize target speed (multiplier * base)
|
||||||
|
float mult_range = Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MAX -
|
||||||
|
Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MIN;
|
||||||
|
float multiplier = Defaults::Enemies::Animation::ROTACIO_ACCEL_MULTIPLIER_MIN +
|
||||||
|
(static_cast<float>(std::rand()) / RAND_MAX) * mult_range;
|
||||||
|
|
||||||
|
animacio_.drotacio_objetivo = animacio_.drotacio_base * multiplier;
|
||||||
|
|
||||||
|
// Randomize duration
|
||||||
|
float dur_range = Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MAX -
|
||||||
|
Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MIN;
|
||||||
|
animacio_.drotacio_duracio = Defaults::Enemies::Animation::ROTACIO_ACCEL_DURACIO_MIN +
|
||||||
|
(static_cast<float>(std::rand()) / RAND_MAX) * dur_range;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float Enemic::calcular_escala_actual() const {
|
||||||
|
float escala = 1.0f;
|
||||||
|
|
||||||
|
// [NEW] Invulnerability LERP prioritza sobre palpitació
|
||||||
|
if (timer_invulnerabilitat_ > 0.0f) {
|
||||||
|
// Calculate t: 0.0 at spawn → 1.0 at end
|
||||||
|
float t_inv = timer_invulnerabilitat_ / Defaults::Enemies::Spawn::INVULNERABILITY_DURATION;
|
||||||
|
float t = 1.0f - t_inv; // 0.0 → 1.0
|
||||||
|
|
||||||
|
// Apply smoothstep: t² * (3 - 2t)
|
||||||
|
float smooth_t = t * t * (3.0f - 2.0f * t);
|
||||||
|
|
||||||
|
// LERP scale from 0.0 to 1.0
|
||||||
|
constexpr float START = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_START;
|
||||||
|
constexpr float END = Defaults::Enemies::Spawn::INVULNERABILITY_SCALE_END;
|
||||||
|
escala = START + (END - START) * smooth_t;
|
||||||
|
} else if (animacio_.palpitacio_activa) {
|
||||||
|
// [EXISTING] Palpitació només quan no invulnerable
|
||||||
|
escala += animacio_.palpitacio_amplitud * std::sin(animacio_.palpitacio_fase);
|
||||||
|
}
|
||||||
|
|
||||||
|
return escala;
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Stage system API implementations
|
||||||
|
|
||||||
|
float Enemic::get_base_velocity() const {
|
||||||
|
switch (tipus_) {
|
||||||
|
case TipusEnemic::PENTAGON:
|
||||||
|
return Defaults::Enemies::Pentagon::VELOCITAT;
|
||||||
|
case TipusEnemic::QUADRAT:
|
||||||
|
return Defaults::Enemies::Quadrat::VELOCITAT;
|
||||||
|
case TipusEnemic::MOLINILLO:
|
||||||
|
return Defaults::Enemies::Molinillo::VELOCITAT;
|
||||||
|
}
|
||||||
|
return 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
float Enemic::get_base_rotation() const {
|
||||||
|
// Return the base rotation speed (drotacio_base if available, otherwise current drotacio_)
|
||||||
|
return animacio_.drotacio_base != 0.0f ? animacio_.drotacio_base : drotacio_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Enemic::set_tracking_strength(float strength) {
|
||||||
|
// Only applies to QUADRAT type
|
||||||
|
if (tipus_ == TipusEnemic::QUADRAT) {
|
||||||
|
tracking_strength_ = strength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Safe spawn helper - checks if position is away from ship
|
||||||
|
bool Enemic::intent_spawn_safe(const Punt& ship_pos, float& out_x, float& out_y) {
|
||||||
|
// Generate random position within safe bounds
|
||||||
|
float min_x, max_x, min_y, max_y;
|
||||||
|
Constants::obtenir_limits_zona_segurs(Defaults::Entities::ENEMY_RADIUS,
|
||||||
|
min_x,
|
||||||
|
max_x,
|
||||||
|
min_y,
|
||||||
|
max_y);
|
||||||
|
|
||||||
|
int range_x = static_cast<int>(max_x - min_x);
|
||||||
|
int range_y = static_cast<int>(max_y - min_y);
|
||||||
|
|
||||||
|
out_x = static_cast<float>((std::rand() % range_x) + static_cast<int>(min_x));
|
||||||
|
out_y = static_cast<float>((std::rand() % range_y) + static_cast<int>(min_y));
|
||||||
|
|
||||||
|
// Check Euclidean distance to ship
|
||||||
|
float dx = out_x - ship_pos.x;
|
||||||
|
float dy = out_y - ship_pos.y;
|
||||||
|
float distancia = std::sqrt(dx * dx + dy * dy);
|
||||||
|
|
||||||
|
// Return true if position is safe (>= 36px from ship)
|
||||||
|
return distancia >= Defaults::Enemies::Spawn::SAFETY_DISTANCE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,30 @@
|
|||||||
|
|
||||||
#include "core/graphics/shape.hpp"
|
#include "core/graphics/shape.hpp"
|
||||||
#include "core/types.hpp"
|
#include "core/types.hpp"
|
||||||
|
#include "game/constants.hpp"
|
||||||
|
|
||||||
|
// Tipus d'enemic
|
||||||
|
enum class TipusEnemic : uint8_t {
|
||||||
|
PENTAGON = 0, // Pentàgon esquivador (zigzag)
|
||||||
|
QUADRAT = 1, // Quadrat perseguidor (tracks ship)
|
||||||
|
MOLINILLO = 2 // Molinillo agressiu (fast, spinning)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estat d'animació (palpitació i rotació accelerada)
|
||||||
|
struct AnimacioEnemic {
|
||||||
|
// Palpitation (breathing effect)
|
||||||
|
bool palpitacio_activa = false;
|
||||||
|
float palpitacio_fase = 0.0f; // Phase in cycle (0.0-2π)
|
||||||
|
float palpitacio_frequencia = 2.0f; // Hz (cycles per second)
|
||||||
|
float palpitacio_amplitud = 0.15f; // Scale variation (±15%)
|
||||||
|
float palpitacio_temps_restant = 0.0f; // Time remaining (seconds)
|
||||||
|
|
||||||
|
// Rotation acceleration (long-term spin modulation)
|
||||||
|
float drotacio_base = 0.0f; // Base rotation speed (rad/s)
|
||||||
|
float drotacio_objetivo = 0.0f; // Target rotation speed (rad/s)
|
||||||
|
float drotacio_t = 0.0f; // Interpolation progress (0.0-1.0)
|
||||||
|
float drotacio_duracio = 0.0f; // Duration of transition (seconds)
|
||||||
|
};
|
||||||
|
|
||||||
class Enemic {
|
class Enemic {
|
||||||
public:
|
public:
|
||||||
@@ -16,7 +40,7 @@ class Enemic {
|
|||||||
: renderer_(nullptr) {}
|
: renderer_(nullptr) {}
|
||||||
Enemic(SDL_Renderer* renderer);
|
Enemic(SDL_Renderer* renderer);
|
||||||
|
|
||||||
void inicialitzar();
|
void inicialitzar(TipusEnemic tipus = TipusEnemic::PENTAGON, const Punt* ship_pos = nullptr);
|
||||||
void actualitzar(float delta_time);
|
void actualitzar(float delta_time);
|
||||||
void dibuixar() const;
|
void dibuixar() const;
|
||||||
|
|
||||||
@@ -25,6 +49,31 @@ class Enemic {
|
|||||||
const Punt& get_centre() const { return centre_; }
|
const Punt& get_centre() const { return centre_; }
|
||||||
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
|
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
|
||||||
void destruir() { esta_ = false; }
|
void destruir() { esta_ = false; }
|
||||||
|
float get_brightness() const { return brightness_; }
|
||||||
|
float get_drotacio() const { return drotacio_; }
|
||||||
|
Punt get_velocitat_vector() const {
|
||||||
|
return {
|
||||||
|
velocitat_ * std::cos(angle_ - Constants::PI / 2.0f),
|
||||||
|
velocitat_ * std::sin(angle_ - Constants::PI / 2.0f)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set ship position reference for tracking behavior
|
||||||
|
void set_ship_position(const Punt* ship_pos) { ship_position_ = ship_pos; }
|
||||||
|
|
||||||
|
// [NEW] Getters for stage system (base stats)
|
||||||
|
float get_base_velocity() const;
|
||||||
|
float get_base_rotation() const;
|
||||||
|
TipusEnemic get_tipus() const { return tipus_; }
|
||||||
|
|
||||||
|
// [NEW] Setters for difficulty multipliers (stage system)
|
||||||
|
void set_velocity(float vel) { velocitat_ = vel; }
|
||||||
|
void set_rotation(float rot) { drotacio_ = rot; animacio_.drotacio_base = rot; }
|
||||||
|
void set_tracking_strength(float strength);
|
||||||
|
|
||||||
|
// [NEW] Invulnerability queries
|
||||||
|
bool es_invulnerable() const { return timer_invulnerabilitat_ > 0.0f; }
|
||||||
|
float get_temps_invulnerabilitat() const { return timer_invulnerabilitat_; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_Renderer* renderer_;
|
SDL_Renderer* renderer_;
|
||||||
@@ -34,12 +83,37 @@ class Enemic {
|
|||||||
|
|
||||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||||
Punt centre_;
|
Punt centre_;
|
||||||
float angle_; // Angle de moviment
|
float angle_; // Angle de moviment
|
||||||
float velocitat_;
|
float velocitat_;
|
||||||
float drotacio_; // Delta rotació visual (rad/s)
|
float drotacio_; // Delta rotació visual (rad/s)
|
||||||
float rotacio_; // Rotació visual acumulada
|
float rotacio_; // Rotació visual acumulada
|
||||||
bool esta_;
|
bool esta_;
|
||||||
float brightness_; // Factor de brillantor (0.0-1.0)
|
float brightness_; // Factor de brillantor (0.0-1.0)
|
||||||
|
|
||||||
|
// [NEW] Enemy type and configuration
|
||||||
|
TipusEnemic tipus_;
|
||||||
|
|
||||||
|
// [NEW] Animation state
|
||||||
|
AnimacioEnemic animacio_;
|
||||||
|
|
||||||
|
// [NEW] Behavior state (type-specific)
|
||||||
|
float tracking_timer_; // For Quadrat: time since last angle update
|
||||||
|
const Punt* ship_position_; // Pointer to ship position (for tracking)
|
||||||
|
float tracking_strength_; // For Quadrat: tracking intensity (0.0-1.5), default 0.5
|
||||||
|
|
||||||
|
// [NEW] Invulnerability state
|
||||||
|
float timer_invulnerabilitat_; // Countdown timer (seconds), 0.0f = vulnerable
|
||||||
|
|
||||||
|
// [EXISTING] Private methods
|
||||||
void mou(float delta_time);
|
void mou(float delta_time);
|
||||||
|
|
||||||
|
// [NEW] Private methods
|
||||||
|
void actualitzar_animacio(float delta_time);
|
||||||
|
void actualitzar_palpitacio(float delta_time);
|
||||||
|
void actualitzar_rotacio_accelerada(float delta_time);
|
||||||
|
void comportament_pentagon(float delta_time);
|
||||||
|
void comportament_quadrat(float delta_time);
|
||||||
|
void comportament_molinillo(float delta_time);
|
||||||
|
float calcular_escala_actual() const; // Returns scale with palpitation applied
|
||||||
|
bool intent_spawn_safe(const Punt& ship_pos, float& out_x, float& out_y);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,14 +22,14 @@ Nau::Nau(SDL_Renderer* renderer)
|
|||||||
esta_tocada_(false),
|
esta_tocada_(false),
|
||||||
brightness_(Defaults::Brightness::NAU) {
|
brightness_(Defaults::Brightness::NAU) {
|
||||||
// [NUEVO] Carregar forma compartida des de fitxer
|
// [NUEVO] Carregar forma compartida des de fitxer
|
||||||
forma_ = Graphics::ShapeLoader::load("ship.shp");
|
forma_ = Graphics::ShapeLoader::load("ship2.shp");
|
||||||
|
|
||||||
if (!forma_ || !forma_->es_valida()) {
|
if (!forma_ || !forma_->es_valida()) {
|
||||||
std::cerr << "[Nau] Error: no s'ha pogut carregar ship.shp" << std::endl;
|
std::cerr << "[Nau] Error: no s'ha pogut carregar ship.shp" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Nau::inicialitzar() {
|
void Nau::inicialitzar(const Punt* spawn_point) {
|
||||||
// Inicialització de la nau (triangle)
|
// Inicialització de la nau (triangle)
|
||||||
// Basat en el codi Pascal original: lines 380-384
|
// Basat en el codi Pascal original: lines 380-384
|
||||||
// Copiat de joc_asteroides.cpp línies 30-44
|
// Copiat de joc_asteroides.cpp línies 30-44
|
||||||
@@ -37,11 +37,17 @@ void Nau::inicialitzar() {
|
|||||||
// [NUEVO] Ja no cal configurar punts polars - la geometria es carrega del
|
// [NUEVO] Ja no cal configurar punts polars - la geometria es carrega del
|
||||||
// fitxer Només inicialitzem l'estat de la instància
|
// fitxer Només inicialitzem l'estat de la instància
|
||||||
|
|
||||||
// Posició inicial al centre de l'àrea de joc
|
// Use custom spawn point if provided, otherwise use center
|
||||||
float centre_x, centre_y;
|
if (spawn_point) {
|
||||||
Constants::obtenir_centre_zona(centre_x, centre_y);
|
centre_.x = spawn_point->x;
|
||||||
centre_.x = centre_x; // 320
|
centre_.y = spawn_point->y;
|
||||||
centre_.y = centre_y; // 213 (not 240!)
|
} else {
|
||||||
|
// Default: center of play area
|
||||||
|
float centre_x, centre_y;
|
||||||
|
Constants::obtenir_centre_zona(centre_x, centre_y);
|
||||||
|
centre_.x = static_cast<int>(centre_x);
|
||||||
|
centre_.y = static_cast<int>(centre_y);
|
||||||
|
}
|
||||||
|
|
||||||
// Estat inicial
|
// Estat inicial
|
||||||
angle_ = 0.0f;
|
angle_ = 0.0f;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "core/graphics/shape.hpp"
|
#include "core/graphics/shape.hpp"
|
||||||
#include "core/types.hpp"
|
#include "core/types.hpp"
|
||||||
|
#include "game/constants.hpp"
|
||||||
|
|
||||||
class Nau {
|
class Nau {
|
||||||
public:
|
public:
|
||||||
@@ -16,7 +17,7 @@ class Nau {
|
|||||||
: renderer_(nullptr) {}
|
: renderer_(nullptr) {}
|
||||||
Nau(SDL_Renderer* renderer);
|
Nau(SDL_Renderer* renderer);
|
||||||
|
|
||||||
void inicialitzar();
|
void inicialitzar(const Punt* spawn_point = nullptr);
|
||||||
void processar_input(float delta_time);
|
void processar_input(float delta_time);
|
||||||
void actualitzar(float delta_time);
|
void actualitzar(float delta_time);
|
||||||
void dibuixar() const;
|
void dibuixar() const;
|
||||||
@@ -25,6 +26,14 @@ class Nau {
|
|||||||
const Punt& get_centre() const { return centre_; }
|
const Punt& get_centre() const { return centre_; }
|
||||||
float get_angle() const { return angle_; }
|
float get_angle() const { return angle_; }
|
||||||
bool esta_viva() const { return !esta_tocada_; }
|
bool esta_viva() const { return !esta_tocada_; }
|
||||||
|
const std::shared_ptr<Graphics::Shape>& get_forma() const { return forma_; }
|
||||||
|
float get_brightness() const { return brightness_; }
|
||||||
|
Punt get_velocitat_vector() const {
|
||||||
|
return {
|
||||||
|
velocitat_ * std::cos(angle_ - Constants::PI / 2.0f),
|
||||||
|
velocitat_ * std::sin(angle_ - Constants::PI / 2.0f)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Col·lisions (Fase 10)
|
// Col·lisions (Fase 10)
|
||||||
void marcar_tocada() { esta_tocada_ = true; }
|
void marcar_tocada() { esta_tocada_ = true; }
|
||||||
@@ -38,8 +47,8 @@ class Nau {
|
|||||||
|
|
||||||
// [NUEVO] Estat de la instància (separat de la geometria)
|
// [NUEVO] Estat de la instància (separat de la geometria)
|
||||||
Punt centre_;
|
Punt centre_;
|
||||||
float angle_; // Angle d'orientació
|
float angle_; // Angle d'orientació
|
||||||
float velocitat_; // Velocitat (px/s)
|
float velocitat_; // Velocitat (px/s)
|
||||||
bool esta_tocada_;
|
bool esta_tocada_;
|
||||||
float brightness_; // Factor de brillantor (0.0-1.0)
|
float brightness_; // Factor de brillantor (0.0-1.0)
|
||||||
|
|
||||||
|
|||||||
@@ -13,15 +13,28 @@
|
|||||||
#include "core/audio/audio.hpp"
|
#include "core/audio/audio.hpp"
|
||||||
#include "core/input/mouse.hpp"
|
#include "core/input/mouse.hpp"
|
||||||
#include "core/rendering/line_renderer.hpp"
|
#include "core/rendering/line_renderer.hpp"
|
||||||
#include "core/system/gestor_escenes.hpp"
|
#include "core/system/context_escenes.hpp"
|
||||||
#include "core/system/global_events.hpp"
|
#include "core/system/global_events.hpp"
|
||||||
|
#include "game/stage_system/stage_loader.hpp"
|
||||||
|
|
||||||
EscenaJoc::EscenaJoc(SDLManager& sdl)
|
// Using declarations per simplificar el codi
|
||||||
|
using GestorEscenes::ContextEscenes;
|
||||||
|
using Escena = ContextEscenes::Escena;
|
||||||
|
using Opcio = ContextEscenes::Opcio;
|
||||||
|
|
||||||
|
EscenaJoc::EscenaJoc(SDLManager& sdl, ContextEscenes& context)
|
||||||
: sdl_(sdl),
|
: sdl_(sdl),
|
||||||
|
context_(context),
|
||||||
debris_manager_(sdl.obte_renderer()),
|
debris_manager_(sdl.obte_renderer()),
|
||||||
|
gestor_puntuacio_(sdl.obte_renderer()),
|
||||||
nau_(sdl.obte_renderer()),
|
nau_(sdl.obte_renderer()),
|
||||||
itocado_(0),
|
itocado_(0),
|
||||||
|
puntuacio_total_(0),
|
||||||
text_(sdl.obte_renderer()) {
|
text_(sdl.obte_renderer()) {
|
||||||
|
// Consumir opcions (preparació per MODE_DEMO futur)
|
||||||
|
auto opcio = context_.consumir_opcio();
|
||||||
|
(void)opcio; // Suprimir warning de variable no usada
|
||||||
|
|
||||||
// Inicialitzar bales amb renderer
|
// Inicialitzar bales amb renderer
|
||||||
for (auto& bala : bales_) {
|
for (auto& bala : bales_) {
|
||||||
bala = Bala(sdl.obte_renderer());
|
bala = Bala(sdl.obte_renderer());
|
||||||
@@ -42,7 +55,7 @@ void EscenaJoc::executar() {
|
|||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
Uint64 last_time = SDL_GetTicks();
|
Uint64 last_time = SDL_GetTicks();
|
||||||
|
|
||||||
while (GestorEscenes::actual == GestorEscenes::Escena::JOC) {
|
while (GestorEscenes::actual == Escena::JOC) {
|
||||||
// Calcular delta_time real
|
// Calcular delta_time real
|
||||||
Uint64 current_time = SDL_GetTicks();
|
Uint64 current_time = SDL_GetTicks();
|
||||||
float delta_time = (current_time - last_time) / 1000.0f;
|
float delta_time = (current_time - last_time) / 1000.0f;
|
||||||
@@ -67,7 +80,7 @@ void EscenaJoc::executar() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Events globals (F1/F2/F3/ESC/QUIT)
|
// Events globals (F1/F2/F3/ESC/QUIT)
|
||||||
if (GlobalEvents::handle(event, sdl_)) {
|
if (GlobalEvents::handle(event, sdl_, context_)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,70 +118,312 @@ void EscenaJoc::inicialitzar() {
|
|||||||
// Basat en el codi Pascal original: line 376
|
// Basat en el codi Pascal original: line 376
|
||||||
std::srand(static_cast<unsigned>(std::time(nullptr)));
|
std::srand(static_cast<unsigned>(std::time(nullptr)));
|
||||||
|
|
||||||
|
// [NEW] Load stage configuration (only once)
|
||||||
|
if (!stage_config_) {
|
||||||
|
stage_config_ = StageSystem::StageLoader::carregar("data/stages/stages.yaml");
|
||||||
|
if (!stage_config_) {
|
||||||
|
std::cerr << "[EscenaJoc] Error: no s'ha pogut carregar stages.yaml" << std::endl;
|
||||||
|
// Continue without stage system (will crash, but helps debugging)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Initialize stage manager
|
||||||
|
stage_manager_ = std::make_unique<StageSystem::StageManager>(stage_config_.get());
|
||||||
|
stage_manager_->inicialitzar();
|
||||||
|
|
||||||
|
// [NEW] Set ship position reference for safe spawn
|
||||||
|
stage_manager_->get_spawn_controller().set_ship_position(&nau_.get_centre());
|
||||||
|
|
||||||
// Inicialitzar estat de col·lisió
|
// Inicialitzar estat de col·lisió
|
||||||
itocado_ = 0;
|
itocado_ = 0;
|
||||||
|
|
||||||
|
// Initialize lives and game over state
|
||||||
|
num_vides_ = Defaults::Game::STARTING_LIVES;
|
||||||
|
game_over_ = false;
|
||||||
|
game_over_timer_ = 0.0f;
|
||||||
|
|
||||||
|
// Initialize score
|
||||||
|
puntuacio_total_ = 0;
|
||||||
|
gestor_puntuacio_.reiniciar();
|
||||||
|
|
||||||
|
// Set spawn point to center of play area
|
||||||
|
Constants::obtenir_centre_zona(punt_spawn_.x, punt_spawn_.y);
|
||||||
|
|
||||||
// Inicialitzar nau
|
// Inicialitzar nau
|
||||||
nau_.inicialitzar();
|
nau_.inicialitzar();
|
||||||
|
|
||||||
// Inicialitzar enemics (ORNIs)
|
// [MODIFIED] Initialize enemies as inactive (stage system will spawn them)
|
||||||
for (auto& enemy : orni_) {
|
for (auto& enemy : orni_) {
|
||||||
enemy.inicialitzar();
|
enemy = Enemic(sdl_.obte_renderer());
|
||||||
|
enemy.set_ship_position(&nau_.get_centre()); // Set ship reference for tracking
|
||||||
|
// DON'T call enemy.inicialitzar() here - stage system handles spawning
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inicialitzar bales
|
// Inicialitzar bales
|
||||||
for (auto& bala : bales_) {
|
for (auto& bala : bales_) {
|
||||||
bala.inicialitzar();
|
bala.inicialitzar();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iniciar música de joc (sense stopMusic, ja s'ha parat en destructor de TITOL)
|
||||||
|
Audio::get()->playMusic("game.ogg");
|
||||||
}
|
}
|
||||||
|
|
||||||
void EscenaJoc::actualitzar(float delta_time) {
|
void EscenaJoc::actualitzar(float delta_time) {
|
||||||
// Actualitzar nau (input + física)
|
// Check game over state first
|
||||||
nau_.processar_input(delta_time);
|
if (game_over_) {
|
||||||
nau_.actualitzar(delta_time);
|
// Game over: only update timer, enemies, bullets, and debris
|
||||||
|
game_over_timer_ -= delta_time;
|
||||||
|
|
||||||
// Actualitzar moviment i rotació dels enemics (ORNIs)
|
if (game_over_timer_ <= 0.0f) {
|
||||||
for (auto& enemy : orni_) {
|
// Aturar música de joc abans de tornar al títol
|
||||||
enemy.actualitzar(delta_time);
|
Audio::get()->stopMusic();
|
||||||
|
// Transició a pantalla de títol
|
||||||
|
context_.canviar_escena(Escena::TITOL);
|
||||||
|
GestorEscenes::actual = Escena::TITOL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enemies and bullets continue moving during game over
|
||||||
|
for (auto& enemy : orni_) {
|
||||||
|
enemy.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& bala : bales_) {
|
||||||
|
bala.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
debris_manager_.actualitzar(delta_time);
|
||||||
|
gestor_puntuacio_.actualitzar(delta_time);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actualitzar moviment de bales (Fase 9)
|
// Check death sequence state
|
||||||
for (auto& bala : bales_) {
|
if (itocado_ > 0.0f) {
|
||||||
bala.actualitzar(delta_time);
|
// Death sequence active: update timer
|
||||||
|
itocado_ += delta_time;
|
||||||
|
|
||||||
|
// Check if death duration completed
|
||||||
|
if (itocado_ >= Defaults::Game::DEATH_DURATION) {
|
||||||
|
// *** PHASE 3: RESPAWN OR GAME OVER ***
|
||||||
|
|
||||||
|
// Decrement lives
|
||||||
|
num_vides_--;
|
||||||
|
|
||||||
|
if (num_vides_ > 0) {
|
||||||
|
// Respawn ship
|
||||||
|
nau_.inicialitzar(&punt_spawn_);
|
||||||
|
itocado_ = 0.0f;
|
||||||
|
} else {
|
||||||
|
// Game over
|
||||||
|
game_over_ = true;
|
||||||
|
game_over_timer_ = Defaults::Game::GAME_OVER_DURATION;
|
||||||
|
itocado_ = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enemies and bullets continue moving during death sequence
|
||||||
|
for (auto& enemy : orni_) {
|
||||||
|
enemy.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& bala : bales_) {
|
||||||
|
bala.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
debris_manager_.actualitzar(delta_time);
|
||||||
|
gestor_puntuacio_.actualitzar(delta_time);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detectar col·lisions bala-enemic (Fase 10)
|
// *** STAGE SYSTEM STATE MACHINE ***
|
||||||
detectar_col·lisions_bales_enemics();
|
|
||||||
|
|
||||||
// Actualitzar fragments d'explosions
|
StageSystem::EstatStage estat = stage_manager_->get_estat();
|
||||||
debris_manager_.actualitzar(delta_time);
|
|
||||||
|
switch (estat) {
|
||||||
|
case StageSystem::EstatStage::LEVEL_START:
|
||||||
|
// Update countdown timer
|
||||||
|
stage_manager_->actualitzar(delta_time);
|
||||||
|
|
||||||
|
// [NEW] Allow ship movement and shooting during intro
|
||||||
|
nau_.processar_input(delta_time);
|
||||||
|
nau_.actualitzar(delta_time);
|
||||||
|
|
||||||
|
// [NEW] Update bullets
|
||||||
|
for (auto& bala : bales_) {
|
||||||
|
bala.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Update debris
|
||||||
|
debris_manager_.actualitzar(delta_time);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case StageSystem::EstatStage::PLAYING: {
|
||||||
|
// [NEW] Update stage manager (spawns enemies, pass pause flag)
|
||||||
|
bool pausar_spawn = (itocado_ > 0.0f); // Pause during death animation
|
||||||
|
stage_manager_->get_spawn_controller().actualitzar(delta_time, orni_, pausar_spawn);
|
||||||
|
|
||||||
|
// [NEW] Check stage completion (only when not in death sequence)
|
||||||
|
if (itocado_ == 0.0f) {
|
||||||
|
auto& spawn_ctrl = stage_manager_->get_spawn_controller();
|
||||||
|
if (spawn_ctrl.tots_enemics_destruits(orni_)) {
|
||||||
|
stage_manager_->stage_completat();
|
||||||
|
Audio::get()->playSound(Defaults::Sound::GOOD_JOB_COMMANDER, Audio::Group::GAME);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [EXISTING] Normal gameplay
|
||||||
|
nau_.processar_input(delta_time);
|
||||||
|
nau_.actualitzar(delta_time);
|
||||||
|
|
||||||
|
for (auto& enemy : orni_) {
|
||||||
|
enemy.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& bala : bales_) {
|
||||||
|
bala.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
detectar_col·lisions_bales_enemics();
|
||||||
|
detectar_col·lisio_nau_enemics();
|
||||||
|
debris_manager_.actualitzar(delta_time);
|
||||||
|
gestor_puntuacio_.actualitzar(delta_time);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case StageSystem::EstatStage::LEVEL_COMPLETED:
|
||||||
|
// Update countdown timer
|
||||||
|
stage_manager_->actualitzar(delta_time);
|
||||||
|
|
||||||
|
// [NEW] Allow ship movement and shooting during outro
|
||||||
|
nau_.processar_input(delta_time);
|
||||||
|
nau_.actualitzar(delta_time);
|
||||||
|
|
||||||
|
// [NEW] Update bullets (allow last shots to continue)
|
||||||
|
for (auto& bala : bales_) {
|
||||||
|
bala.actualitzar(delta_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Update debris (from last destroyed enemies)
|
||||||
|
debris_manager_.actualitzar(delta_time);
|
||||||
|
gestor_puntuacio_.actualitzar(delta_time);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EscenaJoc::dibuixar() {
|
void EscenaJoc::dibuixar() {
|
||||||
// Dibuixar marges de la zona de joc
|
// Draw borders (always visible)
|
||||||
dibuixar_marges();
|
dibuixar_marges();
|
||||||
|
|
||||||
// Dibuixar nau
|
// Check game over state
|
||||||
nau_.dibuixar();
|
if (game_over_) {
|
||||||
|
// Game over: draw enemies, bullets, debris, and "GAME OVER" text
|
||||||
|
|
||||||
// Dibuixar ORNIs (enemics)
|
for (const auto& enemy : orni_) {
|
||||||
for (const auto& enemy : orni_) {
|
enemy.dibuixar();
|
||||||
enemy.dibuixar();
|
}
|
||||||
|
|
||||||
|
for (const auto& bala : bales_) {
|
||||||
|
bala.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
debris_manager_.dibuixar();
|
||||||
|
gestor_puntuacio_.dibuixar();
|
||||||
|
|
||||||
|
// Draw centered "GAME OVER" text
|
||||||
|
const std::string game_over_text = "GAME OVER";
|
||||||
|
constexpr float escala = 2.0f;
|
||||||
|
constexpr float spacing = 4.0f;
|
||||||
|
|
||||||
|
float text_width = text_.get_text_width(game_over_text, escala, spacing);
|
||||||
|
float text_height = text_.get_text_height(escala);
|
||||||
|
|
||||||
|
const SDL_FRect& play_area = Defaults::Zones::PLAYAREA;
|
||||||
|
float x = play_area.x + (play_area.w - text_width) / 2.0f;
|
||||||
|
float y = play_area.y + (play_area.h - text_height) / 2.0f;
|
||||||
|
|
||||||
|
text_.render(game_over_text, {x, y}, escala, spacing);
|
||||||
|
|
||||||
|
dibuixar_marcador();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuixar bales (Fase 9)
|
// [NEW] Stage state rendering
|
||||||
for (const auto& bala : bales_) {
|
StageSystem::EstatStage estat = stage_manager_->get_estat();
|
||||||
bala.dibuixar();
|
|
||||||
|
switch (estat) {
|
||||||
|
case StageSystem::EstatStage::LEVEL_START:
|
||||||
|
// [NEW] Draw ship if alive
|
||||||
|
if (itocado_ == 0.0f) {
|
||||||
|
nau_.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Draw bullets
|
||||||
|
for (const auto& bala : bales_) {
|
||||||
|
bala.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Draw debris
|
||||||
|
debris_manager_.dibuixar();
|
||||||
|
gestor_puntuacio_.dibuixar();
|
||||||
|
|
||||||
|
// [EXISTING] Draw intro message and score
|
||||||
|
dibuixar_missatge_stage(stage_manager_->get_missatge_level_start());
|
||||||
|
dibuixar_marcador();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case StageSystem::EstatStage::PLAYING:
|
||||||
|
// [EXISTING] Normal rendering
|
||||||
|
if (itocado_ == 0.0f) {
|
||||||
|
nau_.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& enemy : orni_) {
|
||||||
|
enemy.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& bala : bales_) {
|
||||||
|
bala.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
debris_manager_.dibuixar();
|
||||||
|
gestor_puntuacio_.dibuixar();
|
||||||
|
dibuixar_marcador();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case StageSystem::EstatStage::LEVEL_COMPLETED:
|
||||||
|
// [NEW] Draw ship if alive
|
||||||
|
if (itocado_ == 0.0f) {
|
||||||
|
nau_.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Draw bullets (allow last shots to be visible)
|
||||||
|
for (const auto& bala : bales_) {
|
||||||
|
bala.dibuixar();
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Draw debris (from last destroyed enemies)
|
||||||
|
debris_manager_.dibuixar();
|
||||||
|
gestor_puntuacio_.dibuixar();
|
||||||
|
|
||||||
|
// [EXISTING] Draw completion message and score
|
||||||
|
dibuixar_missatge_stage(StageSystem::Constants::MISSATGE_LEVEL_COMPLETED);
|
||||||
|
dibuixar_marcador();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dibuixar fragments d'explosions (després d'altres objectes)
|
|
||||||
debris_manager_.dibuixar();
|
|
||||||
|
|
||||||
// Dibuixar marcador
|
|
||||||
dibuixar_marcador();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EscenaJoc::processar_input(const SDL_Event& event) {
|
void EscenaJoc::processar_input(const SDL_Event& event) {
|
||||||
|
// Ignore ship controls during game over
|
||||||
|
if (game_over_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore ship controls during death sequence
|
||||||
|
if (itocado_ > 0.0f) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Processament d'input per events puntuals (no continus)
|
// Processament d'input per events puntuals (no continus)
|
||||||
// L'input continu (fletxes) es processa en actualitzar() amb
|
// L'input continu (fletxes) es processa en actualitzar() amb
|
||||||
// SDL_GetKeyboardState()
|
// SDL_GetKeyboardState()
|
||||||
@@ -217,7 +472,41 @@ void EscenaJoc::processar_input(const SDL_Event& event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EscenaJoc::tocado() {
|
void EscenaJoc::tocado() {
|
||||||
// TODO: Implementar seqüència de mort
|
// Death sequence: 3 phases
|
||||||
|
// Phase 1: First call (itocado_ == 0) - trigger explosion
|
||||||
|
// Phase 2: Animation (0 < itocado_ < 3.0s) - debris animation
|
||||||
|
// Phase 3: Respawn or game over (itocado_ >= 3.0s) - handled in actualitzar()
|
||||||
|
|
||||||
|
if (itocado_ == 0.0f) {
|
||||||
|
// *** PHASE 1: TRIGGER DEATH ***
|
||||||
|
|
||||||
|
// Mark ship as dead (stops rendering and input)
|
||||||
|
nau_.marcar_tocada();
|
||||||
|
|
||||||
|
// Create ship explosion
|
||||||
|
const Punt& ship_pos = nau_.get_centre();
|
||||||
|
float ship_angle = nau_.get_angle();
|
||||||
|
Punt vel_nau = nau_.get_velocitat_vector();
|
||||||
|
// Reduir a 80% la velocitat heretada per la nau (més realista)
|
||||||
|
Punt vel_nau_80 = {vel_nau.x * 0.8f, vel_nau.y * 0.8f};
|
||||||
|
|
||||||
|
debris_manager_.explotar(
|
||||||
|
nau_.get_forma(), // Ship shape (3 lines)
|
||||||
|
ship_pos, // Center position
|
||||||
|
ship_angle, // Ship orientation
|
||||||
|
1.0f, // Normal scale
|
||||||
|
Defaults::Physics::Debris::VELOCITAT_BASE, // 80 px/s
|
||||||
|
nau_.get_brightness(), // Heredar brightness
|
||||||
|
vel_nau_80, // Heredar 80% velocitat
|
||||||
|
0.0f, // Nave: trayectorias rectas (sin drotacio)
|
||||||
|
0.0f // Sin herencia visual (rotación aleatoria)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Start death timer (non-zero to avoid re-triggering)
|
||||||
|
itocado_ = 0.001f;
|
||||||
|
}
|
||||||
|
// Phase 2 is automatic (debris updates in actualitzar())
|
||||||
|
// Phase 3 is handled in actualitzar() when itocado_ >= DEATH_DURATION
|
||||||
}
|
}
|
||||||
|
|
||||||
void EscenaJoc::dibuixar_marges() const {
|
void EscenaJoc::dibuixar_marges() const {
|
||||||
@@ -238,8 +527,17 @@ void EscenaJoc::dibuixar_marges() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void EscenaJoc::dibuixar_marcador() {
|
void EscenaJoc::dibuixar_marcador() {
|
||||||
// Text estàtic (hardcoded)
|
// [MODIFIED] Display current stage number from stage manager
|
||||||
const std::string text = "SCORE: 01000 LIFE: 3 LEVEL: 01";
|
uint8_t stage_num = stage_manager_->get_stage_actual();
|
||||||
|
std::string stage_str = (stage_num < 10) ? "0" + std::to_string(stage_num)
|
||||||
|
: std::to_string(stage_num);
|
||||||
|
|
||||||
|
// Format score with padding to 5 digits (e.g., 150 → "00150")
|
||||||
|
std::string score_str = std::to_string(puntuacio_total_);
|
||||||
|
score_str = std::string(5 - std::min(5, static_cast<int>(score_str.length())), '0') + score_str;
|
||||||
|
|
||||||
|
std::string text = "SCORE: " + score_str + " LIFES: " + std::to_string(num_vides_) +
|
||||||
|
" LEVEL: " + stage_str;
|
||||||
|
|
||||||
// Paràmetres de renderització
|
// Paràmetres de renderització
|
||||||
const float escala = 0.85f;
|
const float escala = 0.85f;
|
||||||
@@ -268,7 +566,7 @@ void EscenaJoc::detectar_col·lisions_bales_enemics() {
|
|||||||
constexpr float SUMA_RADIS_QUADRAT = SUMA_RADIS * SUMA_RADIS; // 826.56
|
constexpr float SUMA_RADIS_QUADRAT = SUMA_RADIS * SUMA_RADIS; // 826.56
|
||||||
|
|
||||||
// Velocitat d'explosió reduïda per efecte suau
|
// Velocitat d'explosió reduïda per efecte suau
|
||||||
constexpr float VELOCITAT_EXPLOSIO = 50.0f; // px/s (en lloc de 80.0f per defecte)
|
constexpr float VELOCITAT_EXPLOSIO = 80.0f; // px/s (en lloc de 80.0f per defecte)
|
||||||
|
|
||||||
// Iterar per totes les bales actives
|
// Iterar per totes les bales actives
|
||||||
for (auto& bala : bales_) {
|
for (auto& bala : bales_) {
|
||||||
@@ -284,6 +582,11 @@ void EscenaJoc::detectar_col·lisions_bales_enemics() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// [NEW] Skip collision if enemy is invulnerable
|
||||||
|
if (enemic.es_invulnerable()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const Punt& pos_enemic = enemic.get_centre();
|
const Punt& pos_enemic = enemic.get_centre();
|
||||||
|
|
||||||
// Calcular distància quadrada (evita sqrt)
|
// Calcular distància quadrada (evita sqrt)
|
||||||
@@ -295,16 +598,41 @@ void EscenaJoc::detectar_col·lisions_bales_enemics() {
|
|||||||
if (distancia_quadrada <= SUMA_RADIS_QUADRAT) {
|
if (distancia_quadrada <= SUMA_RADIS_QUADRAT) {
|
||||||
// *** COL·LISIÓ DETECTADA ***
|
// *** COL·LISIÓ DETECTADA ***
|
||||||
|
|
||||||
// 1. Destruir enemic (marca com inactiu)
|
// 1. Calculate score for enemy type
|
||||||
|
int punts = 0;
|
||||||
|
switch (enemic.get_tipus()) {
|
||||||
|
case TipusEnemic::PENTAGON:
|
||||||
|
punts = Defaults::Enemies::Scoring::PENTAGON_SCORE;
|
||||||
|
break;
|
||||||
|
case TipusEnemic::QUADRAT:
|
||||||
|
punts = Defaults::Enemies::Scoring::QUADRAT_SCORE;
|
||||||
|
break;
|
||||||
|
case TipusEnemic::MOLINILLO:
|
||||||
|
punts = Defaults::Enemies::Scoring::MOLINILLO_SCORE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Add to total score
|
||||||
|
puntuacio_total_ += punts;
|
||||||
|
|
||||||
|
// 3. Create floating score number
|
||||||
|
gestor_puntuacio_.crear(punts, pos_enemic);
|
||||||
|
|
||||||
|
// 4. Destruir enemic (marca com inactiu)
|
||||||
enemic.destruir();
|
enemic.destruir();
|
||||||
|
|
||||||
// 2. Crear explosió de fragments
|
// 2. Crear explosió de fragments
|
||||||
|
Punt vel_enemic = enemic.get_velocitat_vector();
|
||||||
debris_manager_.explotar(
|
debris_manager_.explotar(
|
||||||
enemic.get_forma(), // Forma vectorial del pentàgon
|
enemic.get_forma(), // Forma vectorial del pentàgon
|
||||||
pos_enemic, // Posició central
|
pos_enemic, // Posició central
|
||||||
0.0f, // Angle (enemic té rotació interna)
|
0.0f, // Angle (enemic té rotació interna)
|
||||||
1.0f, // Escala normal
|
1.0f, // Escala normal
|
||||||
VELOCITAT_EXPLOSIO // 50 px/s (explosió suau)
|
VELOCITAT_EXPLOSIO, // 50 px/s (explosió suau)
|
||||||
|
enemic.get_brightness(), // Heredar brightness
|
||||||
|
vel_enemic, // Heredar velocitat
|
||||||
|
enemic.get_drotacio(), // Heredar velocitat angular (trayectorias curvas)
|
||||||
|
0.0f // Sin herencia visual (rotación aleatoria)
|
||||||
);
|
);
|
||||||
|
|
||||||
// 3. Desactivar bala
|
// 3. Desactivar bala
|
||||||
@@ -316,3 +644,112 @@ void EscenaJoc::detectar_col·lisions_bales_enemics() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EscenaJoc::detectar_col·lisio_nau_enemics() {
|
||||||
|
// Only check collisions if ship is alive
|
||||||
|
if (!nau_.esta_viva()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generous collision detection (80% hitbox)
|
||||||
|
constexpr float RADI_NAU = Defaults::Entities::SHIP_RADIUS;
|
||||||
|
constexpr float RADI_ENEMIC = Defaults::Entities::ENEMY_RADIUS;
|
||||||
|
constexpr float SUMA_RADIS =
|
||||||
|
(RADI_NAU + RADI_ENEMIC) * Defaults::Game::COLLISION_SHIP_ENEMY_AMPLIFIER;
|
||||||
|
constexpr float SUMA_RADIS_QUADRAT = SUMA_RADIS * SUMA_RADIS;
|
||||||
|
|
||||||
|
const Punt& pos_nau = nau_.get_centre();
|
||||||
|
|
||||||
|
// Check collision with all active enemies
|
||||||
|
for (const auto& enemic : orni_) {
|
||||||
|
if (!enemic.esta_actiu()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Skip collision if enemy is invulnerable
|
||||||
|
if (enemic.es_invulnerable()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Punt& pos_enemic = enemic.get_centre();
|
||||||
|
|
||||||
|
// Calculate squared distance (avoid sqrt)
|
||||||
|
float dx = static_cast<float>(pos_nau.x - pos_enemic.x);
|
||||||
|
float dy = static_cast<float>(pos_nau.y - pos_enemic.y);
|
||||||
|
float distancia_quadrada = dx * dx + dy * dy;
|
||||||
|
|
||||||
|
// Check collision
|
||||||
|
if (distancia_quadrada <= SUMA_RADIS_QUADRAT) {
|
||||||
|
tocado(); // Trigger death sequence
|
||||||
|
return; // Only one collision per frame
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [NEW] Stage system helper methods
|
||||||
|
|
||||||
|
void EscenaJoc::dibuixar_missatge_stage(const std::string& missatge) {
|
||||||
|
constexpr float escala_base = 1.0f;
|
||||||
|
constexpr float spacing = 2.0f;
|
||||||
|
constexpr float max_width_ratio = 0.9f; // 90% del ancho disponible
|
||||||
|
|
||||||
|
const SDL_FRect& play_area = Defaults::Zones::PLAYAREA;
|
||||||
|
const float max_width = play_area.w * max_width_ratio; // 558px
|
||||||
|
|
||||||
|
// ========== TYPEWRITER EFFECT (PARAMETRIZED) ==========
|
||||||
|
// Get state-specific timing configuration
|
||||||
|
float total_time;
|
||||||
|
float typing_ratio;
|
||||||
|
|
||||||
|
if (stage_manager_->get_estat() == StageSystem::EstatStage::LEVEL_START) {
|
||||||
|
total_time = Defaults::Game::LEVEL_START_DURATION;
|
||||||
|
typing_ratio = Defaults::Game::LEVEL_START_TYPING_RATIO;
|
||||||
|
} else { // LEVEL_COMPLETED
|
||||||
|
total_time = Defaults::Game::LEVEL_COMPLETED_DURATION;
|
||||||
|
typing_ratio = Defaults::Game::LEVEL_COMPLETED_TYPING_RATIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate progress from timer (0.0 at start → 1.0 at end)
|
||||||
|
float remaining_time = stage_manager_->get_timer_transicio();
|
||||||
|
float progress = 1.0f - (remaining_time / total_time);
|
||||||
|
|
||||||
|
// Determine how many characters to show
|
||||||
|
size_t visible_chars;
|
||||||
|
|
||||||
|
if (typing_ratio > 0.0f && progress < typing_ratio) {
|
||||||
|
// Typewriter phase: show partial text
|
||||||
|
float typing_progress = progress / typing_ratio; // Normalize to 0.0-1.0
|
||||||
|
visible_chars = static_cast<size_t>(missatge.length() * typing_progress);
|
||||||
|
if (visible_chars == 0 && progress > 0.0f) {
|
||||||
|
visible_chars = 1; // Show at least 1 character after first frame
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Display phase: show complete text
|
||||||
|
// (Either after typing phase, or immediately if typing_ratio == 0.0)
|
||||||
|
visible_chars = missatge.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create partial message (substring for typewriter)
|
||||||
|
std::string partial_message = missatge.substr(0, visible_chars);
|
||||||
|
// ===================================================
|
||||||
|
|
||||||
|
// Calculate text width at base scale (using FULL message for position calculation)
|
||||||
|
float text_width_at_base = text_.get_text_width(missatge, escala_base, spacing);
|
||||||
|
|
||||||
|
// Auto-scale if text exceeds max width
|
||||||
|
float escala = (text_width_at_base <= max_width)
|
||||||
|
? escala_base
|
||||||
|
: max_width / text_width_at_base;
|
||||||
|
|
||||||
|
// Recalculate dimensions with final scale (using FULL message for centering)
|
||||||
|
float full_text_width = text_.get_text_width(missatge, escala, spacing);
|
||||||
|
float text_height = text_.get_text_height(escala);
|
||||||
|
|
||||||
|
// Calculate position as if FULL text was there (for fixed position typewriter)
|
||||||
|
float x = play_area.x + (play_area.w - full_text_width) / 2.0f;
|
||||||
|
float y = play_area.y + (play_area.h * 0.25f) - (text_height / 2.0f);
|
||||||
|
|
||||||
|
// Render only the partial message (typewriter effect)
|
||||||
|
Punt pos = {x, y};
|
||||||
|
text_.render(partial_message, pos, escala, spacing);
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,19 +10,24 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include "core/graphics/vector_text.hpp"
|
|
||||||
#include "core/rendering/sdl_manager.hpp"
|
|
||||||
#include "core/types.hpp"
|
|
||||||
#include "../constants.hpp"
|
#include "../constants.hpp"
|
||||||
#include "../effects/debris_manager.hpp"
|
#include "../effects/debris_manager.hpp"
|
||||||
|
#include "../effects/gestor_puntuacio_flotant.hpp"
|
||||||
#include "../entities/bala.hpp"
|
#include "../entities/bala.hpp"
|
||||||
#include "../entities/enemic.hpp"
|
#include "../entities/enemic.hpp"
|
||||||
#include "../entities/nau.hpp"
|
#include "../entities/nau.hpp"
|
||||||
|
#include "../stage_system/stage_manager.hpp"
|
||||||
|
#include "core/graphics/vector_text.hpp"
|
||||||
|
#include "core/rendering/sdl_manager.hpp"
|
||||||
|
#include "core/system/context_escenes.hpp"
|
||||||
|
#include "core/types.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
// Classe principal del joc (escena)
|
// Classe principal del joc (escena)
|
||||||
class EscenaJoc {
|
class EscenaJoc {
|
||||||
public:
|
public:
|
||||||
explicit EscenaJoc(SDLManager& sdl);
|
explicit EscenaJoc(SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||||
~EscenaJoc() = default;
|
~EscenaJoc() = default;
|
||||||
|
|
||||||
void executar(); // Bucle principal de l'escena
|
void executar(); // Bucle principal de l'escena
|
||||||
@@ -33,25 +38,42 @@ class EscenaJoc {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
SDLManager& sdl_;
|
SDLManager& sdl_;
|
||||||
|
GestorEscenes::ContextEscenes& context_;
|
||||||
|
|
||||||
// Efectes visuals
|
// Efectes visuals
|
||||||
Effects::DebrisManager debris_manager_;
|
Effects::DebrisManager debris_manager_;
|
||||||
|
Effects::GestorPuntuacioFlotant gestor_puntuacio_;
|
||||||
|
|
||||||
// Estat del joc
|
// Estat del joc
|
||||||
Nau nau_;
|
Nau nau_;
|
||||||
std::array<Enemic, Constants::MAX_ORNIS> orni_;
|
std::array<Enemic, Constants::MAX_ORNIS> orni_;
|
||||||
std::array<Bala, Constants::MAX_BALES> bales_;
|
std::array<Bala, Constants::MAX_BALES> bales_;
|
||||||
Poligon chatarra_cosmica_;
|
Poligon chatarra_cosmica_;
|
||||||
uint16_t itocado_;
|
float itocado_; // Death timer (seconds)
|
||||||
|
|
||||||
|
// Lives and game over system
|
||||||
|
int num_vides_; // Current lives count
|
||||||
|
bool game_over_; // Game over state flag
|
||||||
|
float game_over_timer_; // Countdown timer for auto-return (seconds)
|
||||||
|
Punt punt_spawn_; // Configurable spawn point
|
||||||
|
int puntuacio_total_; // Current score
|
||||||
|
|
||||||
// Text vectorial
|
// Text vectorial
|
||||||
Graphics::VectorText text_;
|
Graphics::VectorText text_;
|
||||||
|
|
||||||
|
// [NEW] Stage system
|
||||||
|
std::unique_ptr<StageSystem::ConfigSistemaStages> stage_config_;
|
||||||
|
std::unique_ptr<StageSystem::StageManager> stage_manager_;
|
||||||
|
|
||||||
// Funcions privades
|
// Funcions privades
|
||||||
void tocado();
|
void tocado();
|
||||||
void detectar_col·lisions_bales_enemics(); // Col·lisions bala-enemic
|
void detectar_col·lisions_bales_enemics(); // Col·lisions bala-enemic
|
||||||
|
void detectar_col·lisio_nau_enemics(); // Ship-enemy collision detection
|
||||||
void dibuixar_marges() const; // Dibuixar vores de la zona de joc
|
void dibuixar_marges() const; // Dibuixar vores de la zona de joc
|
||||||
void dibuixar_marcador(); // Dibuixar marcador de puntuació
|
void dibuixar_marcador(); // Dibuixar marcador de puntuació
|
||||||
|
|
||||||
|
// [NEW] Stage system helpers
|
||||||
|
void dibuixar_missatge_stage(const std::string& missatge);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // ESCENA_JOC_HPP
|
#endif // ESCENA_JOC_HPP
|
||||||
|
|||||||
@@ -13,9 +13,14 @@
|
|||||||
#include "core/graphics/shape_loader.hpp"
|
#include "core/graphics/shape_loader.hpp"
|
||||||
#include "core/input/mouse.hpp"
|
#include "core/input/mouse.hpp"
|
||||||
#include "core/rendering/shape_renderer.hpp"
|
#include "core/rendering/shape_renderer.hpp"
|
||||||
#include "core/system/gestor_escenes.hpp"
|
#include "core/system/context_escenes.hpp"
|
||||||
#include "core/system/global_events.hpp"
|
#include "core/system/global_events.hpp"
|
||||||
|
|
||||||
|
// Using declarations per simplificar el codi
|
||||||
|
using GestorEscenes::ContextEscenes;
|
||||||
|
using Escena = ContextEscenes::Escena;
|
||||||
|
using Opcio = ContextEscenes::Opcio;
|
||||||
|
|
||||||
// Helper: calcular el progrés individual d'una lletra
|
// Helper: calcular el progrés individual d'una lletra
|
||||||
// en funció del progrés global (efecte seqüencial)
|
// en funció del progrés global (efecte seqüencial)
|
||||||
static float calcular_progress_letra(size_t letra_index, size_t num_letras, float global_progress, float threshold) {
|
static float calcular_progress_letra(size_t letra_index, size_t num_letras, float global_progress, float threshold) {
|
||||||
@@ -38,14 +43,20 @@ static float calcular_progress_letra(size_t letra_index, size_t num_letras, floa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EscenaLogo::EscenaLogo(SDLManager& sdl)
|
EscenaLogo::EscenaLogo(SDLManager& sdl, ContextEscenes& context)
|
||||||
: sdl_(sdl),
|
: sdl_(sdl),
|
||||||
|
context_(context),
|
||||||
estat_actual_(EstatAnimacio::PRE_ANIMATION),
|
estat_actual_(EstatAnimacio::PRE_ANIMATION),
|
||||||
temps_estat_actual_(0.0f),
|
temps_estat_actual_(0.0f),
|
||||||
debris_manager_(std::make_unique<Effects::DebrisManager>(sdl.obte_renderer())),
|
debris_manager_(std::make_unique<Effects::DebrisManager>(sdl.obte_renderer())),
|
||||||
lletra_explosio_index_(0),
|
lletra_explosio_index_(0),
|
||||||
temps_des_ultima_explosio_(0.0f) {
|
temps_des_ultima_explosio_(0.0f) {
|
||||||
std::cout << "Escena Logo: Inicialitzant...\n";
|
std::cout << "Escena Logo: Inicialitzant...\n";
|
||||||
|
|
||||||
|
// Consumir opcions (LOGO no processa opcions actualment)
|
||||||
|
auto opcio = context_.consumir_opcio();
|
||||||
|
(void)opcio; // Suprimir warning
|
||||||
|
|
||||||
so_reproduit_.fill(false); // Inicialitzar seguiment de sons
|
so_reproduit_.fill(false); // Inicialitzar seguiment de sons
|
||||||
inicialitzar_lletres();
|
inicialitzar_lletres();
|
||||||
}
|
}
|
||||||
@@ -54,7 +65,7 @@ void EscenaLogo::executar() {
|
|||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
Uint64 last_time = SDL_GetTicks();
|
Uint64 last_time = SDL_GetTicks();
|
||||||
|
|
||||||
while (GestorEscenes::actual == GestorEscenes::Escena::LOGO) {
|
while (GestorEscenes::actual == Escena::LOGO) {
|
||||||
// Calcular delta_time real
|
// Calcular delta_time real
|
||||||
Uint64 current_time = SDL_GetTicks();
|
Uint64 current_time = SDL_GetTicks();
|
||||||
float delta_time = (current_time - last_time) / 1000.0f;
|
float delta_time = (current_time - last_time) / 1000.0f;
|
||||||
@@ -79,7 +90,7 @@ void EscenaLogo::executar() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Events globals (F1/F2/F3/ESC/QUIT)
|
// Events globals (F1/F2/F3/ESC/QUIT)
|
||||||
if (GlobalEvents::handle(event, sdl_)) {
|
if (GlobalEvents::handle(event, sdl_, context_)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,6 +211,10 @@ void EscenaLogo::canviar_estat(EstatAnimacio nou_estat) {
|
|||||||
std::mt19937 g(rd());
|
std::mt19937 g(rd());
|
||||||
std::shuffle(ordre_explosio_.begin(), ordre_explosio_.end(), g);
|
std::shuffle(ordre_explosio_.begin(), ordre_explosio_.end(), g);
|
||||||
}
|
}
|
||||||
|
else if (nou_estat == EstatAnimacio::POST_EXPLOSION)
|
||||||
|
{
|
||||||
|
Audio::get()->playMusic("title.ogg");
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << "[EscenaLogo] Canvi a estat: " << static_cast<int>(nou_estat)
|
std::cout << "[EscenaLogo] Canvi a estat: " << static_cast<int>(nou_estat)
|
||||||
<< "\n";
|
<< "\n";
|
||||||
@@ -225,7 +240,9 @@ void EscenaLogo::actualitzar_explosions(float delta_time) {
|
|||||||
lletra.posicio, // Posició
|
lletra.posicio, // Posició
|
||||||
0.0f, // Angle (sense rotació)
|
0.0f, // Angle (sense rotació)
|
||||||
ESCALA_FINAL, // Escala (lletres a escala final)
|
ESCALA_FINAL, // Escala (lletres a escala final)
|
||||||
VELOCITAT_EXPLOSIO // Velocitat base
|
VELOCITAT_EXPLOSIO, // Velocitat base
|
||||||
|
1.0f, // Brightness màxim (per defecte)
|
||||||
|
{0.0f, 0.0f} // Sense velocitat (per defecte)
|
||||||
);
|
);
|
||||||
|
|
||||||
std::cout << "[EscenaLogo] Explota lletra " << lletra_explosio_index_ << "\n";
|
std::cout << "[EscenaLogo] Explota lletra " << lletra_explosio_index_ << "\n";
|
||||||
@@ -288,7 +305,9 @@ void EscenaLogo::actualitzar(float delta_time) {
|
|||||||
|
|
||||||
case EstatAnimacio::POST_EXPLOSION:
|
case EstatAnimacio::POST_EXPLOSION:
|
||||||
if (temps_estat_actual_ >= DURACIO_POST_EXPLOSION) {
|
if (temps_estat_actual_ >= DURACIO_POST_EXPLOSION) {
|
||||||
GestorEscenes::actual = GestorEscenes::Escena::TITOL;
|
// Transició a pantalla de títol
|
||||||
|
context_.canviar_escena(Escena::TITOL);
|
||||||
|
GestorEscenes::actual = Escena::TITOL;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -389,6 +408,12 @@ void EscenaLogo::processar_events(const SDL_Event& event) {
|
|||||||
// Qualsevol tecla o clic de ratolí salta a la pantalla de títol
|
// Qualsevol tecla o clic de ratolí salta a la pantalla de títol
|
||||||
if (event.type == SDL_EVENT_KEY_DOWN ||
|
if (event.type == SDL_EVENT_KEY_DOWN ||
|
||||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||||
GestorEscenes::actual = GestorEscenes::Escena::TITOL;
|
// Utilitzar context per especificar escena i opció
|
||||||
|
context_.canviar_escena(
|
||||||
|
Escena::TITOL,
|
||||||
|
Opcio::JUMP_TO_TITLE_MAIN
|
||||||
|
);
|
||||||
|
// Backward compatibility: També actualitzar GestorEscenes::actual
|
||||||
|
GestorEscenes::actual = Escena::TITOL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,16 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "game/effects/debris_manager.hpp"
|
||||||
|
#include "core/defaults.hpp"
|
||||||
#include "core/graphics/shape.hpp"
|
#include "core/graphics/shape.hpp"
|
||||||
#include "core/rendering/sdl_manager.hpp"
|
#include "core/rendering/sdl_manager.hpp"
|
||||||
|
#include "core/system/context_escenes.hpp"
|
||||||
#include "core/types.hpp"
|
#include "core/types.hpp"
|
||||||
#include "../effects/debris_manager.hpp"
|
|
||||||
#include "core/defaults.hpp"
|
|
||||||
|
|
||||||
class EscenaLogo {
|
class EscenaLogo {
|
||||||
public:
|
public:
|
||||||
explicit EscenaLogo(SDLManager& sdl);
|
explicit EscenaLogo(SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||||
void executar(); // Bucle principal de l'escena
|
void executar(); // Bucle principal de l'escena
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -31,6 +32,7 @@ class EscenaLogo {
|
|||||||
};
|
};
|
||||||
|
|
||||||
SDLManager& sdl_;
|
SDLManager& sdl_;
|
||||||
|
GestorEscenes::ContextEscenes& context_;
|
||||||
EstatAnimacio estat_actual_; // Estat actual de la màquina
|
EstatAnimacio estat_actual_; // Estat actual de la màquina
|
||||||
float
|
float
|
||||||
temps_estat_actual_; // Temps en l'estat actual (reset en cada transició)
|
temps_estat_actual_; // Temps en l'estat actual (reset en cada transició)
|
||||||
|
|||||||
@@ -3,29 +3,53 @@
|
|||||||
|
|
||||||
#include "escena_titol.hpp"
|
#include "escena_titol.hpp"
|
||||||
|
|
||||||
|
#include <cfloat>
|
||||||
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "core/audio/audio.hpp"
|
#include "core/audio/audio.hpp"
|
||||||
|
#include "core/graphics/shape_loader.hpp"
|
||||||
#include "core/input/mouse.hpp"
|
#include "core/input/mouse.hpp"
|
||||||
#include "core/system/gestor_escenes.hpp"
|
#include "core/rendering/shape_renderer.hpp"
|
||||||
|
#include "core/system/context_escenes.hpp"
|
||||||
#include "core/system/global_events.hpp"
|
#include "core/system/global_events.hpp"
|
||||||
#include "project.h"
|
#include "project.h"
|
||||||
|
|
||||||
EscenaTitol::EscenaTitol(SDLManager& sdl)
|
// Using declarations per simplificar el codi
|
||||||
|
using GestorEscenes::ContextEscenes;
|
||||||
|
using Escena = ContextEscenes::Escena;
|
||||||
|
using Opcio = ContextEscenes::Opcio;
|
||||||
|
|
||||||
|
EscenaTitol::EscenaTitol(SDLManager& sdl, ContextEscenes& context)
|
||||||
: sdl_(sdl),
|
: sdl_(sdl),
|
||||||
|
context_(context),
|
||||||
text_(sdl.obte_renderer()),
|
text_(sdl.obte_renderer()),
|
||||||
estat_actual_(EstatTitol::INIT),
|
estat_actual_(EstatTitol::STARFIELD_FADE_IN),
|
||||||
temps_acumulat_(0.0f) {
|
temps_acumulat_(0.0f),
|
||||||
|
temps_animacio_(0.0f),
|
||||||
|
temps_estat_main_(0.0f),
|
||||||
|
animacio_activa_(false),
|
||||||
|
factor_lerp_(0.0f) {
|
||||||
std::cout << "Escena Titol: Inicialitzant...\n";
|
std::cout << "Escena Titol: Inicialitzant...\n";
|
||||||
|
|
||||||
|
// Processar opció del context
|
||||||
|
auto opcio = context_.consumir_opcio();
|
||||||
|
|
||||||
|
if (opcio == Opcio::JUMP_TO_TITLE_MAIN) {
|
||||||
|
std::cout << "Escena Titol: Opció JUMP_TO_TITLE_MAIN activada\n";
|
||||||
|
estat_actual_ = EstatTitol::MAIN;
|
||||||
|
temps_estat_main_ = 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
// Crear starfield de fons
|
// Crear starfield de fons
|
||||||
Punt centre_pantalla{
|
Punt centre_pantalla{
|
||||||
Defaults::Game::WIDTH / 2.0f,
|
Defaults::Game::WIDTH / 2.0f,
|
||||||
Defaults::Game::HEIGHT / 2.0f};
|
Defaults::Game::HEIGHT / 2.0f};
|
||||||
|
|
||||||
SDL_FRect area_completa{
|
SDL_FRect area_completa{
|
||||||
0, 0,
|
0,
|
||||||
|
0,
|
||||||
static_cast<float>(Defaults::Game::WIDTH),
|
static_cast<float>(Defaults::Game::WIDTH),
|
||||||
static_cast<float>(Defaults::Game::HEIGHT)};
|
static_cast<float>(Defaults::Game::HEIGHT)};
|
||||||
|
|
||||||
@@ -35,13 +59,185 @@ EscenaTitol::EscenaTitol(SDLManager& sdl)
|
|||||||
area_completa,
|
area_completa,
|
||||||
150 // densitat: 150 estrelles (50 per capa)
|
150 // densitat: 150 estrelles (50 per capa)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Brightness depèn de l'opció
|
||||||
|
if (estat_actual_ == EstatTitol::MAIN) {
|
||||||
|
// Si saltem a MAIN, starfield instantàniament brillant
|
||||||
|
starfield_->set_brightness(BRIGHTNESS_STARFIELD);
|
||||||
|
} else {
|
||||||
|
// Flux normal: comença amb brightness 0.0 per fade-in
|
||||||
|
starfield_->set_brightness(0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inicialitzar lletres del títol "ORNI ATTACK!"
|
||||||
|
inicialitzar_titol();
|
||||||
|
|
||||||
|
// Iniciar música de títol si no està sonant
|
||||||
|
if (Audio::get()->getMusicState() != Audio::MusicState::PLAYING) {
|
||||||
|
Audio::get()->playMusic("title.ogg");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EscenaTitol::~EscenaTitol() {
|
||||||
|
// Aturar música de títol quan es destrueix l'escena
|
||||||
|
Audio::get()->stopMusic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void EscenaTitol::inicialitzar_titol() {
|
||||||
|
using namespace Graphics;
|
||||||
|
|
||||||
|
// === LÍNIA 1: "ORNI" ===
|
||||||
|
std::vector<std::string> fitxers_orni = {
|
||||||
|
"title/letra_o.shp",
|
||||||
|
"title/letra_r.shp",
|
||||||
|
"title/letra_n.shp",
|
||||||
|
"title/letra_i.shp"};
|
||||||
|
|
||||||
|
// Pas 1: Carregar formes i calcular amplades per "ORNI"
|
||||||
|
float ancho_total_orni = 0.0f;
|
||||||
|
|
||||||
|
for (const auto& fitxer : fitxers_orni) {
|
||||||
|
auto forma = ShapeLoader::load(fitxer);
|
||||||
|
if (!forma || !forma->es_valida()) {
|
||||||
|
std::cerr << "[EscenaTitol] Error carregant " << fitxer << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcular bounding box de la forma (trobar ancho i altura)
|
||||||
|
float min_x = FLT_MAX;
|
||||||
|
float max_x = -FLT_MAX;
|
||||||
|
float min_y = FLT_MAX;
|
||||||
|
float max_y = -FLT_MAX;
|
||||||
|
|
||||||
|
for (const auto& prim : forma->get_primitives()) {
|
||||||
|
for (const auto& punt : prim.points) {
|
||||||
|
min_x = std::min(min_x, punt.x);
|
||||||
|
max_x = std::max(max_x, punt.x);
|
||||||
|
min_y = std::min(min_y, punt.y);
|
||||||
|
max_y = std::max(max_y, punt.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float ancho_sin_escalar = max_x - min_x;
|
||||||
|
float altura_sin_escalar = max_y - min_y;
|
||||||
|
|
||||||
|
// Escalar ancho, altura i offset amb ESCALA_TITULO
|
||||||
|
float ancho = ancho_sin_escalar * ESCALA_TITULO;
|
||||||
|
float altura = altura_sin_escalar * ESCALA_TITULO;
|
||||||
|
float offset_centre = (forma->get_centre().x - min_x) * ESCALA_TITULO;
|
||||||
|
|
||||||
|
lletres_orni_.push_back({forma, {0.0f, 0.0f}, ancho, altura, offset_centre});
|
||||||
|
|
||||||
|
ancho_total_orni += ancho;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Afegir espaiat entre lletres
|
||||||
|
ancho_total_orni += ESPAI_ENTRE_LLETRES * (lletres_orni_.size() - 1);
|
||||||
|
|
||||||
|
// Calcular posició inicial (centrat horitzontal) per "ORNI"
|
||||||
|
float x_inicial_orni = (Defaults::Game::WIDTH - ancho_total_orni) / 2.0f;
|
||||||
|
float x_actual = x_inicial_orni;
|
||||||
|
|
||||||
|
for (auto& lletra : lletres_orni_) {
|
||||||
|
lletra.posicio.x = x_actual + lletra.offset_centre;
|
||||||
|
lletra.posicio.y = Y_ORNI;
|
||||||
|
x_actual += lletra.ancho + ESPAI_ENTRE_LLETRES;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[EscenaTitol] Línia 1 (ORNI): " << lletres_orni_.size()
|
||||||
|
<< " lletres, ancho total: " << ancho_total_orni << " px\n";
|
||||||
|
|
||||||
|
// === Calcular posició Y dinàmica per "ATTACK!" ===
|
||||||
|
// Totes les lletres ORNI tenen la mateixa altura, utilitzem la primera
|
||||||
|
float altura_orni = lletres_orni_.empty() ? 50.0f : lletres_orni_[0].altura;
|
||||||
|
y_attack_dinamica_ = Y_ORNI + altura_orni + SEPARACION_LINEAS;
|
||||||
|
|
||||||
|
std::cout << "[EscenaTitol] Altura ORNI: " << altura_orni
|
||||||
|
<< " px, Y_ATTACK dinàmica: " << y_attack_dinamica_ << " px\n";
|
||||||
|
|
||||||
|
// === LÍNIA 2: "ATTACK!" ===
|
||||||
|
std::vector<std::string> fitxers_attack = {
|
||||||
|
"title/letra_a.shp",
|
||||||
|
"title/letra_t.shp",
|
||||||
|
"title/letra_t.shp", // T repetida
|
||||||
|
"title/letra_a.shp", // A repetida
|
||||||
|
"title/letra_c.shp",
|
||||||
|
"title/letra_k.shp",
|
||||||
|
"title/letra_exclamacion.shp"};
|
||||||
|
|
||||||
|
// Pas 1: Carregar formes i calcular amplades per "ATTACK!"
|
||||||
|
float ancho_total_attack = 0.0f;
|
||||||
|
|
||||||
|
for (const auto& fitxer : fitxers_attack) {
|
||||||
|
auto forma = ShapeLoader::load(fitxer);
|
||||||
|
if (!forma || !forma->es_valida()) {
|
||||||
|
std::cerr << "[EscenaTitol] Error carregant " << fitxer << std::endl;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calcular bounding box de la forma (trobar ancho i altura)
|
||||||
|
float min_x = FLT_MAX;
|
||||||
|
float max_x = -FLT_MAX;
|
||||||
|
float min_y = FLT_MAX;
|
||||||
|
float max_y = -FLT_MAX;
|
||||||
|
|
||||||
|
for (const auto& prim : forma->get_primitives()) {
|
||||||
|
for (const auto& punt : prim.points) {
|
||||||
|
min_x = std::min(min_x, punt.x);
|
||||||
|
max_x = std::max(max_x, punt.x);
|
||||||
|
min_y = std::min(min_y, punt.y);
|
||||||
|
max_y = std::max(max_y, punt.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float ancho_sin_escalar = max_x - min_x;
|
||||||
|
float altura_sin_escalar = max_y - min_y;
|
||||||
|
|
||||||
|
// Escalar ancho, altura i offset amb ESCALA_TITULO
|
||||||
|
float ancho = ancho_sin_escalar * ESCALA_TITULO;
|
||||||
|
float altura = altura_sin_escalar * ESCALA_TITULO;
|
||||||
|
float offset_centre = (forma->get_centre().x - min_x) * ESCALA_TITULO;
|
||||||
|
|
||||||
|
lletres_attack_.push_back({forma, {0.0f, 0.0f}, ancho, altura, offset_centre});
|
||||||
|
|
||||||
|
ancho_total_attack += ancho;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Afegir espaiat entre lletres
|
||||||
|
ancho_total_attack += ESPAI_ENTRE_LLETRES * (lletres_attack_.size() - 1);
|
||||||
|
|
||||||
|
// Calcular posició inicial (centrat horitzontal) per "ATTACK!"
|
||||||
|
float x_inicial_attack = (Defaults::Game::WIDTH - ancho_total_attack) / 2.0f;
|
||||||
|
x_actual = x_inicial_attack;
|
||||||
|
|
||||||
|
for (auto& lletra : lletres_attack_) {
|
||||||
|
lletra.posicio.x = x_actual + lletra.offset_centre;
|
||||||
|
lletra.posicio.y = y_attack_dinamica_; // Usar posició dinàmica
|
||||||
|
x_actual += lletra.ancho + ESPAI_ENTRE_LLETRES;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[EscenaTitol] Línia 2 (ATTACK!): " << lletres_attack_.size()
|
||||||
|
<< " lletres, ancho total: " << ancho_total_attack << " px\n";
|
||||||
|
|
||||||
|
// Guardar posicions originals per l'animació orbital
|
||||||
|
posicions_originals_orni_.clear();
|
||||||
|
for (const auto& lletra : lletres_orni_) {
|
||||||
|
posicions_originals_orni_.push_back(lletra.posicio);
|
||||||
|
}
|
||||||
|
|
||||||
|
posicions_originals_attack_.clear();
|
||||||
|
for (const auto& lletra : lletres_attack_) {
|
||||||
|
posicions_originals_attack_.push_back(lletra.posicio);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[EscenaTitol] Animació: Posicions originals guardades\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void EscenaTitol::executar() {
|
void EscenaTitol::executar() {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
Uint64 last_time = SDL_GetTicks();
|
Uint64 last_time = SDL_GetTicks();
|
||||||
|
|
||||||
while (GestorEscenes::actual == GestorEscenes::Escena::TITOL) {
|
while (GestorEscenes::actual == Escena::TITOL) {
|
||||||
// Calcular delta_time real
|
// Calcular delta_time real
|
||||||
Uint64 current_time = SDL_GetTicks();
|
Uint64 current_time = SDL_GetTicks();
|
||||||
float delta_time = (current_time - last_time) / 1000.0f;
|
float delta_time = (current_time - last_time) / 1000.0f;
|
||||||
@@ -66,7 +262,7 @@ void EscenaTitol::executar() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Events globals (F1/F2/F3/F4/ESC/QUIT)
|
// Events globals (F1/F2/F3/F4/ESC/QUIT)
|
||||||
if (GlobalEvents::handle(event, sdl_)) {
|
if (GlobalEvents::handle(event, sdl_, context_)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,15 +302,101 @@ void EscenaTitol::actualitzar(float delta_time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch (estat_actual_) {
|
switch (estat_actual_) {
|
||||||
case EstatTitol::INIT:
|
case EstatTitol::STARFIELD_FADE_IN: {
|
||||||
|
temps_acumulat_ += delta_time;
|
||||||
|
|
||||||
|
// Calcular progrés del fade (0.0 → 1.0)
|
||||||
|
float progress = std::min(1.0f, temps_acumulat_ / DURACIO_FADE_IN);
|
||||||
|
|
||||||
|
// Lerp brightness de 0.0 a BRIGHTNESS_STARFIELD
|
||||||
|
float brightness_actual = progress * BRIGHTNESS_STARFIELD;
|
||||||
|
starfield_->set_brightness(brightness_actual);
|
||||||
|
|
||||||
|
// Transició a STARFIELD quan el fade es completa
|
||||||
|
if (temps_acumulat_ >= DURACIO_FADE_IN) {
|
||||||
|
estat_actual_ = EstatTitol::STARFIELD;
|
||||||
|
temps_acumulat_ = 0.0f; // Reset timer per al següent estat
|
||||||
|
starfield_->set_brightness(BRIGHTNESS_STARFIELD); // Assegurar valor final
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case EstatTitol::STARFIELD:
|
||||||
temps_acumulat_ += delta_time;
|
temps_acumulat_ += delta_time;
|
||||||
if (temps_acumulat_ >= DURACIO_INIT) {
|
if (temps_acumulat_ >= DURACIO_INIT) {
|
||||||
estat_actual_ = EstatTitol::MAIN;
|
estat_actual_ = EstatTitol::MAIN;
|
||||||
|
temps_estat_main_ = 0.0f; // Reset timer al entrar a MAIN
|
||||||
|
animacio_activa_ = false; // Comença estàtic
|
||||||
|
factor_lerp_ = 0.0f; // Sense animació encara
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case EstatTitol::MAIN:
|
|
||||||
// No hi ha lògica d'actualització en l'estat MAIN
|
case EstatTitol::MAIN: {
|
||||||
|
temps_estat_main_ += delta_time;
|
||||||
|
|
||||||
|
// Fase 1: Estàtic (0-10s)
|
||||||
|
if (temps_estat_main_ < DELAY_INICI_ANIMACIO) {
|
||||||
|
factor_lerp_ = 0.0f;
|
||||||
|
animacio_activa_ = false;
|
||||||
|
}
|
||||||
|
// Fase 2: Lerp (10-12s)
|
||||||
|
else if (temps_estat_main_ < DELAY_INICI_ANIMACIO + DURACIO_LERP) {
|
||||||
|
float temps_lerp = temps_estat_main_ - DELAY_INICI_ANIMACIO;
|
||||||
|
factor_lerp_ = temps_lerp / DURACIO_LERP; // 0.0 → 1.0 linealment
|
||||||
|
animacio_activa_ = true;
|
||||||
|
}
|
||||||
|
// Fase 3: Animació completa (12s+)
|
||||||
|
else {
|
||||||
|
factor_lerp_ = 1.0f;
|
||||||
|
animacio_activa_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actualitzar animació del logo
|
||||||
|
actualitzar_animacio_logo(delta_time);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case EstatTitol::TRANSITION_TO_GAME:
|
||||||
|
temps_acumulat_ += delta_time;
|
||||||
|
|
||||||
|
// Continuar animació orbital durant la transició
|
||||||
|
actualitzar_animacio_logo(delta_time);
|
||||||
|
|
||||||
|
if (temps_acumulat_ >= DURACIO_TRANSITION) {
|
||||||
|
// Transició a JOC (la música ja s'ha parat en el fade)
|
||||||
|
GestorEscenes::actual = Escena::JOC;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EscenaTitol::actualitzar_animacio_logo(float delta_time) {
|
||||||
|
// Només calcular i aplicar offsets si l'animació està activa
|
||||||
|
if (animacio_activa_) {
|
||||||
|
// Acumular temps escalat
|
||||||
|
temps_animacio_ += delta_time * factor_lerp_;
|
||||||
|
|
||||||
|
// Usar amplituds i freqüències completes
|
||||||
|
float amplitude_x_actual = ORBIT_AMPLITUDE_X;
|
||||||
|
float amplitude_y_actual = ORBIT_AMPLITUDE_Y;
|
||||||
|
float frequency_x_actual = ORBIT_FREQUENCY_X;
|
||||||
|
float frequency_y_actual = ORBIT_FREQUENCY_Y;
|
||||||
|
|
||||||
|
// Calcular offset orbital
|
||||||
|
float offset_x = amplitude_x_actual * std::sin(2.0f * Defaults::Math::PI * frequency_x_actual * temps_animacio_);
|
||||||
|
float offset_y = amplitude_y_actual * std::sin(2.0f * Defaults::Math::PI * frequency_y_actual * temps_animacio_ + ORBIT_PHASE_OFFSET);
|
||||||
|
|
||||||
|
// Aplicar offset a totes les lletres de "ORNI"
|
||||||
|
for (size_t i = 0; i < lletres_orni_.size(); ++i) {
|
||||||
|
lletres_orni_[i].posicio.x = posicions_originals_orni_[i].x + static_cast<int>(std::round(offset_x));
|
||||||
|
lletres_orni_[i].posicio.y = posicions_originals_orni_[i].y + static_cast<int>(std::round(offset_y));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Aplicar offset a totes les lletres de "ATTACK!"
|
||||||
|
for (size_t i = 0; i < lletres_attack_.size(); ++i) {
|
||||||
|
lletres_attack_[i].posicio.x = posicions_originals_attack_[i].x + static_cast<int>(std::round(offset_x));
|
||||||
|
lletres_attack_[i].posicio.y = posicions_originals_attack_[i].y + static_cast<int>(std::round(offset_y));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,27 +406,121 @@ void EscenaTitol::dibuixar() {
|
|||||||
starfield_->dibuixar();
|
starfield_->dibuixar();
|
||||||
}
|
}
|
||||||
|
|
||||||
// En l'estat INIT, només mostrar starfield (sense text)
|
// En els estats STARFIELD_FADE_IN i STARFIELD, només mostrar starfield (sense text)
|
||||||
if (estat_actual_ == EstatTitol::INIT) {
|
if (estat_actual_ == EstatTitol::STARFIELD_FADE_IN || estat_actual_ == EstatTitol::STARFIELD) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Estat MAIN: Dibuixar text de títol i copyright (sobre el starfield)
|
// Estat MAIN i TRANSITION_TO_GAME: Dibuixar títol i text (sobre el starfield)
|
||||||
if (estat_actual_ == EstatTitol::MAIN) {
|
if (estat_actual_ == EstatTitol::MAIN || estat_actual_ == EstatTitol::TRANSITION_TO_GAME) {
|
||||||
// Text principal centrat (vertical i horitzontalment)
|
// === Calcular i renderitzar ombra (només si animació activa) ===
|
||||||
const std::string main_text = "PRESS BUTTON TO PLAY";
|
if (animacio_activa_) {
|
||||||
const float escala_main = 1.0f;
|
float temps_shadow = temps_animacio_ - SHADOW_DELAY;
|
||||||
const float spacing = 2.0f;
|
if (temps_shadow < 0.0f) temps_shadow = 0.0f; // Evitar temps negatiu
|
||||||
|
|
||||||
float text_width = text_.get_text_width(main_text, escala_main, spacing);
|
// Usar amplituds i freqüències completes per l'ombra
|
||||||
float text_height = text_.get_text_height(escala_main);
|
float amplitude_x_shadow = ORBIT_AMPLITUDE_X;
|
||||||
|
float amplitude_y_shadow = ORBIT_AMPLITUDE_Y;
|
||||||
|
float frequency_x_shadow = ORBIT_FREQUENCY_X;
|
||||||
|
float frequency_y_shadow = ORBIT_FREQUENCY_Y;
|
||||||
|
|
||||||
float x_center = (Defaults::Game::WIDTH - text_width) / 2.0f;
|
// Calcular offset de l'ombra
|
||||||
float y_center = (Defaults::Game::HEIGHT - text_height) / 2.0f;
|
float shadow_offset_x = amplitude_x_shadow * std::sin(2.0f * Defaults::Math::PI * frequency_x_shadow * temps_shadow) + SHADOW_OFFSET_X;
|
||||||
|
float shadow_offset_y = amplitude_y_shadow * std::sin(2.0f * Defaults::Math::PI * frequency_y_shadow * temps_shadow + ORBIT_PHASE_OFFSET) + SHADOW_OFFSET_Y;
|
||||||
|
|
||||||
text_.render(main_text, Punt{x_center, y_center}, escala_main, spacing);
|
// === RENDERITZAR OMBRA PRIMER (darrera del logo principal) ===
|
||||||
|
|
||||||
// Copyright a la part inferior (centrat horitzontalment)
|
// Ombra "ORNI"
|
||||||
|
for (size_t i = 0; i < lletres_orni_.size(); ++i) {
|
||||||
|
Punt pos_shadow;
|
||||||
|
pos_shadow.x = posicions_originals_orni_[i].x + static_cast<int>(std::round(shadow_offset_x));
|
||||||
|
pos_shadow.y = posicions_originals_orni_[i].y + static_cast<int>(std::round(shadow_offset_y));
|
||||||
|
|
||||||
|
Rendering::render_shape(
|
||||||
|
sdl_.obte_renderer(),
|
||||||
|
lletres_orni_[i].forma,
|
||||||
|
pos_shadow,
|
||||||
|
0.0f,
|
||||||
|
ESCALA_TITULO,
|
||||||
|
true,
|
||||||
|
1.0f, // progress = 1.0 (totalment visible)
|
||||||
|
SHADOW_BRIGHTNESS // brightness = 0.4 (brillantor reduïda)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ombra "ATTACK!"
|
||||||
|
for (size_t i = 0; i < lletres_attack_.size(); ++i) {
|
||||||
|
Punt pos_shadow;
|
||||||
|
pos_shadow.x = posicions_originals_attack_[i].x + static_cast<int>(std::round(shadow_offset_x));
|
||||||
|
pos_shadow.y = posicions_originals_attack_[i].y + static_cast<int>(std::round(shadow_offset_y));
|
||||||
|
|
||||||
|
Rendering::render_shape(
|
||||||
|
sdl_.obte_renderer(),
|
||||||
|
lletres_attack_[i].forma,
|
||||||
|
pos_shadow,
|
||||||
|
0.0f,
|
||||||
|
ESCALA_TITULO,
|
||||||
|
true,
|
||||||
|
1.0f, // progress = 1.0 (totalment visible)
|
||||||
|
SHADOW_BRIGHTNESS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// === RENDERITZAR LOGO PRINCIPAL (damunt) ===
|
||||||
|
|
||||||
|
// Dibuixar "ORNI" (línia 1)
|
||||||
|
for (const auto& lletra : lletres_orni_) {
|
||||||
|
Rendering::render_shape(
|
||||||
|
sdl_.obte_renderer(),
|
||||||
|
lletra.forma,
|
||||||
|
lletra.posicio,
|
||||||
|
0.0f,
|
||||||
|
ESCALA_TITULO,
|
||||||
|
true,
|
||||||
|
1.0f // Brillantor completa
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dibuixar "ATTACK!" (línia 2)
|
||||||
|
for (const auto& lletra : lletres_attack_) {
|
||||||
|
Rendering::render_shape(
|
||||||
|
sdl_.obte_renderer(),
|
||||||
|
lletra.forma,
|
||||||
|
lletra.posicio,
|
||||||
|
0.0f,
|
||||||
|
ESCALA_TITULO,
|
||||||
|
true,
|
||||||
|
1.0f // Brillantor completa
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Text "PRESS BUTTON TO PLAY" ===
|
||||||
|
// En estat MAIN: sempre visible
|
||||||
|
// En estat TRANSITION: parpellejant (blink amb sinusoide)
|
||||||
|
|
||||||
|
const float spacing = 2.0f; // Espai entre caràcters (usat també per copyright)
|
||||||
|
|
||||||
|
bool mostrar_text = true;
|
||||||
|
if (estat_actual_ == EstatTitol::TRANSITION_TO_GAME) {
|
||||||
|
// Parpelleig: sin oscil·la entre -1 i 1, volem ON quan > 0
|
||||||
|
float fase = temps_acumulat_ * BLINK_FREQUENCY * 2.0f * 3.14159f; // 2π × freq × temps
|
||||||
|
mostrar_text = (std::sin(fase) > 0.0f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mostrar_text) {
|
||||||
|
const std::string main_text = "PRESS BUTTON TO PLAY";
|
||||||
|
const float escala_main = 1.0f;
|
||||||
|
|
||||||
|
float text_width = text_.get_text_width(main_text, escala_main, spacing);
|
||||||
|
|
||||||
|
float x_center = (Defaults::Game::WIDTH - text_width) / 2.0f;
|
||||||
|
float altura_attack = lletres_attack_.empty() ? 50.0f : lletres_attack_[0].altura;
|
||||||
|
float y_center = y_attack_dinamica_ + altura_attack + 70.0f;
|
||||||
|
|
||||||
|
text_.render(main_text, Punt{x_center, y_center}, escala_main, spacing);
|
||||||
|
}
|
||||||
|
|
||||||
|
// === Copyright a la part inferior (centrat horitzontalment) ===
|
||||||
// Convert to uppercase since VectorText only supports A-Z
|
// Convert to uppercase since VectorText only supports A-Z
|
||||||
std::string copyright = Project::COPYRIGHT;
|
std::string copyright = Project::COPYRIGHT;
|
||||||
for (char& c : copyright) {
|
for (char& c : copyright) {
|
||||||
@@ -168,15 +544,35 @@ void EscenaTitol::processar_events(const SDL_Event& event) {
|
|||||||
// Qualsevol tecla o clic de ratolí
|
// Qualsevol tecla o clic de ratolí
|
||||||
if (event.type == SDL_EVENT_KEY_DOWN ||
|
if (event.type == SDL_EVENT_KEY_DOWN ||
|
||||||
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
|
||||||
|
|
||||||
switch (estat_actual_) {
|
switch (estat_actual_) {
|
||||||
case EstatTitol::INIT:
|
case EstatTitol::STARFIELD_FADE_IN:
|
||||||
|
// Saltar directament a MAIN (ometre fade-in i starfield)
|
||||||
|
estat_actual_ = EstatTitol::MAIN;
|
||||||
|
starfield_->set_brightness(BRIGHTNESS_STARFIELD); // Assegurar brightness final
|
||||||
|
temps_estat_main_ = 0.0f; // Reset timer per animació de títol
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EstatTitol::STARFIELD:
|
||||||
// Saltar a MAIN
|
// Saltar a MAIN
|
||||||
estat_actual_ = EstatTitol::MAIN;
|
estat_actual_ = EstatTitol::MAIN;
|
||||||
|
temps_estat_main_ = 0.0f; // Reset timer
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EstatTitol::MAIN:
|
case EstatTitol::MAIN:
|
||||||
// Anar al joc
|
// Utilitzar context per transició a JOC
|
||||||
GestorEscenes::actual = GestorEscenes::Escena::JOC;
|
context_.canviar_escena(Escena::JOC);
|
||||||
|
// NO actualitzar GestorEscenes::actual aquí!
|
||||||
|
// La transició es fa en l'estat TRANSITION_TO_GAME
|
||||||
|
|
||||||
|
// Iniciar transició amb fade-out de música
|
||||||
|
estat_actual_ = EstatTitol::TRANSITION_TO_GAME;
|
||||||
|
temps_acumulat_ = 0.0f; // Reset del comptador
|
||||||
|
Audio::get()->fadeOutMusic(MUSIC_FADE); // Fade
|
||||||
|
Audio::get()->playSound(Defaults::Sound::LASER, Audio::Group::GAME);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EstatTitol::TRANSITION_TO_GAME:
|
||||||
|
// Ignorar inputs durant la transició
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,35 +7,95 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "core/defaults.hpp"
|
||||||
|
#include "core/graphics/shape.hpp"
|
||||||
#include "core/graphics/starfield.hpp"
|
#include "core/graphics/starfield.hpp"
|
||||||
#include "core/graphics/vector_text.hpp"
|
#include "core/graphics/vector_text.hpp"
|
||||||
#include "core/rendering/sdl_manager.hpp"
|
#include "core/rendering/sdl_manager.hpp"
|
||||||
#include "core/defaults.hpp"
|
#include "core/system/context_escenes.hpp"
|
||||||
|
#include "core/types.hpp"
|
||||||
|
|
||||||
class EscenaTitol {
|
class EscenaTitol {
|
||||||
public:
|
public:
|
||||||
explicit EscenaTitol(SDLManager& sdl);
|
explicit EscenaTitol(SDLManager& sdl, GestorEscenes::ContextEscenes& context);
|
||||||
|
~EscenaTitol(); // Destructor per aturar música
|
||||||
void executar(); // Bucle principal de l'escena
|
void executar(); // Bucle principal de l'escena
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Màquina d'estats per la pantalla de títol
|
// Màquina d'estats per la pantalla de títol
|
||||||
enum class EstatTitol {
|
enum class EstatTitol {
|
||||||
INIT, // Pantalla negra inicial (2 segons)
|
STARFIELD_FADE_IN, // Fade-in del starfield (1.5s)
|
||||||
MAIN // Pantalla de títol amb text
|
STARFIELD, // Pantalla con el campo de estrellas
|
||||||
|
MAIN, // Pantalla de títol amb text
|
||||||
|
TRANSITION_TO_GAME // Transició amb fade-out de música i text parpellejant
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura per emmagatzemar informació de cada lletra del títol
|
||||||
|
struct LetraLogo {
|
||||||
|
std::shared_ptr<Graphics::Shape> forma; // Forma vectorial de la lletra
|
||||||
|
Punt posicio; // Posició en pantalla
|
||||||
|
float ancho; // Amplada escalada
|
||||||
|
float altura; // Altura escalada
|
||||||
|
float offset_centre; // Offset del centre per posicionament
|
||||||
};
|
};
|
||||||
|
|
||||||
SDLManager& sdl_;
|
SDLManager& sdl_;
|
||||||
Graphics::VectorText text_; // Sistema de text vectorial
|
GestorEscenes::ContextEscenes& context_;
|
||||||
|
Graphics::VectorText text_; // Sistema de text vectorial
|
||||||
std::unique_ptr<Graphics::Starfield> starfield_; // Camp d'estrelles de fons
|
std::unique_ptr<Graphics::Starfield> starfield_; // Camp d'estrelles de fons
|
||||||
EstatTitol estat_actual_; // Estat actual de la màquina
|
EstatTitol estat_actual_; // Estat actual de la màquina
|
||||||
float temps_acumulat_; // Temps acumulat per l'estat INIT
|
float temps_acumulat_; // Temps acumulat per l'estat INIT
|
||||||
|
|
||||||
|
// Lletres del títol "ORNI ATTACK!"
|
||||||
|
std::vector<LetraLogo> lletres_orni_; // Lletres de "ORNI" (línia 1)
|
||||||
|
std::vector<LetraLogo> lletres_attack_; // Lletres de "ATTACK!" (línia 2)
|
||||||
|
float y_attack_dinamica_; // Posició Y calculada dinàmicament per "ATTACK!"
|
||||||
|
|
||||||
|
// Estat d'animació del logo
|
||||||
|
float temps_animacio_; // Temps acumulat per animació orbital
|
||||||
|
std::vector<Punt> posicions_originals_orni_; // Posicions originals de "ORNI"
|
||||||
|
std::vector<Punt> posicions_originals_attack_; // Posicions originals de "ATTACK!"
|
||||||
|
|
||||||
|
// Estat d'arrencada de l'animació
|
||||||
|
float temps_estat_main_; // Temps acumulat en estat MAIN
|
||||||
|
bool animacio_activa_; // Flag: true quan animació està activa
|
||||||
|
float factor_lerp_; // Factor de lerp actual (0.0 → 1.0)
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
static constexpr float DURACIO_INIT = 2.0f; // Duració de l'estat INIT (2 segons)
|
static constexpr float BRIGHTNESS_STARFIELD = 1.2f; // Brightness del starfield (>1.0 = més brillant)
|
||||||
|
static constexpr float DURACIO_FADE_IN = 3.0f; // Duració del fade-in del starfield (1.5 segons)
|
||||||
|
static constexpr float DURACIO_INIT = 4.0f; // Duració de l'estat INIT (2 segons)
|
||||||
|
static constexpr float DURACIO_TRANSITION = 1.5f; // Duració de la transició (1.5 segons)
|
||||||
|
static constexpr float ESCALA_TITULO = 0.6f; // Escala per les lletres del títol (50%)
|
||||||
|
static constexpr float ESPAI_ENTRE_LLETRES = 10.0f; // Espai entre lletres
|
||||||
|
static constexpr float Y_ORNI = 150.0f; // Posició Y de "ORNI"
|
||||||
|
static constexpr float SEPARACION_LINEAS = 10.0f; // Separació entre "ORNI" i "ATTACK!" (0.0f = pegades)
|
||||||
|
static constexpr float BLINK_FREQUENCY = 3.0f; // Freqüència de parpelleig (3 Hz)
|
||||||
|
static constexpr int MUSIC_FADE = 1000; // Duracio del fade de la musica del titol al començar a jugar
|
||||||
|
|
||||||
|
// Constants d'animació del logo
|
||||||
|
static constexpr float ORBIT_AMPLITUDE_X = 4.0f; // Amplitud oscil·lació horitzontal (píxels)
|
||||||
|
static constexpr float ORBIT_AMPLITUDE_Y = 3.0f; // Amplitud oscil·lació vertical (píxels)
|
||||||
|
static constexpr float ORBIT_FREQUENCY_X = 0.8f; // Velocitat oscil·lació horitzontal (Hz)
|
||||||
|
static constexpr float ORBIT_FREQUENCY_Y = 1.2f; // Velocitat oscil·lació vertical (Hz)
|
||||||
|
static constexpr float ORBIT_PHASE_OFFSET = 1.57f; // Desfasament entre X i Y (90° per circular)
|
||||||
|
|
||||||
|
// Constants d'ombra del logo
|
||||||
|
static constexpr float SHADOW_DELAY = 0.5f; // Retard temporal de l'ombra (segons)
|
||||||
|
static constexpr float SHADOW_BRIGHTNESS = 0.4f; // Multiplicador de brillantor de l'ombra (0.0-1.0)
|
||||||
|
static constexpr float SHADOW_OFFSET_X = 2.0f; // Offset espacial X fix (píxels)
|
||||||
|
static constexpr float SHADOW_OFFSET_Y = 2.0f; // Offset espacial Y fix (píxels)
|
||||||
|
|
||||||
|
// Temporització de l'arrencada de l'animació
|
||||||
|
static constexpr float DELAY_INICI_ANIMACIO = 10.0f; // 10s estàtic abans d'animar
|
||||||
|
static constexpr float DURACIO_LERP = 2.0f; // 2s per arribar a amplitud completa
|
||||||
|
|
||||||
// Mètodes privats
|
// Mètodes privats
|
||||||
void actualitzar(float delta_time);
|
void actualitzar(float delta_time);
|
||||||
|
void actualitzar_animacio_logo(float delta_time); // Actualitza l'animació orbital del logo
|
||||||
void dibuixar();
|
void dibuixar();
|
||||||
void processar_events(const SDL_Event& event);
|
void processar_events(const SDL_Event& event);
|
||||||
|
void inicialitzar_titol(); // Carrega i posiciona les lletres del títol
|
||||||
};
|
};
|
||||||
|
|||||||
167
source/game/stage_system/spawn_controller.cpp
Normal file
167
source/game/stage_system/spawn_controller.cpp
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
// spawn_controller.cpp - Implementació del controlador de spawn
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#include "spawn_controller.hpp"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
SpawnController::SpawnController()
|
||||||
|
: config_(nullptr), temps_transcorregut_(0.0f), index_spawn_actual_(0), ship_position_(nullptr) {}
|
||||||
|
|
||||||
|
void SpawnController::configurar(const ConfigStage* config) {
|
||||||
|
config_ = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnController::iniciar() {
|
||||||
|
if (!config_) {
|
||||||
|
std::cerr << "[SpawnController] Error: config_ és null" << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
reset();
|
||||||
|
generar_spawn_events();
|
||||||
|
|
||||||
|
std::cout << "[SpawnController] Stage " << static_cast<int>(config_->stage_id)
|
||||||
|
<< ": generats " << spawn_queue_.size() << " spawn events" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnController::reset() {
|
||||||
|
spawn_queue_.clear();
|
||||||
|
temps_transcorregut_ = 0.0f;
|
||||||
|
index_spawn_actual_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnController::actualitzar(float delta_time, std::array<Enemic, 15>& orni_array, bool pausar) {
|
||||||
|
if (!config_ || spawn_queue_.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment timer only when not paused
|
||||||
|
if (!pausar) {
|
||||||
|
temps_transcorregut_ += delta_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process spawn events
|
||||||
|
while (index_spawn_actual_ < spawn_queue_.size()) {
|
||||||
|
SpawnEvent& event = spawn_queue_[index_spawn_actual_];
|
||||||
|
|
||||||
|
if (event.spawnejat) {
|
||||||
|
index_spawn_actual_++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (temps_transcorregut_ >= event.temps_spawn) {
|
||||||
|
// Find first inactive enemy
|
||||||
|
for (auto& enemic : orni_array) {
|
||||||
|
if (!enemic.esta_actiu()) {
|
||||||
|
spawn_enemic(enemic, event.tipus, ship_position_);
|
||||||
|
event.spawnejat = true;
|
||||||
|
index_spawn_actual_++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no slot available, try next frame
|
||||||
|
if (!event.spawnejat) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Not yet time for this spawn
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SpawnController::tots_enemics_spawnejats() const {
|
||||||
|
return index_spawn_actual_ >= spawn_queue_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SpawnController::tots_enemics_destruits(const std::array<Enemic, 15>& orni_array) const {
|
||||||
|
if (!tots_enemics_spawnejats()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& enemic : orni_array) {
|
||||||
|
if (enemic.esta_actiu()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t SpawnController::get_enemics_vius(const std::array<Enemic, 15>& orni_array) const {
|
||||||
|
uint8_t count = 0;
|
||||||
|
for (const auto& enemic : orni_array) {
|
||||||
|
if (enemic.esta_actiu()) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t SpawnController::get_enemics_spawnejats() const {
|
||||||
|
return static_cast<uint8_t>(index_spawn_actual_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnController::generar_spawn_events() {
|
||||||
|
if (!config_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < config_->total_enemics; i++) {
|
||||||
|
float spawn_time = config_->config_spawn.delay_inicial +
|
||||||
|
(i * config_->config_spawn.interval_spawn);
|
||||||
|
|
||||||
|
TipusEnemic tipus = seleccionar_tipus_aleatori();
|
||||||
|
|
||||||
|
spawn_queue_.push_back({spawn_time, tipus, false});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TipusEnemic SpawnController::seleccionar_tipus_aleatori() const {
|
||||||
|
if (!config_) {
|
||||||
|
return TipusEnemic::PENTAGON;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Weighted random selection based on distribution
|
||||||
|
int rand_val = std::rand() % 100;
|
||||||
|
|
||||||
|
if (rand_val < config_->distribucio.pentagon) {
|
||||||
|
return TipusEnemic::PENTAGON;
|
||||||
|
} else if (rand_val < config_->distribucio.pentagon + config_->distribucio.quadrat) {
|
||||||
|
return TipusEnemic::QUADRAT;
|
||||||
|
} else {
|
||||||
|
return TipusEnemic::MOLINILLO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnController::spawn_enemic(Enemic& enemic, TipusEnemic tipus, const Punt* ship_pos) {
|
||||||
|
// Initialize enemy (with safe spawn if ship_pos provided)
|
||||||
|
enemic.inicialitzar(tipus, ship_pos);
|
||||||
|
|
||||||
|
// Apply difficulty multipliers
|
||||||
|
aplicar_multiplicadors(enemic);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnController::aplicar_multiplicadors(Enemic& enemic) const {
|
||||||
|
if (!config_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply velocity multiplier
|
||||||
|
float base_vel = enemic.get_base_velocity();
|
||||||
|
enemic.set_velocity(base_vel * config_->multiplicadors.velocitat);
|
||||||
|
|
||||||
|
// Apply rotation multiplier
|
||||||
|
float base_rot = enemic.get_base_rotation();
|
||||||
|
enemic.set_rotation(base_rot * config_->multiplicadors.rotacio);
|
||||||
|
|
||||||
|
// Apply tracking strength (only affects QUADRAT)
|
||||||
|
enemic.set_tracking_strength(config_->multiplicadors.tracking_strength);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
58
source/game/stage_system/spawn_controller.hpp
Normal file
58
source/game/stage_system/spawn_controller.hpp
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
// spawn_controller.hpp - Controlador de spawn d'enemics
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "core/types.hpp"
|
||||||
|
#include "game/entities/enemic.hpp"
|
||||||
|
#include "stage_config.hpp"
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
// Informació de spawn planificat
|
||||||
|
struct SpawnEvent {
|
||||||
|
float temps_spawn; // Temps absolut (segons) per spawnejar
|
||||||
|
TipusEnemic tipus; // Tipus d'enemic
|
||||||
|
bool spawnejat; // Ja s'ha processat?
|
||||||
|
};
|
||||||
|
|
||||||
|
class SpawnController {
|
||||||
|
public:
|
||||||
|
SpawnController();
|
||||||
|
|
||||||
|
// Configuration
|
||||||
|
void configurar(const ConfigStage* config); // Set stage config
|
||||||
|
void iniciar(); // Generate spawn schedule
|
||||||
|
void reset(); // Clear all pending spawns
|
||||||
|
|
||||||
|
// Update
|
||||||
|
void actualitzar(float delta_time, std::array<Enemic, 15>& orni_array, bool pausar = false);
|
||||||
|
|
||||||
|
// Status queries
|
||||||
|
bool tots_enemics_spawnejats() const;
|
||||||
|
bool tots_enemics_destruits(const std::array<Enemic, 15>& orni_array) const;
|
||||||
|
uint8_t get_enemics_vius(const std::array<Enemic, 15>& orni_array) const;
|
||||||
|
uint8_t get_enemics_spawnejats() const;
|
||||||
|
|
||||||
|
// [NEW] Set ship position reference for safe spawn
|
||||||
|
void set_ship_position(const Punt* ship_pos) { ship_position_ = ship_pos; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const ConfigStage* config_; // Non-owning pointer to current stage config
|
||||||
|
std::vector<SpawnEvent> spawn_queue_;
|
||||||
|
float temps_transcorregut_; // Elapsed time since stage start
|
||||||
|
uint8_t index_spawn_actual_; // Next spawn to process
|
||||||
|
|
||||||
|
// Spawn generation
|
||||||
|
void generar_spawn_events();
|
||||||
|
TipusEnemic seleccionar_tipus_aleatori() const;
|
||||||
|
void spawn_enemic(Enemic& enemic, TipusEnemic tipus, const Punt* ship_pos = nullptr);
|
||||||
|
void aplicar_multiplicadors(Enemic& enemic) const;
|
||||||
|
const Punt* ship_position_; // [NEW] Non-owning pointer to ship position
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
100
source/game/stage_system/stage_config.hpp
Normal file
100
source/game/stage_system/stage_config.hpp
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
// stage_config.hpp - Estructures de dades per configuració d'stages
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
// Tipus de mode de spawn
|
||||||
|
enum class ModeSpawn {
|
||||||
|
PROGRESSIVE, // Spawn progressiu amb intervals
|
||||||
|
IMMEDIATE, // Tots els enemics de cop
|
||||||
|
WAVE // Onades de 3-5 enemics (futura extensió)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Configuració de spawn
|
||||||
|
struct ConfigSpawn {
|
||||||
|
ModeSpawn mode;
|
||||||
|
float delay_inicial; // Segons abans del primer spawn
|
||||||
|
float interval_spawn; // Segons entre spawns consecutius
|
||||||
|
};
|
||||||
|
|
||||||
|
// Distribució de tipus d'enemics (percentatges)
|
||||||
|
struct DistribucioEnemics {
|
||||||
|
uint8_t pentagon; // 0-100
|
||||||
|
uint8_t quadrat; // 0-100
|
||||||
|
uint8_t molinillo; // 0-100
|
||||||
|
// Suma ha de ser 100, validat en StageLoader
|
||||||
|
};
|
||||||
|
|
||||||
|
// Multiplicadors de dificultat
|
||||||
|
struct MultiplicadorsDificultat {
|
||||||
|
float velocitat; // 0.5-2.0 típic
|
||||||
|
float rotacio; // 0.5-2.0 típic
|
||||||
|
float tracking_strength; // 0.0-1.5 (aplicat a Quadrat)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Metadades del fitxer YAML
|
||||||
|
struct MetadataStages {
|
||||||
|
std::string version;
|
||||||
|
uint8_t total_stages;
|
||||||
|
std::string descripcio;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Configuració completa d'un stage
|
||||||
|
struct ConfigStage {
|
||||||
|
uint8_t stage_id; // 1-10
|
||||||
|
uint8_t total_enemics; // 5-15
|
||||||
|
ConfigSpawn config_spawn;
|
||||||
|
DistribucioEnemics distribucio;
|
||||||
|
MultiplicadorsDificultat multiplicadors;
|
||||||
|
|
||||||
|
// Validació
|
||||||
|
bool es_valid() const {
|
||||||
|
return stage_id >= 1 && stage_id <= 255 &&
|
||||||
|
total_enemics > 0 && total_enemics <= 15 &&
|
||||||
|
distribucio.pentagon + distribucio.quadrat + distribucio.molinillo == 100;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Configuració completa del sistema (carregada des de YAML)
|
||||||
|
struct ConfigSistemaStages {
|
||||||
|
MetadataStages metadata;
|
||||||
|
std::vector<ConfigStage> stages; // Índex [0] = stage 1
|
||||||
|
|
||||||
|
// Obtenir configuració d'un stage específic
|
||||||
|
const ConfigStage* obte_stage(uint8_t stage_id) const {
|
||||||
|
if (stage_id < 1 || stage_id > stages.size()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return &stages[stage_id - 1];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Constants per missatges de transició
|
||||||
|
namespace Constants {
|
||||||
|
// Pool de missatges per inici de level (selecció aleatòria)
|
||||||
|
inline constexpr std::array<const char*, 12> MISSATGES_LEVEL_START = {
|
||||||
|
"ORNI ALERT!",
|
||||||
|
"INCOMING ORNIS!",
|
||||||
|
"ROLLING THREAT!",
|
||||||
|
"ENEMY WAVE!",
|
||||||
|
"WAVE OF ORNIS DETECTED!",
|
||||||
|
"NEXT SWARM APPROACHING!",
|
||||||
|
"BRACE FOR THE NEXT WAVE!",
|
||||||
|
"ANOTHER ATTACK INCOMING!",
|
||||||
|
"SENSORS DETECT HOSTILE ORNIS...",
|
||||||
|
"UNIDENTIFIED ROLLING OBJECTS INBOUND!",
|
||||||
|
"ENEMY FORCES MOBILIZING!",
|
||||||
|
"PREPARE FOR IMPACT!"
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr const char* MISSATGE_LEVEL_COMPLETED = "GOOD JOB COMMANDER!";
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
251
source/game/stage_system/stage_loader.cpp
Normal file
251
source/game/stage_system/stage_loader.cpp
Normal file
@@ -0,0 +1,251 @@
|
|||||||
|
// stage_loader.cpp - Implementació del carregador de configuració YAML
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#include "stage_loader.hpp"
|
||||||
|
|
||||||
|
#include "core/resources/resource_helper.hpp"
|
||||||
|
#include "external/fkyaml_node.hpp"
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
std::unique_ptr<ConfigSistemaStages> StageLoader::carregar(const std::string& path) {
|
||||||
|
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 carregar " << normalized << std::endl;
|
||||||
|
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<ConfigSistemaStages>();
|
||||||
|
|
||||||
|
// Parse metadata
|
||||||
|
if (!yaml.contains("metadata")) {
|
||||||
|
std::cerr << "[StageLoader] Error: falta camp 'metadata'" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
if (!parse_metadata(yaml["metadata"], config->metadata)) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse stages
|
||||||
|
if (!yaml.contains("stages")) {
|
||||||
|
std::cerr << "[StageLoader] Error: falta camp 'stages'" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!yaml["stages"].is_sequence()) {
|
||||||
|
std::cerr << "[StageLoader] Error: 'stages' ha de ser una llista" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& stage_yaml : yaml["stages"]) {
|
||||||
|
ConfigStage stage;
|
||||||
|
if (!parse_stage(stage_yaml, stage)) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
config->stages.push_back(stage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validar configuració
|
||||||
|
if (!validar_config(*config)) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[StageLoader] Carregats " << config->stages.size()
|
||||||
|
<< " stages correctament" << std::endl;
|
||||||
|
return config;
|
||||||
|
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[StageLoader] Excepció: " << e.what() << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageLoader::parse_metadata(const fkyaml::node& yaml, MetadataStages& meta) {
|
||||||
|
try {
|
||||||
|
if (!yaml.contains("version") || !yaml.contains("total_stages")) {
|
||||||
|
std::cerr << "[StageLoader] Error: metadata incompleta" << std::endl;
|
||||||
|
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() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageLoader::parse_stage(const fkyaml::node& yaml, ConfigStage& stage) {
|
||||||
|
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" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
stage.stage_id = yaml["stage_id"].get_value<uint8_t>();
|
||||||
|
stage.total_enemics = yaml["total_enemies"].get_value<uint8_t>();
|
||||||
|
|
||||||
|
if (!parse_spawn_config(yaml["spawn_config"], stage.config_spawn)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!parse_distribution(yaml["enemy_distribution"], stage.distribucio)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!parse_multipliers(yaml["difficulty_multipliers"], stage.multiplicadors)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!stage.es_valid()) {
|
||||||
|
std::cerr << "[StageLoader] Error: stage " << static_cast<int>(stage.stage_id)
|
||||||
|
<< " no és vàlid" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[StageLoader] Error parsing stage: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageLoader::parse_spawn_config(const fkyaml::node& yaml, ConfigSpawn& config) {
|
||||||
|
try {
|
||||||
|
if (!yaml.contains("mode") || !yaml.contains("initial_delay") ||
|
||||||
|
!yaml.contains("spawn_interval")) {
|
||||||
|
std::cerr << "[StageLoader] Error: spawn_config incompleta" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string mode_str = yaml["mode"].get_value<std::string>();
|
||||||
|
config.mode = parse_spawn_mode(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() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageLoader::parse_distribution(const fkyaml::node& yaml, DistribucioEnemics& dist) {
|
||||||
|
try {
|
||||||
|
if (!yaml.contains("pentagon") || !yaml.contains("quadrat") ||
|
||||||
|
!yaml.contains("molinillo")) {
|
||||||
|
std::cerr << "[StageLoader] Error: enemy_distribution incompleta" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
dist.pentagon = yaml["pentagon"].get_value<uint8_t>();
|
||||||
|
dist.quadrat = yaml["quadrat"].get_value<uint8_t>();
|
||||||
|
dist.molinillo = yaml["molinillo"].get_value<uint8_t>();
|
||||||
|
|
||||||
|
// Validar que suma 100
|
||||||
|
int sum = dist.pentagon + dist.quadrat + dist.molinillo;
|
||||||
|
if (sum != 100) {
|
||||||
|
std::cerr << "[StageLoader] Error: distribució no suma 100 (suma=" << sum << ")" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[StageLoader] Error parsing distribution: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageLoader::parse_multipliers(const fkyaml::node& yaml, MultiplicadorsDificultat& mult) {
|
||||||
|
try {
|
||||||
|
if (!yaml.contains("speed_multiplier") || !yaml.contains("rotation_multiplier") ||
|
||||||
|
!yaml.contains("tracking_strength")) {
|
||||||
|
std::cerr << "[StageLoader] Error: difficulty_multipliers incompleta" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
mult.velocitat = yaml["speed_multiplier"].get_value<float>();
|
||||||
|
mult.rotacio = yaml["rotation_multiplier"].get_value<float>();
|
||||||
|
mult.tracking_strength = yaml["tracking_strength"].get_value<float>();
|
||||||
|
|
||||||
|
// Validar rangs raonables
|
||||||
|
if (mult.velocitat < 0.1f || mult.velocitat > 5.0f) {
|
||||||
|
std::cerr << "[StageLoader] Warning: speed_multiplier fora de rang (0.1-5.0)" << std::endl;
|
||||||
|
}
|
||||||
|
if (mult.rotacio < 0.1f || mult.rotacio > 5.0f) {
|
||||||
|
std::cerr << "[StageLoader] Warning: rotation_multiplier fora de rang (0.1-5.0)" << std::endl;
|
||||||
|
}
|
||||||
|
if (mult.tracking_strength < 0.0f || mult.tracking_strength > 2.0f) {
|
||||||
|
std::cerr << "[StageLoader] Warning: tracking_strength fora de rang (0.0-2.0)" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "[StageLoader] Error parsing multipliers: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ModeSpawn StageLoader::parse_spawn_mode(const std::string& mode_str) {
|
||||||
|
if (mode_str == "progressive") {
|
||||||
|
return ModeSpawn::PROGRESSIVE;
|
||||||
|
} else if (mode_str == "immediate") {
|
||||||
|
return ModeSpawn::IMMEDIATE;
|
||||||
|
} else if (mode_str == "wave") {
|
||||||
|
return ModeSpawn::WAVE;
|
||||||
|
} else {
|
||||||
|
std::cerr << "[StageLoader] Warning: mode de spawn desconegut '" << mode_str
|
||||||
|
<< "', usant PROGRESSIVE" << std::endl;
|
||||||
|
return ModeSpawn::PROGRESSIVE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageLoader::validar_config(const ConfigSistemaStages& config) {
|
||||||
|
if (config.stages.empty()) {
|
||||||
|
std::cerr << "[StageLoader] Error: cap stage carregat" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.stages.size() != config.metadata.total_stages) {
|
||||||
|
std::cerr << "[StageLoader] Warning: nombre de stages (" << config.stages.size()
|
||||||
|
<< ") no coincideix amb metadata.total_stages ("
|
||||||
|
<< static_cast<int>(config.metadata.total_stages) << ")" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
<< ")" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
32
source/game/stage_system/stage_loader.hpp
Normal file
32
source/game/stage_system/stage_loader.hpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
// stage_loader.hpp - Carregador de configuració YAML
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include "external/fkyaml_node.hpp"
|
||||||
|
#include "stage_config.hpp"
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
class StageLoader {
|
||||||
|
public:
|
||||||
|
// Carregar configuració des de fitxer YAML
|
||||||
|
// Retorna nullptr si hi ha errors
|
||||||
|
static std::unique_ptr<ConfigSistemaStages> carregar(const std::string& path);
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Parsing helpers (implementats en .cpp)
|
||||||
|
static bool parse_metadata(const fkyaml::node& yaml, MetadataStages& meta);
|
||||||
|
static bool parse_stage(const fkyaml::node& yaml, ConfigStage& stage);
|
||||||
|
static bool parse_spawn_config(const fkyaml::node& yaml, ConfigSpawn& config);
|
||||||
|
static bool parse_distribution(const fkyaml::node& yaml, DistribucioEnemics& dist);
|
||||||
|
static bool parse_multipliers(const fkyaml::node& yaml, MultiplicadorsDificultat& mult);
|
||||||
|
static ModeSpawn parse_spawn_mode(const std::string& mode_str);
|
||||||
|
|
||||||
|
// Validació
|
||||||
|
static bool validar_config(const ConfigSistemaStages& config);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
146
source/game/stage_system/stage_manager.cpp
Normal file
146
source/game/stage_system/stage_manager.cpp
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
// stage_manager.cpp - Implementació del gestor d'stages
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#include "stage_manager.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "core/defaults.hpp"
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
StageManager::StageManager(const ConfigSistemaStages* config)
|
||||||
|
: config_(config),
|
||||||
|
estat_(EstatStage::LEVEL_START),
|
||||||
|
stage_actual_(1),
|
||||||
|
timer_transicio_(0.0f) {
|
||||||
|
if (!config_) {
|
||||||
|
std::cerr << "[StageManager] Error: config és null" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::inicialitzar() {
|
||||||
|
stage_actual_ = 1;
|
||||||
|
carregar_stage(stage_actual_);
|
||||||
|
canviar_estat(EstatStage::LEVEL_START);
|
||||||
|
|
||||||
|
std::cout << "[StageManager] Inicialitzat a stage " << static_cast<int>(stage_actual_)
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::actualitzar(float delta_time, bool pausar_spawn) {
|
||||||
|
switch (estat_) {
|
||||||
|
case EstatStage::LEVEL_START:
|
||||||
|
processar_level_start(delta_time);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EstatStage::PLAYING:
|
||||||
|
processar_playing(delta_time, pausar_spawn);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EstatStage::LEVEL_COMPLETED:
|
||||||
|
processar_level_completed(delta_time);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::stage_completat() {
|
||||||
|
std::cout << "[StageManager] Stage " << static_cast<int>(stage_actual_) << " completat!"
|
||||||
|
<< std::endl;
|
||||||
|
canviar_estat(EstatStage::LEVEL_COMPLETED);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool StageManager::tot_completat() const {
|
||||||
|
return stage_actual_ >= config_->metadata.total_stages &&
|
||||||
|
estat_ == EstatStage::LEVEL_COMPLETED &&
|
||||||
|
timer_transicio_ <= 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
const ConfigStage* StageManager::get_config_actual() const {
|
||||||
|
return config_->obte_stage(stage_actual_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::canviar_estat(EstatStage nou_estat) {
|
||||||
|
estat_ = nou_estat;
|
||||||
|
|
||||||
|
// Set timer based on state type
|
||||||
|
if (nou_estat == EstatStage::LEVEL_START) {
|
||||||
|
timer_transicio_ = Defaults::Game::LEVEL_START_DURATION;
|
||||||
|
} else if (nou_estat == EstatStage::LEVEL_COMPLETED) {
|
||||||
|
timer_transicio_ = Defaults::Game::LEVEL_COMPLETED_DURATION;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select random message when entering LEVEL_START
|
||||||
|
if (nou_estat == EstatStage::LEVEL_START) {
|
||||||
|
size_t index = static_cast<size_t>(std::rand()) % Constants::MISSATGES_LEVEL_START.size();
|
||||||
|
missatge_level_start_actual_ = Constants::MISSATGES_LEVEL_START[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "[StageManager] Canvi d'estat: ";
|
||||||
|
switch (nou_estat) {
|
||||||
|
case EstatStage::LEVEL_START:
|
||||||
|
std::cout << "LEVEL_START";
|
||||||
|
break;
|
||||||
|
case EstatStage::PLAYING:
|
||||||
|
std::cout << "PLAYING";
|
||||||
|
break;
|
||||||
|
case EstatStage::LEVEL_COMPLETED:
|
||||||
|
std::cout << "LEVEL_COMPLETED";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::processar_level_start(float delta_time) {
|
||||||
|
timer_transicio_ -= delta_time;
|
||||||
|
|
||||||
|
if (timer_transicio_ <= 0.0f) {
|
||||||
|
canviar_estat(EstatStage::PLAYING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::processar_playing(float delta_time, bool pausar_spawn) {
|
||||||
|
// Update spawn controller (pauses when pausar_spawn = true)
|
||||||
|
// Note: The actual enemy array update happens in EscenaJoc::actualitzar()
|
||||||
|
// This is just for internal timekeeping
|
||||||
|
(void)delta_time; // Spawn controller is updated externally
|
||||||
|
(void)pausar_spawn; // Passed to spawn_controller_.actualitzar() by EscenaJoc
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::processar_level_completed(float delta_time) {
|
||||||
|
timer_transicio_ -= delta_time;
|
||||||
|
|
||||||
|
if (timer_transicio_ <= 0.0f) {
|
||||||
|
// Advance to next stage
|
||||||
|
stage_actual_++;
|
||||||
|
|
||||||
|
// Loop back to stage 1 after final stage
|
||||||
|
if (stage_actual_ > config_->metadata.total_stages) {
|
||||||
|
stage_actual_ = 1;
|
||||||
|
std::cout << "[StageManager] Totes les stages completades! Tornant a stage 1"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load next stage
|
||||||
|
carregar_stage(stage_actual_);
|
||||||
|
canviar_estat(EstatStage::LEVEL_START);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void StageManager::carregar_stage(uint8_t stage_id) {
|
||||||
|
const ConfigStage* stage_config = config_->obte_stage(stage_id);
|
||||||
|
if (!stage_config) {
|
||||||
|
std::cerr << "[StageManager] Error: no es pot trobar stage " << static_cast<int>(stage_id)
|
||||||
|
<< std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure spawn controller
|
||||||
|
spawn_controller_.configurar(stage_config);
|
||||||
|
spawn_controller_.iniciar();
|
||||||
|
|
||||||
|
std::cout << "[StageManager] Carregat stage " << static_cast<int>(stage_id) << ": "
|
||||||
|
<< static_cast<int>(stage_config->total_enemics) << " enemics" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
61
source/game/stage_system/stage_manager.hpp
Normal file
61
source/game/stage_system/stage_manager.hpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
// stage_manager.hpp - Gestor d'estat i progressió d'stages
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "spawn_controller.hpp"
|
||||||
|
#include "stage_config.hpp"
|
||||||
|
|
||||||
|
namespace StageSystem {
|
||||||
|
|
||||||
|
// Estats del stage system
|
||||||
|
enum class EstatStage {
|
||||||
|
LEVEL_START, // Pantalla "ENEMY INCOMING" (3s)
|
||||||
|
PLAYING, // Gameplay normal
|
||||||
|
LEVEL_COMPLETED // Pantalla "GOOD JOB COMMANDER!" (3s)
|
||||||
|
};
|
||||||
|
|
||||||
|
class StageManager {
|
||||||
|
public:
|
||||||
|
explicit StageManager(const ConfigSistemaStages* config);
|
||||||
|
|
||||||
|
// Lifecycle
|
||||||
|
void inicialitzar(); // Reset to stage 1
|
||||||
|
void actualitzar(float delta_time, bool pausar_spawn = false);
|
||||||
|
|
||||||
|
// Stage progression
|
||||||
|
void stage_completat(); // Call when all enemies destroyed
|
||||||
|
bool tot_completat() const; // All 10 stages done?
|
||||||
|
|
||||||
|
// Current state queries
|
||||||
|
EstatStage get_estat() const { return estat_; }
|
||||||
|
uint8_t get_stage_actual() const { return stage_actual_; }
|
||||||
|
const ConfigStage* get_config_actual() const;
|
||||||
|
float get_timer_transicio() const { return timer_transicio_; }
|
||||||
|
const std::string& get_missatge_level_start() const { return missatge_level_start_actual_; }
|
||||||
|
|
||||||
|
// Spawn control (delegate to SpawnController)
|
||||||
|
SpawnController& get_spawn_controller() { return spawn_controller_; }
|
||||||
|
const SpawnController& get_spawn_controller() const { return spawn_controller_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const ConfigSistemaStages* config_; // Non-owning pointer
|
||||||
|
SpawnController spawn_controller_;
|
||||||
|
|
||||||
|
EstatStage estat_;
|
||||||
|
uint8_t stage_actual_; // 1-10
|
||||||
|
float timer_transicio_; // Timer for LEVEL_START/LEVEL_COMPLETED (3.0s → 0.0s)
|
||||||
|
std::string missatge_level_start_actual_; // Missatge seleccionat per al level actual
|
||||||
|
|
||||||
|
// State transitions
|
||||||
|
void canviar_estat(EstatStage nou_estat);
|
||||||
|
void processar_level_start(float delta_time);
|
||||||
|
void processar_playing(float delta_time, bool pausar_spawn);
|
||||||
|
void processar_level_completed(float delta_time);
|
||||||
|
void carregar_stage(uint8_t stage_id);
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace StageSystem
|
||||||
20
tools/pack_resources/Makefile
Normal file
20
tools/pack_resources/Makefile
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Makefile per a pack_resources
|
||||||
|
# © 2025 Orni Attack
|
||||||
|
|
||||||
|
CXX = clang++
|
||||||
|
CXXFLAGS = -std=c++20 -Wall -Wextra -I../../source
|
||||||
|
TARGET = pack_resources
|
||||||
|
|
||||||
|
SOURCES = pack_resources.cpp \
|
||||||
|
../../source/core/resources/resource_pack.cpp
|
||||||
|
|
||||||
|
$(TARGET): $(SOURCES)
|
||||||
|
@echo "Compilant $(TARGET)..."
|
||||||
|
@$(CXX) $(CXXFLAGS) -o $(TARGET) $(SOURCES)
|
||||||
|
@echo "✓ $(TARGET) compilat"
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -f $(TARGET)
|
||||||
|
@echo "✓ Netejat"
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
92
tools/pack_resources/pack_resources.cpp
Normal file
92
tools/pack_resources/pack_resources.cpp
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// pack_resources.cpp - Utilitat per crear paquets de recursos
|
||||||
|
// © 2025 Orni Attack
|
||||||
|
|
||||||
|
#include "../../source/core/resources/resource_pack.hpp"
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
std::string input_dir = "data";
|
||||||
|
std::string output_file = "resources.pack";
|
||||||
|
|
||||||
|
// Processar arguments
|
||||||
|
if (argc == 2 && std::string(argv[1]) == "--help") {
|
||||||
|
print_usage(argv[0]);
|
||||||
|
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 << "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";
|
||||||
|
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";
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!pack.savePack(output_file)) {
|
||||||
|
std::cerr << "ERROR: No s'ha pogut guardar el paquet\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";
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -44,8 +44,9 @@ def apply_transform(punto, matrix):
|
|||||||
|
|
||||||
def parse_svg_path(d_attr):
|
def parse_svg_path(d_attr):
|
||||||
"""
|
"""
|
||||||
Convierte comandos SVG path (M y L) a lista de puntos [(x, y), ...]
|
Convierte comandos SVG path (M y L) a lista de polylines separadas.
|
||||||
Ejemplo: "M896,1693L896,1531.23L1219.53,1531.23..." → [(896, 1693), (896, 1531.23), ...]
|
Cada comando M inicia una nueva polyline.
|
||||||
|
Retorna: lista de listas de puntos [[(x,y), ...], [(x,y), ...], ...]
|
||||||
"""
|
"""
|
||||||
# Reemplazar comas por espacios para facilitar parsing
|
# Reemplazar comas por espacios para facilitar parsing
|
||||||
d_attr = d_attr.replace(',', ' ')
|
d_attr = d_attr.replace(',', ' ')
|
||||||
@@ -55,7 +56,9 @@ def parse_svg_path(d_attr):
|
|||||||
d_attr = re.sub(r'([ML])', r'|\1', d_attr)
|
d_attr = re.sub(r'([ML])', r'|\1', d_attr)
|
||||||
commands = [c.strip() for c in d_attr.split('|') if c.strip()]
|
commands = [c.strip() for c in d_attr.split('|') if c.strip()]
|
||||||
|
|
||||||
points = []
|
polylines = [] # Lista de polylines
|
||||||
|
current_polyline = [] # Polyline actual
|
||||||
|
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
if not cmd:
|
if not cmd:
|
||||||
continue
|
continue
|
||||||
@@ -70,18 +73,29 @@ def parse_svg_path(d_attr):
|
|||||||
# Parsear pares de coordenadas
|
# Parsear pares de coordenadas
|
||||||
coords = coords_str.split()
|
coords = coords_str.split()
|
||||||
|
|
||||||
|
# Si es comando M (MoveTo), empezar nueva polyline
|
||||||
|
if cmd_letter == 'M':
|
||||||
|
# Guardar polyline anterior si tiene puntos
|
||||||
|
if current_polyline:
|
||||||
|
polylines.append(current_polyline)
|
||||||
|
current_polyline = []
|
||||||
|
|
||||||
# Procesar en pares (x, y)
|
# Procesar en pares (x, y)
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(coords) - 1:
|
while i < len(coords) - 1:
|
||||||
try:
|
try:
|
||||||
x = float(coords[i])
|
x = float(coords[i])
|
||||||
y = float(coords[i + 1])
|
y = float(coords[i + 1])
|
||||||
points.append((x, y))
|
current_polyline.append((x, y))
|
||||||
i += 2
|
i += 2
|
||||||
except (ValueError, IndexError):
|
except (ValueError, IndexError):
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
return points
|
# No olvidar la última polyline
|
||||||
|
if current_polyline:
|
||||||
|
polylines.append(current_polyline)
|
||||||
|
|
||||||
|
return polylines
|
||||||
|
|
||||||
|
|
||||||
def rect_to_points(rect_elem):
|
def rect_to_points(rect_elem):
|
||||||
@@ -103,11 +117,22 @@ def rect_to_points(rect_elem):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def calc_bounding_box(puntos):
|
def calc_bounding_box(polylines):
|
||||||
"""
|
"""
|
||||||
Calcula bounding box de una lista de puntos
|
Calcula bounding box de una o varias polylines
|
||||||
|
polylines puede ser:
|
||||||
|
- lista de puntos [(x,y), ...]
|
||||||
|
- lista de polylines [[(x,y), ...], [(x,y), ...]]
|
||||||
Retorna: (min_x, max_x, min_y, max_y, ancho, alto)
|
Retorna: (min_x, max_x, min_y, max_y, ancho, alto)
|
||||||
"""
|
"""
|
||||||
|
# Aplanar si es lista de polylines
|
||||||
|
puntos = []
|
||||||
|
if polylines and isinstance(polylines[0], list):
|
||||||
|
for polyline in polylines:
|
||||||
|
puntos.extend(polyline)
|
||||||
|
else:
|
||||||
|
puntos = polylines
|
||||||
|
|
||||||
if not puntos:
|
if not puntos:
|
||||||
return (0, 0, 0, 0, 0, 0)
|
return (0, 0, 0, 0, 0, 0)
|
||||||
|
|
||||||
@@ -125,17 +150,18 @@ def calc_bounding_box(puntos):
|
|||||||
return (min_x, max_x, min_y, max_y, ancho, alto)
|
return (min_x, max_x, min_y, max_y, ancho, alto)
|
||||||
|
|
||||||
|
|
||||||
def normalizar_letra(nombre, puntos, altura_objetivo=100.0):
|
def normalizar_letra(nombre, polylines, altura_objetivo=100.0):
|
||||||
"""
|
"""
|
||||||
Escala y traslada letra para que tenga altura_objetivo pixels
|
Escala y traslada letra para que tenga altura_objetivo pixels
|
||||||
y esté centrada en origen (0, 0) en esquina superior izquierda
|
y esté centrada en origen (0, 0) en esquina superior izquierda
|
||||||
|
|
||||||
Retorna: dict con puntos normalizados, centro, ancho, alto
|
polylines: lista de polylines [[(x,y), ...], [(x,y), ...]]
|
||||||
|
Retorna: dict con polylines normalizadas, centro, ancho, alto
|
||||||
"""
|
"""
|
||||||
if not puntos:
|
if not polylines:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
min_x, max_x, min_y, max_y, ancho, alto = calc_bounding_box(puntos)
|
min_x, max_x, min_y, max_y, ancho, alto = calc_bounding_box(polylines)
|
||||||
|
|
||||||
if alto == 0:
|
if alto == 0:
|
||||||
print(f" [WARN] Letra {nombre}: altura cero")
|
print(f" [WARN] Letra {nombre}: altura cero")
|
||||||
@@ -144,14 +170,26 @@ def normalizar_letra(nombre, puntos, altura_objetivo=100.0):
|
|||||||
# Factor de escala basado en altura
|
# Factor de escala basado en altura
|
||||||
escala = altura_objetivo / alto
|
escala = altura_objetivo / alto
|
||||||
|
|
||||||
# Normalizar puntos:
|
# Normalizar cada polyline:
|
||||||
# 1. Trasladar a origen (restar min_x, min_y)
|
# 1. Trasladar a origen (restar min_x, min_y)
|
||||||
# 2. Aplicar escala
|
# 2. Aplicar escala
|
||||||
puntos_norm = []
|
# 3. Cerrar polyline (último punto = primer punto)
|
||||||
for x, y in puntos:
|
polylines_norm = []
|
||||||
x_norm = (x - min_x) * escala
|
total_puntos = 0
|
||||||
y_norm = (y - min_y) * escala
|
|
||||||
puntos_norm.append((x_norm, y_norm))
|
for polyline in polylines:
|
||||||
|
polyline_norm = []
|
||||||
|
for x, y in polyline:
|
||||||
|
x_norm = (x - min_x) * escala
|
||||||
|
y_norm = (y - min_y) * escala
|
||||||
|
polyline_norm.append((x_norm, y_norm))
|
||||||
|
|
||||||
|
# Cerrar polyline si no está cerrada
|
||||||
|
if polyline_norm and polyline_norm[0] != polyline_norm[-1]:
|
||||||
|
polyline_norm.append(polyline_norm[0])
|
||||||
|
|
||||||
|
polylines_norm.append(polyline_norm)
|
||||||
|
total_puntos += len(polyline_norm)
|
||||||
|
|
||||||
# Calcular dimensiones finales
|
# Calcular dimensiones finales
|
||||||
ancho_norm = ancho * escala
|
ancho_norm = ancho * escala
|
||||||
@@ -162,10 +200,11 @@ def normalizar_letra(nombre, puntos, altura_objetivo=100.0):
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
'nombre': nombre,
|
'nombre': nombre,
|
||||||
'puntos': puntos_norm,
|
'polylines': polylines_norm,
|
||||||
'centro': centro,
|
'centro': centro,
|
||||||
'ancho': ancho_norm,
|
'ancho': ancho_norm,
|
||||||
'alto': alto_norm
|
'alto': alto_norm,
|
||||||
|
'total_puntos': total_puntos
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -178,6 +217,7 @@ def generar_shp(letra_norm, output_dir):
|
|||||||
scale: 1.0
|
scale: 1.0
|
||||||
center: cx, cy
|
center: cx, cy
|
||||||
polyline: x1,y1 x2,y2 x3,y3 ...
|
polyline: x1,y1 x2,y2 x3,y3 ...
|
||||||
|
polyline: x1,y1 x2,y2 x3,y3 ... (si hay múltiples formas)
|
||||||
"""
|
"""
|
||||||
if not letra_norm:
|
if not letra_norm:
|
||||||
return
|
return
|
||||||
@@ -198,13 +238,13 @@ def generar_shp(letra_norm, output_dir):
|
|||||||
f.write(f"center: {letra_norm['centro'][0]:.2f}, {letra_norm['centro'][1]:.2f}\n")
|
f.write(f"center: {letra_norm['centro'][0]:.2f}, {letra_norm['centro'][1]:.2f}\n")
|
||||||
f.write(f"\n")
|
f.write(f"\n")
|
||||||
|
|
||||||
# Polyline con todos los puntos
|
# Generar una línea polyline por cada forma
|
||||||
f.write("polyline: ")
|
for polyline in letra_norm['polylines']:
|
||||||
puntos_str = " ".join([f"{x:.2f},{y:.2f}" for x, y in letra_norm['puntos']])
|
puntos_str = " ".join([f"{x:.2f},{y:.2f}" for x, y in polyline])
|
||||||
f.write(puntos_str)
|
f.write(f"polyline: {puntos_str}\n")
|
||||||
f.write("\n")
|
|
||||||
|
|
||||||
print(f" ✓ {nombre_archivo:20} ({len(letra_norm['puntos']):3} puntos, "
|
print(f" ✓ {nombre_archivo:20} ({len(letra_norm['polylines'])} formas, "
|
||||||
|
f"{letra_norm['total_puntos']:3} puntos, "
|
||||||
f"{letra_norm['ancho']:6.2f} x {letra_norm['alto']:6.2f} px)")
|
f"{letra_norm['ancho']:6.2f} x {letra_norm['alto']:6.2f} px)")
|
||||||
|
|
||||||
|
|
||||||
@@ -232,14 +272,18 @@ def parse_svg(filepath):
|
|||||||
|
|
||||||
print(f"[INFO] Transform matrix: {transform_matrix}")
|
print(f"[INFO] Transform matrix: {transform_matrix}")
|
||||||
|
|
||||||
# Extraer paths y rects
|
# Extraer paths y rects (buscar recursivamente en todos los descendientes)
|
||||||
paths = group.findall('svg:path', ns)
|
paths = group.findall('.//svg:path', ns)
|
||||||
rects = group.findall('svg:rect', ns)
|
rects = group.findall('.//svg:rect', ns)
|
||||||
|
|
||||||
print(f"[INFO] Encontrados {len(paths)} paths y {len(rects)} rects")
|
print(f"[INFO] Encontrados {len(paths)} paths y {len(rects)} rects")
|
||||||
|
|
||||||
# Nombres de las letras para paths (sin I que es un rect)
|
# Nombres de las letras para "ORNI ATTACK!"
|
||||||
nombres_paths = ['J', 'A', 'L', 'G', 'A', 'M', 'E', 'S']
|
# Grupo 1 (top): O, R, N, I (3 paths + 1 rect)
|
||||||
|
# Grupo 2 (bottom): A, T, T, A, C, K, ! (6 paths + 1 path para !)
|
||||||
|
# Total: 9 paths + 1 rect
|
||||||
|
# Asumiendo orden de aparición en SVG:
|
||||||
|
nombres_paths = ['O', 'R', 'N', 'A', 'T', 'T', 'A', 'C', 'K', 'EXCLAMACION']
|
||||||
|
|
||||||
letras = []
|
letras = []
|
||||||
|
|
||||||
@@ -252,15 +296,18 @@ def parse_svg(filepath):
|
|||||||
if not d_attr:
|
if not d_attr:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Parsear puntos del path
|
# Parsear polylines del path (ahora retorna lista de polylines)
|
||||||
puntos = parse_svg_path(d_attr)
|
polylines = parse_svg_path(d_attr)
|
||||||
|
|
||||||
# Aplicar transformación
|
# Aplicar transformación a cada polyline
|
||||||
puntos = [apply_transform(p, transform_matrix) for p in puntos]
|
polylines_transformed = []
|
||||||
|
for polyline in polylines:
|
||||||
|
polyline_transformed = [apply_transform(p, transform_matrix) for p in polyline]
|
||||||
|
polylines_transformed.append(polyline_transformed)
|
||||||
|
|
||||||
letras.append({
|
letras.append({
|
||||||
'nombre': nombres_paths[i],
|
'nombre': nombres_paths[i],
|
||||||
'puntos': puntos
|
'polylines': polylines_transformed
|
||||||
})
|
})
|
||||||
|
|
||||||
# Procesar rects (la letra I es un rect)
|
# Procesar rects (la letra I es un rect)
|
||||||
@@ -268,11 +315,12 @@ def parse_svg(filepath):
|
|||||||
puntos = rect_to_points(rect)
|
puntos = rect_to_points(rect)
|
||||||
|
|
||||||
# Aplicar transformación
|
# Aplicar transformación
|
||||||
puntos = [apply_transform(p, transform_matrix) for p in puntos]
|
puntos_transformed = [apply_transform(p, transform_matrix) for p in puntos]
|
||||||
|
|
||||||
|
# Rect es una sola polyline
|
||||||
letras.append({
|
letras.append({
|
||||||
'nombre': 'I',
|
'nombre': 'I',
|
||||||
'puntos': puntos
|
'polylines': [puntos_transformed]
|
||||||
})
|
})
|
||||||
|
|
||||||
return letras
|
return letras
|
||||||
@@ -326,7 +374,7 @@ def main():
|
|||||||
|
|
||||||
for nombre in sorted(letras_unicas.keys()):
|
for nombre in sorted(letras_unicas.keys()):
|
||||||
letra = letras_unicas[nombre]
|
letra = letras_unicas[nombre]
|
||||||
letra_norm = normalizar_letra(nombre, letra['puntos'], altura_objetivo=100.0)
|
letra_norm = normalizar_letra(nombre, letra['polylines'], altura_objetivo=100.0)
|
||||||
|
|
||||||
if letra_norm:
|
if letra_norm:
|
||||||
generar_shp(letra_norm, output_dir)
|
generar_shp(letra_norm, output_dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user