Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code: Create Players, Projectile and clean up old logic. #1

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Constants.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H

#include <string>

enum struct DirectionEnum { UP = 0, DOWN = 1, RIGHT = 2, LEFT = 3 };

enum struct PlayerStateEnum { IDLE = 0, MOVING = 1, FIRING = 2, EXPLODING = 3 };

// Function to convert Jogador2Direction to string
inline std::string directionToString(DirectionEnum dir) {
switch (dir) {
case DirectionEnum::RIGHT:
return "RIGHT";
case DirectionEnum::UP:
return "UP";
case DirectionEnum::DOWN:
return "DOWN";
case DirectionEnum::LEFT:
return "LEFT";
default:
return "UNKNOWN";
}
}

const int PROJECTILE_MOVING_COOLDOWN = 20;

#endif // CONSTANTS_H
54 changes: 54 additions & 0 deletions src/Player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Player.hpp
#ifndef PLAYER_H
#define PLAYER_H

#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#include "Constants.hpp"
#include "SpriteSet.hpp"

class Player {
public:
std::string playerName;
SpriteSet spriteSet;
SDL_Surface* spriteSurface;

int direction;
SDL_Rect position;

int timer;
PlayerStateEnum state;

Player(const std::string& playerName)
: playerName(playerName),
spriteSet("Jogador1") // Initialize spriteSet here
{
state = PlayerStateEnum::IDLE;
direction = static_cast<int>(DirectionEnum::RIGHT);
spriteSet.addRect(directionToString(DirectionEnum::RIGHT), 0, 0, 32,
32); // Virado para direita
spriteSet.addRect(directionToString(DirectionEnum::UP), 34, 34, 32,
32); // Virado para cima
spriteSet.addRect(directionToString(DirectionEnum::DOWN), 34, 0, 32,
32); // Virado para baixo
spriteSet.addRect(directionToString(DirectionEnum::LEFT), 0, 34, 32,
32); // Virado para esquerda
};

void loadSprite(const std::string& spriteFileName) {
spriteSurface = SDL_LoadBMP(spriteFileName.c_str());
SDL_SetColorKey(spriteSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL,
(Uint16)SDL_MapRGB(spriteSurface->format, 0, 255,
0)); // para por transparencia no verde.
};

void setDirection(DirectionEnum directionEnum) { direction = static_cast<int>(directionEnum); }

private:
};

#endif // PLAYER_H
52 changes: 52 additions & 0 deletions src/Projectile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Player.hpp
#ifndef PROJECTILE_H
#define PROJECTILE_H

#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#include "Constants.hpp"
#include "SpriteSet.hpp"

class Projectile {
public:
int projectileId;
SpriteSet spriteSet;
SDL_Surface* spriteSurface;

int direction;
SDL_Rect position;

int movingCooldown;

Projectile(const int projectileId)
: projectileId(projectileId),
spriteSet("Bala") // Initialize spriteSet here
{
direction = static_cast<int>(DirectionEnum::RIGHT);
spriteSet.addRect(directionToString(DirectionEnum::RIGHT), 0, 0, 32,
32); // Virado para direita
spriteSet.addRect(directionToString(DirectionEnum::UP), 34, 34, 32,
32); // Virado para cima
spriteSet.addRect(directionToString(DirectionEnum::DOWN), 34, 0, 32,
32); // Virado para baixo
spriteSet.addRect(directionToString(DirectionEnum::LEFT), 0, 34, 32,
32); // Virado para esquerda
};

void loadSprite(const std::string& spriteFileName) {
spriteSurface = SDL_LoadBMP(spriteFileName.c_str());
SDL_SetColorKey(spriteSurface, SDL_SRCCOLORKEY | SDL_RLEACCEL,
(Uint16)SDL_MapRGB(spriteSurface->format, 0, 255,
0)); // para por transparencia no verde.
};

void setDirection(DirectionEnum directionEnum) { direction = static_cast<int>(directionEnum); }

private:
};

#endif // PROJECTILE_H
25 changes: 25 additions & 0 deletions src/SpriteSet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SpriteSet.cpp
#include "SpriteSet.hpp"

#include <stdexcept>

SpriteSet::SpriteSet(const std::string& name) : name_(name) {}

void SpriteSet::addRect(const std::string& name, int x, int y, int w, int h) {
SDL_Rect rect = {x, y, w, h};
rects_[name] = rect;
}

const SDL_Rect& SpriteSet::getRect(const std::string& name) const {
auto it = rects_.find(name);
if (it != rects_.end()) {
return it->second;
}
throw std::out_of_range("Sprite name not found: " + name);
}

bool SpriteSet::hasRect(const std::string& name) const { return rects_.find(name) != rects_.end(); }

size_t SpriteSet::size() const { return rects_.size(); }

const std::string& SpriteSet::getName() const { return name_; }
39 changes: 39 additions & 0 deletions src/SpriteSet.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SpriteSet.h
#ifndef SPRITESET_H
#define SPRITESET_H

#include <string>
#include <unordered_map>

#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

class SpriteSet {
public:
// Constructor
SpriteSet(const std::string& name);

// Add a sprite rectangle with a name
void addRect(const std::string& name, int x, int y, int w, int h);

// Retrieve a sprite rectangle by name
const SDL_Rect& getRect(const std::string& name) const;

// Check if a sprite exists
bool hasRect(const std::string& name) const;

// Get the number of rectangles
size_t size() const;

// Get the name of the sprite set
const std::string& getName() const;

private:
std::string name_;
std::unordered_map<std::string, SDL_Rect> rects_;
};

#endif // SPRITESET_H
Loading
Loading