[Updated] Code for use with WASM, [TODO] Make enemy gun follow player, spawn bullets from tip of the barrel

This commit is contained in:
Ninjaws
2024-12-17 23:15:26 +01:00
parent 2400b0500a
commit d740e1a167
25 changed files with 371 additions and 438 deletions
+4 -3
View File
@@ -6,6 +6,7 @@
#include "entities/TextureWrapper.h" #include "entities/TextureWrapper.h"
#include "entities/Vector2i.h" #include "entities/Vector2i.h"
#include <cmath> #include <cmath>
#include <cstdint>
class Enemy class Enemy
{ {
@@ -47,7 +48,7 @@ public:
return this->texture; return this->texture;
} }
u_int8_t getHealth() uint8_t getHealth()
{ {
return this->health; return this->health;
} }
@@ -61,8 +62,8 @@ private:
TextureWrapper texture; TextureWrapper texture;
TextureWrapper gunLeft; TextureWrapper gunLeft;
TextureWrapper gunRight; TextureWrapper gunRight;
u_int8_t health; uint8_t health;
u_int8_t maxHealth = 5; uint8_t maxHealth = 5;
Vector2 velocity = {0, 0}; Vector2 velocity = {0, 0};
float spottingRange = 300; // From how far away enemies can spot the player float spottingRange = 300; // From how far away enemies can spot the player
float gravity = 0.6f; float gravity = 0.6f;
+4 -4
View File
@@ -20,10 +20,10 @@ public:
void logic() void logic()
{ {
if (touched) // if (touched)
{ // {
StateService::getInstance().setScreen(EScreen::VICTORY); // // StateService::getInstance().setScreen(EScreen::VICTORY);
} // }
} }
void draw() void draw()
+12 -15
View File
@@ -66,7 +66,7 @@ public:
void logic() void logic()
{ {
this->crosshair.setPosition(GetMousePosition()); this->crosshair.setPosition(InputService::getInstance().getMousePos());
if (GameService::getInstance().isPaused()) if (GameService::getInstance().isPaused())
{ {
@@ -96,6 +96,9 @@ public:
{ {
GameService::getInstance().getCamera().offset = {(StateService::getInstance().getScreenSize().x / 2.0f) + ((StateService::getInstance().getScreenSize().x / 2.0f) - player.getTexture().getPosition().x), StateService::getInstance().getScreenSize().y / 2.0f}; GameService::getInstance().getCamera().offset = {(StateService::getInstance().getScreenSize().x / 2.0f) + ((StateService::getInstance().getScreenSize().x / 2.0f) - player.getTexture().getPosition().x), StateService::getInstance().getScreenSize().y / 2.0f};
} }
if(player.isFinishReached()) {
StateService::getInstance().setScreen(EScreen::VICTORY);
}
} }
void draw() void draw()
@@ -113,6 +116,7 @@ public:
EndMode2D(); EndMode2D();
drawHud();
if (GameService::getInstance().isPaused()) if (GameService::getInstance().isPaused())
{ {
drawPaused(); drawPaused();
@@ -120,9 +124,9 @@ public:
else if (GameService::getInstance().isGameOver()) else if (GameService::getInstance().isGameOver())
{ {
drawGameOver(); drawGameOver();
} else {
drawCrosshair();
} }
drawHud();
drawCrosshair();
EndDrawing(); EndDrawing();
} }
@@ -184,7 +188,7 @@ private:
const int SPACE_PER_ITEM = TOTAL_SPACE / AMNT_OF_BUTTONS; const int SPACE_PER_ITEM = TOTAL_SPACE / AMNT_OF_BUTTONS;
for (int i = 0; i < AMNT_OF_BUTTONS; i++) for (int i = 0; i < AMNT_OF_BUTTONS; i++)
{ {
Text txt = Text("Play Again", EFont::SANSATION, 30, 0, GRAY); Text txt = Text("Play Again", EFont::SANSATION, 30, 0, BLACK);
gameOverScreenButtons.push_back(txt); gameOverScreenButtons.push_back(txt);
} }
gameOverScreenButtons[0].setColor(WHITE); gameOverScreenButtons[0].setColor(WHITE);
@@ -229,7 +233,7 @@ private:
{ {
if (InputService::getInstance().isKeyPressed(KEY_D) || InputService::getInstance().isKeyPressed(KEY_A)) if (InputService::getInstance().isKeyPressed(KEY_D) || InputService::getInstance().isKeyPressed(KEY_A))
{ {
gameOverScreenButtons[hoveredGameOverScreenButton].setColor(GRAY); gameOverScreenButtons[hoveredGameOverScreenButton].setColor(BLACK);
/** Move up or down depending on the pressed button. +4 is used so that -1 becomes 3 (the last button) */ /** Move up or down depending on the pressed button. +4 is used so that -1 becomes 3 (the last button) */
hoveredGameOverScreenButton = ((hoveredGameOverScreenButton - (InputService::getInstance().isKeyPressed(KEY_D) ? -1 : 1)) + gameOverScreenButtons.size()) % gameOverScreenButtons.size(); hoveredGameOverScreenButton = ((hoveredGameOverScreenButton - (InputService::getInstance().isKeyPressed(KEY_D) ? -1 : 1)) + gameOverScreenButtons.size()) % gameOverScreenButtons.size();
gameOverScreenButtons[hoveredGameOverScreenButton].setColor(WHITE); gameOverScreenButtons[hoveredGameOverScreenButton].setColor(WHITE);
@@ -254,7 +258,7 @@ private:
void drawPaused() void drawPaused()
{ {
// 100, 100, 255, 60 // 100, 100, 255, 60
DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {0, 0, 255, 100}); DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {0, 0, 0, 200});
if (pauseScreenButtons.size() > 0) if (pauseScreenButtons.size() > 0)
{ {
for (int i = 0; i < pauseScreenButtons.size(); i++) for (int i = 0; i < pauseScreenButtons.size(); i++)
@@ -266,7 +270,7 @@ private:
void drawGameOver() void drawGameOver()
{ {
DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {255, 0, 0, 100}); DrawRectangle(0, 0, StateService::getInstance().getScreenSize().x, StateService::getInstance().getScreenSize().y, {100, 0, 0, 200});
gameOverText.draw(); gameOverText.draw();
@@ -280,17 +284,10 @@ private:
{ {
DrawTextureEx( DrawTextureEx(
this->crosshair.getTexture(), this->crosshair.getTexture(),
{GetMousePosition().x - this->crosshair.getSize().x / 2.0f, GetMousePosition().y - this->crosshair.getSize().y / 2.0f}, {InputService::getInstance().getMousePos().x - this->crosshair.getSize().x / 2.0f, InputService::getInstance().getMousePos().y - this->crosshair.getSize().y / 2.0f},
0, 0,
1.2f, 1.2f,
RED); RED);
// DrawTexturePro(
// this->crosshair.getTexture(),
// this->crosshair.getSourceRect(),
// {GetMousePosition().x, GetMousePosition().y, GetMousePosition().x+this->crosshair.getSize().x, GetMousePosition().y+this->crosshair.getSize().y},
// {this->crosshair.getSize().x/2.0f, this->crosshair.getSize().y/2.0f},
// 0,
// WHITE);
} }
void drawHud() void drawHud()
+5
View File
@@ -81,6 +81,10 @@ public:
this->health = std::max(health - damage, 0); this->health = std::max(health - damage, 0);
} }
bool isFinishReached() {
return this->finishReached;
}
protected: protected:
private: private:
TextureWrapper texture; TextureWrapper texture;
@@ -113,6 +117,7 @@ private:
uint8_t health; // Current health of the player uint8_t health; // Current health of the player
uint8_t max_health = 10; // Max health of the player uint8_t max_health = 10; // Max health of the player
bool gainedPowerup = false; bool gainedPowerup = false;
bool finishReached = false;
void applyGravity(); void applyGravity();
void movement(); void movement();
+3 -2
View File
@@ -4,6 +4,7 @@
#include "raylib.h" #include "raylib.h"
#include "services/AssetService.h" #include "services/AssetService.h"
#include "entities/TextureWrapper.h" #include "entities/TextureWrapper.h"
#include <cstdint>
class Powerup { class Powerup {
public: public:
@@ -40,8 +41,8 @@ class Powerup {
bool pickedUp = false; // When the player has picked up the pack bool pickedUp = false; // When the player has picked up the pack
float animationDelay = 0.08f; // How long to wait in between animation frames (in seconds) float animationDelay = 0.08f; // How long to wait in between animation frames (in seconds)
double lastFrame; // Time since the last frame double lastFrame; // Time since the last frame
u_int8_t currentFrame = 0; // Current frame of the animation uint8_t currentFrame = 0; // Current frame of the animation
u_int8_t amountOfFrames = 8; // Total amount of animation frames uint8_t amountOfFrames = 8; // Total amount of animation frames
void animate() { void animate() {
if(GetTime() - lastFrame > animationDelay) { if(GetTime() - lastFrame > animationDelay) {
+37 -31
View File
@@ -3,47 +3,53 @@
#include "raylib.h" #include "raylib.h"
#include "enums/EFont.h" #include "enums/EFont.h"
#include <string>
#include <iostream>
#include "services/AssetService.h" #include "services/AssetService.h"
#include <string>
class Text { class Text
public: {
Text(){} public:
Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100,100}); Text() {}
~Text(){} Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100, 100});
~Text() {
}
Vector2 getDimensions(); Vector2 getDimensions();
void setPosition(Vector2 position) { void setPosition(Vector2 position)
this->position = position; {
} this->position = position;
}
void setColor(Color color) { void setColor(Color color)
this->color = color; {
} this->color = color;
}
void setText(std::string text) { void setText(std::string text)
this->text = text; {
} this->text = text;
}
Vector2 getPosition() { Vector2 getPosition()
return position; {
} return position;
}
std::string getText() { std::string getText()
return this->text; {
} return this->text;
}
void draw(); void draw();
private: private:
std::string text; std::string text;
Font font; Font font;
int fontSize; int fontSize;
int spacing; int spacing;
Color color; Color color;
Vector2 position; Vector2 position;
}; };
#endif #endif
+2 -1
View File
@@ -5,7 +5,8 @@ class Screen
{ {
public: public:
Screen(){} Screen(){}
~Screen() {} ~Screen() {
}
/** /**
* Execute all logic for this frame * Execute all logic for this frame
+2
View File
@@ -14,6 +14,8 @@ class VictoryScreen : public Screen
public: public:
VictoryScreen(); VictoryScreen();
~VictoryScreen() {
}
void logic() override; void logic() override;
void draw() override; void draw() override;
+1
View File
@@ -4,6 +4,7 @@
#include "raylib.h" #include "raylib.h"
#include "services/Service.h" #include "services/Service.h"
#include "services/AssetService.h" #include "services/AssetService.h"
#include "entities/Bullet.h"
#include "entities/PBullet.h" #include "entities/PBullet.h"
#include "entities/EBullet.h" #include "entities/EBullet.h"
#include <optional> #include <optional>
+16 -193
View File
@@ -3,7 +3,6 @@
#include "raylib.h" #include "raylib.h"
#include "services/Service.h" #include "services/Service.h"
#include "services/MapService.h"
#include "entities/Vector2i.h" #include "entities/Vector2i.h"
#include "entities/Crate.h" #include "entities/Crate.h"
#include "entities/Jelly.h" #include "entities/Jelly.h"
@@ -94,198 +93,22 @@ private:
std::vector<Powerup> powerUps; std::vector<Powerup> powerUps;
Flag flag; Flag flag;
void initCrates() void initCrates();
{ void initJellies();
crates.clear(); void initAmmoPacks();
int tileSize = MapService::getInstance().getMap().getTileSize(); void initHealthPacks();
for (int i = 0; i < amountOfCrates; i++) void initPowerups();
{ void initFlag();
crates.push_back(Crate()); void crateLogic();
} void jellyLogic();
crates[0].getObject().setPosition({38.0f * tileSize, 9.0f * tileSize}); void ammoPackLogic();
crates[1].getObject().setPosition({41.0f * tileSize, 5.0f * tileSize}); void healthPackLogic();
crates[2].getObject().setPosition({68.0f * tileSize, 4.0f * tileSize}); void powerupLogic();
void drawCrates();
for (int i = 0; i < crates.size(); i++) void drawJellies();
{ void drawAmmoPacks();
Vector2i crateTilePos = Vector2i(crates[i].getObject().getPosition().x / tileSize, crates[i].getObject().getPosition().y / tileSize); void drawHealthpacks();
MapService::getInstance().getMap().updateColmapTile(crateTilePos, true); void drawPowerups();
}
}
void initJellies()
{
jellies.clear();
}
void initAmmoPacks()
{
ammoPacks.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfAmmoPacks; i++)
{
ammoPacks.push_back(AmmoPack());
}
ammoPacks[0].getObject().setPosition({32.0f * tileSize, 9.0f * tileSize});
ammoPacks[1].getObject().setPosition({48.0f * tileSize, 5.0f * tileSize});
ammoPacks[2].getObject().setPosition({88.0f * tileSize, 9.0f * tileSize});
ammoPacks[3].getObject().setPosition({105.0f * tileSize, 15.0f * tileSize});
}
void initHealthPacks()
{
healthPacks.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfHealthPacks; i++)
{
healthPacks.push_back(HealthPack());
}
healthPacks[0].getObject().setPosition({28.0f * tileSize, 10.0f * tileSize});
healthPacks[1].getObject().setPosition({64.0f * tileSize, 6.0f * tileSize});
healthPacks[2].getObject().setPosition({118.0f * tileSize, 11.0f * tileSize});
healthPacks[3].getObject().setPosition({74.0f * tileSize, 8.0f * tileSize});
}
void initPowerups()
{
powerUps.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfPowerups; i++)
{
powerUps.push_back(Powerup());
}
powerUps[0].getObject().setPosition({41.0f * tileSize+8, 10.0f * tileSize-25});
}
void initFlag() {
int tileSize = MapService::getInstance().getMap().getTileSize();
this->flag = Flag({118.0f*tileSize-tileSize/2.0f, 3.0f * tileSize-8});
}
void crateLogic()
{
for (auto it = crates.begin(); it != crates.end();)
{
auto &crate = *it;
/**
* If the crate has been hit:
* 1. Create a Jelly on the location of the crate
* 2. Remove the collision tile from the collision map
* 3. Remove the crate
*/
if (crate.hasBeenHit())
{
Vector2 pos = crate.getObject().getPosition();
jellies.push_back(Jelly(pos));
Vector2i tilePos = Vector2i(pos.x / MapService::getInstance().getMap().getTileSize(), pos.y / MapService::getInstance().getMap().getTileSize());
MapService::getInstance().getMap().updateColmapTile(tilePos, false);
it = crates.erase(it);
}
else
{
++it;
}
}
}
void jellyLogic()
{
for (int i = 0; i < jellies.size(); i++)
{
jellies[i].logic();
}
}
void ammoPackLogic()
{
for (auto it = ammoPacks.begin(); it != ammoPacks.end();)
{
auto &ammoPack = *it;
if (ammoPack.hasBeenPickedUp())
{
it = ammoPacks.erase(it);
}
else
{
++it;
}
}
}
void healthPackLogic()
{
for (auto it = healthPacks.begin(); it != healthPacks.end();)
{
auto &healthPack = *it;
if (healthPack.hasBeenPickedUp())
{
it = healthPacks.erase(it);
}
else
{
++it;
}
}
}
void powerupLogic()
{
for (auto it = powerUps.begin(); it != powerUps.end();)
{
auto &powerUp = *it;
if (powerUp.hasBeenPickedUp())
{
it = powerUps.erase(it);
}
else
{
powerUp.logic();
++it;
}
}
}
void drawCrates()
{
for (int i = 0; i < crates.size(); i++)
{
crates[i].draw();
}
}
void drawJellies()
{
for (int i = 0; i < jellies.size(); i++)
{
jellies[i].draw();
}
}
void drawAmmoPacks()
{
for (int i = 0; i < ammoPacks.size(); i++)
{
ammoPacks[i].draw();
}
}
void drawHealthpacks()
{
for (int i = 0; i < healthPacks.size(); i++)
{
healthPacks[i].draw();
}
}
void drawPowerups()
{
for (int i = 0; i < powerUps.size(); i++)
{
powerUps[i].draw();
}
}
}; };
#endif #endif
+4 -49
View File
@@ -3,61 +3,16 @@
#include "raylib.h" #include "raylib.h"
#include "services/Service.h" #include "services/Service.h"
#include "services/AssetService.h"
#include "entities/Enemy.h" #include "entities/Enemy.h"
#include <optional> #include <optional>
class EnemyService : public Service<EnemyService> class EnemyService : public Service<EnemyService>
{ {
public: public:
void initEnemies() void initEnemies();
{ void enemyLogic();
this->enemies.clear(); void drawEnemies();
const int AMNT_OF_ENEMIES = 8; std::vector<Enemy> &getEnemies();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < AMNT_OF_ENEMIES; i++)
{
enemies.push_back(Enemy());
}
this->enemies[0].getObject().setPosition({31.0f * tileSize, 15.0f * tileSize});
this->enemies[1].getObject().setPosition({51.0f * tileSize, 10.0f * tileSize});
this->enemies[2].getObject().setPosition({56.0f * tileSize, 5.0f * tileSize});
this->enemies[3].getObject().setPosition({78.0f * tileSize, 10.0f * tileSize});
this->enemies[4].getObject().setPosition({82.0f * tileSize, 15.0f * tileSize});
this->enemies[5].getObject().setPosition({88.0f * tileSize, 4.0f * tileSize});
this->enemies[6].getObject().setPosition({98.0f * tileSize, 11.0f * tileSize});
this->enemies[7].getObject().setPosition({103.0f * tileSize, 15.0f * tileSize});
}
void enemyLogic()
{
for (auto it = enemies.begin(); it != enemies.end();)
{
auto &enemy = *it;
if (enemy.getObject().getPosition().y < 0)
{
it = enemies.erase(it);
}
else
{
enemy.logic();
++it;
}
}
}
void drawEnemies()
{
for (int i = 0; i < enemies.size(); i++)
{
enemies[i].draw();
}
}
std::vector<Enemy> &getEnemies()
{
return this->enemies;
}
protected: protected:
private: private:
+3 -1
View File
@@ -4,6 +4,7 @@
#include "raylib.h" #include "raylib.h"
#include "services/Service.h" #include "services/Service.h"
#include "services/AudioService.h" #include "services/AudioService.h"
#include "services/InputService.h"
#include <vector> #include <vector>
#include <list> #include <list>
@@ -49,7 +50,8 @@ public:
Vector2 getMouseWorldPos() Vector2 getMouseWorldPos()
{ {
Vector2 mouseWorldPos = {GetMousePosition().x + camera.target.x - camera.offset.x, GetMousePosition().y}; Vector2 mouseWorldPos = {InputService::getInstance().getMousePos().x + camera.target.x - camera.offset.x,
InputService::getInstance().getMousePos().y};
return mouseWorldPos; return mouseWorldPos;
} }
+10 -1
View File
@@ -1,8 +1,8 @@
#ifndef INPUT_SERVICE_H #ifndef INPUT_SERVICE_H
#define INPUT_SERVICE_H #define INPUT_SERVICE_H
#include "Service.h"
#include "raylib.h" #include "raylib.h"
#include "Service.h"
#include <vector> #include <vector>
#include <unordered_set> #include <unordered_set>
#include <unordered_map> #include <unordered_map>
@@ -69,6 +69,14 @@ public:
} }
} }
void checkMousePos() {
this->mousePosition = GetMousePosition();
}
Vector2 getMousePos() {
return this->mousePosition;
}
bool isKeyPressed(KeyboardKey key) bool isKeyPressed(KeyboardKey key)
{ {
return keysPressed[key]; return keysPressed[key];
@@ -88,6 +96,7 @@ public:
} }
private: private:
Vector2 mousePosition {0,0};
std::unordered_set<KeyboardKey> watchedKeys; std::unordered_set<KeyboardKey> watchedKeys;
std::unordered_map<KeyboardKey, bool> keysPressed; std::unordered_map<KeyboardKey, bool> keysPressed;
std::unordered_map<KeyboardKey, bool> keysDown; std::unordered_map<KeyboardKey, bool> keysDown;
+7 -9
View File
@@ -1,16 +1,17 @@
#ifndef STATE_SERVICE_H #ifndef STATE_SERVICE_H
#define STATE_SERVICE_H #define STATE_SERVICE_H
#include <iostream>
#include <memory>
#include "services/Service.h"
#include "screens/Screen.h"
#include "enums/EScreen.h"
#include "raylib.h" #include "raylib.h"
#include "enums/EScreen.h"
#include "services/Service.h"
#include "services/InputService.h"
#include "screens/Screen.h"
#include <memory>
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
// #include <emscripten/html5.h>
/** /**
* Handles switching between screens * Handles switching between screens
@@ -34,10 +35,6 @@ public:
void startGame(); void startGame();
static void staticStep() static void staticStep()
{ {
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
std::cout << "Input!" << std::endl;
}
// Access the singleton instance and call step // Access the singleton instance and call step
getInstance().step(); getInstance().step();
} }
@@ -52,6 +49,7 @@ private:
*/ */
void step(); void step();
void handleMusic(); void handleMusic();
}; };
#endif #endif
+2 -1
View File
@@ -2,7 +2,8 @@
# emcc src/main.cpp -o dist/game.js -Wno-missing-braces -I/app/libs/raylib/build/raylib/include -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a -s USE_GLFW=3 -DPLATFORM_WEB # emcc src/main.cpp -o dist/game.js -Wno-missing-braces -I/app/libs/raylib/build/raylib/include -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a -s USE_GLFW=3 -DPLATFORM_WEB
emcc $(find src/ -name "*.cpp") -o dist/game.js -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=268435456 -s MAXIMUM_MEMORY=4294967296 -s --preload-file assets@/assets -s USE_GLFW=3 -DPLATFORM_WEB # emcc $(find src/ -name "*.cpp") -o dist/game.js -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=268435456 -s MAXIMUM_MEMORY=4294967296 -s --preload-file assets@/assets -s USE_GLFW=3 -DPLATFORM_WEB
emcc $(find src/ -name "*.cpp") -o dist/game.js -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
# -I./include # -I./include
# -L$LD_LIBRARY_PATH # -L$LD_LIBRARY_PATH
# -I. # -I.
+1 -48
View File
@@ -1,10 +1,8 @@
#include "entities/Map.h" #include "entities/Map.h"
#include "services/AssetService.h"
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream>
#include "services/AssetService.h"
#include "entities/Map.h"
Map::Map() Map::Map()
{ {
@@ -114,19 +112,6 @@ void Map::loadTileset()
this->tileset = AssetService::getInstance().loadTexture("Textures/tileset.png"); this->tileset = AssetService::getInstance().loadTexture("Textures/tileset.png");
} }
// std::unordered_map<ECorner, Vector2> Map::cornersToTilePos(std::unordered_map<ECorner, Vector2> corners)
// {
// // std::cout << "Starting converting" << std::endl;
// std::unordered_map<ECorner, Vector2> cornersTilePos;
// for (const auto &pair : corners)
// {
// // std::cout << "Foreach pair" << std::endl;
// Vector2 original = corners[pair.first];
// cornersTilePos[pair.first] = {floorf(original.x / this->tileSize), floorf(original.y / this->tileSize)};
// }
// return cornersTilePos;
// }
void Map::draw() void Map::draw()
{ {
for (int i = 0; i < map.size(); i++) for (int i = 0; i < map.size(); i++)
@@ -146,35 +131,3 @@ void Map::draw()
} }
} }
// std::unordered_map<ECorner, bool> Map::checkForCollision(TextureWrapper texture)
// {
// // std::cout << "Converting.." << std::endl;
// std::unordered_map<ECorner, Vector2> cornersTilePos = cornersToTilePos(texture.getCorners());
// // std::cout << "Done with converting" << std::endl;
// std::unordered_map<ECorner, bool> cornerCollisions;
// for (const auto &pair : cornersTilePos)
// {
// // std::cout << "foreach collision check" << std::endl;
// Vector2 pos = cornersTilePos[pair.first];
// // std::cout << pos.x << " " << pos.y << std::endl;
// if(colMap[pos.y][pos.x] == 1) {
// cornerCollisions[pair.first] = true;
// } else {
// cornerCollisions[pair.first] = false;
// }
// }
// // std::cout << "Finished collision checks" << std::endl;
// return cornerCollisions;
// // for (int i = 0; i < tiles.size(); i++)
// // {
// // if (colMap[tiles[i].y][tiles[i].x] == 1)
// // {
// // timesCollision.push_back(i);
// // }
// // }
// }
+3 -3
View File
@@ -3,6 +3,7 @@
#include "services/GameService.h" #include "services/GameService.h"
#include "services/CollectableService.h" #include "services/CollectableService.h"
#include "services/InputService.h" #include "services/InputService.h"
#include "services/MapService.h"
void Player::applyGravity() void Player::applyGravity()
{ {
@@ -171,7 +172,7 @@ void Player::consumableCollision()
{ {
healthPack.setPickedUp(true); healthPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::HEAL_PICKUP); AssetService::getInstance().playSound(ESound::HEAL_PICKUP);
health = std::max(health + healthPack.getHealAmount(), (int)max_health); health = std::min(health + healthPack.getHealAmount(), (int)max_health);
} }
} }
} }
@@ -188,8 +189,6 @@ void Player::consumableCollision()
AssetService::getInstance().playSound(ESound::POWERUP); AssetService::getInstance().playSound(ESound::POWERUP);
jumpPower += powerup.getExtraJumpPower(); jumpPower += powerup.getExtraJumpPower();
gainedPowerup = true; gainedPowerup = true;
// health = std::max(health+powerup.getHealAmount(),(int)max_health);
// }
} }
} }
@@ -200,6 +199,7 @@ void Player::consumableCollision()
this->texture.getPosition().y < flag.getObject().getPosition().y + flag.getObject().getSize().y) this->texture.getPosition().y < flag.getObject().getPosition().y + flag.getObject().getSize().y)
{ {
flag.setTouched(true); flag.setTouched(true);
finishReached = true;
} }
} }
-57
View File
@@ -1,66 +1,9 @@
#include "raylib.h"
// #include "screens/Screen.h"
// #include "screens/TitleScreen.h"
#include "services/StateService.h" #include "services/StateService.h"
// #include <iostream>
// #if defined(PLATFORM_WEB)
// #include <emscripten/emscripten.h>
// #endif
// void step();
// static void gameloop();
int main() int main()
{ {
StateService& stateService = StateService::getInstance(); StateService& stateService = StateService::getInstance();
stateService.setScreenSize({640,544}); stateService.setScreenSize({640,544});
stateService.startGame(); stateService.startGame();
// std::cout << "StateService instance: " << service << std::endl;
// InitWindow(640, 544, "Jelly Rescue");
// // SetTargetFPS(60);
// // setScreen(EScreen::TITLE);
// #if defined(PLATFORM_WEB)
// emscripten_set_main_loop(step, 0, 1);
// #else
// while (!WindowShouldClose())
// {
// step();
// }
// #endif
// CloseWindow();
return 0; return 0;
} }
// static void gameloop()
// {
// // InitAudioDevice();
// InitWindow(640, 544, "Jelly Rescue");
// // SetTargetFPS(60);
// // setScreen(EScreen::TITLE);
// #if defined(PLATFORM_WEB)
// emscripten_set_main_loop(step, 0, 1);
// #else
// while (!WindowShouldClose())
// {
// step();
// }
// #endif
// CloseWindow();
// // CloseAudioDevice();
// }
// void step()
// {
// ClearBackground(BLACK);
// }
+2 -2
View File
@@ -1,7 +1,7 @@
#include "screens/MainScreen.h" #include "screens/MainScreen.h"
#include "raylib.h"
#include "services/AudioService.h" #include "services/AudioService.h"
#include "services/StateService.h" #include "services/StateService.h"
#include "raylib.h"
#include "services/InputService.h" #include "services/InputService.h"
MainScreen::MainScreen() MainScreen::MainScreen()
@@ -60,7 +60,7 @@ void MainScreen::mouseLogic()
Vector2 pos = buttons[i].getPosition(); Vector2 pos = buttons[i].getPosition();
Rectangle rect = {pos.x, pos.y, dim.x, dim.y}; Rectangle rect = {pos.x, pos.y, dim.x, dim.y};
Vector2 mousePos = GetMousePosition(); Vector2 mousePos = InputService::getInstance().getMousePos();
// mousePos.x >= rect.x && mousePos.x <= (rect.x+rect.width) && // mousePos.x >= rect.x && mousePos.x <= (rect.x+rect.width) &&
if ((mousePos.x + textPadding) >= rect.x && mousePos.x <= (rect.x + rect.width + textPadding) && (mousePos.y + textPadding) >= rect.y && mousePos.y <= (rect.y + rect.height + textPadding)) if ((mousePos.x + textPadding) >= rect.x && mousePos.x <= (rect.x + rect.width + textPadding) && (mousePos.y + textPadding) >= rect.y && mousePos.y <= (rect.y + rect.height + textPadding))
{ {
-2
View File
@@ -3,7 +3,6 @@
#include "services/StateService.h" #include "services/StateService.h"
#include "enums/EFont.h" #include "enums/EFont.h"
#include "services/AudioService.h" #include "services/AudioService.h"
#include <iostream>
#include "services/InputService.h" #include "services/InputService.h"
TitleScreen::TitleScreen() TitleScreen::TitleScreen()
@@ -34,7 +33,6 @@ TitleScreen::TitleScreen()
void TitleScreen::logic() void TitleScreen::logic()
{ {
// std::cout << "Next screen!" << std::endl;
if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT)) if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
{ {
StateService::getInstance().setScreen(EScreen::MAIN); StateService::getInstance().setScreen(EScreen::MAIN);
+2 -3
View File
@@ -1,8 +1,7 @@
#include "screens/TutorialScreen.h" #include "screens/TutorialScreen.h"
#include "services/StateService.h"
#include "enums/EFont.h"
#include "enums/EScreen.h" #include "enums/EScreen.h"
#include "enums/EFont.h"
#include "services/StateService.h"
#include "services/InputService.h" #include "services/InputService.h"
TutorialScreen::TutorialScreen() { TutorialScreen::TutorialScreen() {
-1
View File
@@ -1,5 +1,4 @@
#include "services/AssetService.h" #include "services/AssetService.h"
#include <iostream>
AssetService::AssetService() AssetService::AssetService()
{ {
setAssetDir("assets/"); setAssetDir("assets/");
+196
View File
@@ -0,0 +1,196 @@
#include "services/CollectableService.h"
#include "services/MapService.h"
void CollectableService::initCrates()
{
crates.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfCrates; i++)
{
crates.push_back(Crate());
}
crates[0].getObject().setPosition({38.0f * tileSize, 9.0f * tileSize});
crates[1].getObject().setPosition({41.0f * tileSize, 5.0f * tileSize});
crates[2].getObject().setPosition({68.0f * tileSize, 4.0f * tileSize});
for (int i = 0; i < crates.size(); i++)
{
Vector2i crateTilePos = Vector2i(crates[i].getObject().getPosition().x / tileSize, crates[i].getObject().getPosition().y / tileSize);
MapService::getInstance().getMap().updateColmapTile(crateTilePos, true);
}
}
void CollectableService::initJellies()
{
jellies.clear();
}
void CollectableService::initAmmoPacks()
{
ammoPacks.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfAmmoPacks; i++)
{
ammoPacks.push_back(AmmoPack());
}
ammoPacks[0].getObject().setPosition({32.0f * tileSize, 9.0f * tileSize});
ammoPacks[1].getObject().setPosition({48.0f * tileSize, 5.0f * tileSize});
ammoPacks[2].getObject().setPosition({88.0f * tileSize, 9.0f * tileSize});
ammoPacks[3].getObject().setPosition({105.0f * tileSize, 15.0f * tileSize});
}
void CollectableService::initHealthPacks()
{
healthPacks.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfHealthPacks; i++)
{
healthPacks.push_back(HealthPack());
}
healthPacks[0].getObject().setPosition({28.0f * tileSize, 10.0f * tileSize});
healthPacks[1].getObject().setPosition({64.0f * tileSize, 6.0f * tileSize});
healthPacks[2].getObject().setPosition({118.0f * tileSize, 11.0f * tileSize});
healthPacks[3].getObject().setPosition({74.0f * tileSize, 8.0f * tileSize});
}
void CollectableService::initPowerups()
{
powerUps.clear();
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < amountOfPowerups; i++)
{
powerUps.push_back(Powerup());
}
powerUps[0].getObject().setPosition({41.0f * tileSize + 8, 10.0f * tileSize - 25});
}
void CollectableService::initFlag()
{
int tileSize = MapService::getInstance().getMap().getTileSize();
this->flag = Flag({118.0f * tileSize - tileSize / 2.0f, 3.0f * tileSize - 8});
}
void CollectableService::crateLogic()
{
for (auto it = crates.begin(); it != crates.end();)
{
auto &crate = *it;
/**
* If the crate has been hit:
* 1. Create a Jelly on the location of the crate
* 2. Remove the collision tile from the collision map
* 3. Remove the crate
*/
if (crate.hasBeenHit())
{
Vector2 pos = crate.getObject().getPosition();
jellies.push_back(Jelly(pos));
Vector2i tilePos = Vector2i(pos.x / MapService::getInstance().getMap().getTileSize(), pos.y / MapService::getInstance().getMap().getTileSize());
MapService::getInstance().getMap().updateColmapTile(tilePos, false);
it = crates.erase(it);
}
else
{
++it;
}
}
}
void CollectableService::jellyLogic()
{
for (int i = 0; i < jellies.size(); i++)
{
jellies[i].logic();
}
}
void CollectableService::ammoPackLogic()
{
for (auto it = ammoPacks.begin(); it != ammoPacks.end();)
{
auto &ammoPack = *it;
if (ammoPack.hasBeenPickedUp())
{
it = ammoPacks.erase(it);
}
else
{
++it;
}
}
}
void CollectableService::healthPackLogic()
{
for (auto it = healthPacks.begin(); it != healthPacks.end();)
{
auto &healthPack = *it;
if (healthPack.hasBeenPickedUp())
{
it = healthPacks.erase(it);
}
else
{
++it;
}
}
}
void CollectableService::powerupLogic()
{
for (auto it = powerUps.begin(); it != powerUps.end();)
{
auto &powerUp = *it;
if (powerUp.hasBeenPickedUp())
{
it = powerUps.erase(it);
}
else
{
powerUp.logic();
++it;
}
}
}
void CollectableService::drawCrates()
{
for (int i = 0; i < crates.size(); i++)
{
crates[i].draw();
}
}
void CollectableService::drawJellies()
{
for (int i = 0; i < jellies.size(); i++)
{
jellies[i].draw();
}
}
void CollectableService::drawAmmoPacks()
{
for (int i = 0; i < ammoPacks.size(); i++)
{
ammoPacks[i].draw();
}
}
void CollectableService::drawHealthpacks()
{
for (int i = 0; i < healthPacks.size(); i++)
{
healthPacks[i].draw();
}
}
void CollectableService::drawPowerups()
{
for (int i = 0; i < powerUps.size(); i++)
{
powerUps[i].draw();
}
}
+51
View File
@@ -0,0 +1,51 @@
#include "services/EnemyService.h"
#include "services/MapService.h"
void EnemyService::initEnemies()
{
this->enemies.clear();
const int AMNT_OF_ENEMIES = 8;
int tileSize = MapService::getInstance().getMap().getTileSize();
for (int i = 0; i < AMNT_OF_ENEMIES; i++)
{
enemies.push_back(Enemy());
}
this->enemies[0].getObject().setPosition({31.0f * tileSize, 15.0f * tileSize});
this->enemies[1].getObject().setPosition({51.0f * tileSize, 10.0f * tileSize});
this->enemies[2].getObject().setPosition({56.0f * tileSize, 5.0f * tileSize});
this->enemies[3].getObject().setPosition({78.0f * tileSize, 10.0f * tileSize});
this->enemies[4].getObject().setPosition({82.0f * tileSize, 15.0f * tileSize});
this->enemies[5].getObject().setPosition({88.0f * tileSize, 4.0f * tileSize});
this->enemies[6].getObject().setPosition({98.0f * tileSize, 11.0f * tileSize});
this->enemies[7].getObject().setPosition({103.0f * tileSize, 15.0f * tileSize});
}
void EnemyService::enemyLogic()
{
for (auto it = enemies.begin(); it != enemies.end();)
{
auto &enemy = *it;
if (enemy.getObject().getPosition().y < 0)
{
it = enemies.erase(it);
}
else
{
enemy.logic();
++it;
}
}
}
void EnemyService::drawEnemies()
{
for (int i = 0; i < enemies.size(); i++)
{
enemies[i].draw();
}
}
std::vector<Enemy> &EnemyService::getEnemies()
{
return this->enemies;
}
+3 -11
View File
@@ -49,17 +49,9 @@ void StateService::startGame()
// InitWindow(640,544, "Jelly Rescue"); // InitWindow(640,544, "Jelly Rescue");
InitWindow(screenSize.x, screenSize.y, "Jelly Rescue"); InitWindow(screenSize.x, screenSize.y, "Jelly Rescue");
SetTargetFPS(45); SetTargetFPS(45);
#if not defined(PLATFORM_WEB)
HideCursor(); HideCursor();
#endif
// #if defined(PLATFORM_WEB)
// emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_CANVAS, nullptr, 1, staticStep);
// #endif
// #if defined(PLATFORM_WEB)
// emscripten_set_focusin_callback(EMSCRIPTEN_EVENT_TARGET_CANVAS, nullptr, 1, [](const EmscriptenFocusEvent* event) {
// std::cout << "Canvas Focused!" << std::endl;
// });
// #endif
setScreen(EScreen::TITLE); setScreen(EScreen::TITLE);
@@ -67,7 +59,6 @@ void StateService::startGame()
emscripten_set_main_loop(StateService::staticStep, 0, 1); emscripten_set_main_loop(StateService::staticStep, 0, 1);
#else #else
// while (!WindowShouldClose() && instance->gameRunning)
while (!WindowShouldClose() && gameRunning) while (!WindowShouldClose() && gameRunning)
{ {
step(); step();
@@ -90,6 +81,7 @@ void StateService::handleMusic()
void StateService::step() void StateService::step()
{ {
InputService::getInstance().checkKeys(); InputService::getInstance().checkKeys();
InputService::getInstance().checkMousePos();
handleMusic(); handleMusic();
currentScreen->draw(); currentScreen->draw();