Example #1
0
    void GenerateStartChunk()
    {
        GameObject newChunk = chunksAvailable[1];

        List <GameObject> possibleNewChunk = new List <GameObject>();

        for (int i = 0; i < chunksAvailable.Length; i++)
        {
            Chunck currentChunk = chunksAvailable[i].GetComponent <Chunck>();

            if ((currentChunk.right || currentChunk.up) && !currentChunk.left && !currentChunk.down)
            {
                possibleNewChunk.Add(chunksAvailable[i]);
            }
        }

        newChunk = possibleNewChunk[Random.Range(0, possibleNewChunk.Count)];

        GameObject startChunk = GameObject.Instantiate(newChunk);

        startChunk.name = "StartChunk";
        startChunk.transform.position = new Vector2(0 + sizeX / 2f, 0 + sizeY / 2f);

        cells[0, 0].chunk = startChunk.GetComponent <Chunck>();

        GenerateChunkChild(cells[0, 0]);
    }
    void SetBlock(Vector3 p, Block b)
    {
        RaycastHit hit;
        int        i = 0;

        Chunck  chunck   = Chunck.GetChunck(Mathf.FloorToInt(p.x), Mathf.FloorToInt(p.y), Mathf.FloorToInt(p.z));
        Vector3 localPos = chunck.transform.position - p;

        if (b == null)
        {
            map = chunck.GetBlock(p);

            if (map == null)
            {
                map = Block.getBlock(BlockList.Blocks[tmp].BlockName);
            }
            item = GetItem(map);

            if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 7f, lm))
            {
                if (hit.transform.gameObject.tag != "Obje")
                {
                    if (hit.transform.gameObject.tag != "Tree")
                    {
                        if (hit.transform.gameObject.tag != "Player")
                        {
                            GameObject obje = Instantiate(Resources.Load <GameObject>(map.BlockName), BlockHighlight.transform.position + new Vector3(0, 0.5f, 0), Quaternion.identity) as GameObject;
                            obje.GetComponent <Obje>().item = item;
                            Debug.Log("Obje = " + obje.transform.position + "Chunck Pos = " + chunck.transform.position + "P=" + p + "Local Pos=" + localPos);
                        }
                    }
                }
            }
        }
        map = null;
        if ((Mathf.FloorToInt(localPos.x) * -1) == (Chunck.Width))
        {
            Chunck c = Chunck.GetChunck(Mathf.FloorToInt(p.x + 5), Mathf.FloorToInt(p.y), Mathf.FloorToInt(p.z));
            if (c == null)
            {
                return;
            }

            c.SetBlock(p + new Vector3(+1, 0, 0), b);
        }
        else
        {
            Chunck c = Chunck.GetChunck(Mathf.FloorToInt(p.x - 5), Mathf.FloorToInt(p.y), Mathf.FloorToInt(p.z));
            if (c == null)
            {
                return;
            }

            c.SetBlock(p + new Vector3(+1, 0, 0), b);
        }

        chunck.SetBlock(p + new Vector3(+1, 0, 0), b);
    }
 // Todo : Running this coroutine several times quickly will cause client to request several time the same mesh or texture. Solve it.
 public IEnumerator RequestBrickOn(Chunck chunck)
 {
     List<string> bricksToLoad = new List<string>();
     List<string> texturesToLoad = new List<string>();
     foreach (string textureName in chunck.texturedBlocks.Keys)
     {
         if (!textures.ContainsKey(textureName))
         {
             if (!texturesToLoad.Contains(textureName))
             {
                 texturesToLoad.Add(textureName);
             }
         }
         foreach (Block b in chunck.texturedBlocks[textureName])
         {
             if (!bricks.ContainsKey(b.reference))
             {
                 if (!bricksToLoad.Contains(b.reference))
                 {
                     bricksToLoad.Add(b.reference);
                 }
             }
         }
     }
     foreach (string reference in bricksToLoad)
     {
         WWW request = new WWW("http://localhost:8080/bricks/" + reference);
         yield return request;
         if (!bricks.ContainsKey(reference))
         {
             BrickData bData = JsonUtility.FromJson<BrickData>(request.text);
             Brick b = new Brick();
             b.SetData(bData);
             Add(b);
         }
     }
     foreach (string texture in texturesToLoad)
     {
         Texture2D newTexture = new Texture2D(16, 16);
         WWW request = new WWW("http://localhost:8080/textures/" + texture + ".png");
         yield return request;
         request.LoadImageIntoTexture(newTexture);
         if (!textures.ContainsKey(texture))
         {
             Add(texture, newTexture);
         }
     }
     StartCoroutine(chunck.BuildBlocksAsync());
 }
    void Update()
    {
        tmp = Random.Range(2, 10);
        if (Mathf.FloorToInt(Time.time) % 5 == 0 && once)
        {
            for (float x = transform.position.x - viewRange; x < transform.position.x + viewRange; x += Chunck.Width * gridSize)
            {
                for (float z = transform.position.z - viewRange; z < transform.position.z + viewRange; z += Chunck.Width * gridSize)
                {
                    int xx = Mathf.FloorToInt(x / Chunck.Width) * Chunck.Width;
                    int zz = Mathf.FloorToInt(z / Chunck.Width) * Chunck.Width;

                    Chunck chunck = Chunck.GetChunck(Mathf.FloorToInt(xx), 0, Mathf.FloorToInt(zz));
                    if (chunck == null)
                    {
                        for (int y = 0; y < collumnHeight; y++)
                        {
                            int yr = (y * Chunck.Height) /*- (y)*/;

                            for (int ix = 0; ix < gridSize; ix++)
                            {
                                for (int iz = 0; iz < gridSize; iz++)
                                {
                                    GameObject ch = Instantiate(chunckPrefab, new Vector3(xx + (Chunck.Width * ix), yr, zz + (Chunck.Width * iz)), Quaternion.identity);
                                }
                            }
                        }
                    }
                }
            }

            once = false;
        }
        else
        {
            once = true;
        }

        BlockController();
        SundriesController();
        Chunck c = Chunck.GetChunck(10, 90, 10);

        if (c == null)
        {
            return;
        }
    }
Example #5
0
    private void GenerateChucks(Chunck chunck, Vector3 start, Vector3 end, float size, float speed)
    {
        size += Random.Range(0f, 4f);

        var randomPos = Random.Range(start.y, end.y);

        var c = Instantiate(chunck, new Vector3(start.x, randomPos, start.z), Quaternion.identity);

        chunks.Add(c.gameObject);
        var high = c.High.position.y;
        var low  = c.Low.position.y;

        DestroyWallsInRange(high, low, false);

        var walls = c.GetComponentsInChildren <SpawnWallBalls>();

        foreach (var wall in walls)
        {
            wall.Size        = size;
            wall.SpeedBullet = speed;
            _allRandomWalls.Add(wall.gameObject);
        }
    }
Example #6
0
        private void CreateWorldManager(WorldGenerator worldGenerator, Vector3 pos)
        {
            var isInstancingRenderer = false;

            if (createdTime < 9)
            {
                createdTime += 1;

                isInstancingRenderer = true;
            }
            else
            {
                isInstancingRenderer = false;
            }


            var chunck = new Chunck()
            {
                startPos = pos,
                mapData  = null
            };

            chuncks.Add(chunck);

            StartCoroutine(worldGenerator.GetBlockMap(pos, isInstancingRenderer, mapData =>
            {
                var worldManager = new GameObject("WorldManager", typeof(WorldManager)).GetComponent <WorldManager>();

                worldManager.mapData            = mapData;
                worldManager.blockStorageData   = storageData;
                worldManager.InstancingRenderer = isInstancingRenderer;

                chunck.mapData      = mapData;
                chunck.worldManager = worldManager;
            }));
        }
Example #7
0
 public void SetMesh(Chunck[][] sideChuncks)
 {
     this.GetComponent<MeshFilter>().mesh = this.BuildMesh(sideChuncks);
     this.GetComponent<MeshCollider>().sharedMesh = this.GetComponent<MeshFilter>().mesh;
     this.meshIsSet = true;
 }
Example #8
0
    public Mesh BuildMesh(Chunck[][] sideChuncks)
    {
        int[][] extendedMap = new int[CHUNCKSIZE + 3][];
        for (int i = 0; i < CHUNCKSIZE + 3; i++)
        {
            extendedMap[i] = new int[CHUNCKSIZE + 3];
        }

        // Center
        for (int j = 0; j < CHUNCKSIZE; j++)
        {
            for (int i = 0; i < CHUNCKSIZE; i++)
            {
                extendedMap[i + 1][j + 1] = this.map[i + j * CHUNCKSIZE];
            }
        }
        // Top Left
        extendedMap[0][0] = sideChuncks[0][0].map[CHUNCKSIZE * CHUNCKSIZE - 1];
        // Top
        for (int j = 0; j < CHUNCKSIZE; j++)
        {
            extendedMap[0][j + 1] = sideChuncks[0][1].map[(CHUNCKSIZE - 1) + j * CHUNCKSIZE];
        }
        // Top Right
        extendedMap[0][CHUNCKSIZE + 1] = sideChuncks[0][2].map[(CHUNCKSIZE - 1)];
        extendedMap[0][CHUNCKSIZE + 2] = sideChuncks[0][2].map[(CHUNCKSIZE - 1) + CHUNCKSIZE];
        // Right
        for (int i = 0; i < CHUNCKSIZE; i++)
        {
            extendedMap[i + 1][CHUNCKSIZE + 1] = sideChuncks[1][2].map[i];
            extendedMap[i + 1][CHUNCKSIZE + 2] = sideChuncks[1][2].map[i + CHUNCKSIZE];
        }
        // Bottom Right
        extendedMap[CHUNCKSIZE + 1][CHUNCKSIZE + 1] = sideChuncks[2][2].map[0];
        extendedMap[CHUNCKSIZE + 2][CHUNCKSIZE + 1] = sideChuncks[2][2].map[1];
        extendedMap[CHUNCKSIZE + 1][CHUNCKSIZE + 2] = sideChuncks[2][2].map[CHUNCKSIZE];
        extendedMap[CHUNCKSIZE + 2][CHUNCKSIZE + 2] = sideChuncks[2][2].map[1 + CHUNCKSIZE];
        // Bottom
        for (int j = 0; j < CHUNCKSIZE; j++)
        {
            extendedMap[CHUNCKSIZE + 1][j + 1] = sideChuncks[2][1].map[j * CHUNCKSIZE];
            extendedMap[CHUNCKSIZE + 2][j + 1] = sideChuncks[2][1].map[1 + j * CHUNCKSIZE];
        }
        // Bottom Left
        extendedMap[CHUNCKSIZE + 1][0] = sideChuncks[2][0].map[CHUNCKSIZE];
        extendedMap[CHUNCKSIZE + 2][0] = sideChuncks[2][0].map[1 + CHUNCKSIZE];
        // Left
        for (int i = 0; i < CHUNCKSIZE; i++)
        {
            extendedMap[i + 1][0] = sideChuncks[1][0].map[i + (CHUNCKSIZE - 1) * CHUNCKSIZE];
        }

        Mesh m = new Mesh();
        List<Vector3> verticesList = new List<Vector3>();
        List<int> trianglesList = new List<int>();
        List<Vector3> normalsList = new List<Vector3>();

        for (int j = 0; j < CHUNCKSIZE + 1; j++)
        {
            for (int i = 0; i < CHUNCKSIZE + 1; i++)
            {
                verticesList.Add(new Vector3(TILESIZE * i, TILEHEIGHT * extendedMap[i + 1][j + 1], TILESIZE * j));
            }
        }

        for (int i = 0; i < CHUNCKSIZE; i++)
        {
            for (int j = 0; j < CHUNCKSIZE; j++)
            {
                float sqrDiag1 = (verticesList[i + j * (CHUNCKSIZE + 1)] + verticesList[i + 1 + (j + 1) * (CHUNCKSIZE + 1)]).sqrMagnitude;
                float sqrDiag2 = (verticesList[i + 1 + j * (CHUNCKSIZE + 1)] + verticesList[i + (j + 1) * (CHUNCKSIZE + 1)]).sqrMagnitude;

                if (sqrDiag1 <= sqrDiag2)
                {
                    trianglesList.Add(i + j * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + 1 + (j + 1) * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + 1 + j * (CHUNCKSIZE + 1));

                    trianglesList.Add(i + j * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + (j + 1) * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + 1 + (j + 1) * (CHUNCKSIZE + 1));
                }
                else
                {
                    trianglesList.Add(i + j * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + (j + 1) * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + 1 + j * (CHUNCKSIZE + 1));

                    trianglesList.Add(i + 1 + (j + 1) * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + 1 + j * (CHUNCKSIZE + 1));
                    trianglesList.Add(i + (j + 1) * (CHUNCKSIZE + 1));
                }
            }
        }

        for (int j = 0; j < CHUNCKSIZE + 1; j++)
        {
            for (int i = 0; i < CHUNCKSIZE + 1; i++)
            {
                Vector3 v1 = new Vector3(1, extendedMap[i + 2][j + 1] - extendedMap[i + 1][j + 1], 0);
                Vector3 v2 = new Vector3(0, extendedMap[i + 1][j + 2] - extendedMap[i + 1][j + 1], 1);
                Vector3 v3 = new Vector3(-1, extendedMap[i][j + 1] - extendedMap[i + 1][j + 1], 0);
                Vector3 v4 = new Vector3(0, extendedMap[i + 1][j] - extendedMap[i + 1][j + 1], -1);

                Vector3 n1 = Vector3.Cross(v2, v1).normalized;
                Vector3 n2 = Vector3.Cross(v4, v3).normalized;

                normalsList.Add((n1 + n2).normalized);
            }
        }

        m.vertices = verticesList.ToArray();
        m.triangles = trianglesList.ToArray();
        m.normals = normalsList.ToArray();

        return m;
    }
Example #9
0
    public void SetBlock(Vector3 worldPos, Block b)
    {
        Vector3 localPos;

        localPos = worldPos - transform.position;

        if (localPos.x > (Width))
        {
            return;
        }

        if (Mathf.FloorToInt(localPos.x) >= Width || Mathf.FloorToInt(localPos.y) >= Height || Mathf.FloorToInt(localPos.z) >= Width ||
            Mathf.FloorToInt(localPos.x) < 0 || Mathf.FloorToInt(localPos.y) < 0 || Mathf.FloorToInt(localPos.z) < 0)
        {
        }
        else
        {
            //if(Input.GetMouseButtonDown(0)){
            //if(map[Mathf.FloorToInt(localPos.x), Mathf.FloorToInt(localPos.y), Mathf.FloorToInt(localPos.z)].TextureHp > 0){
            //map[Mathf.FloorToInt(localPos.x), Mathf.FloorToInt(localPos.y), Mathf.FloorToInt(localPos.z)].TextureHp -= 1;
            //Debug.Log(map[Mathf.FloorToInt(localPos.x), Mathf.FloorToInt(localPos.y), Mathf.FloorToInt(localPos.z)].TextureHp);
            //	return;
            //}
            //else{
            map[Mathf.FloorToInt(localPos.x), Mathf.FloorToInt(localPos.y), Mathf.FloorToInt(localPos.z)] = b;
            //}
            //}
            //if(Input.GetMouseButtonDown(1)){

            //map[Mathf.FloorToInt(localPos.x), Mathf.FloorToInt(localPos.y), Mathf.FloorToInt(localPos.z)] = b;
            //}
        }

        StartCoroutine(RecalculateMesh());

        if (b != null)
        {
            return;
        }
        if (Mathf.FloorToInt(localPos.x) >= Width - 1)
        {
            if (right == null)
            {
                right = GetChunck(Mathf.FloorToInt(worldPos.x + 1), Mathf.FloorToInt(worldPos.y), Mathf.FloorToInt(worldPos.z));
            }
            StartCoroutine(right.RecalculateMesh());
        }
        if (Mathf.FloorToInt(localPos.x) <= 1)
        {
            if (left == null)
            {
                left = GetChunck(Mathf.FloorToInt(worldPos.x - 1), Mathf.FloorToInt(worldPos.y), Mathf.FloorToInt(worldPos.z));
            }
            StartCoroutine(left.RecalculateMesh());
        }
        if (Mathf.FloorToInt(localPos.z) >= Width - 1)
        {
            if (front == null)
            {
                front = GetChunck(Mathf.FloorToInt(worldPos.x), Mathf.FloorToInt(worldPos.y), Mathf.FloorToInt(worldPos.z + 1));
            }
            StartCoroutine(front.RecalculateMesh());
        }
        if (Mathf.FloorToInt(localPos.z) <= 1)
        {
            if (back == null)
            {
                back = GetChunck(Mathf.FloorToInt(worldPos.x), Mathf.FloorToInt(worldPos.y), Mathf.FloorToInt(worldPos.z - 1));
            }
            StartCoroutine(back.RecalculateMesh());
        }
        if (Mathf.FloorToInt(localPos.y) >= Height - 1)
        {
            if (top == null)
            {
                top = GetChunck(Mathf.FloorToInt(worldPos.x), Mathf.FloorToInt(worldPos.y + 1), Mathf.FloorToInt(worldPos.z - 1));
            }
            StartCoroutine(top.RecalculateMesh());
        }
        if (Mathf.FloorToInt(localPos.y) <= 1)
        {
            if (bottom == null)
            {
                bottom = GetChunck(Mathf.FloorToInt(worldPos.x), Mathf.FloorToInt(worldPos.y - 1), Mathf.FloorToInt(worldPos.z - 1));
                //bottom2 = GetChunck(Mathf.FloorToInt(worldPos.x), Mathf.FloorToInt(worldPos.y - 4), Mathf.FloorToInt(worldPos.z - 1));
                //if (bottom2 == null)
                //{

                //}
                //else
                //{
                //    int zCoordinate = Mathf.FloorToInt((worldPos.z / Width)) * Width;
                //    int xCoordinate = Mathf.FloorToInt((worldPos.x / Width)) * Width;
                //    int yCoordinate = Mathf.FloorToInt((worldPos.y - 4 / Height)) * Height;
                //    GameObject go = Instantiate(chunckPrefab, new Vector3(xCoordinate, yCoordinate, zCoordinate), Quaternion.identity) as GameObject;
                //    go.GetComponent<Chunck>().StartFunction();
                //}
            }
            StartCoroutine(bottom.RecalculateMesh());
        }
    }
Example #10
0
    bool isBlockTransparent(int x, int y, int z)
    {
        if (x >= Width)
        {
            Vector3 worldPosition = new Vector3(x, y, z) + transform.position;

            if (right == null)
            {
                right = GetChunck(Mathf.FloorToInt(worldPosition.x), Mathf.FloorToInt(worldPosition.y), Mathf.FloorToInt(worldPosition.z));
            }
            if (right != this && right != null && right.generatedMap == true)
            {
                if (right.GetBlock(worldPosition) == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        if (x < 0)
        {
            Vector3 worldPosition = new Vector3(x, y, z) + transform.position;

            if (left == null)
            {
                left = GetChunck(Mathf.FloorToInt(worldPosition.x), Mathf.FloorToInt(worldPosition.y), Mathf.FloorToInt(worldPosition.z));
            }
            if (left != this && left != null && left.generatedMap == true)
            {
                if (left.GetBlock(worldPosition) == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        if (z >= Width)
        {
            Vector3 worldPosition = new Vector3(x, y, z) + transform.position;

            if (front == null)
            {
                front = GetChunck(Mathf.FloorToInt(worldPosition.x), Mathf.FloorToInt(worldPosition.y), Mathf.FloorToInt(worldPosition.z));
            }
            if (front != this && front != null && front.generatedMap == true)
            {
                if (front.GetBlock(worldPosition) == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        if (z < 0)
        {
            Vector3 worldPosition = new Vector3(x, y, z) + transform.position;

            if (back == null)
            {
                back = GetChunck(Mathf.FloorToInt(worldPosition.x), Mathf.FloorToInt(worldPosition.y), Mathf.FloorToInt(worldPosition.z));
            }
            if (back != this && back != null && back.generatedMap == true)
            {
                if (back.GetBlock(worldPosition) == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        if (y >= Height)
        {
            Vector3 worldPosition = new Vector3(x, y, z) + transform.position;

            if (top == null)
            {
                top = GetChunck(Mathf.FloorToInt(worldPosition.x), Mathf.FloorToInt(worldPosition.y), Mathf.FloorToInt(worldPosition.z));
            }
            if (top != this && top != null && top.generatedMap == true)
            {
                if (top.GetBlock(worldPosition) == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }
        if (y < 0)
        {
            Vector3 worldPosition = new Vector3(x, y, z) + transform.position;

            if (bottom == null)
            {
                bottom = GetChunck(Mathf.FloorToInt(worldPosition.x), Mathf.FloorToInt(worldPosition.y), Mathf.FloorToInt(worldPosition.z));
            }
            if (bottom != this && bottom != null && bottom.generatedMap == true)
            {
                if (bottom.GetBlock(worldPosition) == null)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }

        if (map[x, y, z] == null)
        {
            return(true);
        }
        if (map[x, y, z].Trasnsparent)
        {
            return(true);
        }

        return(false);
    }
    static public void TrySetMesh(Chunck c)
    {
        if (c == null)
        {
            return;
        }

        int i = c.iPos;
        int j = c.jPos;
        Chunck[][] sideChuncks = new Chunck[3][];
        for (int x = 0; x < 3; x++)
        {
            sideChuncks[x] = new Chunck[3];
            for (int y = 0; y < 3; y++)
            {
                sideChuncks[x][y] = chuncks.Find(o => (o.iPos == i - 1 + x) && (o.jPos == j - 1 + y));
                if (sideChuncks[x][y] == null)
                {
                    return;
                }
            }
        }
        c.SetMesh(sideChuncks);
    }
 static public void UpdateChunckBlocks(Chunck chunck)
 {
     chunck.SetBlock();
 }
 static public void UpdateChunckMesh(Chunck chunck)
 {
     TrySetMesh(chunck);
 }