Skip to content

Commit

Permalink
Merge pull request #63 from pflat/animated_tiles
Browse files Browse the repository at this point in the history
Add support for animeted tiles in tmx files
  • Loading branch information
klaussilveira authored Mar 15, 2024
2 parents 61acb78 + 288ebdc commit cf8e663
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 2 deletions.
14 changes: 14 additions & 0 deletions Source/Urho3D/LuaScript/pkgs/Urho2D/TileMapDefs2D.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ struct TileMapInfo2D
tolua_readonly tolua_property__get_set float mapHeight;
};

struct TileFrameInfo2D
{
unsigned gid_ @ gid;
unsigned duration_ @ duration;
};

enum TileMapLayerType2D
{
LT_TILE_LAYER,
Expand All @@ -47,6 +53,13 @@ class PropertySet2D
const String GetProperty(const String name) const;
};

class FrameSet2D
{
void UpdateTimer(float timeStep);
unsigned GetCurrentFrameGid() const;
unsigned GetNumFrames() const;
};

static const unsigned FLIP_HORIZONTAL;
static const unsigned FLIP_VERTICAL;
static const unsigned FLIP_DIAGONAL;
Expand All @@ -62,6 +75,7 @@ class Tile2D
Sprite2D* GetSprite() const;
bool HasProperty(const String name) const;
const String GetProperty(const String name) const;
bool IsAnimated() const;

tolua_readonly tolua_property__get_set unsigned gid;
tolua_readonly tolua_property__get_set Sprite2D* sprite;
Expand Down
2 changes: 2 additions & 0 deletions Source/Urho3D/LuaScript/pkgs/Urho2D/TileMapLayer2D.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class TileMapLayer2D : Component
void SetDrawOrder(int drawOrder);
void SetVisible(bool visible);

void UpdateAnimations();

int GetDrawOrder() const;
bool IsVisible() const;
bool HasProperty(const String name) const;
Expand Down
45 changes: 43 additions & 2 deletions Source/Urho3D/LuaScript/pkgs/Urho2D/TmxFile2D.pkg
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
$#include "Urho2D/TmxFile2D.h"

// FIXME: complete the bindings for the rest of the class methods
class TmxLayer2D
{
TmxFile2D* GetTmxFile() const;
TileMapLayerType2D GetType() const;
const String GetName() const;
int GetWidth() const;
int GetHeight() const;
bool IsVisible() const;
bool HasProperty(const String name) const;
const String GetProperty(const String name) const;
}

class TmxTileLayer2D : TmxLayer2D
{
Tile2D* GetTile(int x, int y) const;
}

class TmxObjectGroup2D : TmxLayer2D
{
unsigned GetNumObjects() const;
TileMapObject2D* GetObject(unsigned index) const;
}

class TmxImageLayer2D : TmxLayer2D
{
const Vector2 GetPosition() const;
const String GetSource() const;
Sprite2D* GetSprite() const;
}

class TmxFile2D : Resource
{
void SetSpriteTextureEdgeOffset(float offset);
bool SetInfo(Orientation2D orientation, int width, int height, float tileWidth, float tileHeight);
void AddLayer(unsigned index, TmxLayer2D *layer);
void AddLayer(TmxLayer2D* layer);
const TileMapInfo2D GetInfo() const;
Sprite2D* GetTileSprite(unsigned gid) const;

PropertySet2D* GetTilePropertySet(unsigned gid) const;
FrameSet2D* GetTileFrameSet(unsigned gid) const;

void UpdateAnimationTimers(float timeStep);
unsigned GetNumLayers() const;
const TmxLayer2D* GetLayer(unsigned index) const;
void SetSpriteTextureEdgeOffset(float offset);
float GetSpriteTextureEdgeOffset() const;

tolua_readonly tolua_property__get_set TileMapInfo2D info;
tolua_property__get_set float spriteTextureEdgeOffset @ edgeOffset;
};
19 changes: 19 additions & 0 deletions Source/Urho3D/Urho2D/TileMap2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "../Resource/ResourceCache.h"
#include "../Scene/Node.h"
#include "../Scene/Scene.h"
#include "../Scene/SceneEvents.h"
#include "../Urho2D/TileMap2D.h"
#include "../Urho2D/TileMapLayer2D.h"
#include "../Urho2D/TmxFile2D.h"
Expand Down Expand Up @@ -55,6 +56,13 @@ void TileMap2D::RegisterObject(Context* context)
AM_DEFAULT);
}

void TileMap2D::OnNodeSet(Node* node)
{
Scene* scene = GetScene();
if (scene)
SubscribeToEvent(scene, E_SCENEUPDATE , URHO3D_HANDLER(TileMap2D, HandleSceneUpdate));
}

// Transform vector from node-local space to global space
static Vector2 TransformNode2D(const Matrix3x4& transform, Vector2 local)
{
Expand Down Expand Up @@ -191,4 +199,15 @@ Vector<SharedPtr<TileMapObject2D> > TileMap2D::GetTileCollisionShapes(unsigned g
return tmxFile_ ? tmxFile_->GetTileCollisionShapes(gid) : shapes;
}

void TileMap2D::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
{
using namespace SceneUpdate;

float timeStep = eventData[P_TIMESTEP].GetFloat();
tmxFile_->UpdateAnimationTimers(timeStep);

for (unsigned l = 0; l < layers_.Size(); ++l)
layers_[l]->UpdateAnimations();
}

}
8 changes: 8 additions & 0 deletions Source/Urho3D/Urho2D/TileMap2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class URHO3D_API TileMap2D : public Component
/// @nobind
static void RegisterObject(Context* context);

/// Handle node being assigned.
void OnNodeSet(Node* node) override;

/// Visualize the component as debug geometry.
void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) override;

Expand Down Expand Up @@ -79,6 +82,11 @@ class URHO3D_API TileMap2D : public Component
ResourceRef GetTmxFileAttr() const;
///
Vector<SharedPtr<TileMapObject2D> > GetTileCollisionShapes(unsigned gid) const;

private:
/// Handle scene update.
void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);

private:
/// Tmx file.
SharedPtr<TmxFile2D> tmxFile_;
Expand Down
53 changes: 53 additions & 0 deletions Source/Urho3D/Urho2D/TileMapDefs2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,51 @@ const String& PropertySet2D::GetProperty(const String& name) const
return i->second_;
}

FrameSet2D::FrameSet2D() = default;

FrameSet2D::~FrameSet2D() = default;

void FrameSet2D::Load(const XMLElement& element)
{
assert(element.GetName() == "animation");
for (XMLElement frameElem = element.GetChild("frame"); frameElem; frameElem = frameElem.GetNext("frame"))
{
SharedPtr<TileFrameInfo2D> info(new TileFrameInfo2D());
info->gid_ = frameElem.GetUInt("tileid") + 1;
info->duration_ = frameElem.GetUInt("duration");
frames_.Push(info);

lapTime_ += info->duration_;
}

}

void FrameSet2D::UpdateTimer(float timeStep)
{
timeElapsed_ += timeStep * 1000.0f;
if (timeElapsed_ > lapTime_)
timeElapsed_ -= lapTime_;
}

unsigned FrameSet2D::GetCurrentFrameGid() const
{
unsigned minInterval = 0;
unsigned maxInterval = 0;
for (unsigned i = 0; i < frames_.Size(); ++i)
{
minInterval = maxInterval;
maxInterval += frames_[i]->duration_;
if (timeElapsed_ >= static_cast<float>(minInterval) && timeElapsed_ < static_cast<float>(maxInterval))
return frames_[i]->gid_;
}
return frames_[0]->gid_;
}

unsigned FrameSet2D::GetNumFrames() const
{
return frames_.Size();
}

Tile2D::Tile2D() :
gid_(0)
{
Expand All @@ -185,6 +230,14 @@ const String& Tile2D::GetProperty(const String& name) const
return propertySet_->GetProperty(name);
}

bool Tile2D::IsAnimated() const
{
if (!frameSet_)
return false;
else
return true;
}

TileMapObject2D::TileMapObject2D() = default;

unsigned TileMapObject2D::GetNumPoints() const
Expand Down
37 changes: 37 additions & 0 deletions Source/Urho3D/Urho2D/TileMapDefs2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ struct URHO3D_API TileMapInfo2D
bool PositionToTileIndex(int& x, int& y, const Vector2& position) const;
};

struct URHO3D_API TileFrameInfo2D : public RefCounted
{
/// Gid;
unsigned gid_;
/// Duration.
unsigned duration_;
};

/// Tile map layer type.
enum TileMapLayerType2D
{
Expand Down Expand Up @@ -123,6 +131,31 @@ class URHO3D_API PropertySet2D : public RefCounted
HashMap<String, String> nameToValueMapping_;
};

/// Frame set.
class URHO3D_API FrameSet2D : public RefCounted
{
public:
FrameSet2D();
~FrameSet2D() override;

/// Load from XML element.
void Load(const XMLElement& element);
/// Update animation timer.
void UpdateTimer(float timeStep);
/// Get current tile gid of the animation.
unsigned GetCurrentFrameGid() const;
/// Get number of frames.
unsigned GetNumFrames() const;

protected:
/// Animation frame infos.
Vector<SharedPtr<TileFrameInfo2D> > frames_;
/// Time elapsed in the animation (circular timer).
float timeElapsed_{0.0f};
/// Time it takes to complete one animation.
unsigned lapTime_{0};
};

/// Tile flipping flags.
static const unsigned FLIP_HORIZONTAL = 0x80000000u;
static const unsigned FLIP_VERTICAL = 0x40000000u;
Expand Down Expand Up @@ -157,6 +190,8 @@ class URHO3D_API Tile2D : public RefCounted
bool HasProperty(const String& name) const;
/// Return property.
const String& GetProperty(const String& name) const;
/// Checks if tile has animation frames.
bool IsAnimated() const;

private:
friend class TmxTileLayer2D;
Expand All @@ -167,6 +202,8 @@ class URHO3D_API Tile2D : public RefCounted
SharedPtr<Sprite2D> sprite_;
/// Property set.
SharedPtr<PropertySet2D> propertySet_;
/// Frame set.
SharedPtr<FrameSet2D> frameSet_;
};

/// Tile map object.
Expand Down
34 changes: 34 additions & 0 deletions Source/Urho3D/Urho2D/TileMapLayer2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,40 @@ void TileMapLayer2D::SetVisible(bool visible)
}
}

void TileMapLayer2D::UpdateAnimations()
{
if (GetLayerType() != LT_TILE_LAYER)
return;

TmxFile2D* tmxFile = GetTileMap()->GetTmxFile();
for (int y = 0; y < GetHeight(); y++)
{
for (int x = 0; x < GetWidth(); x++)
{
Tile2D* tile = GetTile(x, y);
if (!tile)
continue;

if (tile->IsAnimated())
{
Node* node = GetTileNode(x, y);
if (node)
{
unsigned curGid = tile->GetGid();
FrameSet2D* frameSet = tmxFile->GetTileFrameSet(curGid);
unsigned newGid = frameSet->GetCurrentFrameGid();
if (curGid != newGid)
{
Sprite2D* sprite = tmxFile->GetTileSprite(newGid);
auto* spriteComponent = node->GetComponent<StaticSprite2D>();
spriteComponent->SetSprite(sprite);
}
}
}
}
}
}

TileMap2D* TileMapLayer2D::GetTileMap() const
{
return tileMap_;
Expand Down
3 changes: 3 additions & 0 deletions Source/Urho3D/Urho2D/TileMapLayer2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class URHO3D_API TileMapLayer2D : public Component
/// @property
void SetVisible(bool visible);

/// For tile layers, update animated tile sprites.
void UpdateAnimations();

/// Return tile map.
TileMap2D* GetTileMap() const;

Expand Down
Loading

0 comments on commit cf8e663

Please sign in to comment.