-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added hash function for vector 2 and some other stuff... * Fixing bug with not adding rotation to sprites. * Added loops property to AnimatedSpriteComponent. * Fixed bug with animations sometimes not starting on the first frame.
- Loading branch information
Showing
14 changed files
with
212 additions
and
140 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <set> | ||
#include <unordered_map> | ||
|
||
|
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
52 changes: 52 additions & 0 deletions
52
src/core/ecs/system/systems/animated_sprite_rendering_entity_system.cpp
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,52 @@ | ||
#include "animated_sprite_rendering_entity_system.h" | ||
|
||
AnimatedSpriteRenderingEntitySystem::AnimatedSpriteRenderingEntitySystem() { | ||
renderer = GD::GetContainer()->renderer2D; | ||
componentManager = GD::GetContainer()->componentManager; | ||
cameraManager = GD::GetContainer()->cameraManager; | ||
sceneManager = GD::GetContainer()->sceneManager; | ||
enabled = true; | ||
} | ||
|
||
void AnimatedSpriteRenderingEntitySystem::Render() { | ||
if (IsEnabled()) { | ||
for (Entity entity : entities) { | ||
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity); | ||
AnimatedSpriteComponent animatedSpriteComponent = componentManager->GetComponent<AnimatedSpriteComponent>(entity); | ||
Animation currentAnimation = animatedSpriteComponent.currentAnimation; | ||
AnimationFrame currentFrame = currentAnimation.animationFrames[animatedSpriteComponent.currentFrameIndex]; | ||
if (animatedSpriteComponent.isPlaying) { | ||
unsigned int newIndex = static_cast<unsigned int>(((SDL_GetTicks() - animatedSpriteComponent.startAnimationTickTime) / currentAnimation.speed) % currentAnimation.frames); | ||
if (newIndex != animatedSpriteComponent.currentFrameIndex) { | ||
// Index changed | ||
currentFrame = currentAnimation.animationFrames[newIndex]; | ||
if (newIndex + 1 == currentAnimation.frames) { | ||
// Animation Finished | ||
if (!animatedSpriteComponent.loops) { | ||
animatedSpriteComponent.isPlaying = false; | ||
} | ||
} | ||
animatedSpriteComponent.currentFrameIndex = newIndex; | ||
componentManager->UpdateComponent(entity, animatedSpriteComponent); | ||
} | ||
} | ||
Camera2D camera = cameraManager->GetCurrentCamera2D(); | ||
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity); | ||
Vector2 drawDestinationPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera); | ||
Vector2 drawScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale; | ||
Vector2 drawDestinationSize = Vector2(currentFrame.drawSource.w * drawScale.x, currentFrame.drawSource.h * drawScale.y); | ||
Rect2 drawDestination = Rect2(drawDestinationPosition, drawDestinationSize); | ||
assert(currentFrame.texture != nullptr && "Current frame texture is null"); | ||
renderer->BatchDrawSprite( | ||
currentFrame.texture, | ||
currentFrame.drawSource, | ||
drawDestination, | ||
transform2DComponent.zIndex, | ||
transform2DComponent.rotation, | ||
animatedSpriteComponent.modulate, | ||
animatedSpriteComponent.flipX, | ||
animatedSpriteComponent.flipY | ||
); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
src/core/ecs/system/systems/sprite_rendering_entity_system.cpp
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,34 @@ | ||
#include "sprite_rendering_entity_system.h" | ||
|
||
SpriteRenderingEntitySystem::SpriteRenderingEntitySystem() { | ||
renderer = GD::GetContainer()->renderer2D; | ||
componentManager = GD::GetContainer()->componentManager; | ||
cameraManager = GD::GetContainer()->cameraManager; | ||
sceneManager = GD::GetContainer()->sceneManager; | ||
enabled = true; | ||
} | ||
|
||
void SpriteRenderingEntitySystem::Render() { | ||
if (IsEnabled()) { | ||
for (Entity entity : entities) { | ||
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity); | ||
SpriteComponent spriteComponent = componentManager->GetComponent<SpriteComponent>(entity); | ||
Camera2D camera = cameraManager->GetCurrentCamera2D(); | ||
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity); | ||
Vector2 drawDestinationPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera); | ||
Vector2 drawScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale; | ||
Vector2 drawDestinationSize = Vector2(spriteComponent.drawSource.w * drawScale.x, spriteComponent.drawSource.h * drawScale.y); | ||
spriteComponent.drawDestination = Rect2(drawDestinationPosition, drawDestinationSize); | ||
renderer->BatchDrawSprite( | ||
spriteComponent.texture, | ||
spriteComponent.drawSource, | ||
spriteComponent.drawDestination, | ||
transform2DComponent.zIndex, | ||
transform2DComponent.rotation, | ||
spriteComponent.modulate, | ||
spriteComponent.flipX, | ||
spriteComponent.flipY | ||
); | ||
} | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/core/ecs/system/systems/text_rendering_entity_system.cpp
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,53 @@ | ||
#include "text_rendering_entity_system.h" | ||
|
||
TextRenderingEntitySystem::TextRenderingEntitySystem() { | ||
renderer = GD::GetContainer()->renderer2D; | ||
sceneManager = GD::GetContainer()->sceneManager; | ||
componentManager = GD::GetContainer()->componentManager; | ||
cameraManager = GD::GetContainer()->cameraManager; | ||
enabled = true; | ||
} | ||
|
||
void TextRenderingEntitySystem::OnRegisterEntity(Entity entity) { | ||
EntitySystem::OnRegisterEntity(entity); | ||
UpdateEntityText(entity, componentManager->GetComponent<TextLabelComponent>(entity)); | ||
} | ||
|
||
void TextRenderingEntitySystem::OnEntityDestroyed(Entity entity) { | ||
EntitySystem::OnEntityDestroyed(entity); | ||
textLines.erase(entity); | ||
} | ||
|
||
void TextRenderingEntitySystem::UpdateEntityText(Entity entity, const TextLabelComponent& textLabelComponent) { | ||
textLines.erase(entity); | ||
TextLines entityTextLines = StringUtil::ConvertIntoWordWrappedLines( | ||
textLabelComponent.text, | ||
textLabelComponent.wordWrap, | ||
textLabelComponent.maxCharactersPerLine); | ||
textLines.emplace(entity, entityTextLines); | ||
} | ||
|
||
void TextRenderingEntitySystem::Render() { | ||
if (IsEnabled()) { | ||
for (Entity entity : entities) { | ||
Transform2DComponent transform2DComponent = componentManager->GetComponent<Transform2DComponent>(entity); | ||
Camera2D camera = cameraManager->GetCurrentCamera2D(); | ||
Transform2DComponent parentTransform = SceneNodeHelper::GetCombinedParentsTransforms(sceneManager, componentManager, entity); | ||
Vector2 textLabelPosition = SpaceHandler::WorldToScreen(transform2DComponent.position + parentTransform.position, transform2DComponent.ignoreCamera); | ||
TextLabelComponent textLabelComponent = componentManager->GetComponent<TextLabelComponent>(entity); | ||
Vector2 textLabelScale = !transform2DComponent.ignoreCamera ? transform2DComponent.scale * parentTransform.scale * camera.zoom : transform2DComponent.scale * parentTransform.scale; | ||
// TODO: Replace with more robust word wrapping | ||
for (unsigned int i = 0; i < textLines[entity].lines.size(); i++) { | ||
renderer->BatchDrawFont( | ||
textLabelComponent.font, | ||
textLines[entity].lines[i], | ||
textLabelPosition.x, | ||
textLabelPosition.y + (i * textLabelComponent.font->GetSize()) + (i * textLabelComponent.newLinePadding), | ||
transform2DComponent.zIndex, | ||
textLabelScale.x, | ||
textLabelComponent.color | ||
); | ||
} | ||
} | ||
} | ||
} |
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.