Skip to content

Commit

Permalink
Sprite restructure
Browse files Browse the repository at this point in the history
preparing for more sprite types in the future. gonna work on rect-rect collisions first.
  • Loading branch information
durkisneer1 committed Nov 18, 2023
1 parent ab4ad7a commit 7df3b6d
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 92 deletions.
2 changes: 1 addition & 1 deletion example/include/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <vector>


class Player : public dk::Character {
class Player : public dk::CharacterSprite {
public:
Player(dk::RenderWindow& window, dk::Texture& texture);
~Player() = default;
Expand Down
2 changes: 1 addition & 1 deletion example/include/Wall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <DurkGame.hpp>


class Wall : public dk::Character {
class Wall : public dk::CharacterSprite {
public:
Wall(dk::RenderWindow& window, dk::Texture& texture);
~Wall() = default;
Expand Down
2 changes: 1 addition & 1 deletion example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main() {
window.fill({ 40, 40, 40 });

// Update and draw character objects
for (const auto& chars : dk::Character::getCharacters()) {
for (const auto& chars : dk::SpriteNode::getSpriteNodes()) {
chars->process(deltaTime);
}

Expand Down
2 changes: 1 addition & 1 deletion example/src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Player::Player(dk::RenderWindow& window, dk::Texture& texture)
: dk::Character(window, texture) {
: dk::CharacterSprite(window, texture) {
position = { WIN_SIZE.x / 2, WIN_SIZE.y / 2 };
}

Expand Down
2 changes: 1 addition & 1 deletion example/src/wall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Wall::Wall(dk::RenderWindow& window, dk::Texture& texture)
: dk::Character(window, texture) {
: dk::CharacterSprite(window, texture) {
position = { WIN_SIZE.x / 4, WIN_SIZE.y / 2 };
rect.setCenter(position);
}
Expand Down
37 changes: 0 additions & 37 deletions include/Character.hpp

This file was deleted.

19 changes: 19 additions & 0 deletions include/CharacterSprite.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "SpriteNode.hpp"
#include "Globals.hpp"


namespace dk {
class CharacterSprite : public SpriteNode {
public:
CharacterSprite(dk::RenderWindow& window, dk::Texture& texture);
~CharacterSprite() = default;

protected:
dk::math::Vector2 velocity;

void moveAndSlide();
void move();
};
}
5 changes: 3 additions & 2 deletions include/DurkGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@

#include <SDL_mixer.h> // FIXME: Remove once dk::mixer implemented.

#include "Character.hpp"
#include "CharacterSprite.hpp"
#include "Clock.hpp"
#include "Constants.hpp"
#include "Font.hpp"
#include "Globals.hpp"
#include "Input.hpp"
#include "Math.hpp"
#include "Rect.hpp"
#include "RenderWindow.hpp"
#include "SpriteNode.hpp"
#include "Texture.hpp"
#include "Constants.hpp"


namespace dk {
Expand Down
39 changes: 39 additions & 0 deletions include/SpriteNode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <SDL.h>
#include "Rect.hpp"
#include "Texture.hpp"
#include "Math.hpp"
#include "RenderWindow.hpp"


namespace dk {
class SpriteNode {
public:
SpriteNode(dk::RenderWindow& window, dk::Texture& texture);
~SpriteNode() = default;

dk::math::Vector2 getPosition() const;
dk::Rect getRect() const;

static const std::vector<SpriteNode*>& getSpriteNodes();
static const std::vector<SpriteNode*>& getCharacterSprites();
static const std::vector<SpriteNode*>& getPhysicsSprites();
static const std::vector<SpriteNode*>& getStaticSprites();

virtual void process(double deltaTime) = 0;
protected:
dk::RenderWindow& window;
dk::Texture& texture;

dk::Rect rect;
dk::math::Vector2 position;

static std::vector<SpriteNode*> spriteNodes;
static std::vector<SpriteNode*> characterSprites;
static std::vector<SpriteNode*> physicsSprites;
static std::vector<SpriteNode*> staticSprites;

void draw();
};
}
48 changes: 0 additions & 48 deletions src/character.cpp

This file was deleted.

30 changes: 30 additions & 0 deletions src/character_sprite.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "../include/CharacterSprite.hpp"


namespace dk {
CharacterSprite::CharacterSprite(dk::RenderWindow& window, dk::Texture& texture)
: SpriteNode(window, texture) {
rect = texture.getRect();
characterSprites.push_back(this);
}

void CharacterSprite::moveAndSlide() {
move();

for (auto& target : characterSprites) {
if (target == this) continue;
float distance = position.distanceTo(target->getPosition());
float radiusSum = rect.getSize().x / 2 + target->getRect().getSize().x / 2;
if (distance < radiusSum) {
float overlap = radiusSum - distance;
position += (position - target->getPosition()) * (overlap / distance);
}
}
}

void CharacterSprite::move() {
// velocity.y += GRAVITY; // FIXME: Implement gravity
position += velocity;
rect.setCenter(position);
}
}
43 changes: 43 additions & 0 deletions src/sprite_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "../include/SpriteNode.hpp"


namespace dk {
SpriteNode::SpriteNode(dk::RenderWindow& window, dk::Texture& texture)
: window(window), texture(texture) {
rect = texture.getRect();
spriteNodes.push_back(this);
}

dk::math::Vector2 SpriteNode::getPosition() const {
return position;
}

dk::Rect SpriteNode::getRect() const {
return rect;
}

const std::vector<SpriteNode*>& SpriteNode::getSpriteNodes() {
return spriteNodes;
}

const std::vector<SpriteNode*>& SpriteNode::getCharacterSprites() {
return characterSprites;
}

const std::vector<SpriteNode*>& SpriteNode::getPhysicsSprites() {
return physicsSprites;
}

const std::vector<SpriteNode*>& SpriteNode::getStaticSprites() {
return staticSprites;
}

void SpriteNode::draw() {
texture.blit(rect);
}

std::vector<SpriteNode*> SpriteNode::spriteNodes;
std::vector<SpriteNode*> SpriteNode::characterSprites;
std::vector<SpriteNode*> SpriteNode::physicsSprites;
std::vector<SpriteNode*> SpriteNode::staticSprites;
}

0 comments on commit 7df3b6d

Please sign in to comment.