Move some functions from header file to source file. For all of objects create node for it.

This commit is contained in:
aster 2014-08-07 22:23:38 +08:00
Родитель 573a0abe3d
Коммит b87c4f24fe
6 изменённых файлов: 108 добавлений и 44 удалений

Просмотреть файл

@ -60,10 +60,7 @@ void TileMap2D::SetTmxFile(TmxFile2D* tmxFile)
if (tmxFile_)
{
for (unsigned i = 0; i < layers_.Size(); ++i)
{
if (layers_[i])
layers_[i]->GetNode()->Remove();
}
layers_[i]->GetNode()->Remove();
layers_.Clear();
}
@ -124,10 +121,10 @@ TileMapLayer2D* TileMap2D::GetLayer(unsigned index) const
return layers_[index];
}
TileMapLayer2D* TileMap2D::GetLayer(const String& name) const
TileMapLayer2D* TileMap2D::GetLayerByName(const String& name) const
{
for (unsigned i = 0; i < layers_.Size(); ++i)
if (layers_[i] && name == layers_[i]->GetName())
if (name == layers_[i]->GetName())
return layers_[i];
return 0;

Просмотреть файл

@ -61,7 +61,7 @@ public:
/// Return tile map layer at index.
TileMapLayer2D* GetLayer(unsigned index) const;
/// Return tile map layer by name.
TileMapLayer2D* GetLayer(const String& name) const;
TileMapLayer2D* GetLayerByName(const String& name) const;
/// Set tile map file attribute.
void SetTmxFileAttr(ResourceRef value);

Просмотреть файл

@ -91,8 +91,12 @@ void TileMapLayer2D::SetDrawOrder(int drawOrder)
for (unsigned i = 0; i < nodes_.Size(); ++i)
{
if (nodes_[i])
nodes_[i]->GetComponent<StaticSprite2D>()->SetLayer(drawOrder_);
if (!nodes_[i])
continue;
StaticSprite2D* staticSprite = nodes_[i]->GetComponent<StaticSprite2D>();
if (staticSprite)
staticSprite->SetLayer(drawOrder_);
}
}
@ -142,6 +146,34 @@ Node* TileMapLayer2D::GetTileNode(int x, int y) const
return nodes_[y * tileLayer->width_ + x];
}
unsigned TileMapLayer2D::GetNumObjects() const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP)
return 0;
return nodes_.Size();
}
const TmxObject* TileMapLayer2D::GetObject(unsigned index) const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP)
return 0;
return (const TmxObject*)nodes_[index]->GetVar("ObjectData").GetVoidPtr();
}
Node* TileMapLayer2D::GetObjectNode(unsigned index) const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP)
return 0;
if (index >= nodes_.Size())
return 0;
return nodes_[index];
}
Node* TileMapLayer2D::GetImageNode() const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_IMAGE_LAYER)
@ -197,26 +229,27 @@ void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
for (unsigned i = 0; i < objectGroup->objects_.Size(); ++i)
{
const TmxObject& object = objectGroup->objects_[i];
if (object.type_ != OT_TILE)
continue;
if (object.gid_ <= 0)
continue;
SharedPtr<Node> objectNode(GetNode()->CreateChild("Object"));
objectNode->SetTemporary(true);
objectNode->SetPosition(Vector3(object.x_, object.y_, 0.0f));
Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_);
if (!sprite)
continue;
// Set object data
objectNode->SetVar("ObjectData", (void*)&object);
SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
tileNode->SetTemporary(true);
tileNode->SetPosition(Vector3(object.x_, object.y_, 0.0f));
StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
staticSprite->SetSprite(sprite);
staticSprite->SetLayer(drawOrder_);
staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f));
nodes_.Push(tileNode);
if (object.type_ == OT_TILE)
{
Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_);
if (sprite)
{
StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
staticSprite->SetSprite(sprite);
staticSprite->SetLayer(drawOrder_);
staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f));
}
}
nodes_.Push(objectNode);
}
}

Просмотреть файл

@ -68,6 +68,12 @@ public:
/// Return tile node (for tile layer only).
Node* GetTileNode(int x, int y) const;
/// Return number of tile nodes (for object group only).
unsigned GetNumObjects() const;
/// Return object.
const TmxObject* GetObject(unsigned index) const;
/// Return tile node (for object group only).
Node* GetObjectNode(unsigned index) const;
/// Return image node (for image layer only).
Node* GetImageNode() const;

Просмотреть файл

@ -38,6 +38,33 @@ namespace Urho3D
extern const float PIXEL_SIZE;
TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type) :
tmxFile_(tmxFile),
type_(type)
{
}
TmxLayer2D::~TmxLayer2D()
{
}
TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) :
TmxLayer2D(tmxFile, LT_TILE_LAYER)
{
}
TmxObjectGroup2D::TmxObjectGroup2D(TmxFile2D* tmxFile) :
TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
{
}
TmxImageLayer2D::TmxImageLayer2D(TmxFile2D* tmxFile) :
TmxLayer2D(tmxFile, LT_IMAGE_LAYER)
{
}
TmxFile2D::TmxFile2D(Context* context) :
Resource(context),
width_(0),
@ -156,6 +183,17 @@ const TmxLayer2D* TmxFile2D::GetLayer(unsigned index) const
return layers_[index];
}
const TmxLayer2D* TmxFile2D::GetLayerByName(const String& name) const
{
for (unsigned i = 0; i < layers_.Size(); ++i)
{
if (name == layers_[i]->name_)
return layers_[i];
}
return 0;
}
Sprite2D* TmxFile2D::GetTileSprite(int gid) const
{
HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = tileSprites_.Find(gid);
@ -385,4 +423,5 @@ void TmxFile2D::LoadProperties(const XMLElement& element, HashMap<String, String
peoperties[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
}
}

Просмотреть файл

@ -51,15 +51,8 @@ enum TmxLayerType2D
/// Tmx layer.
struct URHO3D_API TmxLayer2D
{
TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type) :
tmxFile_(tmxFile),
type_(type)
{
}
virtual ~TmxLayer2D()
{
}
TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type);
virtual ~TmxLayer2D();
/// Tmx file.
WeakPtr<TmxFile2D> tmxFile_;
@ -80,9 +73,7 @@ struct URHO3D_API TmxLayer2D
/// Tmx tile layer.
struct URHO3D_API TmxTileLayer2D : TmxLayer2D
{
TmxTileLayer2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_TILE_LAYER)
{
}
TmxTileLayer2D(TmxFile2D* tmxFile);
PODVector<int> tileGids_;
};
@ -122,9 +113,7 @@ struct URHO3D_API TmxObject
/// Tmx image layer.
struct URHO3D_API TmxObjectGroup2D : TmxLayer2D
{
TmxObjectGroup2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
{
}
TmxObjectGroup2D(TmxFile2D* tmxFile);
/// Objects.
Vector<TmxObject> objects_;
@ -133,9 +122,7 @@ struct URHO3D_API TmxObjectGroup2D : TmxLayer2D
/// Tmx image layer.
struct URHO3D_API TmxImageLayer2D : TmxLayer2D
{
TmxImageLayer2D(TmxFile2D* tmxFile) : TmxLayer2D(tmxFile, LT_IMAGE_LAYER)
{
}
TmxImageLayer2D(TmxFile2D* tmxFile);
/// Sprite.
SharedPtr<Sprite2D> sprite_;
@ -171,6 +158,8 @@ public:
unsigned GetNumLayers() const { return layers_.Size(); }
/// Return layer at index.
const TmxLayer2D* GetLayer(unsigned index) const;
/// Return layer by name.
const TmxLayer2D* GetLayerByName(const String& name) const;
/// Return tile sprite by gid.
Sprite2D* GetTileSprite(int gid) const;
/// Return tile properties by gid.