-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
preparing for more sprite types in the future. gonna work on rect-rect collisions first.
- Loading branch information
1 parent
ab4ad7a
commit 7df3b6d
Showing
12 changed files
with
139 additions
and
92 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
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 was deleted.
Oops, something went wrong.
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,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(); | ||
}; | ||
} |
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,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(); | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} |
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,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; | ||
} |