Skip to content

Commit

Permalink
fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-Rana-1718 committed Feb 3, 2024
1 parent 281bf1d commit 797aae5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./a.exe
Binary file modified a.exe
Binary file not shown.
27 changes: 20 additions & 7 deletions main/entityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

class Entity
{

public:

std::string tag;
int e_id;

CShape *cshape = NULL;
CTransform *ctransform = NULL;

int e_health;

bool controllable = false;
Expand All @@ -17,13 +19,23 @@ class Entity
{
tag = t;
e_id = id;
e_health=1;
cshape = new CShape(w, h, color, outline_color, outline_thickness);
ctransform = new CTransform(px, py, sx, sy, 0);
controllable=controll;
e_health = 1;

if (w != 0 || h != 0)
{
cshape = new CShape(w, h, color, outline_color, outline_thickness);
}

if (px != 0 && py != 0 && sx != 0 && sy != 0)
{
ctransform = new CTransform(px, py, sx, sy, 0);
}

controllable = controll;
}

~ Entity() {
~Entity()
{
delete cshape;
delete ctransform;
}
Expand All @@ -40,7 +52,7 @@ class EntityManager
Entity *ptr = new Entity(tag, m_totalEntities++, w, h, px, py, sx, sy, color, outline, outline_thickness, controllable);
m_entities.push_back(ptr);
return ptr;
}
}

void removeEntity(int id)
{
Expand All @@ -50,6 +62,7 @@ class EntityManager
{
delete (m_entities.at(i));
m_entities.erase(m_entities.begin() + i);
break;
}
}
}
Expand Down
58 changes: 38 additions & 20 deletions main/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ sf::Font font;

class Game
{
sf::RenderWindow window;
EntityManager entities;
sf::RenderWindow g_window;
EntityManager g_entities;
std::vector<sf::Text> labels;

bool is_running = true;
Expand All @@ -32,19 +32,21 @@ class Game

Entity *spawnPlayer();
void spawnEnemy();
void spawnBullet();

void displayDevConsole(const std::vector<Entity *> &entities);

public:
Entity *player;
Game()
{
window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML works!");

window.setFramerateLimit(60);
g_window.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "SFML works!");
g_window.setFramerateLimit(60);

player = spawnPlayer();

frames = 0;
is_running = true;

// fonts
if (!font.loadFromFile("assets/noto.ttf"))
Expand All @@ -62,23 +64,23 @@ class Game
while (is_running)
{
sf::Event event;
while (window.pollEvent(event))
while (g_window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
is_running = false;
window.close();
g_window.close();
}
}

SDraw(entities.getEntities());
SMove(entities.getEntities());
SCollision(entities.getEntities());
SDraw(g_entities.getEntities());
SMove(g_entities.getEntities());
SCollision(g_entities.getEntities());
SInput(player);

if (devMode)
{
displayDevConsole(entities.getEntities());
displayDevConsole(g_entities.getEntities());
}

frames++;
Expand All @@ -90,23 +92,23 @@ class Game

void Game::SDraw(const std::vector<Entity *> &entities)
{
window.clear();
g_window.clear();

for (auto &entity : entities)
{
if (entity->cshape != NULL)
{
window.draw(entity->cshape->rect);
g_window.draw(entity->cshape->rect);
}
}

// Draw UI
for (auto &text : labels)
{
window.draw(text);
g_window.draw(text);
}

window.display();
g_window.display();
}

void Game::SMove(const std::vector<Entity *> &entities)
Expand Down Expand Up @@ -150,6 +152,10 @@ void Game::SInput(Entity *player)
{
devMode = true;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
spawnBullet();
}
}

void Game::SCollision(const std::vector<Entity *> &entities)
Expand All @@ -158,7 +164,6 @@ void Game::SCollision(const std::vector<Entity *> &entities)
for (int i = 0; i < entities.size(); i++)
{


if ((entities[i]->ctransform != NULL) && (entities[i]->controllable != true))
{
if ((entities[i]->ctransform->posX < 0) || (entities[i]->ctransform->posX + entities[i]->cshape->width > WINDOW_WIDTH))
Expand All @@ -180,12 +185,20 @@ void Game::SCollision(const std::vector<Entity *> &entities)
entities[i]->ctransform->speedX *= -1;
entities[i]->ctransform->speedY *= -1;
}
else
{
g_entities.removeEntity(entities[j]->e_id);
}

if (entities[j]->tag != "Player")
{
entities[j]->ctransform->speedX *= -1;
entities[j]->ctransform->speedY *= -1;
}
else
{
g_entities.removeEntity(entities[i]->e_id);
}
}
}
}
Expand Down Expand Up @@ -217,7 +230,7 @@ void Game::SUserInterface()

Entity *Game::spawnPlayer()
{
return entities.addEntities("Player", 100.f, 100.f, 100.f, 110.f, 10.f, 10.f, sf::Color(220, 20, 60), sf::Color(255, 255, 255), 1, true);
return g_entities.addEntities("Player", 100.f, 100.f, 100.f, 110.f, 10.f, 10.f, sf::Color(220, 20, 60), sf::Color(255, 255, 255), 1, true);
}

void Game::displayDevConsole(const std::vector<Entity *> &entities)
Expand All @@ -230,8 +243,13 @@ void Game::spawnEnemy()

for (int i = 0; i < 1; i++)
{
entities.addEntities("Enemy", 100.f, 90.f, 300.f+i*10, 300.f+i*10, 1.f, 2.f, sf::Color(0, 255, 128), sf::Color(255, 255, 255), 1);
entities.addEntities("Enemy", 150.f, 80.f, 100.f+i*10, 200.f+i*10, 2.f, 2.f, sf::Color(255, 102, 178), sf::Color(255, 255, 255), 1);
entities.addEntities("Enemy", 200.f, 100.f, 300.f+i*10, 100.f+i*10, 5.f, 3.f, sf::Color(102, 178, 255), sf::Color(255, 255, 255), 1);
g_entities.addEntities("Enemy", 100.f, 90.f, 300.f + i * 10, 300.f + i * 10, 1.f, 2.f, sf::Color(0, 255, 128), sf::Color(255, 255, 255), 1);
g_entities.addEntities("Enemy", 150.f, 80.f, 100.f + i * 10, 200.f + i * 10, 2.f, 2.f, sf::Color(255, 102, 178), sf::Color(255, 255, 255), 1);
g_entities.addEntities("Enemy", 200.f, 100.f, 300.f + i * 10, 100.f + i * 10, 5.f, 3.f, sf::Color(102, 178, 255), sf::Color(255, 255, 255), 1);
}
}

void Game::spawnBullet()
{
g_entities.addEntities("Bullet", 10.f, 10.f, 100.f, 100.f, 1.f, 1.f, sf::Color::White);
}

0 comments on commit 797aae5

Please sign in to comment.