Prototyping AnimatedSprite code, refactored explosion

This commit is contained in:
2025-02-02 20:43:07 -05:00
parent d27119ef28
commit be1e04eac9
10 changed files with 105 additions and 54 deletions

View File

@@ -26,7 +26,6 @@ namespace CaveGame::Client
Event<float> BrushPercentChanged;
void SetBrushSize(float size);
void UpdateBrushSizeSliderState();

View File

@@ -4,17 +4,27 @@
namespace CaveGame::Core {
void Explosion::DrawFrame(const Texture* texture, const AABB2D& quad, const Vector2& pos, const Vector2& scale)
{
JGL::J2D::DrawPartialSprite(texture, pos, quad.minPoint, quad.maxPoint, rotation, {0.5f, 0.5f}, scale);
}
void Explosion::Draw() {
if (!HasDetonated()) { return; }
if (anim_timer >= 1.25f) { return; }
if (HasDetonated()) {
if (anim_timer < 1.25f) {
//JGL::J2D::Begin();
float draw_radius = radius * 1.25f;
float draw_radius = radius * 1.25f;
auto* tex = Client::AssetService::Get()->explosion_sprite;
auto* tex = Client::AssetService::Get()->explosion_sprite;
Vector2 pos = position - (Vector2(draw_radius));
Vector2 scale {draw_radius/16.f, draw_radius/16.f};
if (anim_timer > (4.f / 5.f)) {
DrawFrame(tex, CurrentFrame(), pos, scale);
/*if (anim_timer > (4.f / 5.f)) {
JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION4.minPoint, SP_EXPLOSION4.maxPoint, rotation, {0.5f,0.5f}, {draw_radius/16.f, draw_radius/16.f});
} else if (anim_timer > (3.f / 5.f)) {
JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION3.minPoint, SP_EXPLOSION3.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
@@ -24,10 +34,8 @@ namespace CaveGame::Core {
JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION1.minPoint, SP_EXPLOSION1.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
} else {
JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION0.minPoint, SP_EXPLOSION0.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
}
}*/
//JGL::J2D::End();
}
}
}
}

View File

@@ -68,12 +68,6 @@ void CaveGame::Client::TileTool::SetBrushSize(float size) {
tool_size_label->SetContent(std::format("Size: {}", Math::Round(size, 1)));
}
void CaveGame::Client::TileTool::UpdateBrushSizeSliderState() {
brush_size_slider->Size({0, row_height, 1, 0});
brush_size_slider->Maximum(1.f);
brush_size_slider->Minimum(0.01f);
brush_size_slider->Interval(0.001f);
}
void CaveGame::Client::TileTool::Enable(bool value) {
this->enabled = value;

View File

@@ -18,5 +18,6 @@ target_include_directories(CaveCore PUBLIC ${J3ML_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${mcolor_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${jjx_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${Sockets_SOURCE_DIR}/include)
target_include_directories(CaveCore PUBLIC ${JGL_SOURCE_DIR}/include)
target_link_libraries(CaveCore PUBLIC J3ML jjx mcolor Sockets)
target_link_libraries(CaveCore PUBLIC J3ML JGL jjx mcolor Sockets)

View File

@@ -0,0 +1,39 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
namespace CaveGame::Core
{
struct Frame {
Vector2 origin;
Vector2 offset;
Vector2 size;
//AABB2D quad;
};
class KeyframeAnimation
{
Event<> Began;
Event<> Complete;
Event<> Paused;
Event<> Resumed;
Event<> Cancelled;
std::vector<Frame> keyframes;
float timer;
int keyframe_index;
};
class AnimatedSprite {
public:
AnimatedSprite() { }
void AddFrame(const std::string& frame_id, const Frame& frame);
void SetCurrentFrame();
void DrawFrame();
void DrawCurrentFrame();
void PlayAnimation(const KeyframeAnimation& anim);
protected:
private:
};
}

View File

@@ -1,23 +0,0 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
namespace CaveGame::Core
{
struct Frame {
Vector2 origin;
Vector2 offset;
Vector2 size;
};
class Animator2D {
public:
Animator2D() { }
void AddFrame(const std::string& frame_id, const Frame& frame);
protected:
private:
};
}

View File

@@ -1,12 +1,12 @@
#pragma once
#include "Animator2D.hpp"
#include "AnimatedSprite.hpp"
#include "Entity.hpp"
#include <JGL/types/RenderTarget.h>
namespace CaveGame::Core
{
class Explosion : public Entity, public Animator2D {
class Explosion : public Entity, public AnimatedSprite {
public:
const AABB2D SP_EXPLOSION0 {{0, 0}, {32, 32}};
@@ -24,8 +24,6 @@ namespace CaveGame::Core
detonated = true;
health = 0;
for (int x = -radius; x <= radius; x++) {
for (int y = -radius; y <= radius; y++) {
float dist = Vector2::LengthSquared(Vector2(x, y));
@@ -34,8 +32,8 @@ namespace CaveGame::Core
}
}
}
}
bool HasDetonated() const;
void Draw() override;
@@ -49,7 +47,13 @@ namespace CaveGame::Core
bool detonated = false;
float anim_timer = 0.f;
ITileMap* wrld;
AABB2D quad;
private:
void DrawFrame(const JGL::Texture *texture, const AABB2D &quad, const Vector2 &pos, const Vector2 &scale);
AABB2D CurrentFrame() const;
void SetFrame(const AABB2D &frame_quad);
};
}

View File

@@ -0,0 +1,6 @@
#include <Core/AnimatedSprite.hpp>
namespace CaveGame::Core
{
}

View File

@@ -1,6 +0,0 @@
#include <Core/Animator2D.hpp>
namespace CaveGame::Core
{
}

View File

@@ -1,12 +1,19 @@
#include <Core/Explosion.hpp>
namespace CaveGame::Core {
void Explosion::SetFrame(const AABB2D& frame_quad)
{
this->quad = frame_quad;
}
AABB2D Explosion::CurrentFrame() const { return quad;}
bool Explosion::HasDetonated() const { return detonated;}
void Explosion::Update(float elapsed) {
//Entity::Update(elapsed);
if (fuse > 0.f)
@@ -19,5 +26,27 @@ namespace CaveGame::Core {
if (HasDetonated())
anim_timer += elapsed;
if (HasDetonated()) {
if (anim_timer < 1.25f) {
if (anim_timer > (4.f / 5.f)) {
SetFrame(SP_EXPLOSION4);
//JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION4.minPoint, SP_EXPLOSION4.maxPoint, rotation, {0.5f,0.5f}, {draw_radius/16.f, draw_radius/16.f});
} else if (anim_timer > (3.f / 5.f)) {
SetFrame(SP_EXPLOSION3);
//JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION3.minPoint, SP_EXPLOSION3.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
} else if (anim_timer > (2.f / 5.f)) {
SetFrame(SP_EXPLOSION2);
//JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION2.minPoint, SP_EXPLOSION2.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
} else if (anim_timer > (1.f / 5.f)) {
SetFrame(SP_EXPLOSION1);
//JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION1.minPoint, SP_EXPLOSION1.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
} else {
SetFrame(SP_EXPLOSION0);
//JGL::J2D::DrawPartialSprite(tex, position-(Vector2(draw_radius)), SP_EXPLOSION0.minPoint, SP_EXPLOSION0.maxPoint, rotation, {0.5f, 0.5f}, {draw_radius/16.f, draw_radius/16.f});
}
}
}
}
}