Browse Source

Add tile map AngelScript binding and sample.

aster 11 years ago
parent
commit
56c677c759

+ 0 - 1
Bin/Data/LuaScripts/36_Urho2DTileMap.lua

@@ -4,7 +4,6 @@
 --     - Displaying the scene using the Renderer subsystem
 --     - Displaying the scene using the Renderer subsystem
 --     - Handling keyboard to move and zoom 2D camera
 --     - Handling keyboard to move and zoom 2D camera
 
 
-
 require "LuaScripts/Utilities/Sample"
 require "LuaScripts/Utilities/Sample"
 
 
 function Start()
 function Start()

+ 7 - 7
Source/Engine/LuaScript/pkgs/Urho2D/TileMapLayer2D.pkg

@@ -7,26 +7,26 @@ class TileMapLayer2D : Component
 
 
     int GetDrawOrder() const;
     int GetDrawOrder() const;
     bool IsVisible() const;
     bool IsVisible() const;
-    int GetWidth() const;
-    int GetHeight() const;
     bool HasProperty(const String& name) const;
     bool HasProperty(const String& name) const;
     const String& GetProperty(const String& name) const;
     const String& GetProperty(const String& name) const;
     TileMapLayerType2D GetLayerType() const;
     TileMapLayerType2D GetLayerType() const;
+
+    int GetWidth() const;
+    int GetHeight() const;
     Node* GetTileNode(int x, int y) const;
     Node* GetTileNode(int x, int y) const;
     Tile2D* GetTile(int x, int y) const;
     Tile2D* GetTile(int x, int y) const;
-    unsigned GetNumObjectNodes() const;
-    Node* GetObjectNode(unsigned index) const;
+    
     unsigned GetNumObjects() const;
     unsigned GetNumObjects() const;
     TileObject2D* GetObject(unsigned index) const;
     TileObject2D* GetObject(unsigned index) const;
-    Node* GetImageNode() const;
+    Node* GetObjectNode(unsigned index) const;
 
 
+    Node* GetImageNode() const;
 
 
     tolua_readonly tolua_property__get_set int drawOrder;
     tolua_readonly tolua_property__get_set int drawOrder;
     tolua_readonly tolua_property__is_set bool visible;
     tolua_readonly tolua_property__is_set bool visible;
+    tolua_readonly tolua_property__get_set TileMapLayerType2D layerType;
     tolua_readonly tolua_property__get_set int width;
     tolua_readonly tolua_property__get_set int width;
     tolua_readonly tolua_property__get_set int height;
     tolua_readonly tolua_property__get_set int height;
-    tolua_readonly tolua_property__get_set TileMapLayerType2D layerType;
-    tolua_readonly tolua_property__get_set unsigned numObjectNodes;
     tolua_readonly tolua_property__get_set unsigned numObjects;
     tolua_readonly tolua_property__get_set unsigned numObjects;
     tolua_readonly tolua_property__get_set Node* imageNode;
     tolua_readonly tolua_property__get_set Node* imageNode;
 };
 };

+ 93 - 2
Source/Engine/Script/Urho2DAPI.cpp

@@ -21,9 +21,9 @@
 //
 //
 
 
 #include "Precompiled.h"
 #include "Precompiled.h"
+#include "AnimatedSprite2D.h"
 #include "Animation2D.h"
 #include "Animation2D.h"
 #include "AnimationSet2D.h"
 #include "AnimationSet2D.h"
-#include "AnimatedSprite2D.h"
 #include "APITemplates.h"
 #include "APITemplates.h"
 #include "CollisionBox2D.h"
 #include "CollisionBox2D.h"
 #include "CollisionChain2D.h"
 #include "CollisionChain2D.h"
@@ -40,9 +40,9 @@
 #include "ConstraintPrismatic2D.h"
 #include "ConstraintPrismatic2D.h"
 #include "ConstraintPulley2D.h"
 #include "ConstraintPulley2D.h"
 #include "ConstraintRevolute2D.h"
 #include "ConstraintRevolute2D.h"
+#include "ConstraintRope2D.h"
 #include "ConstraintWeld2D.h"
 #include "ConstraintWeld2D.h"
 #include "ConstraintWheel2D.h"
 #include "ConstraintWheel2D.h"
-#include "ConstraintRope2D.h"
 #include "Drawable2D.h"
 #include "Drawable2D.h"
 #include "ParticleEffect2D.h"
 #include "ParticleEffect2D.h"
 #include "ParticleEmitter2D.h"
 #include "ParticleEmitter2D.h"
@@ -52,6 +52,10 @@
 #include "Sprite2D.h"
 #include "Sprite2D.h"
 #include "SpriteSheet2D.h"
 #include "SpriteSheet2D.h"
 #include "StaticSprite2D.h"
 #include "StaticSprite2D.h"
+#include "TileMap2D.h"
+#include "TileMapDefs2D.h"
+#include "TileMapLayer2D.h"
+#include "TmxFile2D.h"
 
 
 #ifdef _MSC_VER
 #ifdef _MSC_VER
 #pragma warning(disable:4345)
 #pragma warning(disable:4345)
@@ -189,6 +193,88 @@ static void RegisterParticleEmitter2D(asIScriptEngine* engine)
     engine->RegisterObjectMethod("ParticleEmitter2D", "ParticleEffect2D@+ get_effect() const", asMETHOD(ParticleEmitter2D, GetEffect), asCALL_THISCALL);
     engine->RegisterObjectMethod("ParticleEmitter2D", "ParticleEffect2D@+ get_effect() const", asMETHOD(ParticleEmitter2D, GetEffect), asCALL_THISCALL);
 }
 }
 
 
+static void RegisterTileMapDefs2D(asIScriptEngine* engine)
+{
+    engine->RegisterEnum("TileMapLayerType2D");
+    engine->RegisterEnumValue("TileMapLayerType2D", "LT_TILE_LAYER", LT_TILE_LAYER);
+    engine->RegisterEnumValue("TileMapLayerType2D", "LT_OBJECT_GROUP", LT_OBJECT_GROUP);
+    engine->RegisterEnumValue("TileMapLayerType2D", "LT_IMAGE_LAYER", LT_IMAGE_LAYER);
+    engine->RegisterEnumValue("TileMapLayerType2D", "LT_INVALID", LT_INVALID);
+
+    engine->RegisterEnum("TileObjectType2D");
+    engine->RegisterEnumValue("TileObjectType2D", "OT_RECTANGLE", OT_RECTANGLE);
+    engine->RegisterEnumValue("TileObjectType2D", "OT_ELLIPSE", OT_ELLIPSE);
+    engine->RegisterEnumValue("TileObjectType2D", "OT_POLYGON", OT_POLYGON);
+    engine->RegisterEnumValue("TileObjectType2D", "OT_POLYLINE", OT_POLYLINE);
+    engine->RegisterEnumValue("TileObjectType2D", "OT_TILE", OT_TILE);
+    engine->RegisterEnumValue("TileObjectType2D", "OT_INVALID", OT_INVALID);
+
+    RegisterRefCounted<PropertySet2D>(engine, "PropertySet2D");
+    engine->RegisterObjectMethod("PropertySet2D", "bool HasProperty(const String&in) const", asMETHOD(PropertySet2D, HasProperty), asCALL_THISCALL);
+    engine->RegisterObjectMethod("PropertySet2D", "const String& GetProperty(const String&in) const", asMETHOD(PropertySet2D, HasProperty), asCALL_THISCALL);
+
+    RegisterRefCounted<Tile2D>(engine, "Tile2D");
+    engine->RegisterObjectMethod("Tile2D", "int get_gid() const", asMETHOD(Tile2D, GetGid), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Tile2D", "Sprite2D@ get_sprite() const", asMETHOD(Tile2D, GetSprite), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Tile2D", "bool HasProperty(const String&in) const", asMETHOD(Tile2D, HasProperty), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Tile2D", "const String& GetProperty(const String&in) const", asMETHOD(Tile2D, HasProperty), asCALL_THISCALL);
+
+    RegisterRefCounted<TileObject2D>(engine, "TileObject2D");
+    engine->RegisterObjectMethod("TileObject2D", "TileObjectType2D get_type() const", asMETHOD(TileObject2D, GetType), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "const Vector2& get_position() const", asMETHOD(TileObject2D, GetPosition), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "const Vector2& get_size() const", asMETHOD(TileObject2D, GetSize), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "uint get_numPoints() const", asMETHOD(TileObject2D, GetNumPoints), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "const Vector2& GetPoint(uint) const", asMETHOD(TileObject2D, GetPoint), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "int get_tileGid() const", asMETHOD(TileObject2D, GetTileGid), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "Sprite2D@ get_tileSprite() const", asMETHOD(TileObject2D, GetTileSprite), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "bool HasProperty(const String&in) const", asMETHOD(TileObject2D, HasProperty), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileObject2D", "const String& GetProperty(const String&in) const", asMETHOD(TileObject2D, HasProperty), asCALL_THISCALL);
+}
+
+static void RegisterTmxFile2D(asIScriptEngine* engine)
+{
+    RegisterResource<TmxFile2D>(engine, "TmxFile2D");
+}
+
+static void RegisterTileMapLayer2D(asIScriptEngine* engine)
+{
+    RegisterComponent<TileMap2D>(engine, "TileMap2D");
+    RegisterComponent<TileMapLayer2D>(engine, "TileMapLayer2D");
+    engine->RegisterObjectMethod("TileMapLayer2D", "void set_drawOrder(int)", asMETHOD(TileMapLayer2D, SetDrawOrder), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "int get_rrawOrder() const", asMETHOD(TileMapLayer2D, GetDrawOrder), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "void set_visible()", asMETHOD(TileMapLayer2D, SetVisible), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "bool get_visible() const", asMETHOD(TileMapLayer2D, IsVisible), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "bool HasProperty(const String&in) const", asMETHOD(TileMapLayer2D, HasProperty), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "const String& GetProperty(const String&in) const", asMETHOD(TileMapLayer2D, HasProperty), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "TileMapLayerType2D get_layerType() const", asMETHOD(TileMapLayer2D, GetLayerType), asCALL_THISCALL);
+
+    // For tile layer only
+    engine->RegisterObjectMethod("TileMapLayer2D", "int get_width() const", asMETHOD(TileMapLayer2D, GetWidth), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "int get_height() const", asMETHOD(TileMapLayer2D, GetHeight), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "Tile2D@ GetTile(int, int) const", asMETHOD(TileMapLayer2D, GetTile), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "Node@ GetTileNode(int, int) const", asMETHOD(TileMapLayer2D, GetTileNode), asCALL_THISCALL);
+
+    // For object group only
+    engine->RegisterObjectMethod("TileMapLayer2D", "uint get_numObjects() const", asMETHOD(TileMapLayer2D, GetNumObjects), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "TileObject2D@ GetObject(uint) const", asMETHOD(TileMapLayer2D, GetObject), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMapLayer2D", "Node@ GetObjectNode(uint) const", asMETHOD(TileMapLayer2D, GetObjectNode), asCALL_THISCALL);
+    
+    // For image layer only
+    engine->RegisterObjectMethod("TileMapLayer2D", "Node@ get_imageNode() const", asMETHOD(TileMapLayer2D, GetImageNode), asCALL_THISCALL);
+}
+
+static void RegisterTileMap2D(asIScriptEngine* engine)
+{
+    engine->RegisterObjectMethod("TileMap2D", "void set_tmxFile(TmxFile2D@)", asMETHOD(TileMap2D, SetTmxFile), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "TmxFile2D@ get_tmxFile() const", asMETHOD(TileMap2D, GetTmxFile), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "int get_width() const", asMETHOD(TileMap2D, GetWidth), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "int get_height() const", asMETHOD(TileMap2D, GetHeight), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "float get_tileWidth() const", asMETHOD(TileMap2D, GetTileWidth), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "float get_tileHeight() const", asMETHOD(TileMap2D, GetTileHeight), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "uint get_numLayers() const", asMETHOD(TileMap2D, GetNumLayers), asCALL_THISCALL);
+    engine->RegisterObjectMethod("TileMap2D", "TileMapLayer2D@ GetLayer(uint) const", asMETHOD(TileMap2D, GetLayer), asCALL_THISCALL);
+}
+
 static void RegisterRigidBody2D(asIScriptEngine* engine)
 static void RegisterRigidBody2D(asIScriptEngine* engine)
 {
 {
     engine->RegisterEnum("BodyType2D");
     engine->RegisterEnum("BodyType2D");
@@ -626,6 +712,11 @@ void RegisterUrho2DAPI(asIScriptEngine* engine)
     RegisterParticleEffect2D(engine);
     RegisterParticleEffect2D(engine);
     RegisterParticleEmitter2D(engine);
     RegisterParticleEmitter2D(engine);
 
 
+    RegisterTileMapDefs2D(engine);
+    RegisterTmxFile2D(engine);
+    RegisterTileMapLayer2D(engine);
+    RegisterTileMap2D(engine);
+
     RegisterRigidBody2D(engine);
     RegisterRigidBody2D(engine);
     RegisterPhysicsWorld2D(engine);
     RegisterPhysicsWorld2D(engine);
 
 

+ 24 - 32
Source/Engine/Urho2D/TileMapLayer2D.cpp

@@ -129,16 +129,6 @@ void TileMapLayer2D::SetVisible(bool visible)
     }
     }
 }
 }
 
 
-int TileMapLayer2D::GetWidth() const
-{
-    return tmxLayer_ ? tmxLayer_->GetWidth(): 0;
-}
-
-int TileMapLayer2D::GetHeight() const
-{
-    return tmxLayer_ ? tmxLayer_->GetHeight(): 0;
-}
-
 bool TileMapLayer2D::HasProperty(const String& name) const
 bool TileMapLayer2D::HasProperty(const String& name) const
 {
 {
     if (!tmxLayer_)
     if (!tmxLayer_)
@@ -159,42 +149,33 @@ TileMapLayerType2D TileMapLayer2D::GetLayerType() const
     return tmxLayer_ ? tmxLayer_->GetType() : LT_INVALID;
     return tmxLayer_ ? tmxLayer_->GetType() : LT_INVALID;
 }
 }
 
 
-Node* TileMapLayer2D::GetTileNode(int x, int y) const
+int TileMapLayer2D::GetWidth() const
 {
 {
-    if (!tileLayer_)
-        return 0;
-    
-    if (x < 0 || x >= tileLayer_->GetWidth() || y < 0 || y >= tileLayer_->GetHeight())
-        return 0;
-    
-    return nodes_[y * tileLayer_->GetWidth() + x];
+    return tmxLayer_ ? tmxLayer_->GetWidth(): 0;
 }
 }
 
 
-Tile2D* TileMapLayer2D::GetTile(int x, int y) const
+int TileMapLayer2D::GetHeight() const
 {
 {
-    if (!tileLayer_)
-        return 0;
-    
-    return tileLayer_->GetTile(x, y);
+    return tmxLayer_ ? tmxLayer_->GetHeight(): 0;
 }
 }
 
 
-unsigned TileMapLayer2D::GetNumObjectNodes() const
+Tile2D* TileMapLayer2D::GetTile(int x, int y) const
 {
 {
-    if (!objectGroup_)
+    if (!tileLayer_)
         return 0;
         return 0;
 
 
-    return nodes_.Size();
+    return tileLayer_->GetTile(x, y);
 }
 }
 
 
-Node* TileMapLayer2D::GetObjectNode(unsigned index) const
+Node* TileMapLayer2D::GetTileNode(int x, int y) const
 {
 {
-    if (!objectGroup_)
+    if (!tileLayer_)
         return 0;
         return 0;
-
-    if (index >= nodes_.Size())
+    
+    if (x < 0 || x >= tileLayer_->GetWidth() || y < 0 || y >= tileLayer_->GetHeight())
         return 0;
         return 0;
-
-    return nodes_[index];
+    
+    return nodes_[y * tileLayer_->GetWidth() + x];
 }
 }
 
 
 unsigned TileMapLayer2D::GetNumObjects() const
 unsigned TileMapLayer2D::GetNumObjects() const
@@ -213,6 +194,17 @@ TileObject2D* TileMapLayer2D::GetObject(unsigned index) const
     return objectGroup_->GetObject(index);
     return objectGroup_->GetObject(index);
 }
 }
 
 
+Node* TileMapLayer2D::GetObjectNode(unsigned index) const
+{
+    if (!objectGroup_)
+        return 0;
+
+    if (index >= nodes_.Size())
+        return 0;
+
+    return nodes_[index];
+}
+
 Node* TileMapLayer2D::GetImageNode() const
 Node* TileMapLayer2D::GetImageNode() const
 {
 {
     if (!imageLayer_)
     if (!imageLayer_)

+ 6 - 8
Source/Engine/Urho2D/TileMapLayer2D.h

@@ -60,10 +60,6 @@ public:
     int GetDrawOrder() const { return drawOrder_; }
     int GetDrawOrder() const { return drawOrder_; }
     /// Return visible.
     /// Return visible.
     bool IsVisible() const { return visible_; }
     bool IsVisible() const { return visible_; }
-    /// Return width.
-    int GetWidth() const;
-    /// Return height.
-    int GetHeight() const;
     /// Return has property
     /// Return has property
     bool HasProperty(const String& name) const;
     bool HasProperty(const String& name) const;
     /// Return property.
     /// Return property.
@@ -71,19 +67,21 @@ public:
     /// Return layer type.
     /// Return layer type.
     TileMapLayerType2D GetLayerType() const;
     TileMapLayerType2D GetLayerType() const;
 
 
+    /// Return width (for tile layer only).
+    int GetWidth() const;
+    /// Return height (for tile layer only).
+    int GetHeight() const;
     /// Return tile node (for tile layer only).
     /// Return tile node (for tile layer only).
     Node* GetTileNode(int x, int y) const;
     Node* GetTileNode(int x, int y) const;
     /// Return tile (for tile layer only).
     /// Return tile (for tile layer only).
     Tile2D* GetTile(int x, int y) const;
     Tile2D* GetTile(int x, int y) const;
 
 
-    /// Return number of object nodes (for object group only).
-    unsigned GetNumObjectNodes() const;
-    /// Return object node (for object group only).
-    Node* GetObjectNode(unsigned index) const;
     /// Return number of objects (for object group only).
     /// Return number of objects (for object group only).
     unsigned GetNumObjects() const;
     unsigned GetNumObjects() const;
     /// Return object (for object group only).
     /// Return object (for object group only).
     TileObject2D* GetObject(unsigned index) const;
     TileObject2D* GetObject(unsigned index) const;
+    /// Return object node (for object group only).
+    Node* GetObjectNode(unsigned index) const;
     
     
     /// Return image node (for image layer only).
     /// Return image node (for image layer only).
     Node* GetImageNode() const;
     Node* GetImageNode() const;