[Added] Crates, Hud for bullets and health

This commit is contained in:
Ninjaws
2024-12-15 00:12:16 +01:00
parent 55792bb954
commit de958b02e6
17 changed files with 448 additions and 38 deletions
+5 -6
View File
@@ -16,7 +16,6 @@ public:
{
this->position = pos;
this->direction = direction;
std::cout << direction << std::endl;
}
// ~Bullet() {}
@@ -25,8 +24,8 @@ public:
{
move();
hasCollided = false;
envCollision();
objCollision();
envCollision();
// // detectCollision();
}
@@ -42,14 +41,14 @@ public:
protected:
Color color = RED;
Vector2 position;
int size = 7;
bool hasCollided = false;
private:
// TextureWrapper object;
Vector2 position;
float direction;
float velocity = 15.0f;
int size = 7;
bool hasCollided = false;
void move()
{
@@ -59,7 +58,7 @@ private:
}
void envCollision()
{
if (position.y < 0 || position.y > GetScreenHeight())
if (position.x < 0 || position.y < 0 || position.y > GetScreenHeight())
{
hasCollided = true;
return;
+48
View File
@@ -0,0 +1,48 @@
#ifndef CRATE_H
#define CRATE_H
#include "services/AssetService.h"
#include "entities/TextureWrapper.h"
#include "entities/PBullet.h"
class Crate {
public:
Crate() {
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CRATE),{32,32});
}
Crate(Vector2 pos) {
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CRATE),{32,32}, pos);
}
void logic() {
}
bool hasBeenHit() {
return this->hit;
}
void setHit(bool hit) {
this->hit = hit;
}
void draw() {
DrawTexture(this->texture.getTexture(), this->texture.getPosition().x, this->texture.getPosition().y, WHITE);
}
TextureWrapper& getObject() {
return this->texture;
}
private:
TextureWrapper texture;
bool hit; // Whether a PBullet has hit the create
};
#endif
+66 -5
View File
@@ -8,6 +8,7 @@
#include "services/MapService.h"
#include "services/InputService.h"
#include "services/GameService.h"
#include "services/CollectableService.h"
#include "entities/Player.h"
#include "entities/Text.h"
#include "entities/Bullet.h"
@@ -47,6 +48,8 @@ public:
this->player.setPosition({200, 350});
this->crosshair = TextureWrapper(AssetService::getInstance().getSprite(ESprite::CROSSHAIR), {15, 15}, {0, 0}, {0, 0, 15, 15});
CollectableService::getInstance().initCollectables();
initPauseScreen();
initGameOverScreen();
@@ -80,8 +83,9 @@ public:
AudioService::getInstance().pauseMusic();
return;
}
GameService::getInstance().bulletLogic();
CollectableService::getInstance().collectableLogic();
this->player.logic();
if (this->player.getTexture().getPosition().x > (StateService::getInstance().getScreenSize().x / 2.0f))
@@ -104,6 +108,7 @@ public:
// }
GameService::getInstance().drawBullets();
this->map.draw();
CollectableService::getInstance().drawCollectables();
this->player.draw();
EndMode2D();
@@ -116,6 +121,7 @@ public:
{
drawGameOver();
}
drawHud();
drawCrosshair();
EndDrawing();
}
@@ -250,10 +256,6 @@ private:
}
}
void shootLogic()
{
}
void drawPaused()
{
// 100, 100, 255, 60
@@ -290,6 +292,65 @@ private:
// 0,
// WHITE);
}
void drawHud()
{
drawHudHealth();
drawHudBullets();
}
void drawHudBullets()
{
Vector2i pixel_pos = Vector2i(5, 40);
Texture2D ammoSprite = AssetService::getInstance().getSprite(ESprite::HUD_AMMO);
Vector2i display_pos;
for (int i = 0; i < this->player.getAmmo(); i++)
{
if (i <= 49)
{
display_pos = Vector2i(pixel_pos.x + ammoSprite.width * i, pixel_pos.y);
}
if (i > 49 && i <= 99)
{
display_pos = Vector2i(pixel_pos.x + ammoSprite.width * (i - (50)), pixel_pos.y + ammoSprite.height);
}
else if (i > 99 && i <= 149)
{
display_pos = Vector2i(pixel_pos.x + ammoSprite.width * (i - (100)), pixel_pos.y + ammoSprite.height * 2);
}
else if (i > 149 && i <= 199)
{
display_pos = Vector2i(pixel_pos.x + ammoSprite.width * (i - (150)), pixel_pos.y + ammoSprite.height * 3);
}
else if (i > 199 && i <= 249)
{
display_pos = Vector2i(pixel_pos.x + ammoSprite.width * (i - (200)), pixel_pos.y + ammoSprite.height * 4);
}
DrawTexture(ammoSprite, display_pos.x, display_pos.y, WHITE);
}
}
void drawHudHealth()
{
Vector2i pixel_pos = Vector2i(5, 5);
Texture2D noHealthSprite = AssetService::getInstance().getSprite(ESprite::HUD_HEART_EMPTY);
Texture2D healthSprite = AssetService::getInstance().getSprite(ESprite::HUD_HEART);
// sVector2f coord_pos = GetWindow().mapPixelToCoords(pixel_pos);
for (int i = 0; i < this->player.getMaxHealth(); i++)
{
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);
DrawTexture(noHealthSprite, pos.x, pos.y, WHITE);
}
for (int i = 0; i < this->player.getHealth(); i++)
{
// healthSprite[i].setPosition(coord_pos.x + healthTexture.getSize().x * i, coord_pos.y);
// DrawTexture(ammoSprite, display_pos.x, display_pos.y, WHITE);
Vector2i pos = Vector2i(pixel_pos.x + healthSprite.width * i, pixel_pos.y);
// noHealthSprite[i].setPosition(coord_pos.x + noHealthTexture.getSize().x * i, coord_pos.y);
DrawTexture(healthSprite, pos.x, pos.y, WHITE);
}
}
};
#endif
+6 -1
View File
@@ -1,9 +1,10 @@
#ifndef MAP_H
#define MAP_H
#include <vector>
#include "raylib.h"
#include "entities/TextureWrapper.h"
#include "entities/Vector2i.h"
#include <vector>
#include <cmath>
/**
@@ -28,6 +29,10 @@ public:
return this->colMap;
}
void updateColmapTile(Vector2i tile, bool collision) {
this->colMap[tile.y][tile.x] = collision ? 1 : 0;
}
private:
/** Holds the number for every single tile, which refers to a certain sprite in the tileset */
std::vector<std::vector<Vector2>> map;
+19 -7
View File
@@ -1,23 +1,35 @@
#ifndef PBULLET_H
#define PBULLET_H
#include "services/CollectableService.h"
#include "services/AssetService.h"
#include "entities/Bullet.h"
#include <iostream>
class PBullet : public Bullet {
class PBullet : public Bullet
{
public:
PBullet() {}
PBullet(Vector2 pos, float angle) : Bullet(pos, angle) {
PBullet(Vector2 pos, float angle) : Bullet(pos, angle)
{
this->color = YELLOW;
}
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 &&
this->position.y + this->size > crate.getObject().getPosition().y && this->position.y - this->size < crate.getObject().getPosition().y ) {
crate.setHit(true);
AssetService::getInstance().playSound(ESound::CRATE_BREAK);
// AssetService::getInstance().playSound(ESound::JELLY_FREED);
this->hasCollided = true;
}
}
}
};
#endif
+30 -9
View File
@@ -9,6 +9,7 @@
#include "services/GameService.h"
#include "entities/TextureWrapper.h"
#include "enums/ECorner.h"
#include "enums/ESound.h"
#include "entities/Vector2i.h"
#include "entities/PBullet.h"
@@ -20,11 +21,12 @@ public:
this->texture = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER), {32, 32}, {200, 200});
prevFrame = GetTime();
spriteAnimation = {0, 0};
spriteAnimation = {0, Right};
this->gunRight = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER_GUN_RIGHT), {58, 29}, {0, 0});
this->gunLeft = TextureWrapper(AssetService::getInstance().getSprite(ESprite::PLAYER_GUN_LEFT), {58, 29}, {0, 0});
this->ammo = max_ammo;
this->prevShot = GetTime();
this->health = max_health;
}
~Player()
{
@@ -54,6 +56,18 @@ public:
this->texture.setPosition(pos);
}
int getAmmo() {
return this->ammo;
}
uint8_t getHealth() {
return this->health;
}
uint8_t getMaxHealth() {
return this->max_health;
}
protected:
private:
TextureWrapper texture;
@@ -63,9 +77,9 @@ private:
float jumpPower = 7.2f;
float moveSpeed = 5.0f;
float gravity = 0.6f;
float animationDelay = 0.2f; // In seconds
float animationDelay = 0.2f; // In seconds
double prevFrame;
Vector2 spriteAnimation; // Keeps track of (1) Sprite cycling and (2) Player direction
Vector2i spriteAnimation; // Keeps track of (1) Sprite cycling and (2) Player direction
enum Direction
{
Down,
@@ -79,10 +93,12 @@ private:
bool groundCollision; // Keeps track of whether or not the player has collided with the ground
bool crateTopCollision; // Whether the player is standing on top of a crate
int ammo;
int max_ammo = 100;
float firingRate= 0.2f; // Delay in seconds
uint8_t ammo; // Current ammo of the player
uint8_t max_ammo = 100; // Max ammo of the player
float firingRate= 0.2f; // Delay in seconds
double prevShot; // The last time the player fired a bullet
uint8_t health; // Current health of the player
uint8_t max_health = 10; // Max health of the player
void applyGravity()
{
@@ -278,6 +294,7 @@ private:
{
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();
@@ -335,14 +352,18 @@ private:
if (InputService::getInstance().isMouseButtonDown(MOUSE_BUTTON_LEFT) && GetTime() - prevShot > firingRate && groundCollision)
{
prevShot = GetTime();
if (ammo == 0) {
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 bulletStartLocation = {this->texture.getPosition().x + 10, this->texture.getPosition().y + 10};
Vector2 bulletStartLocation = {this->texture.getPosition().x + 10, this->texture.getPosition().y + 10};
Vector2 mouseWorldPos = GameService::getInstance().getMouseWorldPos();
if (spriteAnimation.y == Left)
@@ -353,7 +374,7 @@ private:
if (spriteAnimation.y == Right)
{
bulletStartLocation.y = this->texture.getPosition().y + 10;
bulletStartLocation.x = this->texture.getPosition().x + 37;
bulletStartLocation.x = this->texture.getPosition().x + 22;
}
if (mouseWorldPos.x > this->texture.getPosition().x + (this->gunLeft.getSize().x) && spriteAnimation.y == Right ||