-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: char base class and collision detection
- Loading branch information
Showing
16 changed files
with
204 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef CHARACTER_H_ | ||
#define CHARACTER_H_ | ||
|
||
#include "constants.hpp" | ||
#include "entity.hpp" | ||
#include <chrono> | ||
#include <random> | ||
#include <string> | ||
#include <thread> | ||
|
||
namespace nano { | ||
class Character : public IEntity { | ||
protected: | ||
int x = 0, y = 0; | ||
SDL_Texture *texture; | ||
std::string spritePath; | ||
|
||
public: | ||
Character(std::string name, int x, int y, SDL_Renderer *renderer, | ||
std::string spritePath); | ||
void render(SDL_Renderer *renderer) override; | ||
void handleEvent(SDL_Event *event) override; | ||
void update() override; | ||
bool isColliding(const SDL_Rect &box) override; | ||
virtual ~Character(); | ||
std::string getName() override; | ||
SDL_Rect getBoundingBox() override; | ||
void handleCollision() override; | ||
}; | ||
} // namespace nano | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include "../include/character.hpp" | ||
|
||
using namespace nano; | ||
|
||
Character::Character(std::string name, int x, int y, SDL_Renderer *renderer, | ||
std::string spritePath) { | ||
texture = IMG_LoadTexture(renderer, spritePath.c_str()); | ||
if (!texture) { | ||
SDL_Log("Failed to load character texture: %s", IMG_GetError()); | ||
exit(1); | ||
} | ||
this->name = name; | ||
this->x = x; | ||
this->y = y; | ||
this->boundingBox = {this->x, this->y, TILE_SIZE, TILE_SIZE}; | ||
|
||
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, | ||
"%s bounding box: h:%d,w:%d,x:%d,y:%d", this->name.c_str(), | ||
this->boundingBox.h, this->boundingBox.w, this->boundingBox.x, | ||
this->boundingBox.y); | ||
}; | ||
|
||
void Character::render(SDL_Renderer *renderer) { | ||
SDL_Rect CharacterRect = {x, y, TILE_SIZE, TILE_SIZE}; | ||
SDL_RenderCopy(renderer, texture, NULL, &CharacterRect); | ||
} | ||
|
||
void Character::handleEvent(SDL_Event *event) {} | ||
|
||
void Character::update() {} | ||
|
||
bool Character::isColliding(const SDL_Rect &box) { | ||
return (this->boundingBox.x < box.x + box.w) && | ||
(this->boundingBox.x + this->boundingBox.w > box.x) && | ||
(this->boundingBox.y < box.y + box.h) && | ||
(this->boundingBox.y + this->boundingBox.h > box.y); | ||
} | ||
|
||
std::string Character::getName() { return this->name; }; | ||
|
||
SDL_Rect Character::getBoundingBox() { return this->boundingBox; }; | ||
|
||
void Character::handleCollision() {} | ||
|
||
Character::~Character() { SDL_DestroyTexture(texture); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.