Пример #1
0
        public override void AddSprite(SpritePropTypeData spriteProp, Matrix4x4 transform)
        {
            if (spriteProp == null)
            {
                return;
            }
            string NodeId = spriteProp.NodeId;

            if (spriteProp.sprite == null)
            {
                return;
            }

            FlipSpriteTransform(ref transform, spriteProp.sprite);

            GameObject item = null;

            // Try to reuse an object from the pool
            if (pooledObjects.ContainsKey(NodeId) && pooledObjects [NodeId].Count > 0)
            {
                item = pooledObjects [NodeId].Dequeue();
                SetTransform(item.transform, transform);
            }
            else
            {
                // Pool is exhausted for this object
                item = BuildSpriteObject(spriteProp, transform, NodeId);
            }
            item.isStatic = spriteProp.IsStaticObject;
        }
Пример #2
0
 /// <summary>
 /// Request the creation of a sprite object
 /// </summary>
 /// <param name="spriteProp">The sprite game object template reference</param>
 /// <param name="transform">The transform of the prop</param>
 public virtual void AddSprite(SpritePropTypeData spriteProp, Matrix4x4 transform)
 {
 }
Пример #3
0
        protected GameObject BuildSpriteObject(SpritePropTypeData spriteData, Matrix4x4 transform, string NodeId)
        {
            if (spriteData.sprite == null)
            {
                return(null);
            }
            var gameObj = new GameObject(spriteData.sprite.name);

            // Setup the sprite
            var spriteRenderer = gameObj.AddComponent <SpriteRenderer>();

            spriteRenderer.sprite       = spriteData.sprite;
            spriteRenderer.color        = spriteData.color;
            spriteRenderer.sortingOrder = spriteData.orderInLayer;

            if (spriteData.materialOverride != null)
            {
                spriteRenderer.material = spriteData.materialOverride;
            }
            if (spriteData.sortingLayerName != null && spriteData.sortingLayerName.Length > 0)
            {
                spriteRenderer.sortingLayerName = spriteData.sortingLayerName;
            }

            // Setup the sprite collision
            var collisionType = spriteData.collisionType;

            if (collisionType != DungeonSpriteCollisionType.None)
            {
                Vector2 baseScaleMultiplier = Vector2.one;
                var     sprite        = spriteData.sprite;
                var     pixelsPerUnit = sprite.pixelsPerUnit;
                baseScaleMultiplier.x = sprite.rect.width / pixelsPerUnit;
                baseScaleMultiplier.y = sprite.rect.height / pixelsPerUnit;

                Collider2D collider = null;
                if (collisionType == DungeonSpriteCollisionType.Box)
                {
                    var boxCollider = gameObj.AddComponent <BoxCollider2D>();
                    boxCollider.size = Vector3.Scale(spriteData.physicsSize, baseScaleMultiplier);
                    collider         = boxCollider;
                }
                else if (collisionType == DungeonSpriteCollisionType.Circle)
                {
                    var circleCollider = gameObj.AddComponent <CircleCollider2D>();
                    circleCollider.radius = spriteData.physicsRadius * baseScaleMultiplier.x;
                    collider = circleCollider;
                }
                else if (collisionType == DungeonSpriteCollisionType.Polygon)
                {
                    collider = gameObj.AddComponent <PolygonCollider2D>();
                }

                if (collider != null)
                {
                    collider.sharedMaterial = spriteData.physicsMaterial;
                    collider.offset         = Vector3.Scale(spriteData.physicsOffset, baseScaleMultiplier);
                }
            }


            // Set the transform
            Matrix.DecomposeMatrix(ref transform, out _position, out _rotation, out _scale);
            gameObj.transform.position   = _position;
            gameObj.transform.rotation   = _rotation;
            gameObj.transform.localScale = _scale;

            // Setup dungeon related parameters
            if (itemParent != null)
            {
                gameObj.transform.parent = itemParent.transform;
            }

            var data = gameObj.AddComponent <DungeonSceneProviderData> ();

            data.NodeId            = NodeId;
            data.dungeon           = dungeon;
            data.affectsNavigation = spriteData.affectsNavigation;

            return(gameObj);
        }
Пример #4
0
        public void BuildFromGraph(Graph graph)
        {
            Props.Clear();
            if (graph == null)
            {
                return;
            }
            var nodes = graph.Nodes.ToArray();

            Array.Sort(nodes, new LeftToRightNodeComparer());

            foreach (var node in nodes)
            {
                if (node is VisualNode)
                {
                    var visualNode = node as VisualNode;

                    foreach (var meshParentNode in visualNode.GetParentNodes())
                    {
                        if (meshParentNode is MarkerNode)
                        {
                            var markerNode = meshParentNode as MarkerNode;

                            PropTypeData item = null;
                            if (visualNode is GameObjectNode)
                            {
                                var meshItem = new GameObjectPropTypeData();
                                var goNode   = visualNode as GameObjectNode;

                                meshItem.Template = goNode.Template;

                                item = meshItem;
                            }
                            else if (visualNode is GameObjectArrayNode)
                            {
                                var arrayPropData = new GameObjectArrayPropTypeData();
                                var arrayNode     = visualNode as GameObjectArrayNode;

                                if (arrayNode == null || arrayNode.Templates == null)
                                {
                                    arrayPropData.Templates = new GameObject[0];
                                }
                                else
                                {
                                    var count = arrayNode.Templates.Length;
                                    arrayPropData.Templates = new GameObject[count];
                                    System.Array.Copy(arrayNode.Templates, arrayPropData.Templates, count);
                                }
                                item = arrayPropData;
                            }
                            else if (visualNode is SpriteNode)
                            {
                                var spriteItem = new SpritePropTypeData();
                                var spriteNode = visualNode as SpriteNode;

                                spriteItem.sprite           = spriteNode.sprite;
                                spriteItem.color            = spriteNode.color;
                                spriteItem.materialOverride = spriteNode.materialOverride;
                                spriteItem.sortingLayerName = spriteNode.sortingLayerName;
                                spriteItem.orderInLayer     = spriteNode.orderInLayer;

                                spriteItem.collisionType   = spriteNode.collisionType;
                                spriteItem.physicsMaterial = spriteNode.physicsMaterial;
                                spriteItem.physicsOffset   = spriteNode.physicsOffset;
                                spriteItem.physicsSize     = spriteNode.physicsSize;
                                spriteItem.physicsRadius   = spriteNode.physicsRadius;

                                item = spriteItem;
                            }
                            else
                            {
                                // Unsupported visual node type
                                continue;
                            }

                            // Set the common settings
                            item.NodeId                 = visualNode.Id.ToString();
                            item.AttachToSocket         = markerNode.Caption;
                            item.Affinity               = visualNode.attachmentProbability;
                            item.ConsumeOnAttach        = visualNode.consumeOnAttach;
                            item.Offset                 = visualNode.offset;
                            item.IsStaticObject         = visualNode.IsStatic;
                            item.affectsNavigation      = visualNode.affectsNavigation;
                            item.UseSelectionRule       = visualNode.selectionRuleEnabled;
                            item.SelectorRuleClassName  = visualNode.selectionRuleClassName;
                            item.UseTransformRule       = visualNode.transformRuleEnabled;
                            item.TransformRuleClassName = visualNode.transformRuleClassName;
                            item.useSpatialConstraint   = visualNode.useSpatialConstraint;
                            item.spatialConstraint      = visualNode.spatialConstraint;

                            var emitterNodes = visualNode.GetChildNodes();
                            foreach (var childNode in emitterNodes)
                            {
                                if (childNode is MarkerEmitterNode)
                                {
                                    var emitterNode = childNode as MarkerEmitterNode;
                                    if (emitterNode.Marker != null)
                                    {
                                        PropChildSocketData childData = new PropChildSocketData();
                                        childData.Offset     = emitterNode.offset;
                                        childData.SocketType = emitterNode.Marker.Caption;
                                        item.ChildSockets.Add(childData);
                                    }
                                }
                            }
                            Props.Add(item);
                        }
                    }
                }
            }
        }
Пример #5
0
 /// <summary>
 /// Request the creation of a sprite object
 /// </summary>
 /// <param name="spriteProp">The sprite game object template reference</param>
 /// <param name="transform">The transform of the prop</param>
 public virtual GameObject AddSprite(SpritePropTypeData spriteProp, Matrix4x4 transform)
 {
     return(null);
 }
Пример #6
0
 /// <summary>
 /// Request the creation of a sprite object
 /// </summary>
 /// <param name="spriteProp">The sprite game object template reference</param>
 /// <param name="transform">The transform of the prop</param>
 public virtual GameObject AddSprite(SpritePropTypeData spriteProp, Matrix4x4 transform, IDungeonSceneObjectInstantiator objectInstantiator)
 {
     return(null);
 }