예제 #1
0
        private void CreatePoint(int x, int y, Sprite sprite, BlockSetProfile.BlockInfo blockInfo,
                                 BlockSetProfile.BlockType isColliding, bool noLightEffect)
        {
            var textureSize = new Vector2(sprite.texture.width, sprite.texture.height);
            var uvRect      = sprite.textureRect;

            var         startVert = _vertices.Count;
            const float add       = 0.01f;
            float       z         = isColliding == BlockSetProfile.BlockType.CollidingWall ? 0 : 5;

            _vertices.Add(new Vector3(x - add, y - add, z));
            _vertices.Add(new Vector3(x + 1 + add, y - add, z));
            _vertices.Add(new Vector3(x - add, y + 1 + add, z));
            _vertices.Add(new Vector3(x + 1 + add, y + 1 + add, z));

            _uvs.Add(new Vector2(uvRect.xMin / textureSize.x, uvRect.yMin / textureSize.y)); // 0, 0
            _uvs.Add(new Vector2(uvRect.xMax / textureSize.x, uvRect.yMin / textureSize.y)); // 1, 0
            _uvs.Add(new Vector2(uvRect.xMin / textureSize.x, uvRect.yMax / textureSize.y)); // 0, 1
            _uvs.Add(new Vector2(uvRect.xMax / textureSize.x, uvRect.yMax / textureSize.y)); // 1, 1

            _triangles.Add(startVert + 2);
            _triangles.Add(startVert + 1);
            _triangles.Add(startVert);
            _triangles.Add(startVert + 1);
            _triangles.Add(startVert + 2);
            _triangles.Add(startVert + 3);

            //for (int i = 0; i < 4; i++)
            //{
            _lightAbsorptionColors.Add(noLightEffect ? new Color() : blockInfo.LightAbsorption);
            _lightEmissionColors.Add(noLightEffect ? new Color() : blockInfo.LightEmission);
            //}
        }
예제 #2
0
        private CollMapPoint GetCollMapPoint(int x, int y, BlockSetProfile.BlockType?colliding = null)
        {
            CollMapPoint cachedPoint;

            if (_collMapPoints.TryGetValue(new Point2(x, y), out cachedPoint) &&
                (colliding == null || colliding.Value == cachedPoint.blockType))
            {
                return(cachedPoint);
            }

            float noise = GetNoise(x, y);

            List <BlockSetProfile.BlockInfo> matchingBlockInfos = blockSet.blockInfos.FindAll(b =>
                                                                                              b.minNoise <= noise && b.maxNoise >= noise &&
                                                                                              (colliding == null || colliding.Value == b.blockType));

            if (matchingBlockInfos.Count == 0 && colliding != null)
            {
                matchingBlockInfos = blockSet.blockInfos
                                     .FindAll(b => colliding.Value == b.blockType);
            }

            if (matchingBlockInfos.Count == 0)
            {
                Debug.LogError("No matching blocks found");
                return(default(CollMapPoint));
            }

            SimpleRng rand = new SimpleRng(Util.Hash(seed, x, y));

            BlockSetProfile.BlockInfo blockInfo = matchingBlockInfos
                                                  .RandomElement(bi => bi.weight, rand);

            CollMapPoint point = new CollMapPoint(noise)
            {
                blockInfo = blockInfo, blockType = blockInfo.blockType
            };

            _collMapPoints[new Point2(x, y)] = point;
            return(point);
        }
예제 #3
0
        private void CreatePoint(int x, int y, BlockSetProfile.BlockInfo blockInfo, int compactInfo,
                                 bool noLightEffects, SimpleRng rand, BlockSetProfile.BlockType?isColliding = null)
        {
            Sprite sprite = blockInfo.spriteInfo
                            .RandomElement(ti => 1, rand);

            //if (tilingInfo == null)
            //{
            //    Debug.LogError("Tiling info not found");
            //    return;
            //}

            //var sprite = tilingInfo.Sprite;

            if (sprite == null)
            {
                Debug.LogError("Tiling info is broken");
                return;
            }

            CreatePoint(x, y, sprite, blockInfo, isColliding ?? blockInfo.blockType, noLightEffects);
        }
예제 #4
0
 public CollMapPoint(float noise)
 {
     Noise     = noise;
     BlockInfo = null;
     BlockType = BlockSetProfile.BlockType.Empty;
 }
예제 #5
0
        private GameObject GenerateBlocksJoined(int xOffest, int yOffest)
        {
            SimpleRng rand = new SimpleRng(Util.Hash(seed, xOffest, yOffest));

            _vertices.Clear();
            _uvs.Clear();
            _triangles.Clear();
            _lightAbsorptionColors.Clear();
            _lightEmissionColors.Clear();

            GameObject meshObj = Instantiate(meshObjectPrefab);

            meshObj.name = "Block Mesh { X = " + xOffest / ChunkSize + "; Y = " + yOffest / ChunkSize + " }";
            Transform meshObjTransform = meshObj.transform;

            meshObjTransform.position = meshObjTransform.position.WithXy(xOffest, yOffest);
            meshObjTransform.parent   = container;

            CollMapPoint[,] collMap = new CollMapPoint[ChunkSize, ChunkSize];

            for (int x = 0; x < ChunkSize; x++)
            {
                for (int y = 0; y < ChunkSize; y++)
                {
                    collMap[x, y] = GetCollMapPoint(x + xOffest, y + yOffest);
                }
            }

            for (int x = 0; x < ChunkSize; x++)
            {
                for (int y = 0; y < ChunkSize; y++)
                {
                    BlockSetProfile.BlockInfo blockInfo = collMap[x, y].blockInfo;

                    if (blockInfo.aditionalObjectPrefab != null && blockInfo.aditionalObjectProbability >= rand.Value)
                    {
                        GameObject addObj = Instantiate(blockInfo.aditionalObjectPrefab);

                        addObj.transform.parent        = meshObjTransform;
                        addObj.transform.localPosition = blockInfo.aditionalObjectPrefab
                                                         .transform.position.WithXy(x + 0.5f, y + 0.5f);
                    }

                    if (blockInfo.spriteInfo.Length == 0)
                    {
                        Debug.LogError("Sprite Info is broken");
                        continue;
                    }

                    int compactInfo =
                        (SafeIndex(collMap, x, y + 1, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .blockType == BlockSetProfile.BlockType.CollidingWall
                            ? 1
                            : 0) +
                        (SafeIndex(collMap, x + 1, y, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .blockType == BlockSetProfile.BlockType.CollidingWall
                            ? 2
                            : 0) +
                        (SafeIndex(collMap, x, y - 1, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .blockType == BlockSetProfile.BlockType.CollidingWall
                            ? 4
                            : 0) +
                        (SafeIndex(collMap, x - 1, y, ChunkSize, ChunkSize,
                                   () => GetCollMapPoint(x + xOffest, y + yOffest))
                         .blockType == BlockSetProfile.BlockType.CollidingWall
                            ? 8
                            : 0);

                    CreatePoint(x, y, blockInfo, compactInfo, false, rand);
                }
            }

            Mesh blockMesh = new Mesh {
                vertices  = _vertices.ToArray(),
                uv        = _uvs.ToArray(),
                triangles = _triangles.ToArray()
            };

            blockMesh.RecalculateBounds();

            MeshFilter meshFilter = meshObj.GetComponent <MeshFilter>();

            meshFilter.mesh = blockMesh;

            MeshRenderer meshRenderer = meshObj.GetComponent <MeshRenderer>();
            Texture2D    texture      = blockSet.blockInfos
                                        .First(bi => bi.spriteInfo.Any(si => si != null))
                                        .spriteInfo.First(ti => ti != null)
                                        .texture;
            MaterialPropertyBlock mpb = new MaterialPropertyBlock();

            mpb.SetTexture("_MainTex", texture);
            meshRenderer.SetPropertyBlock(mpb);

            for (int x = 0; x < ChunkSize; x++)
            {
                int yStart = 0;
                for (int y = 0; y < ChunkSize; y++)
                {
                    if (collMap[x, y].blockInfo.blockType != BlockSetProfile.BlockType.CollidingWall)
                    {
                        if (y - yStart > 0)
                        {
                            GameObject obj = new GameObject {
                                layer = meshObj.layer
                            };
                            obj.transform.parent        = meshObjTransform;
                            obj.transform.localPosition = new Vector3(x, 0);
                            obj.name = "Collider x = " + x;
                            BoxCollider2D coll = obj.AddComponent <BoxCollider2D>();
                            coll.size   = new Vector2(1, (y - yStart));
                            coll.offset = new Vector2(0.5f, yStart + coll.size.y / 2f);
                        }
                        yStart = y + 1;
                    }
                }
                if (ChunkSize - yStart > 0)
                {
                    GameObject obj = new GameObject {
                        layer = meshObj.layer
                    };
                    obj.transform.parent        = meshObjTransform;
                    obj.transform.localPosition = new Vector3(x, 0);
                    obj.name = "Collider x = " + x;
                    BoxCollider2D coll = obj.AddComponent <BoxCollider2D>();
                    coll.size   = new Vector2(1, (ChunkSize - yStart));
                    coll.offset = new Vector2(0.5f, yStart + coll.size.y / 2f);
                }
            }

            GameObject lightObstaclesObject = Instantiate(lightObstaclesPrefab);

            lightObstaclesObject.transform.parent        = meshObjTransform;
            lightObstaclesObject.transform.localPosition = Vector3.zero;
            //lightObstaclesObject.transform.localPosition += new Vector3(0, 0, -10);
            MeshFilter lightObstaclesMeshFilter = lightObstaclesObject.GetComponent <MeshFilter>();

            lightObstaclesMeshFilter.mesh = ChunkMeshFromColors(_lightAbsorptionColors);

            GameObject ambientLightObject = Instantiate(ambientLightPrefab);

            ambientLightObject.transform.parent        = meshObjTransform;
            ambientLightObject.transform.localPosition = Vector3.zero;
            //ambientLightObject.transform.localPosition += new Vector3(0, 0, -5);
            MeshFilter ambientLightMeshFilter = ambientLightObject.GetComponent <MeshFilter>();

            ambientLightMeshFilter.mesh = ChunkMeshFromColors(_lightEmissionColors);

            return(meshObj);
        }