[Added] Enemy player detection and shooting, [Moved] Lots of entity code to cpp files to avoid circular dependencies

This commit is contained in:
Ninjaws
2024-12-17 20:08:35 +01:00
parent e8c78770b5
commit 2400b0500a
21 changed files with 820 additions and 701 deletions
+3 -1
View File
@@ -53,6 +53,8 @@
"typeinfo": "cpp", "typeinfo": "cpp",
"variant": "cpp", "variant": "cpp",
"format": "cpp", "format": "cpp",
"text_encoding": "cpp" "text_encoding": "cpp",
"ratio": "cpp",
"cinttypes": "cpp"
} }
} }
+23 -24
View File
@@ -8,44 +8,43 @@
#include "services/AssetService.h" #include "services/AssetService.h"
#include "services/StateService.h" #include "services/StateService.h"
class Background { class Background
public: {
Background() { public:
Background()
{
} }
Background(EBackground background){ Background(EBackground background)
// Texture2D texture = AssetService::getInstance().getBackground(background); {
TextureWrapper wrapper = TextureWrapper(AssetService::getInstance().getBackground(background), StateService::getInstance().getScreenSize(), {0,0}); TextureWrapper wrapper = TextureWrapper(AssetService::getInstance().getBackground(background), StateService::getInstance().getScreenSize(), {0, 0});
wrapper.setSourceRect({0,0,wrapper.getSize().x, wrapper.getSize().y}); wrapper.setSourceRect({0, 0, wrapper.getSize().x, wrapper.getSize().y});
textures.push_back(wrapper); textures.push_back(wrapper);
} }
Background(std::vector<TextureWrapper> backgrounds){ Background(std::vector<TextureWrapper> backgrounds)
for(int i = 0; i < backgrounds.size(); i++) { {
for (int i = 0; i < backgrounds.size(); i++)
{
this->textures.push_back(backgrounds[i]); this->textures.push_back(backgrounds[i]);
} }
} }
~Background(){} ~Background() {}
// Vector2 getPosition(); void draw()
// void updatePosition(Vector2 deltaPos); {
// void setPosition(Vector2 position); for (int i = 0; i < textures.size(); i++)
void draw(){ {
for(int i = 0; i < textures.size(); i++) {
TextureWrapper texture = textures[i]; TextureWrapper texture = textures[i];
// Rectangle sourceRect = {0, 0, texture.getSize().x, texture.getSize().y}; DrawTextureRec(texture.getTexture(), texture.getSourceRect(), texture.getPosition(), WHITE);
DrawTextureRec(texture.getTexture(), texture.getSourceRect(), texture.getPosition(),WHITE);
} }
} }
Vector2 getBackgroundSize() { Vector2 getBackgroundSize()
TextureWrapper lastbg = this->textures[this->textures.size()-1]; {
TextureWrapper lastbg = this->textures[this->textures.size() - 1];
return {lastbg.getPosition().x + lastbg.getSize().x, lastbg.getPosition().y + lastbg.getSize().y}; return {lastbg.getPosition().x + lastbg.getSize().x, lastbg.getPosition().y + lastbg.getSize().y};
} }
private: private:
std::vector<TextureWrapper> textures; std::vector<TextureWrapper> textures;
// Vector2 position;
// Vector2 size;
}; };
#endif #endif
+3 -28
View File
@@ -2,11 +2,9 @@
#define BULLET_H #define BULLET_H
#include "raylib.h" #include "raylib.h"
#include <cmath>
#include "entities/TextureWrapper.h" #include "entities/TextureWrapper.h"
#include "entities/Vector2i.h" #include "entities/Vector2i.h"
#include "services/MapService.h" #include <cmath>
#include <iostream>
class Bullet class Bullet
{ {
@@ -18,8 +16,6 @@ public:
this->direction = direction; this->direction = direction;
} }
// ~Bullet() {}
void logic() void logic()
{ {
move(); move();
@@ -31,7 +27,7 @@ public:
void draw() void draw()
{ {
DrawCircleGradient(position.x, position.y, size, color, BLUE); DrawCircleGradient(position.x, position.y, size, color, BLACK);
} }
bool getHasCollided() bool getHasCollided()
@@ -46,37 +42,16 @@ protected:
bool hasCollided = false; bool hasCollided = false;
private: private:
// TextureWrapper object;
float direction; float direction;
float velocity = 15.0f; float velocity = 15.0f;
void move() void move()
{ {
// float directionInRadians = this->direction * M_PI / 180.0f;
position.x += velocity * cos(direction); position.x += velocity * cos(direction);
position.y += velocity * sin(direction); position.y += velocity * sin(direction);
} }
void envCollision() void envCollision();
{
if (position.x < 0 || position.y < 0 || position.y > GetScreenHeight())
{
hasCollided = true;
return;
}
const int tileSize = MapService::getInstance().getMap().getTileSize();
Vector2i tilePos = Vector2i((int)this->position.x / tileSize, (int)this->position.y / tileSize);
if (MapService::getInstance().getMap().getColMap()[tilePos.y][tilePos.x] == 1)
{
hasCollided = true;
return;
}
}
virtual void objCollision() = 0; virtual void objCollision() = 0;
// void handleCollision() {
// }
}; };
#endif #endif
+18 -11
View File
@@ -4,33 +4,40 @@
#include "services/AssetService.h" #include "services/AssetService.h"
#include "entities/TextureWrapper.h" #include "entities/TextureWrapper.h"
class Crate { class Crate
{
public: public:
Crate() { Crate()
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CRATE),{32,32}); {
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CRATE), {32, 32});
} }
Crate(Vector2 pos) { Crate(Vector2 pos)
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CRATE),{32,32}, pos); {
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CRATE), {32, 32}, pos);
} }
void logic() { void logic()
{
} }
bool hasBeenHit() { bool hasBeenHit()
{
return this->hit; return this->hit;
} }
void setHit(bool hit) { void setHit(bool hit)
{
this->hit = hit; this->hit = hit;
} }
void draw() { void draw()
{
DrawTexture(this->texture.getTexture(), this->texture.getPosition().x, this->texture.getPosition().y, WHITE); DrawTexture(this->texture.getTexture(), this->texture.getPosition().x, this->texture.getPosition().y, WHITE);
} }
TextureWrapper& getObject() { TextureWrapper &getObject()
{
return this->texture; return this->texture;
} }
+18 -16
View File
@@ -1,20 +1,22 @@
// #ifndef EBULLET_H #ifndef EBULLET_H
// #define EBULLET_H #define EBULLET_H
// #include "Bullet.h" #include "services/AssetService.h"
// #include <iostream> #include "entities/Bullet.h"
#include <cstdint>
// class EBullet : public Bullet { class EBullet : public Bullet
// public: {
public:
EBullet() {}
EBullet(Vector2 pos, float angle) : Bullet(pos, angle)
{
this->color = RED;
}
// EBullet() {} private:
// ~EBullet() {} uint8_t damage = 1;
void objCollision() override;
};
// void detectCollision() override { #endif
// std::cout << "Enemy bullets" << std::endl;
// }
// private:
// };
// #endif
+15 -142
View File
@@ -2,9 +2,9 @@
#define ENEMY_H #define ENEMY_H
#include "raylib.h" #include "raylib.h"
#include "services/MapService.h"
#include "services/AssetService.h" #include "services/AssetService.h"
#include "entities/TextureWrapper.h" #include "entities/TextureWrapper.h"
#include "entities/Vector2i.h"
#include <cmath> #include <cmath>
class Enemy class Enemy
@@ -16,6 +16,7 @@ public:
this->gunRight = TextureWrapper(AssetService::getInstance().getSprite(ESprite::ENEMY_GUN_RIGHT), {97, 27}); this->gunRight = TextureWrapper(AssetService::getInstance().getSprite(ESprite::ENEMY_GUN_RIGHT), {97, 27});
this->gunLeft = TextureWrapper(AssetService::getInstance().getSprite(ESprite::ENEMY_GUN_LEFT), {97, 27}); this->gunLeft = TextureWrapper(AssetService::getInstance().getSprite(ESprite::ENEMY_GUN_LEFT), {97, 27});
prevFrame = GetTime(); prevFrame = GetTime();
lastShot = GetTime();
/** Randomizes between 0 and 1 (+1 to get Left and Right in directions), so they dont all move in the same direction */ /** Randomizes between 0 and 1 (+1 to get Left and Right in directions), so they dont all move in the same direction */
int randDir = (int)((int)(GetTime() * 1000000) % 2); int randDir = (int)((int)(GetTime() * 1000000) % 2);
@@ -32,23 +33,7 @@ public:
health = maxHealth; health = maxHealth;
} }
void logic() void logic();
{
if (health == 0)
{
fall();
}
else
{
walk();
checkForCollision();
animate();
// if (!hasCollided)
// {
// }
}
texture.move(velocity);
}
void draw() void draw()
{ {
@@ -93,131 +78,19 @@ private:
Right, Right,
Up Up
}; // Used in conjuction with spriteAnimation, makes sure sprite direction matches with enemy direction }; // Used in conjuction with spriteAnimation, makes sure sprite direction matches with enemy direction
float shotsPerSecond = 8.0f;
double lastShot;
bool playerSpotted = false;
void walk() void walk();
{ void checkForCollision();
if (spriteAnimation.y == Left) std::vector<Vector2i> getCorners();
{ void animate();
velocity.x = -moveSpeed; void drawHealthBar();
} void drawGun();
else void fall();
{ void detectPlayer();
velocity.x = moveSpeed; void shoot();
}
}
void checkForCollision()
{
std::vector corners = getCorners();
std::vector<std::vector<int>> colMap = MapService::getInstance().getMap().getColMap();
hasCollided = false;
if (colMap[corners[2].y][corners[2].x] == 0 || colMap[corners[3].y][corners[3].x] == 0) // No ground below enemy
{
hasCollided = true;
}
else if (colMap[corners[0].y][corners[0].x] == 1 || colMap[corners[1].y][corners[1].x] == 1)
{
hasCollided = true;
}
if (hasCollided)
{
if (spriteAnimation.y == Left)
spriteAnimation.y = Right;
else
spriteAnimation.y = Left;
velocity.x *= -1;
}
}
std::vector<Vector2i> getCorners()
{
float bottom = texture.getPosition().y + texture.getSize().y + velocity.y;
float left = texture.getPosition().x + velocity.x;
float right = texture.getPosition().x + texture.getSize().x + velocity.x;
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 / 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 / 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
std::vector<Vector2i> 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);
return corners;
}
void animate()
{
if (GetTime() - prevFrame > animationDelay || hasCollided)
{
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
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();
}
}
void drawHealthBar()
{
float barLength = (this->texture.getSize().x / maxHealth) * health;
DrawRectangle(this->texture.getPosition().x, this->texture.getPosition().y - 12.0f, barLength, 3, RED);
}
void drawGun()
{
if (this->spriteAnimation.y == Right)
{
DrawTexturePro(
this->gunRight.getTexture(),
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunRight.getSize().x,
this->gunRight.getSize().y},
{this->gunRight.getSize().x / 3.0f, this->gunRight.getSize().y / 2.0f},
this->gunRight.getRotation(),
WHITE);
}
else if (this->spriteAnimation.y == Left)
{
DrawTexturePro(
this->gunLeft.getTexture(),
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunLeft.getSize().x,
this->gunLeft.getSize().y},
{this->gunLeft.getSize().x - this->gunLeft.getSize().x / 3.0f, this->gunLeft.getSize().y / 2.0f},
this->gunLeft.getRotation(),
WHITE);
}
}
void fall()
{
velocity.y += gravity;
}
}; };
#endif #endif
+15 -24
View File
@@ -10,10 +10,9 @@
#include "services/GameService.h" #include "services/GameService.h"
#include "services/CollectableService.h" #include "services/CollectableService.h"
#include "services/EnemyService.h" #include "services/EnemyService.h"
#include "entities/Player.h" #include "services/PlayerService.h"
#include "services/BulletService.h"
#include "entities/Text.h" #include "entities/Text.h"
#include "entities/Bullet.h"
#include "entities/PBullet.h"
#include <sstream> #include <sstream>
class Game class Game
@@ -44,10 +43,9 @@ public:
camera.target = (Vector2){screenSize.x / 2.0f, screenSize.y / 2.0f}; // Initially at the center of the world camera.target = (Vector2){screenSize.x / 2.0f, screenSize.y / 2.0f}; // Initially at the center of the world
camera.rotation = 0.0f; camera.rotation = 0.0f;
camera.zoom = 1.0f; camera.zoom = 1.0f;
// this->camera = camera;
GameService::getInstance().setCamera(camera); GameService::getInstance().setCamera(camera);
this->player = Player(); BulletService::getInstance().initBullets();
this->player.setPosition({200, 350}); PlayerService::getInstance().initPlayer();
this->crosshair = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CROSSHAIR), {15, 15}, {0, 0}, {0, 0, 15, 15}); this->crosshair = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CROSSHAIR), {15, 15}, {0, 0}, {0, 0, 15, 15});
CollectableService::getInstance().initCollectables(); CollectableService::getInstance().initCollectables();
@@ -84,20 +82,19 @@ public:
if (InputService::getInstance().isKeyPressed(KEY_ENTER)) if (InputService::getInstance().isKeyPressed(KEY_ENTER))
{ {
GameService::getInstance().setPaused(true); GameService::getInstance().setPaused(true);
AudioService::getInstance().pauseMusic();
return; return;
} }
GameService::getInstance().bulletLogic(); BulletService::getInstance().bulletLogic();
CollectableService::getInstance().collectableLogic(); CollectableService::getInstance().collectableLogic();
EnemyService::getInstance().enemyLogic(); EnemyService::getInstance().enemyLogic();
this->player.logic(); auto& player = PlayerService::getInstance().getPlayer();
if (this->player.getTexture().getPosition().x > (StateService::getInstance().getScreenSize().x / 2.0f) && player.logic();
this->player.getTexture().getPosition().x + (StateService::getInstance().getScreenSize().x / 2.0f) < background.getBackgroundSize().x) if (player.getTexture().getPosition().x > (StateService::getInstance().getScreenSize().x / 2.0f) &&
player.getTexture().getPosition().x + (StateService::getInstance().getScreenSize().x / 2.0f) < background.getBackgroundSize().x)
{ {
GameService::getInstance().getCamera().offset = {(StateService::getInstance().getScreenSize().x / 2.0f) + ((StateService::getInstance().getScreenSize().x / 2.0f) - this->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};
} }
} }
@@ -108,16 +105,11 @@ public:
this->background.draw(); this->background.draw();
// std::vector<PBullet> pbs = GameService::getInstance().getPlayerBullets(); BulletService::getInstance().drawBullets();
// for (int i = 0; i < GameService::getInstance().getPlayerBullets().size(); i++)
// {
// GameService::getInstance().getPlayerBullets()[i].draw();
// }
GameService::getInstance().drawBullets();
this->map.draw(); this->map.draw();
CollectableService::getInstance().drawCollectables(); CollectableService::getInstance().drawCollectables();
EnemyService::getInstance().drawEnemies(); EnemyService::getInstance().drawEnemies();
this->player.draw(); PlayerService::getInstance().getPlayer().draw();
EndMode2D(); EndMode2D();
@@ -137,7 +129,6 @@ public:
private: private:
Background background; Background background;
Map map; Map map;
Player player;
TextureWrapper crosshair; TextureWrapper crosshair;
std::vector<Enemy> enemies; std::vector<Enemy> enemies;
@@ -314,7 +305,7 @@ private:
Vector2i pixel_pos = Vector2i(5, 40); Vector2i pixel_pos = Vector2i(5, 40);
Texture2D ammoSprite = AssetService::getInstance().getSprite(ESprite::HUD_AMMO); Texture2D ammoSprite = AssetService::getInstance().getSprite(ESprite::HUD_AMMO);
Vector2i display_pos; Vector2i display_pos;
for (int i = 0; i < this->player.getAmmo(); i++) for (int i = 0; i < PlayerService::getInstance().getPlayer().getAmmo(); i++)
{ {
if (i <= 49) if (i <= 49)
{ {
@@ -346,13 +337,13 @@ private:
Texture2D noHealthSprite = AssetService::getInstance().getSprite(ESprite::HUD_HEART_EMPTY); Texture2D noHealthSprite = AssetService::getInstance().getSprite(ESprite::HUD_HEART_EMPTY);
Texture2D healthSprite = AssetService::getInstance().getSprite(ESprite::HUD_HEART); Texture2D healthSprite = AssetService::getInstance().getSprite(ESprite::HUD_HEART);
// sVector2f coord_pos = GetWindow().mapPixelToCoords(pixel_pos); // sVector2f coord_pos = GetWindow().mapPixelToCoords(pixel_pos);
for (int i = 0; i < this->player.getMaxHealth(); i++) for (int i = 0; i < PlayerService::getInstance().getPlayer().getMaxHealth(); i++)
{ {
Vector2i pos = Vector2i(pixel_pos.x + noHealthSprite.width * i, pixel_pos.y); Vector2i pos = Vector2i(pixel_pos.x + noHealthSprite.width * i, pixel_pos.y);
// noHealthSprite[i].setPosition(coord_pos.x + noHealthTexture.getSize().x * i, coord_pos.y); // noHealthSprite[i].setPosition(coord_pos.x + noHealthTexture.getSize().x * i, coord_pos.y);
DrawTexture(noHealthSprite, pos.x, pos.y, WHITE); DrawTexture(noHealthSprite, pos.x, pos.y, WHITE);
} }
for (int i = 0; i < this->player.getHealth(); i++) for (int i = 0; i < PlayerService::getInstance().getPlayer().getHealth(); i++)
{ {
// healthSprite[i].setPosition(coord_pos.x + healthTexture.getSize().x * i, coord_pos.y); // healthSprite[i].setPosition(coord_pos.x + healthTexture.getSize().x * i, coord_pos.y);
// DrawTexture(ammoSprite, display_pos.x, display_pos.y, WHITE); // DrawTexture(ammoSprite, display_pos.x, display_pos.y, WHITE);
-1
View File
@@ -2,7 +2,6 @@
#define JELLY_H #define JELLY_H
#include "raylib.h" #include "raylib.h"
#include "services/MapService.h"
#include "services/AssetService.h" #include "services/AssetService.h"
#include "entities/TextureWrapper.h" #include "entities/TextureWrapper.h"
+1 -33
View File
@@ -1,11 +1,8 @@
#ifndef PBULLET_H #ifndef PBULLET_H
#define PBULLET_H #define PBULLET_H
#include "services/CollectableService.h"
#include "services/AssetService.h" #include "services/AssetService.h"
#include "services/EnemyService.h"
#include "entities/Bullet.h" #include "entities/Bullet.h"
#include <iostream>
class PBullet : public Bullet class PBullet : public Bullet
{ {
@@ -17,36 +14,7 @@ public:
} }
private: private:
void objCollision() override void objCollision() override;
{
/** Check for collisions with creates */
for (auto &crate : CollectableService::getInstance().getCrates())
{
if(this->position.x + this->size > crate.getObject().getPosition().x && this->position.x-this->size < crate.getObject().getPosition().x + crate.getObject().getSize().x &&
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;
}
}
for (auto &enemy : EnemyService::getInstance().getEnemies())
{
/** If the enemy is already falling of the map, dont allow bullets to hit it anymore */
if(enemy.getHealth() == 0)
continue;
if(this->position.x + this->size > enemy.getObject().getPosition().x && this->position.x-this->size < enemy.getObject().getPosition().x + enemy.getObject().getSize().x &&
this->position.y + this->size > enemy.getObject().getPosition().y && this->position.y-this->size < enemy.getObject().getPosition().y + enemy.getObject().getSize().y) {
enemy.takeDamage();
if(enemy.getHealth() == 0) {
AssetService::getInstance().playSound(ESound::ENEMY_DEFEATED);
}
this->hasCollided = true;
}
}
}
}; };
#endif #endif
+26 -353
View File
@@ -2,17 +2,16 @@
#define PLAYER_H #define PLAYER_H
#include "raylib.h" #include "raylib.h"
#include "services/AssetService.h"
#include "services/MapService.h"
#include "services/InputService.h"
#include "services/GameService.h"
#include "entities/TextureWrapper.h"
#include "enums/ECorner.h" #include "enums/ECorner.h"
#include "enums/ESound.h" #include "enums/ESound.h"
#include "services/AssetService.h"
#include "entities/TextureWrapper.h"
#include "entities/Vector2i.h" #include "entities/Vector2i.h"
#include "entities/PBullet.h"
#include <unordered_map> #include <unordered_map>
#include <algorithm> #include <algorithm>
#include <cstdint>
class PBullet;
class Player class Player
{ {
@@ -35,6 +34,11 @@ public:
void logic() void logic()
{ {
/** Fall out of the map when the jelly has been killed */
if(this->health == 0) {
fall();
return;
}
rotateGun(); rotateGun();
shoot(); shoot();
movement(); movement();
@@ -73,6 +77,10 @@ public:
return this->max_health; return this->max_health;
} }
void takeDamage(uint8_t damage) {
this->health = std::max(health - damage, 0);
}
protected: protected:
private: private:
TextureWrapper texture; TextureWrapper texture;
@@ -106,353 +114,18 @@ private:
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;
void applyGravity() void applyGravity();
{ void movement();
if (!groundCollision) void move(Vector2 velocity);
{ void animate();
velocity.y += gravity; void positionUpdates();
} void handleCollision();
else void consumableCollision();
{ void outofboundsCheck();
// velocity.y = 0.0f; void rotateGun();
} void drawGun();
} void shoot();
void fall(); // Lets the player fall out of the map when he dies
void movement()
{
if (InputService::getInstance().isKeyDown(KEY_A))
{
spriteAnimation.y = Left;
velocity.x = -moveSpeed;
animate();
}
else if (InputService::getInstance().isKeyDown(KEY_D))
{
spriteAnimation.y = Right;
velocity.x = moveSpeed;
animate();
}
else
{
velocity.x = 0.0f;
}
positionUpdates();
handleCollision();
if (!groundCollision)
{
velocity.y += gravity;
}
else
{
velocity.y = 0.0f;
}
if (InputService::getInstance().isKeyPressed(KEY_W) && groundCollision)
{
velocity.y -= jumpPower;
}
move(velocity);
}
void move(Vector2 velocity)
{
this->texture.move(velocity);
}
void animate()
{
if (GetTime() - prevFrame > animationDelay)
{
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
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();
}
}
void positionUpdates()
{
const Vector2 pos = this->texture.getPosition();
const Vector2 size = this->texture.getSize();
const int tileSize = MapService::getInstance().getMap().getTileSize();
const float bottom = pos.y + size.y + velocity.y;
const float left = pos.x + velocity.x;
const float right = pos.x + size.x + velocity.x;
const float top = pos.y + velocity.y;
Vector2i topLeft(Vector2i((int)(left + 5) / tileSize, (int)top / tileSize)); // +5 to separate top from side
Vector2i topRight(Vector2i((int)(right - 5) / tileSize, (int)top / tileSize)); // -5 to separate top from side
Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize));
Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize));
Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize));
Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / 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
tiles.clear();
tiles.push_back(topLeft);
tiles.push_back(topRight);
tiles.push_back(leftTop);
tiles.push_back(leftBottom);
tiles.push_back(rightTop);
tiles.push_back(rightBottom);
tiles.push_back(bottomLeft);
tiles.push_back(bottomRight);
}
void handleCollision()
{
timesCollision.clear();
for (int i = 0; i < tiles.size(); i++)
{
if (MapService::getInstance().getMap().getColMap()[tiles[i].y][tiles[i].x] == 1)
{
timesCollision.push_back(i);
}
}
groundCollision = false;
if (timesCollision.size() > 0)
{
for (int i = 0; i < timesCollision.size(); i++)
{
if (timesCollision[i] == 0 || timesCollision[i] == 1) // Topleft and Topright
{
velocity.y = 0;
}
else if (timesCollision[i] == 2 || timesCollision[i] == 3 || // Lefttop and Leftbottom
timesCollision[i] == 4 || timesCollision[i] == 5) // Righttop and Lefttop
{
velocity.x = 0;
}
else if (timesCollision[i] == 6 || timesCollision[i] == 7) // Bottomleft and Bottomright
{
groundCollision = true;
}
}
}
// Set velocity.x based on going right or left
// Take current position of all 4 corners
// Apply velocity
// Calculate the 8 points
// Add them to a list
// Empty vector of amount of collisions
// Loop through the points
// Check the collisionmap for intersection
// If so, add item to the vector of collisions
// Set groundcollision to false
// Loop through collisions
// Based on their location (TopLeft, RightTop, etc) change the velocity
// Apply gravity if not on the floor
// If jumping and on the floor, go up
// Move using velocity
// Vector2 tempPos = this->texture.getPosition();
// this->texture.move(velocity);
// groundCollision = false;
// std::unordered_map<ECorner, bool> collisions = MapService::getInstance().getMap().checkForCollision(this->texture);
// if (collisions[ECorner::BOTTOM_LEFT] || collisions[ECorner::BOTTOM_RIGHT])
// {
// velocity.y = 0.0f;
// groundCollision = true;
// this->texture.setPosition({this->texture.getPosition().x, floorf(this->texture.getPosition().y / MapService::getInstance().getMap().getTileSize()) * MapService::getInstance().getMap().getTileSize()});
// }
// if (collisions[ECorner::LEFT_TOP] && collisions[ECorner::LEFT_BOTTOM])
// {
// velocity.x = 0.0f;
// this->texture.setPosition({ceilf(this->texture.getPosition().x / MapService::getInstance().getMap().getTileSize()) * MapService::getInstance().getMap().getTileSize(), this->texture.getPosition().y});
// }
// if (collisions[ECorner::TOP_LEFT] || collisions[ECorner::TOP_RIGHT])
// {
// velocity.y = 0.0f;
// this->texture.setPosition({this->texture.getPosition().x, (ceilf(this->texture.getPosition().y / MapService::getInstance().getMap().getTileSize())) * MapService::getInstance().getMap().getTileSize()});
// }
// if (collisions[ECorner::RIGHT_TOP] && collisions[ECorner::RIGHT_BOTTOM])
// {
// velocity.x = 0.0f;
// this->texture.setPosition({floorf(this->texture.getPosition().x / MapService::getInstance().getMap().getTileSize()) * MapService::getInstance().getMap().getTileSize(), this->texture.getPosition().y});
// }
}
void consumableCollision()
{
/** Check for collisions with ammo packs */
for (auto &ammoPack : CollectableService::getInstance().getAmmoPacks())
{
if (this->texture.getPosition().x + this->texture.getSize().x > ammoPack.getObject().getPosition().x &&
this->texture.getPosition().x<ammoPack.getObject().getPosition().x + ammoPack.getObject().getSize().x &&
this->texture.getPosition().y + this->texture.getSize().y> ammoPack.getObject().getPosition().y &&
this->texture.getPosition().y < ammoPack.getObject().getPosition().y + ammoPack.getObject().getSize().y)
{
ammoPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::AMMO_PICKUP);
ammo += ammoPack.getAmmoAmount();
}
}
/** Check for collisions with health packs */
for (auto &healthPack : CollectableService::getInstance().getHealthPacks())
{
if (this->texture.getPosition().x + this->texture.getSize().x > healthPack.getObject().getPosition().x &&
this->texture.getPosition().x<healthPack.getObject().getPosition().x + healthPack.getObject().getSize().x &&
this->texture.getPosition().y + this->texture.getSize().y> healthPack.getObject().getPosition().y &&
this->texture.getPosition().y < healthPack.getObject().getPosition().y + healthPack.getObject().getSize().y)
{
if (health < max_health)
{
healthPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::HEAL_PICKUP);
health = std::max(health + healthPack.getHealAmount(), (int)max_health);
}
}
}
/** Checks for collisions with powerups */
for (auto &powerup : CollectableService::getInstance().getPowerups())
{
if (this->texture.getPosition().x + this->texture.getSize().x > powerup.getObject().getPosition().x &&
this->texture.getPosition().x<powerup.getObject().getPosition().x + powerup.getObject().getSize().x &&
this->texture.getPosition().y + this->texture.getSize().y> powerup.getObject().getPosition().y &&
this->texture.getPosition().y < powerup.getObject().getPosition().y + powerup.getObject().getSize().y)
{
// if(health < max_health) {
powerup.setPickedUp(true);
AssetService::getInstance().playSound(ESound::POWERUP);
jumpPower += powerup.getExtraJumpPower();
gainedPowerup = true;
// health = std::max(health+powerup.getHealAmount(),(int)max_health);
// }
}
}
/** Checks for collisions with the finish flag */
auto &flag = CollectableService::getInstance().getFlag();
if (this->texture.getPosition().x + this->texture.getSize().x > flag.getObject().getPosition().x &&
this->texture.getPosition().x<flag.getObject().getPosition().x + flag.getObject().getSize().x &&
this->texture.getPosition().y + this->texture.getSize().y> flag.getObject().getPosition().y &&
this->texture.getPosition().y < flag.getObject().getPosition().y + flag.getObject().getSize().y)
{
flag.setTouched(true);
}
}
void outofboundsCheck()
{
if (this->texture.getPosition().y > StateService::getInstance().getScreenSize().y)
{
this->health = 0;
GameService::getInstance().setGameOver(true);
AudioService::getInstance().setMusic(EMusic::GAME_OVER);
AudioService::getInstance().playMusic();
}
}
void rotateGun()
{
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right)
{
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);
}
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
float gunRotationLeft = atan2(GameService::getInstance().getMouseWorldPos().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
(this->texture.getPosition().x + this->texture.getSize().x / 2.0) - GameService::getInstance().getMouseWorldPos().x);
this->gunLeft.setRotation(-gunRotationLeft * 180 / M_PI);
}
}
void drawGun()
{
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + this->gunLeft.getSize().x && spriteAnimation.y == Right)
{
// + this->texture.getSize().x / 2.0f
// + this->texture.getSize().y / 2.0f
DrawTexturePro(
this->gunRight.getTexture(),
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunRight.getSize().x,
this->gunRight.getSize().y},
{this->gunRight.getSize().x / 2.0f, this->gunRight.getSize().y / 2.0f},
this->gunRight.getRotation(),
WHITE);
}
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
DrawTexturePro(
this->gunLeft.getTexture(),
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunLeft.getSize().x,
this->gunLeft.getSize().y},
{this->gunLeft.getSize().x / 2.0f, this->gunLeft.getSize().y / 2.0f},
this->gunLeft.getRotation(),
WHITE);
}
}
void shoot()
{
if (InputService::getInstance().isMouseButtonDown(MOUSE_BUTTON_LEFT) && GetTime() - prevShot > firingRate && groundCollision)
{
prevShot = GetTime();
if (ammo == 0 || (GameService::getInstance().getMouseWorldPos().x > (this->texture.getPosition().x - this->gunLeft.getSize().x / 2.0f) && spriteAnimation.y == Left) ||
(GameService::getInstance().getMouseWorldPos().x < (this->texture.getPosition().x + this->texture.getSize().x + gunLeft.getSize().x / 2.0f) && spriteAnimation.y == Right))
{
// Play empty gun sound
AssetService::getInstance().playSound(ESound::NO_AMMO);
return;
}
// Play shooting sound
AssetService::getInstance().playSound(ESound::SHOOT);
ammo--;
Vector2 mouseWorldPos = GameService::getInstance().getMouseWorldPos();
if (mouseWorldPos.x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right ||
mouseWorldPos.x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
float angleShot = atan2(mouseWorldPos.y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
mouseWorldPos.x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0));
/** Making the bullet originate from the gun's mouth */
float offsetDistance = this->gunLeft.getSize().x / 2;
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};
GameService::getInstance().addPlayerBullet(PBullet(bulletStartLocation, angleShot));
}
}
}
}; };
#endif #endif
-21
View File
@@ -3,7 +3,6 @@
#include "raylib.h" #include "raylib.h"
#include "enums/ECorner.h" #include "enums/ECorner.h"
#include <unordered_map>
class TextureWrapper { class TextureWrapper {
public: public:
@@ -53,26 +52,6 @@ class TextureWrapper {
this->position = {this->position.x + deltaPos.x, this->position.y + deltaPos.y}; this->position = {this->position.x + deltaPos.x, this->position.y + deltaPos.y};
} }
/** Returns the 4 corners of the texture */
// std::unordered_map<ECorner, Vector2> getCorners() {
// std::unordered_map<ECorner, Vector2> corners;
// float top = this->position.y;
// float right = this->position.x+this->size.x;
// float bottom = this->position.y + this->size.y;
// float left = this->position.x;
// float margin = 8; // Space to the corner
// corners[ECorner::TOP_LEFT] = {left+margin, top};
// corners[ECorner::TOP_RIGHT] = {right-margin, top};
// corners[ECorner::RIGHT_TOP] = {right, top+margin};
// corners[ECorner::RIGHT_BOTTOM] = {right, bottom-margin};
// corners[ECorner::BOTTOM_RIGHT] = {right-margin, bottom};
// corners[ECorner::BOTTOM_LEFT] = {left+margin, bottom};
// corners[ECorner::LEFT_BOTTOM] = {left, bottom-margin};
// corners[ECorner::LEFT_TOP] = {left, top+margin};
// return corners;
// }
private: private:
Texture2D texture; Texture2D texture;
Rectangle sourceRect; Rectangle sourceRect;
+2 -1
View File
@@ -11,7 +11,8 @@ enum class ESound {
POWERUP, POWERUP,
PLAYER_DAMAGE, PLAYER_DAMAGE,
ENEMY_DEFEATED, ENEMY_DEFEATED,
VICTORY VICTORY,
PLAYER_SPOTTED
}; };
#endif #endif
+56
View File
@@ -0,0 +1,56 @@
#ifndef BULLET_SERVICE_H
#define BULLET_SERVICE_H
#include "raylib.h"
#include "services/Service.h"
#include "services/AssetService.h"
#include "entities/PBullet.h"
#include "entities/EBullet.h"
#include <optional>
#include <memory>
class BulletService : public Service<BulletService>
{
public:
void initBullets()
{
this->bullets.clear();
}
void bulletLogic()
{
for (auto it = this->bullets.begin(); it != this->bullets.end();)
{
(*it)->logic();
if ((*it)->getHasCollided())
it = this->bullets.erase(it);
else
++it;
}
}
void drawBullets()
{
for (auto it = this->bullets.begin(); it != this->bullets.end();)
{
(*it)->draw();
++it;
}
}
void addPlayerBullet(Vector2 position, float direction)
{
this->bullets.push_back(std::make_unique<PBullet>(position, direction));
}
void addEnemyBullet(Vector2 position, float direction)
{
this->bullets.push_back(std::make_unique<EBullet>(position, direction));
}
protected:
private:
std::vector<std::unique_ptr<Bullet>> bullets;
};
#endif
+12 -34
View File
@@ -1,9 +1,9 @@
#ifndef GAME_SERVICE_H #ifndef GAME_SERVICE_H
#define GAME_SERVICE_H #define GAME_SERVICE_H
#include "raylib.h"
#include "services/Service.h" #include "services/Service.h"
#include "entities/Game.h" #include "services/AudioService.h"
#include "entities/PBullet.h"
#include <vector> #include <vector>
#include <list> #include <list>
@@ -23,12 +23,20 @@ public:
void setPaused(bool paused) void setPaused(bool paused)
{ {
this->paused = paused; this->paused = paused;
if (this->paused)
AudioService::getInstance().pauseMusic();
} }
void setGameOver(bool gameOver) void setGameOver(bool gameOver)
{ {
this->gameOver = gameOver; this->gameOver = gameOver;
if (this->gameOver)
{
AudioService::getInstance().setMusic(EMusic::GAME_OVER);
AudioService::getInstance().playMusic();
} }
}
void setCamera(Camera2D camera) void setCamera(Camera2D camera)
{ {
this->camera = camera; this->camera = camera;
@@ -39,46 +47,18 @@ public:
return this->camera; return this->camera;
} }
/**
* Returns the position of the mouse in world coordinates
*/
Vector2 getMouseWorldPos() Vector2 getMouseWorldPos()
{ {
Vector2 mouseWorldPos = {GetMousePosition().x + camera.target.x - camera.offset.x, GetMousePosition().y}; Vector2 mouseWorldPos = {GetMousePosition().x + camera.target.x - camera.offset.x, GetMousePosition().y};
return mouseWorldPos; return mouseWorldPos;
} }
Vector2 toWorldPos(Vector2i pixelPos) { Vector2 toWorldPos(Vector2i pixelPos)
{
Vector2 worldPos = {(float)pixelPos.x + camera.target.x - camera.offset.x, (float)pixelPos.y}; Vector2 worldPos = {(float)pixelPos.x + camera.target.x - camera.offset.x, (float)pixelPos.y};
return worldPos; return worldPos;
} }
void addPlayerBullet(PBullet bullet)
{
this->playerBullets.push_back(bullet);
}
void bulletLogic()
{
for (auto it = this->playerBullets.begin(); it != this->playerBullets.end();)
{
it->logic();
if (it->getHasCollided())
it = this->playerBullets.erase(it);
else
++it;
}
}
void drawBullets()
{
for (auto it = this->playerBullets.begin(); it != this->playerBullets.end();)
{
it->draw();
++it;
}
}
void resetGame() void resetGame()
{ {
paused = false; paused = false;
@@ -90,8 +70,6 @@ private:
bool paused = false; bool paused = false;
bool gameOver = false; bool gameOver = false;
Camera2D camera; Camera2D camera;
std::list<PBullet> playerBullets;
}; };
#endif #endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef PLAYER_SERVICE_H
#define PLAYER_SERVICE_H
#include "raylib.h"
#include "services/Service.h"
#include "entities/Player.h"
#include <memory>
class PlayerService : public Service<PlayerService>
{
public:
void initPlayer()
{
this->player = std::make_unique<Player>();
this->player->setPosition({200, 350});
}
Player& getPlayer() {
return *this->player;
}
protected:
private:
std::unique_ptr<Player> player;
};
#endif
+19
View File
@@ -0,0 +1,19 @@
#include "entities/Bullet.h"
#include "services/MapService.h"
void Bullet::envCollision()
{
if (position.x < 0 || position.y < 0 || position.y > GetScreenHeight())
{
hasCollided = true;
return;
}
const int tileSize = MapService::getInstance().getMap().getTileSize();
Vector2i tilePos = Vector2i((int)this->position.x / tileSize, (int)this->position.y / tileSize);
if (MapService::getInstance().getMap().getColMap()[tilePos.y][tilePos.x] == 1)
{
hasCollided = true;
return;
}
}
+18
View File
@@ -0,0 +1,18 @@
#include "entities/EBullet.h"
#include "services/PlayerService.h"
void EBullet::objCollision()
{
Player& player = PlayerService::getInstance().getPlayer();
/** If the player is already falling of the map, dont allow bullets to hit it anymore */
if(player.getHealth() == 0)
return;
if(this->position.x + this->size > player.getTexture().getPosition().x && this->position.x-this->size < player.getTexture().getPosition().x + player.getTexture().getSize().x &&
this->position.y + this->size > player.getTexture().getPosition().y && this->position.y-this->size < player.getTexture().getPosition().y + player.getTexture().getSize().y) {
player.takeDamage(this->damage);
AssetService::getInstance().playSound(ESound::PLAYER_DAMAGE);
this->hasCollided = true;
}
}
+213
View File
@@ -0,0 +1,213 @@
#include "entities/Enemy.h"
#include "services/PlayerService.h"
#include "services/MapService.h"
#include "services/BulletService.h"
void Enemy::logic()
{
if (health == 0)
{
fall();
}
else
{
detectPlayer();
if (playerSpotted)
{
shoot();
}
else
{
walk();
checkForCollision();
animate();
}
}
texture.move(velocity);
}
void Enemy::walk()
{
if (spriteAnimation.y == Left)
{
velocity.x = -moveSpeed;
}
else
{
velocity.x = moveSpeed;
}
}
void Enemy::checkForCollision()
{
std::vector corners = getCorners();
std::vector<std::vector<int>> colMap = MapService::getInstance().getMap().getColMap();
hasCollided = false;
if (colMap[corners[2].y][corners[2].x] == 0 || colMap[corners[3].y][corners[3].x] == 0) // No ground below enemy
{
hasCollided = true;
}
else if (colMap[corners[0].y][corners[0].x] == 1 || colMap[corners[1].y][corners[1].x] == 1)
{
hasCollided = true;
}
if (hasCollided)
{
if (spriteAnimation.y == Left)
spriteAnimation.y = Right;
else
spriteAnimation.y = Left;
velocity.x *= -1;
}
}
std::vector<Vector2i> Enemy::getCorners()
{
float bottom = texture.getPosition().y + texture.getSize().y + velocity.y;
float left = texture.getPosition().x + velocity.x;
float right = texture.getPosition().x + texture.getSize().x + velocity.x;
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 / 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 / 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
std::vector<Vector2i> 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);
return corners;
}
void Enemy::animate()
{
if (GetTime() - prevFrame > animationDelay || hasCollided)
{
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
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();
}
}
void Enemy::drawHealthBar()
{
float barLength = (this->texture.getSize().x / maxHealth) * health;
DrawRectangle(this->texture.getPosition().x, this->texture.getPosition().y - 12.0f, barLength, 3, RED);
}
void Enemy::drawGun()
{
if (this->spriteAnimation.y == Right)
{
DrawTexturePro(
this->gunRight.getTexture(),
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunRight.getSize().x,
this->gunRight.getSize().y},
{this->gunRight.getSize().x / 3.0f, this->gunRight.getSize().y / 2.0f},
this->gunRight.getRotation(),
WHITE);
}
else if (this->spriteAnimation.y == Left)
{
DrawTexturePro(
this->gunLeft.getTexture(),
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunLeft.getSize().x,
this->gunLeft.getSize().y},
{this->gunLeft.getSize().x - this->gunLeft.getSize().x / 3.0f, this->gunLeft.getSize().y / 2.0f},
this->gunLeft.getRotation(),
WHITE);
}
}
void Enemy::fall()
{
velocity.y += gravity;
}
void Enemy::detectPlayer()
{
// playerSpotted = false;
Player &player = PlayerService::getInstance().getPlayer();
if (this->health > 0)
{
bool spotted = false;
if (this->spriteAnimation.y == Left && this->texture.getPosition().x - player.getTexture().getPosition().x <= spottingRange &&
this->texture.getPosition().x - player.getTexture().getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y >= player.getTexture().getPosition().y)
{
spotted = true;
}
else if (spriteAnimation.y == Right && player.getTexture().getPosition().x - this->texture.getPosition().x <= spottingRange &&
player.getTexture().getPosition().x - this->texture.getPosition().x > 55 && this->texture.getPosition().y + this->texture.getSize().y >= player.getTexture().getPosition().y)
{
spotted = true;
}
/** Don't set it again if the detection continues */
if (spotted && !this->playerSpotted)
{
AssetService::getInstance().playSound(ESound::PLAYER_SPOTTED);
this->playerSpotted = true;
velocity.x = 0.0f;
}
else if (!spotted)
{
this->playerSpotted = false;
}
}
}
void Enemy::shoot()
{
Player &player = PlayerService::getInstance().getPlayer();
/** Stop shooting if the player is dead (and falling out of the map) */
if (player.getHealth() > 0 && playerSpotted && GetTime() - this->lastShot >= (1 / this->shotsPerSecond))
{
this->lastShot = GetTime();
// Play shooting sound
AssetService::getInstance().playSound(ESound::SHOOT);
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 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));
/** 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);
}
}
}
+33
View File
@@ -0,0 +1,33 @@
#include "entities/PBullet.h"
#include "services/CollectableService.h"
#include "services/EnemyService.h"
void PBullet::objCollision()
{
/** Check for collisions with creates */
for (auto &crate : CollectableService::getInstance().getCrates())
{
if(this->position.x + this->size > crate.getObject().getPosition().x && this->position.x-this->size < crate.getObject().getPosition().x + crate.getObject().getSize().x &&
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);
this->hasCollided = true;
}
}
for (auto &enemy : EnemyService::getInstance().getEnemies())
{
/** If the enemy is already falling of the map, dont allow bullets to hit it anymore */
if(enemy.getHealth() == 0)
continue;
if(this->position.x + this->size > enemy.getObject().getPosition().x && this->position.x-this->size < enemy.getObject().getPosition().x + enemy.getObject().getSize().x &&
this->position.y + this->size > enemy.getObject().getPosition().y && this->position.y-this->size < enemy.getObject().getPosition().y + enemy.getObject().getSize().y) {
enemy.takeDamage();
if(enemy.getHealth() == 0) {
AssetService::getInstance().playSound(ESound::ENEMY_DEFEATED);
}
this->hasCollided = true;
}
}
}
+305
View File
@@ -0,0 +1,305 @@
#include "entities/Player.h"
#include "services/BulletService.h"
#include "services/GameService.h"
#include "services/CollectableService.h"
#include "services/InputService.h"
void Player::applyGravity()
{
if (!groundCollision)
{
velocity.y += gravity;
}
else
{
// velocity.y = 0.0f;
}
}
void Player::movement()
{
if (InputService::getInstance().isKeyDown(KEY_A))
{
spriteAnimation.y = Left;
velocity.x = -moveSpeed;
animate();
}
else if (InputService::getInstance().isKeyDown(KEY_D))
{
spriteAnimation.y = Right;
velocity.x = moveSpeed;
animate();
}
else
{
velocity.x = 0.0f;
}
positionUpdates();
handleCollision();
if (!groundCollision)
{
velocity.y += gravity;
}
else
{
velocity.y = 0.0f;
}
if (InputService::getInstance().isKeyPressed(KEY_W) && groundCollision)
{
velocity.y -= jumpPower;
}
move(velocity);
}
void Player::move(Vector2 velocity)
{
this->texture.move(velocity);
}
void Player::animate()
{
if (GetTime() - prevFrame > animationDelay)
{
spriteAnimation.x = std::fmod(spriteAnimation.x + 1, 3);
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();
}
}
void Player::positionUpdates()
{
const Vector2 pos = this->texture.getPosition();
const Vector2 size = this->texture.getSize();
const int tileSize = MapService::getInstance().getMap().getTileSize();
const float bottom = pos.y + size.y + velocity.y;
const float left = pos.x + velocity.x;
const float right = pos.x + size.x + velocity.x;
const float top = pos.y + velocity.y;
Vector2i topLeft(Vector2i((int)(left + 5) / tileSize, (int)top / tileSize)); // +5 to separate top from side
Vector2i topRight(Vector2i((int)(right - 5) / tileSize, (int)top / tileSize)); // -5 to separate top from side
Vector2i leftTop(Vector2i((int)left / tileSize, (int)(top + 5) / tileSize));
Vector2i leftBottom(Vector2i((int)left / tileSize, (int)(bottom - 5) / tileSize));
Vector2i rightTop(Vector2i((int)right / tileSize, (int)(top + 5) / tileSize));
Vector2i rightBottom(Vector2i((int)right / tileSize, (int)(bottom - 5) / 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
tiles.clear();
tiles.push_back(topLeft);
tiles.push_back(topRight);
tiles.push_back(leftTop);
tiles.push_back(leftBottom);
tiles.push_back(rightTop);
tiles.push_back(rightBottom);
tiles.push_back(bottomLeft);
tiles.push_back(bottomRight);
}
void Player::handleCollision()
{
timesCollision.clear();
for (int i = 0; i < tiles.size(); i++)
{
if (MapService::getInstance().getMap().getColMap()[tiles[i].y][tiles[i].x] == 1)
{
timesCollision.push_back(i);
}
}
groundCollision = false;
if (timesCollision.size() > 0)
{
for (int i = 0; i < timesCollision.size(); i++)
{
if (timesCollision[i] == 0 || timesCollision[i] == 1) // Topleft and Topright
{
velocity.y = 0;
}
else if (timesCollision[i] == 2 || timesCollision[i] == 3 || // Lefttop and Leftbottom
timesCollision[i] == 4 || timesCollision[i] == 5) // Righttop and Lefttop
{
velocity.x = 0;
}
else if (timesCollision[i] == 6 || timesCollision[i] == 7) // Bottomleft and Bottomright
{
groundCollision = true;
}
}
}
}
void Player::consumableCollision()
{
/** Check for collisions with ammo packs */
for (auto &ammoPack : CollectableService::getInstance().getAmmoPacks())
{
if (this->texture.getPosition().x + this->texture.getSize().x > ammoPack.getObject().getPosition().x &&
this->texture.getPosition().x<ammoPack.getObject().getPosition().x + ammoPack.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> ammoPack.getObject().getPosition().y &&
this->texture.getPosition().y < ammoPack.getObject().getPosition().y + ammoPack.getObject().getSize().y)
{
ammoPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::AMMO_PICKUP);
ammo += ammoPack.getAmmoAmount();
}
}
/** Check for collisions with health packs */
for (auto &healthPack : CollectableService::getInstance().getHealthPacks())
{
if (this->texture.getPosition().x + this->texture.getSize().x > healthPack.getObject().getPosition().x &&
this->texture.getPosition().x<healthPack.getObject().getPosition().x + healthPack.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> healthPack.getObject().getPosition().y &&
this->texture.getPosition().y < healthPack.getObject().getPosition().y + healthPack.getObject().getSize().y)
{
if (health < max_health)
{
healthPack.setPickedUp(true);
AssetService::getInstance().playSound(ESound::HEAL_PICKUP);
health = std::max(health + healthPack.getHealAmount(), (int)max_health);
}
}
}
/** Checks for collisions with powerups */
for (auto &powerup : CollectableService::getInstance().getPowerups())
{
if (this->texture.getPosition().x + this->texture.getSize().x > powerup.getObject().getPosition().x &&
this->texture.getPosition().x<powerup.getObject().getPosition().x + powerup.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> powerup.getObject().getPosition().y &&
this->texture.getPosition().y < powerup.getObject().getPosition().y + powerup.getObject().getSize().y)
{
// if(health < max_health) {
powerup.setPickedUp(true);
AssetService::getInstance().playSound(ESound::POWERUP);
jumpPower += powerup.getExtraJumpPower();
gainedPowerup = true;
// health = std::max(health+powerup.getHealAmount(),(int)max_health);
// }
}
}
/** Checks for collisions with the finish flag */
auto &flag = CollectableService::getInstance().getFlag();
if (this->texture.getPosition().x + this->texture.getSize().x > flag.getObject().getPosition().x &&
this->texture.getPosition().x<flag.getObject().getPosition().x + flag.getObject().getSize().x &&this->texture.getPosition().y + this->texture.getSize().y> flag.getObject().getPosition().y &&
this->texture.getPosition().y < flag.getObject().getPosition().y + flag.getObject().getSize().y)
{
flag.setTouched(true);
}
}
void Player::shoot()
{
if (InputService::getInstance().isMouseButtonDown(MOUSE_BUTTON_LEFT) && GetTime() - prevShot > firingRate && groundCollision)
{
prevShot = GetTime();
if (this->ammo == 0 || (GameService::getInstance().getMouseWorldPos().x > (this->texture.getPosition().x - this->gunLeft.getSize().x / 2.0f) && spriteAnimation.y == Left) ||
(GameService::getInstance().getMouseWorldPos().x < (this->texture.getPosition().x + this->texture.getSize().x + gunLeft.getSize().x / 2.0f) && spriteAnimation.y == Right))
{
// Play empty gun sound
AssetService::getInstance().playSound(ESound::NO_AMMO);
return;
}
// Play shooting sound
AssetService::getInstance().playSound(ESound::SHOOT);
ammo--;
Vector2 mouseWorldPos = GameService::getInstance().getMouseWorldPos();
if (mouseWorldPos.x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right ||
mouseWorldPos.x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
float angleShot = atan2(mouseWorldPos.y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
mouseWorldPos.x - (this->texture.getPosition().x + this->texture.getSize().x / 2.0));
/** Making the bullet originate from the gun's mouth */
float offsetDistance = this->gunLeft.getSize().x / 2;
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().addPlayerBullet(bulletStartLocation, angleShot);
}
}
}
void Player::outofboundsCheck()
{
if (this->texture.getPosition().y > StateService::getInstance().getScreenSize().y)
{
this->health = 0;
GameService::getInstance().setGameOver(true);
}
}
void Player::rotateGun()
{
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right)
{
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);
}
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
float gunRotationLeft = atan2(GameService::getInstance().getMouseWorldPos().y - (this->texture.getPosition().y + this->texture.getSize().y / 2.0),
(this->texture.getPosition().x + this->texture.getSize().x / 2.0) - GameService::getInstance().getMouseWorldPos().x);
this->gunLeft.setRotation(-gunRotationLeft * 180 / M_PI);
}
}
void Player::drawGun()
{
if (GameService::getInstance().getMouseWorldPos().x > this->texture.getPosition().x + this->gunLeft.getSize().x && spriteAnimation.y == Right)
{
// + this->texture.getSize().x / 2.0f
// + this->texture.getSize().y / 2.0f
DrawTexturePro(
this->gunRight.getTexture(),
{0, 0, this->gunRight.getSize().x, this->gunRight.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunRight.getSize().x,
this->gunRight.getSize().y},
{this->gunRight.getSize().x / 2.0f, this->gunRight.getSize().y / 2.0f},
this->gunRight.getRotation(),
WHITE);
}
else if (GameService::getInstance().getMouseWorldPos().x + (this->gunLeft.getSize().x / 2) < this->texture.getPosition().x && spriteAnimation.y == Left)
{
DrawTexturePro(
this->gunLeft.getTexture(),
{0, 0, this->gunLeft.getSize().x, this->gunLeft.getSize().y},
{this->texture.getPosition().x + this->texture.getSize().x / 2.0f,
this->texture.getPosition().y + this->texture.getSize().y / 2.0f,
this->gunLeft.getSize().x,
this->gunLeft.getSize().y},
{this->gunLeft.getSize().x / 2.0f, this->gunLeft.getSize().y / 2.0f},
this->gunLeft.getRotation(),
WHITE);
}
}
void Player::fall()
{
outofboundsCheck();
velocity.y += gravity;
move(velocity);
}
+1
View File
@@ -31,6 +31,7 @@ void AssetService::setAssets()
sounds[ESound::VICTORY] = loadSound("Audio/Sounds/victorySound.wav"); sounds[ESound::VICTORY] = loadSound("Audio/Sounds/victorySound.wav");
sounds[ESound::PLAYER_DAMAGE] = loadSound("Audio/Sounds/WaterBalloon.wav"); sounds[ESound::PLAYER_DAMAGE] = loadSound("Audio/Sounds/WaterBalloon.wav");
sounds[ESound::ENEMY_DEFEATED] = loadSound("Audio/Sounds/pain.wav"); sounds[ESound::ENEMY_DEFEATED] = loadSound("Audio/Sounds/pain.wav");
sounds[ESound::PLAYER_SPOTTED] = loadSound("Audio/Sounds/playerSpotted.wav");
backgrounds[EBackground::TUTORIAL] = loadTexture("Textures/TutorialBackground2.png"); backgrounds[EBackground::TUTORIAL] = loadTexture("Textures/TutorialBackground2.png");
backgrounds[EBackground::GAME1] = loadTexture("Textures/GameBackground_1.png"); backgrounds[EBackground::GAME1] = loadTexture("Textures/GameBackground_1.png");