diff --git a/.github/workflows/build_web.yml b/.github/workflows/build_web.yml index 9c84b83b..2b9a2f3e 100644 --- a/.github/workflows/build_web.yml +++ b/.github/workflows/build_web.yml @@ -1,11 +1,9 @@ name: Web Build (Emscripten) -on: - push: - branches: - wasm +on: [push, pull_request] jobs: - Push: + Build: + if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c07cfbf..419253a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,8 @@ include_directories(bomberman sources) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/wal) add_subdirectory(${PROJECT_SOURCE_DIR}/lib/Ray) +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2") + if (EMSCRIPTEN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") @@ -70,11 +72,15 @@ set(SOURCES sources/Component/Button/ButtonComponent.hpp sources/System/MenuControllable/MenuControllableSystem.cpp sources/System/MenuControllable/MenuControllableSystem.hpp + sources/Component/Animator/AnimatorComponent.cpp + sources/Component/Animator/AnimatorComponent.hpp + sources/System/Animator/AnimatorSystem.cpp + sources/System/Animator/AnimatorSystem.hpp ) add_executable(bomberman sources/main.cpp ${SOURCES} - ) + ) target_include_directories(bomberman PUBLIC sources) target_link_libraries(bomberman PUBLIC wal ray) diff --git a/README.md b/README.md index 0d19b943..6bfa9002 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ mkdir build cd build cmake .. cmake --build . -cd - +cd .. ``` Enjoy ! diff --git a/assets/wall/breakable_wall.mtl b/assets/map/breakable_wall.mtl similarity index 100% rename from assets/wall/breakable_wall.mtl rename to assets/map/breakable_wall.mtl diff --git a/assets/wall/breakable_wall.obj b/assets/map/breakable_wall.obj similarity index 100% rename from assets/wall/breakable_wall.obj rename to assets/map/breakable_wall.obj diff --git a/assets/map/breakable_wall.png b/assets/map/breakable_wall.png new file mode 100644 index 00000000..db05f964 Binary files /dev/null and b/assets/map/breakable_wall.png differ diff --git a/assets/wall/floor.mtl b/assets/map/bumper.mtl similarity index 100% rename from assets/wall/floor.mtl rename to assets/map/bumper.mtl diff --git a/assets/wall/hole.obj b/assets/map/bumper.obj similarity index 100% rename from assets/wall/hole.obj rename to assets/map/bumper.obj diff --git a/assets/map/bumper.png b/assets/map/bumper.png new file mode 100644 index 00000000..a86b67f2 Binary files /dev/null and b/assets/map/bumper.png differ diff --git a/assets/wall/hole.mtl b/assets/map/floor.mtl similarity index 100% rename from assets/wall/hole.mtl rename to assets/map/floor.mtl diff --git a/assets/wall/floor.obj b/assets/map/floor.obj similarity index 100% rename from assets/wall/floor.obj rename to assets/map/floor.obj diff --git a/assets/wall/floor.png b/assets/map/floor.png similarity index 100% rename from assets/wall/floor.png rename to assets/map/floor.png diff --git a/assets/map/hole.mtl b/assets/map/hole.mtl new file mode 100644 index 00000000..f6f7ca22 --- /dev/null +++ b/assets/map/hole.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl floor +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd hole.png diff --git a/assets/map/hole.obj b/assets/map/hole.obj new file mode 100644 index 00000000..094da9d2 --- /dev/null +++ b/assets/map/hole.obj @@ -0,0 +1,54 @@ +# Blender v2.92.0 OBJ File: '' +# www.blender.org +mtllib upper_floor.mtl +o floor +v -0.500000 -0.499144 0.500854 +v -0.500000 -0.500854 -0.499144 +v 0.500000 -0.499144 0.500855 +v 0.500000 -0.500855 -0.499144 +v 0.500000 0.500854 0.499144 +v 0.500000 0.499144 -0.500854 +v -0.500000 0.500855 0.499144 +v -0.500000 0.499144 -0.500855 +vt 0.000977 0.667790 +vt 0.000977 0.333151 +vt 0.333659 0.667790 +vt 0.333659 0.333151 +vt 0.666341 0.667790 +vt 0.666341 0.333151 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.000977 0.667790 +vt 0.000977 1.002429 +vt 0.666341 0.667790 +vt 0.666341 1.002429 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.666341 0.333151 +vt 0.999024 0.333151 +vt 0.666341 0.667790 +vt 0.999024 0.667790 +vt 0.999024 1.002429 +vt 0.666341 1.002429 +vt 0.999024 0.667790 +vt 0.666341 0.667790 +vn -0.0000 -1.0000 0.0017 +vn 1.0000 -0.0000 0.0000 +vn 0.0000 1.0000 -0.0017 +vn -1.0000 0.0000 -0.0000 +vn 0.0000 -0.0017 -1.0000 +vn -0.0000 0.0017 1.0000 +usemtl floor +s 1 +f 1/1/1 2/2/1 3/3/1 +f 3/3/1 2/2/1 4/4/1 +f 3/3/2 4/4/2 5/5/2 +f 5/5/2 4/4/2 6/6/2 +f 5/7/3 6/8/3 7/9/3 +f 7/9/3 6/8/3 8/10/3 +f 7/11/4 8/12/4 1/13/4 +f 1/13/4 8/12/4 2/14/4 +f 2/15/5 8/16/5 4/17/5 +f 4/17/5 8/16/5 6/18/5 +f 7/19/6 1/20/6 5/21/6 +f 5/21/6 1/20/6 3/22/6 diff --git a/assets/map/hole.png b/assets/map/hole.png new file mode 100644 index 00000000..3a79a9e0 Binary files /dev/null and b/assets/map/hole.png differ diff --git a/assets/map/stairs.mtl b/assets/map/stairs.mtl new file mode 100644 index 00000000..7925a729 --- /dev/null +++ b/assets/map/stairs.mtl @@ -0,0 +1,12 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl Cube.001 +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/assets/map/stairs.obj b/assets/map/stairs.obj new file mode 100644 index 00000000..519fef7e --- /dev/null +++ b/assets/map/stairs.obj @@ -0,0 +1,61 @@ +# hole.obj +# + +mtllib hole.mtl +o hole + +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 + +vt 0.000000 0.000000 +vt 0.333333 0.000000 +vt 0.666666 0.000000 +vt 1.000000 0.000000 +vt 0.000000 0.500000 +vt 0.333333 0.500000 +vt 0.666666 0.500000 +vt 1.000000 0.500000 +vt 0.000000 1.000000 +vt 0.333333 1.000000 +vt 0.666666 1.000000 +vt 1.000000 1.000000 + +vn 0.000000 0.000000 1.000000 +vn 0.000000 1.000000 0.000000 +vn 0.000000 0.000000 -1.000000 +vn 0.000000 -1.000000 0.000000 +vn 1.000000 0.000000 0.000000 +vn -1.000000 0.000000 0.000000 + +g floor +usemtl floor +# Back +s 1 +f 1/1/1 2/4/1 3/9/1 +f 3/9/1 2/4/1 4/12/1 +# Top +s 2 +f 3/1/2 4/4/2 5/9/2 +f 5/9/2 4/4/2 6/12/2 +# Front +s 3 +f 5/1/3 6/4/3 7/9/3 +f 7/9/3 6/4/3 8/12/3 +# Bottom +s 4 +f 7/1/4 8/4/4 1/9/4 +f 1/9/4 8/4/4 2/12/4 +# Right +s 5 +f 2/1/5 8/4/5 4/9/5 +f 4/9/5 8/4/5 6/12/5 +# Left +s 6 +f 7/1/6 1/4/6 5/9/6 +f 5/9/6 1/4/6 3/12/6 \ No newline at end of file diff --git a/assets/map/stairs.png b/assets/map/stairs.png new file mode 100644 index 00000000..aa732ced Binary files /dev/null and b/assets/map/stairs.png differ diff --git a/assets/map/test.mtl b/assets/map/test.mtl new file mode 100644 index 00000000..86d5a0cf --- /dev/null +++ b/assets/map/test.mtl @@ -0,0 +1,54 @@ +# Blender v2.92.0 OBJ File: '' +# www.blender.org +mtllib breakable_wall.mtl +o cube +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 +vt 0.000000 0.000000 +vt 0.333333 0.000000 +vt 0.000000 0.500000 +vt 0.333333 0.500000 +vt 0.666666 1.000000 +vt 1.000000 1.000000 +vt 0.666666 0.500000 +vt 1.000000 0.500000 +vt 0.000000 1.000000 +vt 0.333333 1.000000 +vt 0.000000 0.500000 +vt 0.333333 0.500000 +vt 0.666666 0.000000 +vt 1.000000 0.000000 +vt 0.666666 0.500000 +vt 1.000000 0.500000 +vt 0.666666 0.500000 +vt 0.666666 1.000000 +vt 0.333333 0.000000 +vt 0.666666 0.000000 +vt 0.333333 0.500000 +vt 0.666666 0.500000 +vn 0.0000 -0.0000 1.0000 +vn 0.0000 1.0000 0.0000 +vn 0.0000 0.0000 -1.0000 +vn 0.0000 -1.0000 -0.0000 +vn 1.0000 0.0000 0.0000 +vn -1.0000 0.0000 0.0000 +usemtl cube +s 1 +f 1/1/1 2/2/1 3/3/1 +f 3/3/1 2/2/1 4/4/1 +f 3/5/2 4/6/2 5/7/2 +f 5/7/2 4/6/2 6/8/2 +f 5/9/3 6/10/3 7/11/3 +f 7/11/3 6/10/3 8/12/3 +f 7/13/4 8/14/4 1/15/4 +f 1/15/4 8/14/4 2/16/4 +f 2/17/5 8/12/5 4/18/5 +f 4/18/5 8/12/5 6/10/5 +f 7/19/6 1/20/6 5/21/6 +f 5/21/6 1/20/6 3/22/6 diff --git a/assets/map/test.obj b/assets/map/test.obj new file mode 100644 index 00000000..144c9997 --- /dev/null +++ b/assets/map/test.obj @@ -0,0 +1,61 @@ +# floor.obj +# + +mtllib floor.mtl +o floor + +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 + +vt 0.000000 0.000000 +vt 0.333333 0.000000 +vt 0.666666 0.000000 +vt 1.000000 0.000000 +vt 0.000000 0.500000 +vt 0.333333 0.500000 +vt 0.666666 0.500000 +vt 1.000000 0.500000 +vt 0.000000 1.000000 +vt 0.333333 1.000000 +vt 0.666666 1.000000 +vt 1.000000 1.000000 + +vn 0.000000 0.000000 1.000000 +vn 0.000000 1.000000 0.000000 +vn 0.000000 0.000000 -1.000000 +vn 0.000000 -1.000000 0.000000 +vn 1.000000 0.000000 0.000000 +vn -1.000000 0.000000 0.000000 + +g floor +usemtl floor +# Back +s 1 +f 1/1/1 2/4/1 3/9/1 +f 3/9/1 2/4/1 4/12/1 +# Top +s 2 +f 3/1/2 4/4/2 5/9/2 +f 5/9/2 4/4/2 6/12/2 +# Front +s 3 +f 5/1/3 6/4/3 7/9/3 +f 7/9/3 6/4/3 8/12/3 +# Bottom +s 4 +f 7/1/4 8/4/4 1/9/4 +f 1/9/4 8/4/4 2/12/4 +# Right +s 5 +f 2/1/5 8/4/5 4/9/5 +f 4/9/5 8/4/5 6/12/5 +# Left +s 6 +f 7/1/6 1/4/6 5/9/6 +f 5/9/6 1/4/6 3/12/6 \ No newline at end of file diff --git a/assets/map/test.png b/assets/map/test.png new file mode 100644 index 00000000..bdbc5fda Binary files /dev/null and b/assets/map/test.png differ diff --git a/assets/wall/unbreakable_wall.mtl b/assets/map/unbreakable_wall.mtl similarity index 100% rename from assets/wall/unbreakable_wall.mtl rename to assets/map/unbreakable_wall.mtl diff --git a/assets/wall/unbreakable_wall.obj b/assets/map/unbreakable_wall.obj similarity index 100% rename from assets/wall/unbreakable_wall.obj rename to assets/map/unbreakable_wall.obj diff --git a/assets/wall/unbreakable_wall.png b/assets/map/unbreakable_wall.png similarity index 100% rename from assets/wall/unbreakable_wall.png rename to assets/map/unbreakable_wall.png diff --git a/assets/map/upper_floor.mtl b/assets/map/upper_floor.mtl new file mode 100644 index 00000000..47c30151 --- /dev/null +++ b/assets/map/upper_floor.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl floor +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd upper_floor.png diff --git a/assets/map/upper_floor.obj b/assets/map/upper_floor.obj new file mode 100644 index 00000000..094da9d2 --- /dev/null +++ b/assets/map/upper_floor.obj @@ -0,0 +1,54 @@ +# Blender v2.92.0 OBJ File: '' +# www.blender.org +mtllib upper_floor.mtl +o floor +v -0.500000 -0.499144 0.500854 +v -0.500000 -0.500854 -0.499144 +v 0.500000 -0.499144 0.500855 +v 0.500000 -0.500855 -0.499144 +v 0.500000 0.500854 0.499144 +v 0.500000 0.499144 -0.500854 +v -0.500000 0.500855 0.499144 +v -0.500000 0.499144 -0.500855 +vt 0.000977 0.667790 +vt 0.000977 0.333151 +vt 0.333659 0.667790 +vt 0.333659 0.333151 +vt 0.666341 0.667790 +vt 0.666341 0.333151 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.000977 0.667790 +vt 0.000977 1.002429 +vt 0.666341 0.667790 +vt 0.666341 1.002429 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.666341 0.333151 +vt 0.999024 0.333151 +vt 0.666341 0.667790 +vt 0.999024 0.667790 +vt 0.999024 1.002429 +vt 0.666341 1.002429 +vt 0.999024 0.667790 +vt 0.666341 0.667790 +vn -0.0000 -1.0000 0.0017 +vn 1.0000 -0.0000 0.0000 +vn 0.0000 1.0000 -0.0017 +vn -1.0000 0.0000 -0.0000 +vn 0.0000 -0.0017 -1.0000 +vn -0.0000 0.0017 1.0000 +usemtl floor +s 1 +f 1/1/1 2/2/1 3/3/1 +f 3/3/1 2/2/1 4/4/1 +f 3/3/2 4/4/2 5/5/2 +f 5/5/2 4/4/2 6/6/2 +f 5/7/3 6/8/3 7/9/3 +f 7/9/3 6/8/3 8/10/3 +f 7/11/4 8/12/4 1/13/4 +f 1/13/4 8/12/4 2/14/4 +f 2/15/5 8/16/5 4/17/5 +f 4/17/5 8/16/5 6/18/5 +f 7/19/6 1/20/6 5/21/6 +f 5/21/6 1/20/6 3/22/6 diff --git a/assets/map/upper_floor.png b/assets/map/upper_floor.png new file mode 100644 index 00000000..307706a5 Binary files /dev/null and b/assets/map/upper_floor.png differ diff --git a/assets/map/upper_floor_hole.mtl b/assets/map/upper_floor_hole.mtl new file mode 100644 index 00000000..829daba3 --- /dev/null +++ b/assets/map/upper_floor_hole.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'None' +# Material Count: 1 + +newmtl floor +Ns 225.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.800000 0.800000 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 +map_Kd upper_floor_hole.png diff --git a/assets/map/upper_floor_hole.obj b/assets/map/upper_floor_hole.obj new file mode 100644 index 00000000..2042783c --- /dev/null +++ b/assets/map/upper_floor_hole.obj @@ -0,0 +1,54 @@ +# Blender v2.92.0 OBJ File: '' +# www.blender.org +mtllib upper_floor_hole.mtl +o floor +v -0.500000 -0.499144 0.500854 +v -0.500000 -0.500854 -0.499144 +v 0.500000 -0.499144 0.500855 +v 0.500000 -0.500855 -0.499144 +v 0.500000 0.500854 0.499144 +v 0.500000 0.499144 -0.500854 +v -0.500000 0.500855 0.499144 +v -0.500000 0.499144 -0.500855 +vt 0.000977 0.667790 +vt 0.000977 0.333151 +vt 0.333659 0.667790 +vt 0.333659 0.333151 +vt 0.666341 0.667790 +vt 0.666341 0.333151 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.000977 0.667790 +vt 0.000977 1.002429 +vt 0.666341 0.667790 +vt 0.666341 1.002429 +vt 0.333659 0.667790 +vt 0.333659 1.002429 +vt 0.666341 0.333151 +vt 0.999024 0.333151 +vt 0.666341 0.667790 +vt 0.999024 0.667790 +vt 0.999024 1.002429 +vt 0.666341 1.002429 +vt 0.999024 0.667790 +vt 0.666341 0.667790 +vn -0.0000 -1.0000 0.0017 +vn 1.0000 -0.0000 0.0000 +vn 0.0000 1.0000 -0.0017 +vn -1.0000 0.0000 -0.0000 +vn 0.0000 -0.0017 -1.0000 +vn -0.0000 0.0017 1.0000 +usemtl floor +s 1 +f 1/1/1 2/2/1 3/3/1 +f 3/3/1 2/2/1 4/4/1 +f 3/3/2 4/4/2 5/5/2 +f 5/5/2 4/4/2 6/6/2 +f 5/7/3 6/8/3 7/9/3 +f 7/9/3 6/8/3 8/10/3 +f 7/11/4 8/12/4 1/13/4 +f 1/13/4 8/12/4 2/14/4 +f 2/15/5 8/16/5 4/17/5 +f 4/17/5 8/16/5 6/18/5 +f 7/19/6 1/20/6 5/21/6 +f 5/21/6 1/20/6 3/22/6 diff --git a/assets/map/upper_floor_hole.png b/assets/map/upper_floor_hole.png new file mode 100644 index 00000000..43da7cd6 Binary files /dev/null and b/assets/map/upper_floor_hole.png differ diff --git a/assets/wall/breakable_wall.png b/assets/wall/breakable_wall.png deleted file mode 100644 index 8e18cca8..00000000 Binary files a/assets/wall/breakable_wall.png and /dev/null differ diff --git a/assets/wall/hole.png b/assets/wall/hole.png deleted file mode 100644 index fe9ee288..00000000 Binary files a/assets/wall/hole.png and /dev/null differ diff --git a/build_web.sh b/build_web.sh index b5975bba..0fde576d 100755 --- a/build_web.sh +++ b/build_web.sh @@ -8,4 +8,4 @@ fi source ./emsdk/emsdk_env.sh mkdir -p build_web emcmake cmake -S . -B build_web -DPLATFORM=Web && -cmake --build build_web \ No newline at end of file +cd build_web && make -j $2 \ No newline at end of file diff --git a/lib/Ray/sources/Audio/Music.cpp b/lib/Ray/sources/Audio/Music.cpp index b397c04e..db3dae78 100644 --- a/lib/Ray/sources/Audio/Music.cpp +++ b/lib/Ray/sources/Audio/Music.cpp @@ -8,57 +8,50 @@ #include "Audio/Music.hpp" #include +RAY::Cache<::Music> RAY::Audio::Music::_musicsCache(LoadMusicStream, UnloadMusicStream); + RAY::Audio::Music::Music(const std::string &path): - _music(LoadMusicStream(path.c_str())) + _music(this->_musicsCache.fetch(path.c_str())) { } -RAY::Audio::Music::Music() -{ -} - -RAY::Audio::Music::~Music() -{ - UnloadMusicStream(_music); -} - bool RAY::Audio::Music::isPlaying(void) { - return IsMusicStreamPlaying(_music); + return IsMusicStreamPlaying(*_music); } RAY::Audio::Music &RAY::Audio::Music::play(void) { - PlayMusicStream(_music); + PlayMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::stop(void) { - StopMusicStream(_music); + StopMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::pause(void) { - PauseMusicStream(_music); + PauseMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::resume(void) { - ResumeMusicStream(_music); + ResumeMusicStream(*_music); return *this; } RAY::Audio::Music &RAY::Audio::Music::setVolume(float volume) { - SetMusicVolume(_music, volume); + SetMusicVolume(*_music, volume); return *this; } RAY::Audio::Music &RAY::Audio::Music::setPitch(float pitch) { - SetMusicPitch(_music, pitch); + SetMusicPitch(*_music, pitch); return *this; } diff --git a/lib/Ray/sources/Audio/Music.hpp b/lib/Ray/sources/Audio/Music.hpp index 85f31c52..c28e8089 100644 --- a/lib/Ray/sources/Audio/Music.hpp +++ b/lib/Ray/sources/Audio/Music.hpp @@ -9,9 +9,8 @@ #define MUSIC_HPP_ #include "Audio/IAudio.hpp" - #include - +#include "Utils/Cache.hpp" namespace RAY::Audio { @@ -22,11 +21,8 @@ namespace RAY::Audio //! @brief Load Music stream from file Music(const std::string &path); - //! @brief Default constructor - Music(); - //! @brief Default destructor - ~Music(); + ~Music() = default; //! @brief A copy constructor constructor Music(const Music &Music) = default; @@ -56,7 +52,9 @@ namespace RAY::Audio Music &setPitch(float pitch) override; private: - ::Music _music; + std::shared_ptr<::Music> _music; + + static RAY::Cache<::Music> _musicsCache; }; } diff --git a/lib/Ray/sources/Audio/Sound.cpp b/lib/Ray/sources/Audio/Sound.cpp index 4bfcb95a..da8df7ba 100644 --- a/lib/Ray/sources/Audio/Sound.cpp +++ b/lib/Ray/sources/Audio/Sound.cpp @@ -7,58 +7,51 @@ #include "Audio/Sound.hpp" +RAY::Cache<::Sound> RAY::Audio::Sound::_soundsCache(LoadSound, UnloadSound); + RAY::Audio::Sound::Sound(const std::string &path): - _sound(LoadSound(path.c_str())) + _sound(_soundsCache.fetch(path.c_str())) { } -RAY::Audio::Sound::Sound() -{ -} - -RAY::Audio::Sound::~Sound() -{ - UnloadSound(_sound); -} - bool RAY::Audio::Sound::isPlaying(void) { - return IsSoundPlaying(_sound); + return IsSoundPlaying(*_sound); } RAY::Audio::Sound &RAY::Audio::Sound::play(void) { - PlaySound(_sound); + PlaySound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::stop(void) { - StopSound(_sound); + StopSound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::pause(void) { - PauseSound(_sound); + PauseSound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::resume(void) { - ResumeSound(_sound); + ResumeSound(*_sound); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::setVolume(float volume) { - SetSoundVolume(_sound, volume); + SetSoundVolume(*_sound, volume); return *this; } RAY::Audio::Sound &RAY::Audio::Sound::setPitch(float pitch) { - SetSoundPitch(_sound, pitch); + SetSoundPitch(*_sound, pitch); return *this; } diff --git a/lib/Ray/sources/Audio/Sound.hpp b/lib/Ray/sources/Audio/Sound.hpp index 32dd8cb0..00ba3ddc 100644 --- a/lib/Ray/sources/Audio/Sound.hpp +++ b/lib/Ray/sources/Audio/Sound.hpp @@ -9,7 +9,7 @@ #define SOUND_HPP_ #include "Audio/IAudio.hpp" - +#include "Utils/Cache.hpp" #include @@ -22,11 +22,8 @@ namespace RAY::Audio //! @brief Load Sound stream from file Sound(const std::string &path); - //! @brief Default constructor - Sound(); - //! @brief Default destructor - ~Sound(); + ~Sound() = default; //! @brief A copy constructor constructor Sound(const Sound &sound) = default; @@ -56,7 +53,9 @@ namespace RAY::Audio Sound &setPitch(float pitch) override; private: - ::Sound _sound; + std::shared_ptr<::Sound> _sound; + + static RAY::Cache<::Sound> _soundsCache; }; } diff --git a/lib/Ray/sources/Utils/Cache.hpp b/lib/Ray/sources/Utils/Cache.hpp index 7f041853..653933f5 100644 --- a/lib/Ray/sources/Utils/Cache.hpp +++ b/lib/Ray/sources/Utils/Cache.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace RAY { //! @brief A templated class used to cache ressources, indexed with a string diff --git a/lib/Ray/sources/Window.cpp b/lib/Ray/sources/Window.cpp index 1e97d6a7..418c09ab 100644 --- a/lib/Ray/sources/Window.cpp +++ b/lib/Ray/sources/Window.cpp @@ -168,6 +168,11 @@ void RAY::Window::setIcon(RAY::Image &img) SetWindowIcon(img); } +void RAY::Window::drawFPS(const RAY::Vector2 &position) +{ + DrawFPS(position.x, position.y); +} + bool RAY::Window::isReady() const { return IsWindowReady(); diff --git a/lib/Ray/sources/Window.hpp b/lib/Ray/sources/Window.hpp index c811494a..f0e30e33 100644 --- a/lib/Ray/sources/Window.hpp +++ b/lib/Ray/sources/Window.hpp @@ -126,6 +126,8 @@ namespace RAY { //! @brief Draw a 3d mesh with material and transform void draw(const Mesh &mesh, const Material &material, const Matrix &transform); + //! @brief Draws current FPS on the frame + void drawFPS(const Vector2 &position); //! @return true if the window's context has been correctly initialized bool isReady() const; diff --git a/lib/wal/sources/Models/Callback.hpp b/lib/wal/sources/Models/Callback.hpp index 2c988db4..dc12a18b 100644 --- a/lib/wal/sources/Models/Callback.hpp +++ b/lib/wal/sources/Models/Callback.hpp @@ -64,7 +64,4 @@ namespace WAL this->addCallback(callback); } }; - - template - static constexpr Callback EmptyCallback; } // namespace WAL \ No newline at end of file diff --git a/lib/wal/sources/Wal.hpp b/lib/wal/sources/Wal.hpp index 85a7804e..de6bb394 100644 --- a/lib/wal/sources/Wal.hpp +++ b/lib/wal/sources/Wal.hpp @@ -84,7 +84,7 @@ namespace WAL //! @brief True if the engine should close after the end of the current tick. bool shouldClose = false; //! @brief The time between each fixed update. - static constexpr std::chrono::nanoseconds timestep = std::chrono::milliseconds(16); + static constexpr std::chrono::nanoseconds timestep = std::chrono::milliseconds(32); //! @brief Create a new system in place. //! @return The wal instance used to call this function is returned. This allow method chaining. diff --git a/sources/Component/Animator/AnimatorComponent.cpp b/sources/Component/Animator/AnimatorComponent.cpp new file mode 100644 index 00000000..fa5d8cc2 --- /dev/null +++ b/sources/Component/Animator/AnimatorComponent.cpp @@ -0,0 +1,16 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include "AnimatorComponent.hpp" + +namespace BBM { + AnimatorComponent::AnimatorComponent(WAL::Entity &entity) + : WAL::Component(entity) + {} + + WAL::Component *AnimatorComponent::clone(WAL::Entity &entity) const + { + return new AnimatorComponent(entity); + } +} \ No newline at end of file diff --git a/sources/Component/Animator/AnimatorComponent.hpp b/sources/Component/Animator/AnimatorComponent.hpp new file mode 100644 index 00000000..0c31b576 --- /dev/null +++ b/sources/Component/Animator/AnimatorComponent.hpp @@ -0,0 +1,28 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include + +namespace BBM { + class AnimatorComponent : public WAL::Component + { + public: + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief An Animator component can't be instantiated, it should be derived. + explicit AnimatorComponent(WAL::Entity &entity); + + //! @brief An Animator component can't be instantiated, it should be derived. + AnimatorComponent(const AnimatorComponent &) = default; + + //! @brief default destructor + ~AnimatorComponent() override = default; + + //! @brief An Animator component can't be assigned + AnimatorComponent &operator=(const AnimatorComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/Component/Button/ButtonComponent.hpp b/sources/Component/Button/ButtonComponent.hpp index 869ad5a2..42055be6 100644 --- a/sources/Component/Button/ButtonComponent.hpp +++ b/sources/Component/Button/ButtonComponent.hpp @@ -8,6 +8,7 @@ #include "Component/Component.hpp" #include "Entity/Entity.hpp" #include +#include "Wal.hpp" namespace BBM { @@ -18,7 +19,7 @@ namespace BBM { public: //! @brief onEvent callback - WAL::Callback onEvent; + WAL::Callback onEvent; //! @brief button which is at the top of this button WAL::Entity *_up; @@ -41,7 +42,7 @@ namespace BBM { } //! @brief Constructor with the 3 callback - ButtonComponent(WAL::Entity &entity, WAL::Callback callback) + ButtonComponent(WAL::Entity &entity, WAL::Callback callback) : WAL::Component(entity), onEvent(callback), _up(nullptr), _down(nullptr), _left(nullptr), _right(nullptr) { } diff --git a/sources/Component/Health/HealthComponent.cpp b/sources/Component/Health/HealthComponent.cpp index fe81fabb..5499d243 100644 --- a/sources/Component/Health/HealthComponent.cpp +++ b/sources/Component/Health/HealthComponent.cpp @@ -6,16 +6,14 @@ #include "HealthComponent.hpp" +#include + namespace BBM { - HealthComponent::HealthComponent(WAL::Entity &entity) - : WAL::Component(entity), - _healthPoint() - {} - - HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint) - : WAL::Component(entity), - _healthPoint(healthPoint) + HealthComponent::HealthComponent(WAL::Entity &entity, unsigned int healthPoint, std::function callback) + : WAL::Component(entity), + _healthPoint(healthPoint), + onDeath(std::move(callback)) {} WAL::Component *HealthComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/Health/HealthComponent.hpp b/sources/Component/Health/HealthComponent.hpp index 2eadafc5..d8ff7531 100644 --- a/sources/Component/Health/HealthComponent.hpp +++ b/sources/Component/Health/HealthComponent.hpp @@ -34,12 +34,9 @@ namespace BBM //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; - - //! @brief A Health component can't be instantiated, it should be derived. - explicit HealthComponent(WAL::Entity &entity); //! @brief Constructor - HealthComponent(WAL::Entity &entity, unsigned int healthPoint); + explicit HealthComponent(WAL::Entity &entity, unsigned int healthPoint = 1, std::function callback = {}); //! @brief A Health component can't be instantiated, it should be derived. HealthComponent(const HealthComponent &) = default; diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 722f0433..c4709164 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -19,7 +19,7 @@ namespace BBM auto &pos = entity.getComponent(); const auto &wallPos = wall.getComponent(); auto diff = pos.position + mov->getVelocity() - wallPos.position; - std::cout << diff << std::endl; +// mov->_velocity = Vector3f(); if (diff.x <= 0 && mov->_velocity.x < 0) mov->_velocity.x = 0; if (diff.x >= 0 && mov->_velocity.x > 0) @@ -30,10 +30,23 @@ namespace BBM mov->_velocity.z = 0; } + const std::string MapGenerator::assetsPath = "./assets/"; + const std::string MapGenerator::wallAssetsPath = MapGenerator::assetsPath + "map/"; + const std::string MapGenerator::imageExtension = ".png"; + const std::string MapGenerator::objExtension = ".obj"; + const std::string MapGenerator::breakableWallPath = MapGenerator::wallAssetsPath + "breakable_wall"; + const std::string MapGenerator::unbreakableWallPath = MapGenerator::wallAssetsPath + "unbreakable_wall"; + const std::string MapGenerator::floorPath = MapGenerator::wallAssetsPath + "floor"; + const std::string MapGenerator::secondFloorPath = MapGenerator::wallAssetsPath + "upper_floor"; + const std::string MapGenerator::stairsPath = MapGenerator::wallAssetsPath + "stairs"; + const std::string MapGenerator::bumperPath = MapGenerator::wallAssetsPath + "bumper"; + const std::string MapGenerator::holePath = MapGenerator::wallAssetsPath + "hole"; + const std::string MapGenerator::secondFloorHolePath = MapGenerator::secondFloorPath + "_hole"; + void MapGenerator::generateUnbreakableBlock(int width, int height, std::shared_ptr scene) { - std::string unbreakableObj = "assets/wall/unbreakable_wall.obj"; - std::string unbreakablePnj = "assets/wall/unbreakable_wall.png"; + static const std::string unbreakableObj = unbreakableWallPath + objExtension; + static const std::string unbreakablePng = unbreakableWallPath + imageExtension; for (int i = 0; i < width + 1; i++) { for (int j = 0; j < height + 1; j++) { @@ -41,7 +54,7 @@ namespace BBM scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj)); + .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); } } } @@ -49,43 +62,61 @@ namespace BBM void MapGenerator::generateWall(int width, int height, std::shared_ptr scene) { - std::string unbreakableObj = "assets/wall/unbreakable_wall.obj"; - std::string unbreakablePnj = "assets/wall/unbreakable_wall.png"; + static const std::string unbreakableObj = unbreakableWallPath + objExtension; + static const std::string unbreakablePnj = unbreakableWallPath + imageExtension; scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); + .addComponent(unbreakableObj, + std::make_pair(MAP_DIFFUSE, unbreakablePnj), + RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); + .addComponent(unbreakableObj, + std::make_pair(MAP_DIFFUSE, unbreakablePnj), + RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") - .addComponent(Vector3f(width + 1, 0, (height + 1) / 2)) + .addComponent(Vector3f(width + 1, 0, height / 2)) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); + .addComponent(unbreakableObj, + std::make_pair(MAP_DIFFUSE, unbreakablePnj), + RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") - .addComponent(Vector3f(-1, 0, (height + 1) / 2)) + .addComponent(Vector3f(-1, 0, height / 2)) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 3)); + .addComponent(unbreakableObj, + std::make_pair(MAP_DIFFUSE, unbreakablePnj), + RAY::Vector3(1, 1, height + 1)); } - void MapGenerator::generateFloor(int width, int height, std::shared_ptr scene) + void MapGenerator::generateFloor(MapBlock map, int width, int height, std::shared_ptr scene) { - scene->addEntity("Floor") - .addComponent(Vector3f(width / 2, -1, height / 2)) - .addComponent("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png"), RAY::Vector3(width + 2, 0, height + 2)); + static const std::string floorObj = floorPath + objExtension; + static const std::string floorPng = floorPath + imageExtension; + + for (int i = 0; i < width + 1; i++) { + for (int j = 0; j < height + 1; j++) { + if (map[std::make_tuple(i, 0, j)] != HOLE && map[std::make_tuple(i, -1, j)] != BUMPER) + scene->addEntity("Unbreakable Wall") + .addComponent(Vector3f(i, -1, j)) + .addComponent(floorObj, + std::make_pair(MAP_DIFFUSE, floorPng)); + } + } } void MapGenerator::createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType) { std::map elements = { - {BREAKABLE, &createBreakable}, + {BREAKABLE, &createBreakable}, {UNBREAKABLE, &createUnbreakable}, - {HOLE, &createHole}, - {FLOOR, &createFloor}, - /* {BUMPER, &createBumper}, - {STAIRS, &createStairs} */ + {HOLE, &createHole}, + {FLOOR, &createFloor}, + {BUMPER, &createBumper}, + {STAIRS, &createStairs}, + {UPPERFLOOR, &createUpperFloor}, }; try { @@ -98,77 +129,116 @@ namespace BBM void MapGenerator::createBreakable(Vector3f coords, std::shared_ptr scene) { + static const std::string breakableObj = breakableWallPath + objExtension; + static const std::string breakablePng = breakableWallPath + imageExtension; + scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent("assets/wall/breakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/breakable_wall.png")); + .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } void MapGenerator::createFloor(Vector3f coords, std::shared_ptr scene) { + static const std::string floorObj = floorPath + objExtension; + static const std::string floorPng = floorPath + imageExtension; + scene->addEntity("Floor") .addComponent(Vector3f(coords)) - .addComponent("assets/wall/floor.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/floor.png")); + //.addComponent(1) + .addComponent(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } + void MapGenerator::createUpperFloor(Vector3f coords, std::shared_ptr scene) + { + static const std::string floorObj = secondFloorPath + objExtension; + static const std::string floorPng = secondFloorPath + imageExtension; + + scene->addEntity("Upper Floor") + .addComponent(Vector3f(coords)) + .addComponent(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); + } + + void MapGenerator::createUnbreakable(Vector3f coords, std::shared_ptr scene) { + static const std::string UnbreakableObj = unbreakableWallPath + objExtension; + static const std::string UnbreakablePng = unbreakableWallPath + imageExtension; + scene->addEntity("Unbreakable Block") .addComponent(coords) .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) - .addComponent("assets/wall/unbreakable_wall.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/unbreakable_wall.png")); + .addComponent(UnbreakableObj, + std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } void MapGenerator::createHole(Vector3f coords, std::shared_ptr scene) { - scene->addEntity("Hole Block") - .addComponent(Vector3f(coords.x, coords.y - 1, coords.z)) - .addComponent("assets/wall/hole.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/hole.png")); - /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { - if (other.hasComponent()) { - auto &health = other.getComponent(); - health.takeDmg(health.getHealthPoint()); - } - }); */ + static const std::string holeObj = holePath + objExtension; + static const std::string holePng = holePath + imageExtension; + static const std::string secondFloorObj = secondFloorHolePath + objExtension; + static const std::string secondFloorPng = secondFloorHolePath + imageExtension; + + WAL::Entity &holeEntity = scene->addEntity("Hole Block"); + + holeEntity.addComponent(Vector3f(coords.x, coords.y - 1, coords.z)); + + if (coords.y == 0) + holeEntity.addComponent(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); + else + holeEntity.addComponent(secondFloorObj, std::make_pair(MAP_DIFFUSE, secondFloorPng)); + /*.addComponent([](WAL::Entity &other, const WAL::Entity &entity) { + if (other.hasComponent()) { + auto &health = other.getComponent(); + health.takeDmg(health.getHealthPoint()); + } + }, [](WAL::Entity &other, const WAL::Entity &entity){}); */ } void MapGenerator::createBumper(Vector3f coords, std::shared_ptr scene) { + static const std::string bumperObj = bumperPath + objExtension; + static const std::string bumperPng = bumperPath + imageExtension; + scene->addEntity("Bumper Block") - .addComponent(Vector3f(coords.x, coords.y - 1, coords.z)) - .addComponent("assets/wall/bumper_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/bumper_block.png")); - /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { - if (other.hasComponent()) { - auto &movable = other.getComponent(); - movable.addForce(Vector3f(0, 5, 0)); - } - } */ + .addComponent(Vector3f(coords.x, coords.y, coords.z)) + .addComponent(bumperObj, std::make_pair(MAP_DIFFUSE, bumperPng)); + /* .addComponent([](const WAL::Entity &entity, WAL::Entity &other) { + if (other.hasComponent()) { + auto &movable = other.getComponent(); + movable.addForce(Vector3f(0, 5, 0)); + } + }); */ } void MapGenerator::createStairs(Vector3f coords, std::shared_ptr scene) { + static const std::string stairsObj = stairsPath + objExtension; + static const std::string stairsPng = stairsPath + imageExtension; + scene->addEntity("Stairs Block") .addComponent(coords) //.addComponent(1) - .addComponent("assets/wall/stairs_block.obj", std::make_pair(MAP_DIFFUSE, "assets/wall/stairs_block.png")); + .addComponent(stairsObj, std::make_pair(MAP_DIFFUSE, stairsPng)); } - bool MapGenerator::isCloseToBlockType(std::map, BlockType> map, int x, int y, int z, BlockType blockType) + bool MapGenerator::isCloseToBlockType(std::map, BlockType> map, int x, int y, int z, + BlockType blockType) { return (map[std::make_tuple(x - 1, y, z)] == blockType || - map[std::make_tuple(x + 1, y, z)] == blockType || - map[std::make_tuple(x, y, z + 1)] == blockType || - map[std::make_tuple(x, y, z - 1)] == blockType); + map[std::make_tuple(x + 1, y, z)] == blockType || + map[std::make_tuple(x, y, z + 1)] == blockType || + map[std::make_tuple(x, y, z - 1)] == blockType); } MapGenerator::BlockType MapGenerator::getRandomBlockType() - { - double rnd = static_cast(std::rand())/RAND_MAX; - + { + double rnd = static_cast(std::rand()) / RAND_MAX; + if (rnd > 0.95) return HOLE; - if (rnd > 0.10) + if (rnd > 0.25) return BREAKABLE; return NOTHING; } @@ -178,22 +248,32 @@ namespace BBM double rnd = static_cast(std::rand()) / RAND_MAX; if (rnd > 0.60) { - for (int i = 0; i < width; i++) { + for (int i = 0; i < width + 1; i++) { map[std::make_tuple(i, 1, height)] = map[std::make_tuple(i, 0, height)]; - map[std::make_tuple(i, 0, height)] = FLOOR; - } - for (int j = 0; j < height; j++) { - map[std::make_tuple(width, 1, j)] = map[std::make_tuple(width, 0, j)]; - map[std::make_tuple(width, 0, j)] = FLOOR; + map[std::make_tuple(i, 0, height)] = UPPERFLOOR; + map[std::make_tuple(i, 1, 0)] = map[std::make_tuple(i, 0, 0)]; + map[std::make_tuple(i, 0, 0)] = UPPERFLOOR; } + map[std::make_tuple(0, 0, height - 1)] = STAIRS; + map[std::make_tuple(0, 0, 1)] = STAIRS; + map[std::make_tuple(width, 0, height - 1)] = STAIRS; + map[std::make_tuple(width, 0, 1)] = STAIRS; + map[std::make_tuple(width / 2, -1, height - 1)] = BUMPER; + map[std::make_tuple(width / 2, -1, 1)] = BUMPER; } if (rnd > 0.30) { - for (int i = width - width/4; i < width + width/4 + 1; i++) { - for (int j = height - height/4; j < height + height/4 + 1; j++) { + for (int i = width / 2 - width / 4; i < width / 2 + width / 4 + 1; i++) { + for (int j = height / 2 - height / 4; j < height / 2 + height / 4 + 1; j++) { + if (map[std::make_tuple(i, 0, j)] == FLOOR) + continue; map[std::make_tuple(i, 1, j)] = map[std::make_tuple(i, 0, j)]; - map[std::make_tuple(i, 0, j)] = FLOOR; + map[std::make_tuple(i, 0, j)] = UPPERFLOOR; } } + map[std::make_tuple(width / 2 - width / 8, 0, height / 2 + height / 4 + 1)] = STAIRS; + map[std::make_tuple(width / 2 + width / 8, 0, height / 2 - height / 4 - 1)] = STAIRS; + map[std::make_tuple(width / 2 - width / 4 - 1, 0, height / 2 - height / 8)] = STAIRS; + map[std::make_tuple(width / 2 + width / 4 + 1, 0, height / 2 + height / 8)] = STAIRS; } return map; } @@ -208,50 +288,58 @@ namespace BBM return map; } + MapGenerator::MapBlock MapGenerator::cleanBreakable(MapBlock map, int width, int height) + { + for (int i = 0; i < width + 1; i++) + for (int j = 0; j < height; j++) { + if (map[std::make_tuple(i, 0, j)] == BREAKABLE && isCloseToBlockType(map, i, 0, j, STAIRS)) + map[std::make_tuple(i, 0, j)] = NOTHING; + if (map[std::make_tuple(i, 0, j)] == BREAKABLE && map[std::make_tuple(i, -1, j)] == BUMPER) + map[std::make_tuple(i, 0, j)] = NOTHING; + } + return (map); + } + MapGenerator::MapBlock MapGenerator::createMap(int width, int height) { MapBlock map; width = width % 2 ? width + 1 : width; height = height % 2 ? height + 1 : height; - for (int i = 0; i < width; i++) { + for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) map[std::make_tuple(i, 0, j)] = NOTHING; - } map = createSpawner(map, width, height); for (int i = 0; i < width + 1; i++) { for (int j = 0; j < height + 1; j++) { if (map[std::make_tuple(i, 0, j)] == SPAWNER) continue; - if (isCloseToBlockType(map, i , 0, j, SPAWNER)) + if (isCloseToBlockType(map, i, 0, j, SPAWNER)) { map[std::make_tuple(i, 0, j)] = NOTHING; - else + } else { map[std::make_tuple(i, 0, j)] = getRandomBlockType(); + } if (map[std::make_tuple(i, 0, j)] == UNBREAKABLE && isCloseToBlockType(map, i, 0, j, UNBREAKABLE)) map[std::make_tuple(i, 0, j)] = BREAKABLE; } } - for (int i = 0; i < width + 1; i++) { - for (int j = 0; j < height + 1; j++) { + for (int i = 0; i < width + 1; i++) + for (int j = 0; j < height + 1; j++) if (!((i + 1) % 2) && !((j + 1) % 2)) map[std::make_tuple(i, 0, j)] = UNBREAKABLE; - } - } map = createHeight(map, width, height); + map = cleanBreakable(map, width, height); return (map); } - void MapGenerator::loadMap(int width, int height, std::map, - BlockType> map, std::shared_ptr scene) + void MapGenerator::loadMap(int width, int height, MapBlock map, std::shared_ptr scene) { generateWall(width, height, scene); - generateFloor(width, height, scene); - for (int x = 0; x < width + 1; x++) { - for (int z = 0; z < height + 1; z++) { - for (int y = 0; y < 1 + 1; y++) + generateFloor(map, width, height, scene); + for (int x = 0; x < width + 1; x++) + for (int z = 0; z < height + 1; z++) + for (int y = -1; y < 1 + 1; y++) createElement(Vector3f(x, y, z), scene, map[std::make_tuple(x, y, z)]); - } - } } } // namespace BBM \ No newline at end of file diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index 4059925b..f6b082a8 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -19,6 +19,7 @@ #include "Component/Component.hpp" #include "Component/Position/PositionComponent.hpp" #include "Component/Health/HealthComponent.hpp" +#include "Component/Collision/CollisionComponent.hpp" #include "Component/Movable/MovableComponent.hpp" namespace BBM @@ -32,6 +33,7 @@ namespace BBM NOTHING, BREAKABLE, HOLE, + UPPERFLOOR, FLOOR, BUMPER, STAIRS, @@ -67,7 +69,7 @@ namespace BBM //! @param height Height of the map //! @param scene Scene where the map is instanced //! @brief Generate the floor of the map - static void generateFloor(int width, int height, std::shared_ptr scene); + static void generateFloor(MapBlock map, int width, int height, std::shared_ptr scene); //! @param coords coords of the element //! @param scene Scene where the map is instanced @@ -96,9 +98,14 @@ namespace BBM //! @param coords coords of the element //! @param scene Scene where the map is instanced - //! @brief Create bumper of the map + //! @brief Create floor of the map static void createFloor(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create upper floor of the map + static void createUpperFloor(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element //! @param scene Scene where the map is instanced @@ -117,6 +124,36 @@ namespace BBM //! @brief Generate height for the map static MapBlock createHeight(MapBlock map, int width, int height); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Clean breakable on stairs, bumpers, etc.. + static MapBlock cleanBreakable(MapBlock map, int width, int height); + + + static const std::string assetsPath; + + static const std::string wallAssetsPath; + + static const std::string imageExtension; + + static const std::string objExtension; + + static const std::string unbreakableWallPath; + + static const std::string breakableWallPath; + + static const std::string floorPath; + + static const std::string stairsPath; + + static const std::string bumperPath; + + static const std::string secondFloorPath; + + static const std::string holePath; + + static const std::string secondFloorHolePath; public: static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index 629a11b8..b3dacf2b 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -9,6 +9,8 @@ #include #include "Vector/Vector2.hpp" +#define PI_NUMBER 3.14159265359 + namespace BBM { //! @brief A Vector2 data type. (templated to allow any kind of vector2) @@ -120,6 +122,13 @@ namespace BBM return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2)); } + double angle(const Vector2 &o) const + { + float dot = this->x * o.x + this->y * o.y; + float det = this->x * o.y - this->y * o.x; + return (std::atan2(det, dot) * (180.0f / PI_NUMBER)); + } + double magnitude() const { return std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2)); diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index de5acf15..8bdd510c 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -26,6 +26,8 @@ #include "Runner.hpp" #include "Models/GameState.hpp" #include +#include +#include #include "Component/Animation/AnimationsComponent.hpp" #include "System/Animation/AnimationsSystem.hpp" #include "Map/Map.hpp" @@ -68,6 +70,7 @@ namespace BBM RAY::TraceLog::setLevel(LOG_WARNING); RAY::Window &window = RAY::Window::getInstance(1920, 1080, "Bomberman"); wal.addSystem() + .addSystem() .addSystem(window); } @@ -88,7 +91,7 @@ namespace BBM .addComponent("Press space", 70, RAY::Vector2(), ORANGE) .addComponent() .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { gameState.nextScene = BBM::GameState::SceneID::MainMenuScene; }); @@ -115,57 +118,57 @@ namespace BBM auto &play = scene->addEntity("play button") .addComponent(1920 / 2.5, 1080 - 540, 0) .addComponent("assets/buttons/button_new_game.png") - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_new_game.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_new_game_hovered.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { gameState.nextScene = BBM::GameState::SceneID::GameScene; }); auto &settings = scene->addEntity("settings button") .addComponent(1920 / 2.5, 1080 - 360, 0) .addComponent("assets/buttons/button_settings.png") - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_settings.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_settings_hovered.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { gameState.nextScene = BBM::GameState::SceneID::SettingsScene; }); auto &exit = scene->addEntity("exit button") .addComponent(1920 / 2.5, 1080 - 180, 0) .addComponent("assets/buttons/button_exit.png") - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_exit.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_exit_hovered.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { //close window }); @@ -214,11 +217,11 @@ namespace BBM .addComponent(1920 / 2.5, 1080 - 540, 0) .addComponent("Music Volume", 70, RAY::Vector2(), ORANGE) .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { entity.getComponent().drawable->setColor(BLACK); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { entity.getComponent().drawable->setColor(ORANGE); }); @@ -227,13 +230,13 @@ namespace BBM .addComponent(1920 / 3, 1080 - 540, 0) .addComponent("assets/buttons/button_plus.png") .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_plus.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); @@ -244,13 +247,13 @@ namespace BBM .addComponent(1920 / 1.5, 1080 - 540, 0) .addComponent("assets/buttons/button_minus.png") .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_minus.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); @@ -261,8 +264,11 @@ namespace BBM .addComponent(1920 / 2.5, 1080 - 360, 0) .addComponent("Sound Volume", 70, RAY::Vector2(), ORANGE) .addComponent() - .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) + { + entity.getComponent().drawable->setColor(BLACK); + }) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { entity.getComponent().drawable->setColor(ORANGE); }); @@ -271,13 +277,13 @@ namespace BBM .addComponent(1920 / 3, 1080 - 360, 0) .addComponent("assets/buttons/button_plus.png") .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_plus.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); @@ -288,13 +294,13 @@ namespace BBM .addComponent(1920 / 1.5, 1080 - 360, 0) .addComponent("assets/buttons/button_minus.png") .addComponent() - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_minus.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); @@ -304,38 +310,40 @@ namespace BBM auto &debug = scene->addEntity("debug text") .addComponent(1920 / 2.5, 1080 - 180, 0) .addComponent("Debug Mode: Off", 70, RAY::Vector2(), ORANGE) - .addComponent([this](WAL::Entity &entity, WAL::Wal &wal) + .addComponent([](WAL::Entity &entity, WAL::Wal &wal) { RAY2D::Text *text = dynamic_cast(entity.getComponent().drawable.get()); - if (text->getString().find("Off") != std::string::npos) + if (text->getString().find("Off") != std::string::npos) { text->setText("Debug Mode: On"); - else + wal.getSystem().setDebug(true); + } else { text->setText("Debug Mode: Off"); - wal.getSystem. + wal.getSystem().setDebug(false); + } }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { entity.getComponent().drawable->setColor(BLACK); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { entity.getComponent().drawable->setColor(ORANGE); }); auto &back = scene->addEntity("back to menu") .addComponent(10, 10, 0) .addComponent("assets/buttons/button_back.png") - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { gameState.nextScene = BBM::GameState::SceneID::MainMenuScene; }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); texture->use("assets/buttons/button_back.png"); }) - .addComponent([](WAL::Entity &entity) + .addComponent([](WAL::Entity &entity, WAL::Wal &) { RAY::Texture *texture = dynamic_cast(entity.getComponent().drawable.get()); @@ -365,14 +373,24 @@ namespace BBM .addComponent() .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() + .addComponent() .addComponent() - .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 1) + .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(1) - .addComponent(); - + .addComponent() + .addComponent(1, [](WAL::Entity &entity) { + auto &animation = entity.getComponent(); + animation.setAnimIndex(5); + }); scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); +// scene->addEntity("cube") +// .addComponent(5, 0, 5) +// .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) +// .addComponent() +// .addComponent() +// .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); std::srand(std::time(nullptr)); MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; diff --git a/sources/System/Animator/AnimatorSystem.cpp b/sources/System/Animator/AnimatorSystem.cpp new file mode 100644 index 00000000..f03aeb9b --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.cpp @@ -0,0 +1,37 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#include +#include +#include +#include +#include +#include +#include "AnimatorSystem.hpp" +#include "Component/Renderer/Drawable3DComponent.hpp" + +using Keyboard = RAY::Controller::Keyboard; +namespace RAY3D = RAY::Drawables::Drawables3D; +using Key = RAY::Controller::Keyboard::Key; + +namespace BBM +{ + AnimatorSystem::AnimatorSystem(WAL::Wal &wal) + : System(wal) + {} + + void AnimatorSystem::onFixedUpdate(WAL::ViewEntity &entity) + { + const auto &controllable = entity.get(); + auto drawable = entity.get().drawable.get(); + auto &animation = entity.get(); + auto anim = dynamic_cast(drawable); + if (anim && controllable.move != Vector2f(0, 0)) { + anim->setRotationAngle(controllable.move.angle(Vector2f(-1, 0))); + animation.setAnimIndex(0); + return; + } + animation.setAnimIndex(1); + } +} \ No newline at end of file diff --git a/sources/System/Animator/AnimatorSystem.hpp b/sources/System/Animator/AnimatorSystem.hpp new file mode 100644 index 00000000..fcddfc13 --- /dev/null +++ b/sources/System/Animator/AnimatorSystem.hpp @@ -0,0 +1,29 @@ +// +// Created by hbenjamin on 03/06/2021. +// + +#pragma once + +#include +#include "Component/Animation/AnimationsComponent.hpp" +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle Animator entities. + class AnimatorSystem : public WAL::System + { + public: + //! @inherit + void onFixedUpdate(WAL::ViewEntity &entity) override; + + //! @brief A default constructor + AnimatorSystem(WAL::Wal &wal); + //! @brief An Animator system is copy constructable + AnimatorSystem(const AnimatorSystem &) = default; + //! @brief A default destructor + ~AnimatorSystem() override = default; + //! @brief An Animator system is assignable. + AnimatorSystem &operator=(const AnimatorSystem &) = default; + }; +} \ No newline at end of file diff --git a/sources/System/Health/HealthSystem.cpp b/sources/System/Health/HealthSystem.cpp index 29e0db1f..4419e9e1 100644 --- a/sources/System/Health/HealthSystem.cpp +++ b/sources/System/Health/HealthSystem.cpp @@ -3,6 +3,7 @@ // Edited by Benjamin Henry on 2021-05-20. // +#include #include "HealthSystem.hpp" #include "Component/Health/HealthComponent.hpp" #include "Component/Controllable/ControllableComponent.hpp" @@ -18,7 +19,8 @@ namespace BBM { auto &health = entity.get(); - if (health.getHealthPoint() == 0) + if (health.getHealthPoint() == 0) { health.onDeath(entity); + } } } \ No newline at end of file diff --git a/sources/System/MenuControllable/MenuControllableSystem.cpp b/sources/System/MenuControllable/MenuControllableSystem.cpp index 47550893..af84242e 100644 --- a/sources/System/MenuControllable/MenuControllableSystem.cpp +++ b/sources/System/MenuControllable/MenuControllableSystem.cpp @@ -48,12 +48,12 @@ namespace BBM this->updateCurrentButton(); for (auto &[buttonEntity, clickComponent]: buttons) { if (buttonEntity == *currentButton) { - buttonEntity.getComponent().onEvent(buttonEntity); + buttonEntity.getComponent().onEvent(buttonEntity, wal); if (select) - clickComponent.onEvent(buttonEntity); + clickComponent.onEvent(buttonEntity, wal); continue; } - buttonEntity.getComponent().onEvent(buttonEntity); + buttonEntity.getComponent().onEvent(buttonEntity, wal); } } diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index bd6a3adc..29629946 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -8,16 +8,18 @@ #include "Component/Renderer/CameraComponent.hpp" #include "Component/Position/PositionComponent.hpp" #include "Component/Renderer/Drawable2DComponent.hpp" -#include "Drawables/ADrawable2D.hpp" #include "Drawables/ADrawable3D.hpp" namespace BBM { - RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window) + RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window, bool debugMode) : System(wal), _window(window), - _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE) - {} + _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE), + _debugMode(debugMode) + { + this->_window.setFPS(this->FPS); + } void RenderSystem::onSelfUpdate() { @@ -37,6 +39,8 @@ namespace BBM drawable.drawable->setPosition(Vector2f(pos.position.x, pos.position.y)); drawable.drawable->drawOn(this->_window); } + if (this->_debugMode) + this->_window.drawFPS(Vector2f()); this->_window.endDrawing(); } @@ -47,4 +51,9 @@ namespace BBM _camera.setPosition(pos.position); _camera.setTarget(cam.target); } + + void RenderSystem::setDebug(bool debug) + { + this->_debugMode = debug; + } } \ No newline at end of file diff --git a/sources/System/Renderer/RenderSystem.hpp b/sources/System/Renderer/RenderSystem.hpp index 9c5c4c27..e790a741 100644 --- a/sources/System/Renderer/RenderSystem.hpp +++ b/sources/System/Renderer/RenderSystem.hpp @@ -21,6 +21,13 @@ namespace BBM //! @brief The camera used to render. RAY::Camera::Camera3D _camera; + + //! @brief Defines if the debug informations must be displayed or not + bool _debugMode; + + //! @brief Window framerate limit + static constexpr short FPS = 60; + public: //! @brief A method called after all entities that this system manage has been updated. //! @note render on screen here @@ -29,8 +36,11 @@ namespace BBM //! @inherit void onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) override; + //! @param debug true if debug mode should be enabled + void setDebug(bool debug); + //! @brief ctor - RenderSystem(WAL::Wal &wal, RAY::Window &window); + RenderSystem(WAL::Wal &wal, RAY::Window &window, bool debugMode = false); //! @brief Default copy ctor RenderSystem(const RenderSystem &) = default; //! @brief Default dtor