Beispiel #1
0
 // Generate the sprites that are placed on the cunk. this must only happen once in a chunks life time.
 // TODO : Move the generation to a separate class to improve system workflow, and extensibility.
 public void GenerateSprites()
 {
     if (curCunk.sprites != null)
     {
         if (curCunk.sprites.Length != 0)
         {
             // If the programer is not complying with the above instructions, then force a re-geneation of all sprites on the chunk.
             // This may be only good in editing mode.
             if (spriteContainer != null)
             {
                 GameObject.Destroy(spriteContainer);
                 spriteContainer = new GameObject("Sprites");
                 spriteContainer.transform.SetParent(transform);
                 spriteContainer.transform.position = transform.position;
             }
             else
             {
                 spriteContainer = new GameObject("Sprites");
                 spriteContainer.transform.SetParent(transform);
                 spriteContainer.transform.position = transform.position;
             }
             GameObject            spriteObj;
             SpriteRenderer        spriteRender;
             MaterialPropertyBlock spriteProps;
             spriteProps = new MaterialPropertyBlock();
             for (int i = 0; i < curCunk.sprites.Length; i++)
             {
                 spriteObj          = new GameObject(curCunk.sprites[i].spriteId.ToString());
                 spriteObj.isStatic = true;
                 spriteObj.transform.SetParent(spriteContainer.transform);
                 spriteObj.transform.localPosition = new Vector3(curCunk.sprites[i].posx, 0.2f, curCunk.sprites[i].posy);
                 spriteRender          = spriteObj.AddComponent <SpriteRenderer>();
                 spriteRender.material = ScenePrimer.StandardSprite;
                 spriteRender.GetPropertyBlock(spriteProps);
                 spriteRender.transform.eulerAngles = new Vector2(90, 0);
                 spriteRender.sprite = XCPManager.PngToSprite(XCPManager.currentXCP.spriteTextures[curCunk.sprites[i].spriteId]);
                 spriteProps.SetTexture("_tex", spriteRender.sprite.texture);
                 spriteProps.SetColor("_col", MapDataConverter.ColToColor32(XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[MapDataManager.mapDataCurrentLayer].forColor));
                 spriteRender.SetPropertyBlock(spriteProps);
                 if (ScenePrimer.curEditorPrimer != null)
                 {
                     spriteObj.AddComponent <BoxCollider>().isTrigger = true; spriteObj.AddComponent <SpriteInfo>().spriteIndex = i;
                 }
                 if (XCPManager.currentXCP.spriteTextures[curCunk.sprites[i].spriteId].colliders != null)
                 {
                     for (int col = 0; i < XCPManager.currentXCP.spriteTextures[curCunk.sprites[i].spriteId].colliders.Length; i++)
                     {
                         BoxCollider colider = spriteRender.gameObject.AddComponent <BoxCollider>();
                     }
                 }
                 spriteRender.sortingLayerName = EditorPrimer.GoodIdToBadUnityId(curCunk.sprites[i].sortLayer + 1);
                 spriteRender.sortingOrder     = SessionManager.SpriteSortByPos(spriteRender);
             }
         }
     }
 }
Beispiel #2
0
        public void BuildMesh(FastLineData lineData)
        {
            Mesh mesh = new Mesh();

            Vector3[] vertices = new Vector3[lineData.linesVectors.Length * 2];
            Vector3   curVector;
            Vector3   nextVector;

            for (int i = 0; i < lineData.linesVectors.Length; i++)
            {
                // Operate only if there is a next vector, The line generator needs 2 points to generate
                if (lineData.linesVectors.Length > i + 1)
                {
                    curVector       = lineData.linesVectors[i];
                    nextVector      = lineData.linesVectors[i + 1];
                    vertices[i]     = new Vector3(curVector.x + lineData.widthMultiplyer, curVector.y, curVector.z + lineData.widthMultiplyer);
                    vertices[i + 1] = new Vector3(nextVector.x + lineData.widthMultiplyer, nextVector.y, nextVector.z + lineData.widthMultiplyer);
                    vertices[i + 2] = new Vector3(curVector.x - lineData.widthMultiplyer, curVector.y, curVector.z - lineData.widthMultiplyer);
                    vertices[i + 3] = new Vector3(nextVector.x - lineData.widthMultiplyer, nextVector.y, nextVector.z - lineData.widthMultiplyer);
                }
            }

            mesh.vertices = vertices;
            int multiplayer = lineData.linesVectors.Length - 1;

            int[] triangles = new int[6 * multiplayer];
            int   curVert   = 0;

            for (int i = 0; i < lineData.linesVectors.Length - 1; i++)
            {
                if (i != 0)
                {
                    curVert = curVert + 6;
                }
                triangles[curVert]     = 0;
                triangles[curVert + 1] = 2;
                triangles[curVert + 2] = 3;

                triangles[curVert + 3] = 3;
                triangles[curVert + 4] = 1;
                triangles[curVert + 5] = 0;
            }

            mesh.triangles = triangles;
            gameObject.AddComponent <MeshFilter>().mesh = mesh;
            curRenderer                  = gameObject.AddComponent <MeshRenderer>();
            curRenderer.material         = lineData.lineMaterial;
            curRenderer.receiveShadows   = false;
            curRenderer.sortingLayerName = EditorPrimer.GoodIdToBadUnityId(0);
            curRenderer.GetPropertyBlock(propertyBlock);
            propertyBlock.SetColor("_Color", lineData.lineColor);
            curRenderer.SetPropertyBlock(propertyBlock);
        }
Beispiel #3
0
        public override void ToolUpdate()
        {
            if (ScenePrimer.curEditorPrimer.curSpriteMode)
            {
                if (ScenePrimer.curEditorPrimer.curSpriteId != -1)
                {
                    spriteMode = true;
                    curTileSelector.gameObject.SetActive(false);
                    Icon.gameObject.SetActive(true);
                    Icon = EditorPrimer.spriteIconRenderer;

                    Icon.GetPropertyBlock(spriteProps);
                    Icon.sprite = XCPManager.PngToSprite(XCPManager.currentXCP.spriteTextures[ScenePrimer.curEditorPrimer.curSpriteId]);
                    spriteProps.SetColor("_col", MapDataConverter.ColToColor32(XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[MapDataManager.mapDataCurrentLayer].forColor));
                    spriteProps.SetTexture("_tex", Icon.sprite.texture);
                    Icon.SetPropertyBlock(spriteProps);

                    Icon.sortingLayerName = EditorPrimer.GoodIdToBadUnityId(ScenePrimer.curEditorPrimer.curSortingLayer + 1);
                }
            }
        }
Beispiel #4
0
        public override void ToolPrimaryUse()
        {
            if (spriteMode)
            {
                usesDrag    = false;
                targetChunk = SessionManager.GetChunk(Icon.transform.position, SessionManager.GetChunkCluster(Icon.transform.position), false);
                if (targetChunk != null)
                {
                    Icon.transform.position = SessionManager.ObjPosition(Icon.transform.position);
                    if (targetChunk.sprites == null)
                    {
                        targetChunk.sprites              = new Spr[1];
                        targetChunk.sprites[0].posx      = Icon.transform.position.x;
                        targetChunk.sprites[0].posy      = Icon.transform.position.z;
                        targetChunk.sprites[0].sortLayer = (ushort)ScenePrimer.curEditorPrimer.curSortingLayer;
                        targetChunk.sprites[0].spriteId  = (ushort)ScenePrimer.curEditorPrimer.curSpriteId;
                    }
                    else
                    {
                        Array.Resize(ref targetChunk.sprites, targetChunk.sprites.Length + 1);
                        targetChunk.sprites[targetChunk.sprites.Length - 1]           = new Spr();
                        targetChunk.sprites[targetChunk.sprites.Length - 1].posx      = Icon.transform.position.x;
                        targetChunk.sprites[targetChunk.sprites.Length - 1].posy      = Icon.transform.position.z;
                        targetChunk.sprites[targetChunk.sprites.Length - 1].sortLayer = (ushort)ScenePrimer.curEditorPrimer.curSortingLayer;
                        targetChunk.sprites[targetChunk.sprites.Length - 1].spriteId  = (ushort)ScenePrimer.curEditorPrimer.curSpriteId;
                        targetChunk.sprites[targetChunk.sprites.Length - 1].sortLayer = (ushort)ScenePrimer.curEditorPrimer.curSortingLayer;
                    }
                    if (targetChunk != null)
                    {
                        if (targetChunk.meshObjRef == null)
                        {
                            targetChunk.meshObjRef = targetChunk.chunkObjRef.GetComponent <TileMesh2D>();
                        }
                    }
                    targetChunk.meshObjRef.curCunk = targetChunk;
                    if (targetChunk.meshObjRef.spriteContainer != null)
                    {
                        GameObject spriteObj = new GameObject(ScenePrimer.curEditorPrimer.curSpriteId.ToString());
                        spriteObj.transform.SetParent(targetChunk.meshObjRef.spriteContainer.transform);
                        spriteObj.transform.localPosition = Icon.transform.position;
                        if (targetChunk.sprites.Length == 1)
                        {
                            spriteObj.AddComponent <SpriteInfo>().spriteIndex = 0;
                        }
                        else
                        {
                            spriteObj.AddComponent <SpriteInfo>().spriteIndex = targetChunk.sprites.Length - 1;
                        }
                        SpriteRenderer spriteRender = spriteObj.AddComponent <SpriteRenderer>();

                        spriteRender.material = ScenePrimer.curEditorPrimer.worldMaterial;
                        spriteRender.transform.eulerAngles = new Vector2(90, 0);
                        spriteRender.sprite           = XCPManager.PngToSprite(XCPManager.currentXCP.spriteTextures[ScenePrimer.curEditorPrimer.curSpriteId]);
                        spriteRender.sortingOrder     = SessionManager.SpriteSortByPos(spriteRender);
                        spriteRender.sortingLayerName = EditorPrimer.GoodIdToBadUnityId(ScenePrimer.curEditorPrimer.curSortingLayer + 1);
                        spriteObj.AddComponent <BoxCollider>().isTrigger = true;

                        spriteRender.GetPropertyBlock(spriteProps);
                        spriteRender.sprite = XCPManager.PngToSprite(XCPManager.currentXCP.spriteTextures[ScenePrimer.curEditorPrimer.curSpriteId]);
                        spriteProps.SetColor("_col", MapDataConverter.ColToColor32(XCPManager.currentXCP.xpcMaps[MapDataManager.mapDataXCPIndex].mapLayers[MapDataManager.mapDataCurrentLayer].forColor));
                        spriteProps.SetTexture("_tex", spriteRender.sprite.texture);
                        spriteRender.SetPropertyBlock(spriteProps);
                    }
                    else
                    {
                        targetChunk.meshObjRef.GenerateSprites();
                    }
                }
            }
            else
            {
                usesDrag        = true;
                cursorTransform = CameraManager.curCamera.ScreenToWorldPoint(Input.mousePosition);

                cursorTransform.x = (float)System.Math.Round(cursorTransform.x / tileSize) * tileSize - tileSize * 0.5f;
                cursorTransform.z = (float)System.Math.Round(cursorTransform.z / tileSize) * tileSize + tileSize * 0.5f;

                SessionManager.PlaceTile(cursorTransform, ScenePrimer.curEditorPrimer.curLayer, ScenePrimer.curEditorPrimer.curTileId, true, false);
            }
        }