From 33f6504b6c7801d20c9ac70a297fc6f88dc089e1 Mon Sep 17 00:00:00 2001 From: Ninjaws Date: Thu, 19 Dec 2024 22:40:59 +0100 Subject: [PATCH] [Added] Ability to serve the project using nginx on localhost:4600 --- Dockerfile | 37 ++++++++++++- README.md | 39 +++++++++++++- docker-compose.yml | 13 +++++ html/game.html | 56 ------------------- html/index.html | 114 +++++++++++++++++++++++++++++++++++++++ include/entities/Crate.h | 2 +- include/entities/Game.h | 2 + include/entities/Jelly.h | 4 +- src/entities/Enemy.cpp | 73 +++++-------------------- src/entities/PBullet.cpp | 1 + 10 files changed, 220 insertions(+), 121 deletions(-) create mode 100644 docker-compose.yml delete mode 100644 html/game.html create mode 100644 html/index.html diff --git a/Dockerfile b/Dockerfile index 1eab433..2de1b50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -63,4 +63,39 @@ RUN cd libs/raylib/src && \ emar rcs libraylib.a rcore.o rshapes.o rtextures.o rtext.o rmodels.o utils.o raudio.o && \ cd ../.. -CMD ["bash"] +# CMD ["bash"] + +FROM development as build + +RUN mkdir dist html src include assets + +COPY include include +COPY src src +COPY html html +COPY assets assets + +# CMD ["tail", "-f", "/dev/null"] +# Run the production build command to generate game.js and game.wasm +RUN source /app/libs/emsdk/emsdk_env.sh && \ + emcc $(find src/ -name "*.cpp") -o dist/game.js -O3 -Wno-missing-braces \ + -I./include -I/app/libs/raylib/build/raylib/include \ + -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a \ + -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=536870912 -s MAXIMUM_MEMORY=4294967296 \ + -s --preload-file assets@/assets -s USE_GLFW=3 -s ASSERTIONS=2 -DPLATFORM_WEB + +# CMD ["tail", "-f", "/dev/null"] + +FROM nginx:alpine as production + +# Remove the default nginx index.html +RUN rm -rf /usr/share/nginx/html/* + +# Copy the generated files from the production stage into the Nginx html directory +COPY --from=build /app/dist/ /usr/share/nginx/html/ +COPY --from=build /app/html/index.html /usr/share/nginx/html/ + +# Expose port 80 to allow Nginx to serve the files +EXPOSE 80 + +# Start Nginx server +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/README.md b/README.md index 0d2989a..e1aca3d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,39 @@ # WebJelly-Rescue -Jelly Rescue, but now for the web! +[Jelly Rescue](https://github.com/Ninjaws/JellyGame), but now for the web! + +## Installation +### Install Docker +- On Windows and Mac +``` +Install Docker Desktop +Windows: https://docs.docker.com/desktop/install/windows-install/ +Mac: https://docs.docker.com/desktop/install/mac-install/ +``` +- On Linux +```bash +# For Debian-based systems +sudo apt install -y docker.io +sudo apt install docker-compose + +#For Arch-based systems +sudo pacman -S docker.io +sudo pacman -S docker-compose + +# Making Docker run now and on startup +sudo systemctl start docker +sudo systemctl enable docker + +# Using the CLI without sudo +sudo usermod -aG docker $USER +sudo reboot +``` + +## How to run +- Navigate to the root of the project +- Open a terminal +- Start the container: +```bash +docker compose up #(might take a few minutes to build) +``` +- Visit https://localhost:4600 to play the game locally +- Or visit play it live [here!](https://jelly.ianvink.nl) diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e3345e3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +services: + frontend: + build: + context: . + dockerfile: Dockerfile + target: production + image: jelly-rescue-prod + container_name: jelly-rescue-prod-cont + ports: + - "4600:80" + restart: always + cpu_period: 100000 # Default is 100,000 microseconds (100ms) + cpu_quota: 30000 # 5% of the total CPU time \ No newline at end of file diff --git a/html/game.html b/html/game.html deleted file mode 100644 index a6ee798..0000000 --- a/html/game.html +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - raylib web game - - - - - - -

- - - - - \ No newline at end of file diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..728fa2a --- /dev/null +++ b/html/index.html @@ -0,0 +1,114 @@ + + + + + + + Jelly Rescue + + + + + +

+ + +
+

+ + + + + diff --git a/include/entities/Crate.h b/include/entities/Crate.h index c9f86d5..3e9ccb5 100644 --- a/include/entities/Crate.h +++ b/include/entities/Crate.h @@ -43,7 +43,7 @@ public: private: TextureWrapper texture; - bool hit; // Whether a PBullet has hit the crate + bool hit = false; // Whether a PBullet has hit the crate }; #endif \ No newline at end of file diff --git a/include/entities/Game.h b/include/entities/Game.h index a4b89ca..d0360c8 100644 --- a/include/entities/Game.h +++ b/include/entities/Game.h @@ -14,6 +14,7 @@ #include "services/BulletService.h" #include "entities/Text.h" #include +#include class Game { @@ -49,6 +50,7 @@ public: this->crosshair = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CROSSHAIR), {15, 15}, {0, 0}, {0, 0, 15, 15}); CollectableService::getInstance().initCollectables(); + std::cout << "Initting" << std::endl; initHud(); initPauseScreen(); diff --git a/include/entities/Jelly.h b/include/entities/Jelly.h index 7d42aa0..b221a37 100644 --- a/include/entities/Jelly.h +++ b/include/entities/Jelly.h @@ -10,13 +10,13 @@ class Jelly public: Jelly(){ this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::JELLY),{32,32}); - AssetService::getInstance().playSound(ESound::JELLY_FREED); + // AssetService::getInstance().playSound(ESound::JELLY_FREED); } Jelly(Vector2 startPos) { this->startPosition = startPos; this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::JELLY),{32,32},startPos); - AssetService::getInstance().playSound(ESound::JELLY_FREED); + // AssetService::getInstance().playSound(ESound::JELLY_FREED); } void logic() diff --git a/src/entities/Enemy.cpp b/src/entities/Enemy.cpp index 3ab3b08..7c85ccc 100644 --- a/src/entities/Enemy.cpp +++ b/src/entities/Enemy.cpp @@ -73,23 +73,16 @@ std::vector Enemy::getCorners() float top = texture.getPosition().y + velocity.y; int tileSize = MapService::getInstance().getMap().getTileSize(); - // Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize)); - // Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize)); - Vector2i leftCenter = Vector2i((int)(left -(spriteAnimation.y == Left ? gunLeft.getSize().x*(2.0f/3.0f) : 0.0f)) / tileSize, (int)(bottom - texture.getSize().y / 2.0) / tileSize); - // Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize)); - // Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / tileSize)); - Vector2i rightCenter = Vector2i((int)(right+ (spriteAnimation.y == Right ? gunRight.getSize().x*(2.0f/3.0f) : 0.0f)) / tileSize, (int)(bottom - texture.getSize().y / 2.0) / tileSize); + Vector2i leftCenter = Vector2i((int)(left - (spriteAnimation.y == Left ? gunLeft.getSize().x * (2.0f / 3.0f) : 0.0f)) / tileSize, (int)(bottom - texture.getSize().y / 2.0) / tileSize); + Vector2i rightCenter = Vector2i((int)(right + (spriteAnimation.y == Right ? gunRight.getSize().x * (2.0f / 3.0f) : 0.0f)) / tileSize, (int)(bottom - texture.getSize().y / 2.0) / tileSize); - Vector2i bottomLeft(Vector2i((int)(left + 5) / tileSize, (int)bottom / tileSize)); // +5 to separate bottom from side - Vector2i bottomRight(Vector2i((int)(right - 5) / tileSize, (int)bottom / tileSize)); // -5 to separate bottom from side + Vector2i bottomLeft(Vector2i((int)(left + 5) / tileSize, (int)bottom / tileSize)); + Vector2i bottomRight(Vector2i((int)(right - 5) / tileSize, (int)bottom / tileSize)); std::vector corners; corners.push_back(leftCenter); corners.push_back(rightCenter); - // corners.push_back(leftBottom); - // corners.push_back(rightTop); - // corners.push_back(rightBottom); corners.push_back(bottomLeft); corners.push_back(bottomRight); @@ -105,7 +98,6 @@ void Enemy::animate() Rectangle sourceRect = this->texture.getSourceRect(); sourceRect.x = spriteAnimation.x * this->texture.getSize().x; sourceRect.y = spriteAnimation.y * this->texture.getSize().y; - // sourceRect.x = std::fmod(sourceRect.x, 3 * this->texture.getSize().x); this->texture.setSourceRect(sourceRect); prevFrame = GetTime(); } @@ -154,7 +146,6 @@ void Enemy::fall() void Enemy::detectPlayer() { - // playerSpotted = false; Player &player = PlayerService::getInstance().getPlayer(); if (this->health > 0) { @@ -168,8 +159,6 @@ void Enemy::detectPlayer() float gunRotationLeft = atan2(playerCenter.y - enemyCenter.y, enemyCenter.x - playerCenter.x); float target = -gunRotationLeft * 180 / M_PI; - // float delta = target - this->gunLeft.getRotation(); - // float sign = std::copysign(1.0f, delta); this->gunLeft.setRotation(std::min(this->gunLeft.getRotation() + aimSpeed, target)); } else if (spriteAnimation.y == Right && player.getTexture().getPosition().x - this->texture.getPosition().x <= spottingRange && @@ -179,8 +168,6 @@ void Enemy::detectPlayer() float gunRotationRight = atan2(playerCenter.y - enemyCenter.y, playerCenter.x - enemyCenter.x); float target = gunRotationRight * 180 / M_PI; - - // this->gunRight.setRotation(gunRotationRight * 180 / M_PI); this->gunRight.setRotation(std::max(this->gunRight.getRotation() - aimSpeed, target)); } @@ -195,8 +182,8 @@ void Enemy::detectPlayer() { this->playerSpotted = false; /** Slowly ease the gun back to default position */ - this->gunLeft.setRotation(std::max(this->gunLeft.getRotation() - aimSpeed/2.0f, 0.0f)); - this->gunRight.setRotation(std::min(this->gunRight.getRotation() + aimSpeed/2.0f, 0.0f)); + this->gunLeft.setRotation(std::max(this->gunLeft.getRotation() - aimSpeed / 2.0f, 0.0f)); + this->gunRight.setRotation(std::min(this->gunRight.getRotation() + aimSpeed / 2.0f, 0.0f)); } } } @@ -215,49 +202,15 @@ void Enemy::shoot() float offsetDistance = this->gunLeft.getSize().x - this->gunLeft.getSize().x / 3; Vector2 playerCenter = {player.getTexture().getPosition().x + player.getTexture().getSize().x / 2.0f, player.getTexture().getPosition().y + player.getTexture().getSize().y / 2.0f}; Vector2 enemyCenter = {texture.getPosition().x + texture.getSize().x / 2.0f, texture.getPosition().y + texture.getSize().y / 2.0f}; - - // if (spriteAnimation.y == Left) - // { + float angleShot = atan2(playerCenter.y - enemyCenter.y, + playerCenter.x - enemyCenter.x); - float angleShot = atan2(playerCenter.y - enemyCenter.y, - playerCenter.x - enemyCenter.x); + float offsetX = cos(angleShot) * offsetDistance; + float offsetY = sin(angleShot) * offsetDistance; + Vector2 bulletStartLocation = {this->texture.getPosition().x + this->texture.getSize().x / 2.0f + offsetX, + this->texture.getPosition().y + this->texture.getSize().y / 2.0f + offsetY}; - // float gunRotationRadians = this->gunLeft.getRotation() * (M_PI / 180.0f); // Convert degrees to radians - float offsetX = cos(angleShot) * offsetDistance; - float offsetY = sin(angleShot) * offsetDistance; - Vector2 bulletStartLocation = {this->texture.getPosition().x + this->texture.getSize().x / 2.0f + offsetX, - this->texture.getPosition().y + this->texture.getSize().y / 2.0f + offsetY}; - - BulletService::getInstance().addEnemyBullet(bulletStartLocation, angleShot); - // } - - // if ((player.getTexture().getPosition().x > this->texture.getPosition().x + this->gunLeft.getSize().x && spriteAnimation.y == Right) || - // (player.getTexture().getPosition().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)) - // { - - // // float gunRotationRight = atan2(GameService::getInstance().getMouseWorldPos().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0), - // // GameService::getInstance().getMouseWorldPos().x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0)); - // // this->gunRight.setRotation(gunRotationRight * 180 / M_PI); - - // float angleShot = atan2(player.getTexture().getPosition().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0), - // player.getTexture().getPosition().x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0)); - - // // this->gunRight.setRotation(angleShot * 180 / M_PI); - // // this->gunLeft.setRotation(-angleShot * 180 / M_PI); - // /** Making the bullet originate from the gun's mouth */ - // float offsetDistance = this->gunLeft.getSize().x - this->gunLeft.getSize().x / 3; - // float offsetX = cos(angleShot) * offsetDistance; - // float offsetY = sin(angleShot) * offsetDistance; - // Vector2 bulletStartLocation = {this->texture.getPosition().x + this->texture.getSize().x / 2.0f + offsetX, - // this->texture.getPosition().y + this->texture.getSize().y / 2.0f + offsetY}; - - // // BulletService::getInstance().addEnemyBullet(bulletStartLocation, angleShot); - // } - // else - // { - // // this->gunRight.setRotation(0); - // // this->gunLeft.setRotation(0); - // } + BulletService::getInstance().addEnemyBullet(bulletStartLocation, angleShot); } } \ No newline at end of file diff --git a/src/entities/PBullet.cpp b/src/entities/PBullet.cpp index 936d370..9772e96 100644 --- a/src/entities/PBullet.cpp +++ b/src/entities/PBullet.cpp @@ -11,6 +11,7 @@ void PBullet::objCollision() this->position.y + this->size > crate.getObject().getPosition().y && this->position.y-this->size < crate.getObject().getPosition().y + crate.getObject().getSize().y) { crate.setHit(true); AssetService::getInstance().playSound(ESound::CRATE_BREAK); + AssetService::getInstance().playSound(ESound::JELLY_FREED); this->hasCollided = true; } }