Ejemplo n.º 1
0
 void Start()
 {
     if (instance == null)
     {
         instance = this;
     }
     else if (instance != this)
     {
         Debug.LogError("Destruction2D: Multiple Managers Detected!");
         Destroy(this);
     }
 }
Ejemplo n.º 2
0
    void StartSpriteRenderer()
    {
        spriteRenderer = GetComponent <SpriteRenderer>();

        if (spriteRenderer == null)
        {
            Debug.LogError("Destruction2D: Game object is missing SpriteRenderer Component");
        }

        if (chunks.enabled)
        {
            StartChunks();
        }
        else
        {
            Sprite sprite = spriteRenderer.sprite;
            pixelsPerUnit = sprite.pixelsPerUnit;
            Debug.Log("sprite.pixelsPerUnit");

            if (originalSprite == null)
            {
                originalSprite = sprite;
            }

            if (replaceSprite == false)
            {
                originalSpriteMaterial             = new Material(Shader.Find("Sprites/Default"));
                originalSpriteMaterial.mainTexture = originalSprite.texture;
            }

            Texture2D texture = originalSprite.texture;

            renderTexture = new RenderTexture((int)sprite.rect.width, (int)sprite.rect.height, 32);
            outputTexture = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);

            switch (textureType)
            {
            case TextureType.Mesh:
                outputMaterial             = new Material(Shader.Find("Sprites/Default"));
                outputMaterial.mainTexture = texture;

                Destroy(spriteRenderer);

                break;

            case TextureType.Sprite:
                break;
            }

            Destruction2DManager.RequestBufferEvent(this);
        }
    }
Ejemplo n.º 3
0
    public static void Initialize()
    {
        if (instance != null)
        {
            return;
        }

        GameObject manager = new GameObject();

        manager.name = "Destruction 2D Manager";

        instance = manager.AddComponent <Destruction2DManager>();
    }
Ejemplo n.º 4
0
    public void Start()
    {
        shape.SetOrigin(this);

        Destruction2DManager.Initialize();

        switch (textureType)
        {
        case TextureType.SpriteShape:
            StartSpriteShape();
            return;

        case TextureType.Tilemap:
            StartTilemap();
            return;

        default:
            StartSpriteRenderer();
            return;
        }
    }
Ejemplo n.º 5
0
    public void AddModifier(Texture2D texture, Vector2 position, Vector2 size, float rotation)
    {
        Polygon2D poly = Polygon2D.CreateFromRect(size);

        poly.ToRotationItself(rotation * Mathf.Deg2Rad);
        poly.ToOffsetItself(new Vector2D(position));

        List <Polygon2D> polys = shape.GetWorld();

        if (polys.Count > 0)
        {
            bool touch = false;

            foreach (Polygon2D p in polys)
            {
                if (Math2D.PolyCollidePoly(poly, p) == true)
                {
                    touch = true;
                }
            }

            if (touch == false)
            {
                return;
            }
        }
        else
        {
            Polygon2D p     = GetBoundPolygon();
            bool      touch = false;

            p = p.ToWorldSpace(transform);

            if (Math2D.PolyCollidePoly(p, poly) == true)
            {
                touch = true;
            }

            if (touch == false)
            {
                return;
            }
        }

        Vector2 pos = transform.InverseTransformPoint(position);

        float ratioX = 1;

        if (transform.localScale.x != transform.localScale.y)
        {
            ratioX = transform.localScale.x / transform.localScale.y;
            size.y = size.y * ratioX;
        }

        size.x /= transform.localScale.x;
        size.y /= transform.localScale.y;
        size.y /= ratioX;

        DestructionModifier modifier = new DestructionModifier(texture, pos, size, rotation);

        modifiers.Add(modifier);

        modifiersAdded = true;         // add only if colides

        if (textureType == TextureType.SpriteShape)
        {
            buffer.renderCamera.enabled = true;
        }
        else
        {
            Destruction2DManager.RequestBufferEvent(this);             // Check if it is already requested
        }
    }
Ejemplo n.º 6
0
    public bool DestroyByPolygon(EraseBrush EraseBrush)
    {
        //CrumbsEffect.isSlicing = false;

        if (EraseBrush.GetWorldShape().pointsList.Count < 3)
        {
            return(false);
        }

        List <Polygon2D> polys = shape.GetWorld();

        if (polys.Count > 0)
        {
            bool touch   = false;
            bool outside = false;

            foreach (Polygon2D p in polys)
            {
                if (Math2D.PolyCollidePoly(EraseBrush.GetWorldShape(), p) == true)
                {
                    touch = true;
                }

                if (EraseBrush.GetWorldShape().PolyInPoly(p) == false)
                {
                    outside = true;
                }
            }



            if (touch == false)
            {
                return(false);
            }
            // CrumbsEffect.isSlicing = true;
            if (outside == false)
            {
                Destroy(gameObject);
                return(true);
            }
        }
        else
        {
            Polygon2D bound = GetBoundPolygon();
            bool      touch = false;

            bound.ToWorldSpaceItself(transform);

            if (Math2D.PolyCollidePoly(EraseBrush.GetWorldShape(), bound) == true)
            {
                touch = true;
            }

            if (touch == false)
            {
                return(false);
            }
        }

        eraseEvents.Add(new DestructionEvent(EraseBrush));

        if (textureType == TextureType.SpriteShape)
        {
            buffer.renderCamera.enabled = true;
        }
        else
        {
            Destruction2DManager.RequestBufferEvent(this);
        }
        return(true);
    }
Ejemplo n.º 7
0
    void StartSpriteShape()
    {
        if (init == true)
        {
            return;
        }


        Rect rect = new Rect();

        switch (spriteShapeBounds)
        {
        case SpriteShapeBounds.Renderer:
            Bounds bounds = new Bounds();
            bool   found  = false;
            foreach (Component c in gameObject.GetComponents <Component>())
            {
                if (c.GetType().ToString() == "UnityEngine.U2D.SpriteShapeRenderer")
                {
                    UnityEngine.U2D.SpriteShapeRenderer ssr = (UnityEngine.U2D.SpriteShapeRenderer)c;

                    bounds = ssr.bounds;
                    found  = true;

                    break;
                }
            }
            if (found)
            {
                rect = new Rect(bounds.center.x - bounds.extents.x - transform.position.x, bounds.center.y - bounds.extents.y - transform.position.y, bounds.extents.x * 2, bounds.extents.y * 2);
            }
            else
            {
                Debug.LogWarning("Destruction2D: SpriteShape game object is missing SpriteShapeRenderer");
            }
            break;

        case SpriteShapeBounds.Collider:
            List <Polygon2D> polygons = Polygon2DList.CreateFromGameObject(gameObject);
            if (polygons.Count < 1)
            {
                Debug.LogError("Destruction2D: SpriteShape game object is missing collider");
                return;
            }

            rect = polygons[0].GetBounds();
            break;
        }

        GameObject newGamObject = new GameObject();

        newGamObject.name = gameObject.name;
        newGamObject.transform.position   = transform.position;
        newGamObject.transform.rotation   = transform.rotation;
        newGamObject.transform.localScale = transform.localScale;
        newGamObject.transform.parent     = transform.parent;

        foreach (Component c in gameObject.GetComponents <Component>())
        {
            if (c.GetType().ToString() == "UnityEngine.PolygonCollider2D")
            {
                continue;
            }
            if (c.GetType().ToString() == "UnityEngine.EdgeCollider2D")
            {
                continue;
            }
            if (c.GetType().ToString() != "UnityEngine.U2D.SpriteShapeController" && c.GetType().ToString() != "UnityEngine.U2D.SpriteShapeRenderer")
            {
                if (CopyComponent(c, newGamObject, gameObject) == true)
                {
                    Destroy(c);
                }
            }
        }

        Destruction2D destructible = newGamObject.GetComponent <Destruction2D>();

        destructible.init = true;

        Destroy(gameObject.GetComponent <EdgeCollider2D>());

        PolygonCollider2D collider = gameObject.GetComponent <PolygonCollider2D>();

        if (collider != null)
        {
            newGamObject.AddComponent <PolygonCollider2D>();
            Destroy(collider);
        }

        gameObject.transform.parent     = Destruction2DManager.instance.transform;
        gameObject.name                 = "Buffer: " + gameObject.name;
        gameObject.transform.rotation   = Quaternion.Euler(0, 0, 0);
        gameObject.transform.localScale = new Vector3(1, 1, 1);

        destructible.buffer = Destruction2DManager.CreateCustomBuffer(gameObject);

        destructible.pixelsPerUnit = scaledPixelsPerUnit;

        destructible.filterMode = filterMode;

        int textureWidth  = (int)(rect.width * destructible.pixelsPerUnit);
        int textureHeight = (int)(rect.height * destructible.pixelsPerUnit);

        destructible.renderTexture = new RenderTexture(textureWidth, textureHeight, 32);
        destructible.outputTexture = new Texture2D(textureWidth, textureHeight);

        destructible.buffer.renderCamera.orthographicSize = (rect.height) / 2;
        destructible.buffer.renderCamera.transform.Translate(rect.center.x, rect.center.y, -1f);
        destructible.buffer.renderCamera.farClipPlane  = 5;
        destructible.buffer.renderCamera.targetTexture = destructible.renderTexture;

        newGamObject.transform.Translate(rect.center.x, rect.center.y, -1f);

        destructible.spriteRenderer = newGamObject.AddComponent <SpriteRenderer>();

        // Set Up Destructible
        destructible.buffer.SetDestructible(destructible);

        destructible.buffer.renderCamera.enabled = true;

        destructible.originalSprite = Sprite.Create(destructible.outputTexture, new Rect(0.0f, 0.0f, destructible.renderTexture.width, destructible.renderTexture.height), new Vector2(0.5f, 0.5f), pixelsPerUnit, 2, SpriteMeshType.FullRect);
    }
Ejemplo n.º 8
0
    void StartTilemap()
    {
        if (init == true)
        {
            return;
        }

        Tilemap         tilemap  = GetComponent <Tilemap>();
        TilemapRenderer renderer = GetComponent <TilemapRenderer>();

        BoundsInt bounds = tilemap.cellBounds;

        Rect rect = new Rect();

        rect.x = bounds.x * gridSize.x;
        rect.y = bounds.y * gridSize.y;

        rect.width  = bounds.size.x * gridSize.x;
        rect.height = bounds.size.y * gridSize.y;

        GameObject newGamObject = new GameObject();

        newGamObject.name = gameObject.name;
        newGamObject.transform.position   = transform.position;
        newGamObject.transform.rotation   = transform.rotation;
        newGamObject.transform.localScale = transform.localScale;
        newGamObject.transform.parent     = transform.parent;

        foreach (Component c in gameObject.GetComponents <Component>())
        {
            if (c.GetType().ToString() == "UnityEngine.PolygonCollider2D")
            {
                continue;
            }
            if (c.GetType().ToString() == "UnityEngine.EdgeCollider2D")
            {
                continue;
            }
            if (c.GetType().ToString() != "UnityEngine.Tilemaps.Tilemap" && c.GetType().ToString() != "UnityEngine.Tilemaps.TilemapRenderer")
            {
                if (CopyComponent(c, newGamObject, gameObject) == true)
                {
                    Destroy(c);
                }
            }
        }

        Destruction2D destructible = newGamObject.GetComponent <Destruction2D>();

        destructible.init = true;

        newGamObject.AddComponent <PolygonCollider2D>();

        //PolygonCollider2D collider = gameObject.GetComponent<PolygonCollider2D>();
        //if (collider != null) {
        //	newGamObject.AddComponent<PolygonCollider2D>();
        //	Destroy(collider);
        //}

        GameObject newGridObject = new GameObject();

        newGridObject.name = "Buffer: " + gameObject.name;

        Grid grid = newGridObject.AddComponent <Grid>();

        grid.cellSize = new Vector3(gridSize.x, gridSize.y, 0);



        newGridObject.transform.parent = Destruction2DManager.instance.transform;

        gameObject.transform.parent     = newGridObject.transform;
        gameObject.name                 = "Tilemap";
        gameObject.transform.rotation   = Quaternion.Euler(0, 0, 0);
        gameObject.transform.localScale = new Vector3(1, 1, 1);
        gameObject.transform.position   = new Vector3(0, 0, 0);

        destructible.buffer = Destruction2DManager.CreateCustomBuffer(newGridObject);

        destructible.pixelsPerUnit = scaledPixelsPerUnit;

        int textureWidth  = (int)(rect.width * destructible.pixelsPerUnit);
        int textureHeight = (int)(rect.height * destructible.pixelsPerUnit);

        destructible.renderTexture = new RenderTexture(textureWidth, textureHeight, 32);
        destructible.outputTexture = new Texture2D(textureWidth, textureHeight);

        // Filter Mode???
        destructible.filterMode = filterMode;
        // destructible.outputTexture.filterMode = filterMode;

        destructible.buffer.renderCamera.orthographicSize = (rect.height) / 2;
        destructible.buffer.renderCamera.transform.Translate(rect.center.x, rect.center.y, -1f);
        destructible.buffer.renderCamera.farClipPlane  = 5;
        destructible.buffer.renderCamera.targetTexture = destructible.renderTexture;

        newGamObject.transform.Translate(rect.center.x, rect.center.y, -1f);

        destructible.spriteRenderer = newGamObject.AddComponent <SpriteRenderer>();

        // Set Up Destructible
        destructible.buffer.SetDestructible(destructible);

        destructible.buffer.renderCamera.enabled = true;

        destructible.originalSprite = Sprite.Create(destructible.outputTexture, new Rect(0.0f, 0.0f, destructible.renderTexture.width, destructible.renderTexture.height), new Vector2(0.5f, 0.5f), pixelsPerUnit, 2, SpriteMeshType.FullRect);

        //Destroy(gameObject.GetComponent<EdgeCollider2D>());
    }