This commit is contained in:
aster 2014-08-08 23:24:04 +08:00
Родитель b87c4f24fe
Коммит 4d840600b3
5 изменённых файлов: 1942 добавлений и 1118 удалений

Разница между файлами не показана из-за своего большого размера Загрузить разницу

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

@ -67,8 +67,12 @@ void TileMapLayer2D::SetTmxLayer(const TmxLayer2D* tmxLayer)
nodes_.Clear();
}
tileLayer_ = 0;
objectGroup_ = 0;
imageLayer_ = 0;
tmxLayer_ = tmxLayer;
if (!tmxLayer_)
return;
@ -114,11 +118,6 @@ void TileMapLayer2D::SetVisible(bool visible)
}
}
TmxLayerType2D TileMapLayer2D::GetLayerType() const
{
return tmxLayer_ ? tmxLayer_->type_ : LT_INVALID;
}
const String& TileMapLayer2D::GetName() const
{
return tmxLayer_ ? tmxLayer_->name_ : String::EMPTY;
@ -134,44 +133,108 @@ int TileMapLayer2D::GetHeight() const
return tmxLayer_ ? tmxLayer_->height_ : 0;
}
bool TileMapLayer2D::HasProperty(const String& name) const
{
if (!tmxLayer_)
return false;
return tmxLayer_->HasProperty(name);
}
const String& TileMapLayer2D::GetProperty(const String& name) const
{
if (!tmxLayer_)
return String::EMPTY;
return tmxLayer_->GetProperty(name);
}
TmxLayerType2D TileMapLayer2D::GetLayerType() const
{
return tmxLayer_ ? tmxLayer_->type_ : LT_INVALID;
}
Node* TileMapLayer2D::GetTileNode(int x, int y) const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_TILE_LAYER)
if (!tileLayer_)
return 0;
if (x < 0 || x >= tileLayer_->width_ || y < 0 || y >= tileLayer_->height_)
return 0;
return nodes_[y * tileLayer_->width_ + x];
}
const TmxTile* TileMapLayer2D::GetTile(int x, int y) const
{
if (!tileLayer_)
return 0;
if (x < 0 || x >= tileLayer_->width_ || y < 0 || y >= tileLayer_->height_)
return 0;
const TmxTileLayer2D* tileLayer = (const TmxTileLayer2D*)tmxLayer_;
if (x < 0 || x >= tileLayer->width_ || y < 0 || y >= tileLayer->height_)
const TmxTile& tile = tileLayer_->tileGids_[y * tileLayer_->width_ + x];
return tile.gid_ == 0 ? 0 : &tile;
}
bool TileMapLayer2D::HasTileProperty(int x, int y, const String& name) const
{
const TmxTile* tile = GetTile(x, y);
return tile && tile->HasProperty(name);
}
const String& TileMapLayer2D::GetTileProperty(int x, int y, const String& name) const
{
const TmxTile* tile = GetTile(x, y);
if (!tile)
return String::EMPTY;
return tile->GetProperty(name);
}
Node* TileMapLayer2D::GetObjectNode(unsigned index) const
{
if (!objectGroup_)
return false;
if (index >= nodes_.Size())
return 0;
return nodes_[y * tileLayer->width_ + x];
return nodes_[index];
}
unsigned TileMapLayer2D::GetNumObjects() const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP)
if (!objectGroup_)
return 0;
return nodes_.Size();
}
const TmxObject* TileMapLayer2D::GetObject(unsigned index) const
const TmxObject2D* TileMapLayer2D::GetObject(unsigned index) const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP)
if (objectGroup_)
return 0;
return (const TmxObject*)nodes_[index]->GetVar("ObjectData").GetVoidPtr();
return &objectGroup_->objects_[index];
}
Node* TileMapLayer2D::GetObjectNode(unsigned index) const
bool TileMapLayer2D::HasObjectProperty(unsigned index, const String& name) const
{
if (!tmxLayer_ || tmxLayer_->type_ != LT_OBJECT_GROUP)
return 0;
const TmxObject2D* object = GetObject(index);
if (!object)
return false;
if (index >= nodes_.Size())
return 0;
return object->HasProperty(name);
}
return nodes_[index];
const String& TileMapLayer2D::GetObjectProperty(unsigned index, const String& name) const
{
const TmxObject2D* object = GetObject(index);
if (!object)
return String::EMPTY;
return object->GetProperty(name);
}
Node* TileMapLayer2D::GetImageNode() const
@ -187,6 +250,8 @@ Node* TileMapLayer2D::GetImageNode() const
void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
{
tmxLayer_ = tileLayer;
int width = tileLayer->width_;
int height = tileLayer->height_;
nodes_.Resize(width * height);
@ -199,12 +264,8 @@ void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
{
for (int x = 0; x < width; ++x)
{
int gid = tileLayer->tileGids_[y * width + x];
if (gid <= 0)
continue;
Sprite2D* sprite = tmxFile->GetTileSprite(gid);
if (!sprite)
const TmxTile& tile = tileLayer->tileGids_[y * width + x];
if (tile.gid_ == 0 || !tile.sprite_)
continue;
SharedPtr<Node> tileNode(GetNode()->CreateChild("Tile"));
@ -212,7 +273,7 @@ void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
tileNode->SetPosition(Vector3(x * tileWidth, y * tileHeight, 0.0f));
StaticSprite2D* staticSprite = tileNode->CreateComponent<StaticSprite2D>();
staticSprite->SetSprite(sprite);
staticSprite->SetSprite(tile.sprite_);
staticSprite->SetLayer(drawOrder_);
staticSprite->SetOrderInLayer((height - 1 - y) * width + x);
@ -221,14 +282,15 @@ void TileMapLayer2D::SetTileLayer(const TmxTileLayer2D* tileLayer)
}
}
void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
{
objectGroup_ = objectGroup;
TmxFile2D* tmxFile = objectGroup->tmxFile_;
for (unsigned i = 0; i < objectGroup->objects_.Size(); ++i)
{
const TmxObject& object = objectGroup->objects_[i];
const TmxObject2D& object = objectGroup->objects_[i];
SharedPtr<Node> objectNode(GetNode()->CreateChild("Object"));
objectNode->SetTemporary(true);
@ -239,11 +301,10 @@ void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
if (object.type_ == OT_TILE)
{
Sprite2D* sprite = tmxFile->GetTileSprite(object.gid_);
if (sprite)
if (object.gid_ && object.sprite_)
{
StaticSprite2D* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
staticSprite->SetSprite(sprite);
staticSprite->SetSprite(object.sprite_);
staticSprite->SetLayer(drawOrder_);
staticSprite->SetOrderInLayer((int)((10.0f - object.y_) * 100.0f));
}
@ -255,6 +316,8 @@ void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
void TileMapLayer2D::SetImageLayer(const TmxImageLayer2D* imageLayer)
{
imageLayer_ = imageLayer;
if (!imageLayer->sprite_)
return;

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

@ -56,24 +56,40 @@ public:
int GetDrawOrder() const { return drawOrder_; }
/// Return visible.
bool IsVisible() const { return visible_; }
/// Return layer type.
TmxLayerType2D GetLayerType() const;
/// Return name.
const String& GetName() const;
/// Return width.
int GetWidth() const;
/// Return height.
int GetHeight() const;
/// Return has property
bool HasProperty(const String& name) const;
/// Return property.
const String& GetProperty(const String& name) const;
/// Return layer type.
TmxLayerType2D GetLayerType() const;
/// 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 (for tile layer only).
const TmxTile* GetTile(int x, int y) const;
/// Return tile has property.
bool HasTileProperty(int x, int y, const String& name) const;
/// Return tile property.
const String& GetTileProperty(int x, int y, const String& name) const;
/// Return tile node (for object group only).
Node* GetObjectNode(unsigned index) const;
/// Return number of tile nodes (for object group only).
unsigned GetNumObjects() const;
/// Return object (for object group only).
const TmxObject2D* GetObject(unsigned index) const;
/// Return object has property.
bool HasObjectProperty(unsigned index, const String& name) const;
/// Return object property.
const String& GetObjectProperty(unsigned index, const String& name) const;
/// Return image node (for image layer only).
Node* GetImageNode() const;
@ -87,6 +103,12 @@ private:
/// Tmx layer.
const TmxLayer2D* tmxLayer_;
/// Tile layer.
const TmxTileLayer2D* tileLayer_;
/// Object group.
const TmxObjectGroup2D* objectGroup_;
/// Image layer.
const TmxImageLayer2D* imageLayer_;
/// Draw order.
int drawOrder_;
/// Visible.

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

@ -38,6 +38,19 @@ namespace Urho3D
extern const float PIXEL_SIZE;
bool TmxProperties2D::HasProperty(const String& name) const
{
return properties_.Find(name) != properties_.End();
}
const String& TmxProperties2D::GetProperty(const String& name) const
{
HashMap<String, String>::ConstIterator i = properties_.Find(name);
if (i == properties_.End())
return String::EMPTY;
return i->second_;
}
TmxLayer2D::TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type) :
tmxFile_(tmxFile),
type_(type)
@ -49,11 +62,47 @@ TmxLayer2D::~TmxLayer2D()
{
}
TmxTile::TmxTile() :
gid_(0),
properties_(0)
{
}
bool TmxTile::HasProperty(const String& name) const
{
if (!properties_)
return false;
return properties_->HasProperty(name);
}
const String& TmxTile::GetProperty(const String& name) const
{
if (!properties_)
return String::EMPTY;
return properties_->GetProperty(name);
}
TmxTileLayer2D::TmxTileLayer2D(TmxFile2D* tmxFile) :
TmxLayer2D(tmxFile, LT_TILE_LAYER)
{
}
unsigned TmxObject2D::GetNumPoints() const
{
return points_.Size();
}
const Vector2& TmxObject2D::GetPoint(unsigned index) const
{
if (index >= points_.Size())
return Vector2::ZERO;
return points_[index];
}
TmxObjectGroup2D::TmxObjectGroup2D(TmxFile2D* tmxFile) :
TmxLayer2D(tmxFile, LT_OBJECT_GROUP)
{
@ -194,24 +243,6 @@ const TmxLayer2D* TmxFile2D::GetLayerByName(const String& name) const
return 0;
}
Sprite2D* TmxFile2D::GetTileSprite(int gid) const
{
HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = tileSprites_.Find(gid);
if (i == tileSprites_.End())
return 0;
return i->second_;
}
const HashMap<String, String>* TmxFile2D::GetTileProperties(int gid) const
{
HashMap<int, HashMap<String, String> >::ConstIterator i = tileProperties_.Find(gid);
if (i == tileProperties_.End())
return 0;
return &(i->second_);
}
bool TmxFile2D::LoadTileSet(const XMLElement& element)
{
XMLElement imageElem = element.GetChild("image");
@ -253,7 +284,7 @@ bool TmxFile2D::LoadTileSet(const XMLElement& element)
{
if (tileElem.HasChild("properties"))
{
HashMap<String, String> properties;
TmxProperties2D properties;
LoadProperties(tileElem.GetChild("properties"), properties);
tileProperties_[firstgid + tileElem.GetInt("id")] = properties;
}
@ -295,16 +326,37 @@ bool TmxFile2D::LoadLayer(const XMLElement& element)
int gid = tileElem.GetInt("gid");
tileElem = tileElem.GetNext("tile");
tileLayer->tileGids_[y * tileLayer->width_ + x] = gid;
TmxTile tile;
tile.gid_ = gid;
tile.sprite_ = GetTileSprite(gid);
tile.properties_ = GetTileProperties(gid);
tileLayer->tileGids_[y * tileLayer->width_ + x] = tile;
}
}
if (element.HasChild("properties"))
LoadProperties(element.GetChild("properties"), tileLayer->properties_);
LoadProperties(element.GetChild("properties"), *tileLayer);
return true;
}
Sprite2D* TmxFile2D::GetTileSprite(int gid) const
{
HashMap<int, SharedPtr<Sprite2D> >::ConstIterator i = tileSprites_.Find(gid);
if (i == tileSprites_.End())
return 0;
return i->second_;
}
const TmxProperties2D* TmxFile2D::GetTileProperties(int gid) const
{
HashMap<int, TmxProperties2D>::ConstIterator i = tileProperties_.Find(gid);
if (i == tileProperties_.End())
return 0;
return &(i->second_);
}
bool TmxFile2D::LoadObjectGroup(const XMLElement& element)
{
TmxObjectGroup2D* objectGroup = new TmxObjectGroup2D(this);
@ -316,8 +368,8 @@ bool TmxFile2D::LoadObjectGroup(const XMLElement& element)
for (XMLElement objectElem = element.GetChild("object"); objectElem; objectElem = objectElem.GetNext("object"))
{
objectGroup->objects_.Push(TmxObject());
TmxObject& object = objectGroup->objects_.Back();
objectGroup->objects_.Push(TmxObject2D());
TmxObject2D& object = objectGroup->objects_.Back();
object.x_ = objectElem.GetInt("x") * PIXEL_SIZE;
// Flip y
@ -339,6 +391,7 @@ bool TmxFile2D::LoadObjectGroup(const XMLElement& element)
{
object.type_ = OT_TILE;
object.gid_ = objectElem.GetInt("gid");
object.sprite_ = GetTileSprite(object.gid_);
}
else
{
@ -363,11 +416,11 @@ bool TmxFile2D::LoadObjectGroup(const XMLElement& element)
}
if (objectElem.HasChild("properties"))
LoadProperties(objectElem.GetChild("properties"), object.properties_);
LoadProperties(objectElem.GetChild("properties"), object);
}
if (element.HasChild("properties"))
LoadProperties(element.GetChild("properties"), objectGroup->properties_);
LoadProperties(element.GetChild("properties"), *objectGroup);
return true;
}
@ -401,7 +454,7 @@ bool TmxFile2D::LoadImageLayer(const XMLElement& element)
imageLayer->sprite_ = sprite;
if (element.HasChild("properties"))
LoadProperties(element.GetChild("properties"), imageLayer->properties_);
LoadProperties(element.GetChild("properties"), *imageLayer);
return true;
}
@ -417,10 +470,10 @@ void TmxFile2D::LoadLayerInfo(const XMLElement& element, TmxLayer2D* layer)
layer->visible_ = true;
}
void TmxFile2D::LoadProperties(const XMLElement& element, HashMap<String, String>& peoperties)
void TmxFile2D::LoadProperties(const XMLElement& element, TmxProperties2D& properties)
{
for (XMLElement propertyElem = element.GetChild("property"); propertyElem; propertyElem = propertyElem.GetNext("property"))
peoperties[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
properties.properties_[propertyElem.GetAttribute("name")] = propertyElem.GetAttribute("value");
}

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

@ -35,6 +35,18 @@ class TmxFile2D;
class XMLElement;
class XMLFile;
/// Tmx peroperies.
struct URHO3D_API TmxProperties2D
{
/// Return has property (use for script).
bool HasProperty(const String& name) const;
/// Return property value (use for script).
const String& GetProperty(const String& name) const;
/// Properties.
HashMap<String, String> properties_;
};
/// Tmx layer type.
enum TmxLayerType2D
{
@ -49,7 +61,7 @@ enum TmxLayerType2D
};
/// Tmx layer.
struct URHO3D_API TmxLayer2D
struct URHO3D_API TmxLayer2D : TmxProperties2D
{
TmxLayer2D(TmxFile2D* tmxFile, TmxLayerType2D type);
virtual ~TmxLayer2D();
@ -66,8 +78,24 @@ struct URHO3D_API TmxLayer2D
int height_;
/// Visible.
bool visible_;
};
/// Tmx tile.
struct URHO3D_API TmxTile
{
TmxTile();
/// Return has property.
bool HasProperty(const String& name) const;
/// Return property.
const String& GetProperty(const String& name) const;
/// Gid.
int gid_;
/// Sprite.
SharedPtr<Sprite2D> sprite_;
/// Properties.
HashMap<String, String> properties_;
const TmxProperties2D* properties_;
};
/// Tmx tile layer.
@ -75,7 +103,8 @@ struct URHO3D_API TmxTileLayer2D : TmxLayer2D
{
TmxTileLayer2D(TmxFile2D* tmxFile);
PODVector<int> tileGids_;
/// Tile.
Vector<TmxTile> tileGids_;
};
/// Object type.
@ -94,8 +123,13 @@ enum TmxObjectType
};
/// Tmx object.
struct URHO3D_API TmxObject
struct URHO3D_API TmxObject2D : TmxProperties2D
{
/// Return number of points (use for script).
unsigned GetNumPoints() const;
/// Return point at index (use for script).
const Vector2& GetPoint(unsigned index) const;
/// Object type.
TmxObjectType type_;
/// Position.
@ -104,10 +138,11 @@ struct URHO3D_API TmxObject
float width_, height_;
/// Points(for polygon and polyline).
Vector<Vector2> points_;
/// Gid (for tile).
int gid_;
/// Properties.
HashMap<String, String> properties_;
/// Sprite (for tile).
SharedPtr<Sprite2D> sprite_;
};
/// Tmx image layer.
@ -116,7 +151,7 @@ struct URHO3D_API TmxObjectGroup2D : TmxLayer2D
TmxObjectGroup2D(TmxFile2D* tmxFile);
/// Objects.
Vector<TmxObject> objects_;
Vector<TmxObject2D> objects_;
};
/// Tmx image layer.
@ -160,16 +195,16 @@ public:
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.
const HashMap<String, String>* GetTileProperties(int gid) const;
private:
/// Load tile set.
bool LoadTileSet(const XMLElement& element);
/// Load layer.
bool LoadLayer(const XMLElement& element);
/// Return tile sprite by gid.
Sprite2D* GetTileSprite(int gid) const;
/// Return tile properties by gid.
const TmxProperties2D* GetTileProperties(int gid) const;
/// Load object group.
bool LoadObjectGroup(const XMLElement& element);
/// Load image layer.
@ -177,7 +212,7 @@ private:
/// Load layer info.
void LoadLayerInfo(const XMLElement& element, TmxLayer2D* layer);
/// Load properties.
void LoadProperties(const XMLElement& element, HashMap<String, String>& peoperties);
void LoadProperties(const XMLElement& element, TmxProperties2D& peoperties);
/// XML file used during loading.
SharedPtr<XMLFile> loadXMLFile_;
@ -194,7 +229,7 @@ private:
/// Gid to tile sprite mapping.
HashMap<int, SharedPtr<Sprite2D> > tileSprites_;
/// Gid to tile properties mapping.
HashMap<int, HashMap<String, String> > tileProperties_;
HashMap<int, TmxProperties2D> tileProperties_;
/// Layers.
Vector<TmxLayer2D*> layers_;
};