Example #1
0
    public static bool GenerateMesh(Globals.MeshData Mesh, BlockClass Block)
    {
        bool refreshRequired = false;

        // if (State < StateEnum.ValidCP) {
        //     CalculateControlPoint(this);
        // }
        Block.State = StateEnum.Meshed;
        for (byte Dir = 0; Dir < 6; Dir++)
        {
            var B0 = (byte)(Block.SubBlock.Occlude & (1 << Dir));
            if (B0 <= 0)
            {
                continue;
            }
            if (!(Block.Neighbor[Globals.DirectionVector[Dir].x + 1, Globals.DirectionVector[Dir].y + 1, Globals.DirectionVector[Dir].z + 1] == null))
            {
                var B1 = (byte)(Block.Neighbor[Globals.DirectionVector[Dir].x + 1, Globals.DirectionVector[Dir].y + 1, Globals.DirectionVector[Dir].z + 1].SubBlock.Occlude & (1 << (5 - Dir)));
                if (B1 == 0)
                {
                    if (Block.addFace(Mesh, Dir))
                    {
                        refreshRequired = true;
                    }
                    else
                    {
                        Block.State = StateEnum.ValidCP;
                        return(false);
                    }
                }
            }
        }
        return(refreshRequired);
    }
Example #2
0
    public void OnCollision2D(Collision2D collider)
    {
        if (!wasCollision && collider.gameObject.tag != "Catapult")
        {
            // GameObject.Find("Manager").GetComponent<ObjectManagerScript>().SetNextVagetable();
            wasCollision = true;
        }
        int damage;

        switch (collider.gameObject.tag)
        {
        case "Block":
            BlockClass block = collider.gameObject.GetComponent <BlockController>().Block;
            damage = Mathf.RoundToInt(Damage *
                                      (block.CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude +
                                       CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.velocity.magnitude) * PhysicsConstants.MagnitudeCoefficient *
                                      CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.mass *PhysicsConstants.MassCoefficient);
            block.Health -= damage;

            Debug.Log("Vegetable to block   :   " + damage);

            block.CheckHealth();
            break;

        case "Meat":
            MeatClass meat = collider.gameObject.GetComponent <MeatController>().Meat;
            damage = Mathf.RoundToInt(Damage *
                                      (meat.CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude +
                                       CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.velocity.magnitude) * PhysicsConstants.MagnitudeCoefficient *
                                      CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.mass *PhysicsConstants.MassCoefficient);
            meat.Health -= damage;

            Debug.Log("Vegetable to meat   :   " + damage);

            meat.CheckHealth();
            break;

        case "Catapult":

            break;

        case "Vegetable":

            break;

        default:
            damage = Mathf.RoundToInt(PhysicsConstants.DefaultDamage *
                                      CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.velocity.magnitude *PhysicsConstants.MagnitudeCoefficient *
                                      CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.mass *PhysicsConstants.MassCoefficient);
            Health -= damage;

            Debug.Log("Default to vegetable   :   " + damage);

            CheckHealth();
            break;
        }
    }
Example #3
0
 private void MatchAllBlocks()
 {
     foreach (GameObject fooObj in GameObject.FindGameObjectsWithTag("gameblock"))
     {
         if (fooObj.name == "Block(Clone)")
         {
             BlockClass solver = (BlockClass)fooObj.GetComponent(typeof(BlockClass));
             solver.ClearIfGray();
             solver.ClearAllMatches();
         }
     }
 }
Example #4
0
    // Status and states
    public ChunkObject()
    {
        _solidGeometry  = new GeometryData();
        _liquidGeometry = new GeometryData();

        Blocks = new BlockClass[BlockProperties.ChunkSizeP2][][];

        for (var x = 0; x < BlockProperties.ChunkSize + 2; x++)
        {
            Blocks[x] = new BlockClass[BlockProperties.ChunkSizeP2][];
            for (var y = 0; y < BlockProperties.ChunkSizeP2; y++)
            {
                Blocks[x][y] = new BlockClass[BlockProperties.ChunkSizeP2];
            }
        }
    }
Example #5
0
    public bool SetNeighbor(BlockClass B, Vector3Int Offset)
    {
        Neighbor[Offset.x + 1, Offset.y + 1, Offset.z + 1] = B;
        if (Offset.x >= 0 && Offset.y >= 0 && Offset.z >= 0)  // Because only positive offsets are used for CP calculations.  Any change to negative neighbor doesn't effect CP
        {
            State = StateEnum.Seeded;
        }

        if (B.Neighbor[-Offset.x + 1, -Offset.y + 1, -Offset.z + 1] != this)
        {
            B.Neighbor[-Offset.x + 1, -Offset.y + 1, -Offset.z + 1] = this;
            if (Offset.x <= 0 && Offset.y <= 0 && Offset.z <= 0)
            {   // Neighbor would have to recalc CP because Parent is positve offset
                B.State = StateEnum.Seeded;
            }
            else if (B.state > StateEnum.ValidCP)
            {
                // If Neighbor had already generated its mesh, it will need to remesh.
                B.State = StateEnum.ValidCP;
            }
        }
        return(true);
    }
Example #6
0
    public void InitChunk()
    {
        // SW.Reset();
        // SW.Start();
        Mesh.Clear();
        //Mesh meshData = gameObject.GetComponent<MeshFilter>().mesh;
        meshData.Clear();

        State = StateEnum.Raw;
        this.gameObject.SetActive(true);
        // Initialize Neighbor Block
        Neighbor = new ChunkClass[3, 3, 3];
        // Set Center Block to own Block
        Neighbor[1, 1, 1] = this;

        Blocks = new BlockClass[Globals.ChunkSize][][];
        for (int x = 0; x < Globals.ChunkSize; x++)
        {
            Blocks[x] = new BlockClass[Globals.ChunkSize][];
            for (int y = 0; y < Globals.ChunkSize; y++)
            {
                Blocks[x][y] = new BlockClass[Globals.ChunkSize];
                for (int z = 0; z < Globals.ChunkSize; z++)
                {
                    Blocks[x][y][z]       = new BlockClass(Position + new Vector3Int(x, y, z));
                    Blocks[x][y][z].Chunk = this;
                }
            }
        }
        LinkChunks();
        LinkBlocks();
        State = StateEnum.Initialized;
        Seed();
        StartCoroutine(UpdateItAllCR());
        //SW.Stop();
        //UnityEngine.Debug.Log(SW.ElapsedMilliseconds);
    }
Example #7
0
    public void InitBlockFromCube(Vector3 blockOrigin)
    {
        var G = gameObject;

        Block = new BlockClass(WorldScript.ActiveWorld.ActiveBlockType, blockOrigin);
        chunkVertices.Clear();
        chunkTriangles.Clear();
        chunkUV.Clear();
        //####################################
        Corners.Add(new Vector3(0.5f, 0.5f, 0.5f));
        Corners.Add(new Vector3(-0.5f, 0.5f, 0.5f));
        Corners.Add(new Vector3(0.5f, -0.5f, 0.5f));
        Corners.Add(new Vector3(-0.5f, -0.5f, 0.5f));
        Corners.Add(new Vector3(0.5f, 0.5f, -0.5f));
        Corners.Add(new Vector3(-0.5f, 0.5f, -0.5f));
        Corners.Add(new Vector3(0.5f, -0.5f, -0.5f));
        Corners.Add(new Vector3(-0.5f, -0.5f, -0.5f));
        for (byte Dir = 0; Dir < 6; Dir++)
        {
            var DirUV = Dir;
            for (var i = 0; i < 4; i++)
            {
                //Vector3 Pt = BlockProperties.FacePts[i];
                //Pt *= 0.5f;
                //Corners.Add(BlockProperties.FacePts[BlockProperties.BlockFaces[Dir, i]] + new Vector3(0.5f, 0.5f, 0.5f));
                var Pnt = BlockProperties.FacePts[BlockProperties.BlockFaces[Dir, i]] + new Vector3(0.5f, 0.5f, 0.5f);

                chunkVertices.Add(Pnt);
            }

            var V1 = chunkVertices[chunkVertices.Count - 1] - chunkVertices[chunkVertices.Count - 2];
            var V2 = chunkVertices[chunkVertices.Count - 3] - chunkVertices[chunkVertices.Count - 2];
            var N  = Vector3.Cross(V1, V2).normalized;
            if (N.y > .3)
            {
                DirUV = (byte)BlockClass.Direction.Up;
            }
            var sc = chunkVertices.Count - 4;
            chunkTriangles.Add(sc);
            chunkTriangles.Add(sc + 1);
            chunkTriangles.Add(sc + 3);
            chunkTriangles.Add(sc + 1);
            chunkTriangles.Add(sc + 2);
            chunkTriangles.Add(sc + 3);
            var UV = Block.GetTex();
            var uv = new Vector2(UV[DirUV].x / 16f, (15 - UV[DirUV].y) / 16f);
            chunkUV.Add(uv);
            chunkUV.Add(new Vector2(uv.x + BlockProperties.TUnit, uv.y));
            chunkUV.Add(new Vector2(uv.x + BlockProperties.TUnit, uv.y + BlockProperties.TUnit));
            chunkUV.Add(new Vector2(uv.x, uv.y + BlockProperties.TUnit));
        }

        //####################################
        // Generate Mesh
        Mesh         Rmesh;
        MeshCollider Rcol;

        Rmesh = G.GetComponent <MeshFilter>().mesh;
        Rcol  = G.GetComponent <MeshCollider>();
        Rmesh.Clear();
        Rmesh.vertices  = chunkVertices.ToArray();
        Rmesh.triangles = chunkTriangles.ToArray();
        Rmesh.uv        = chunkUV.ToArray();
        Rmesh.RecalculateNormals();
        if (G.GetComponent <MeshCollider>())
        {
            Rcol.sharedMesh = Rmesh;
        }
    }
 public void AddBlock(BlockClass block, Vector2 Position, Quaternion Rotation)
 {
     block.Position = Position;
     block.Rotation = Rotation;
     Blocks.Add(block);
 }
Example #9
0
    public void Seed()
    {
        //Basic Height Information
        MyNoise.SetAxisScales(1, 1, 1);
        MyNoise.SetNoiseType(FastNoiseSIMD.NoiseType.Cubic);
        MyNoise.SetFrequency(0.01f);
        var LongPeriod = MyNoise.GetSampledNoiseSet(_position.x, 0, _position.z, BlockProperties.ChunkSizeP2, 1,
                                                    BlockProperties.ChunkSizeP2, 1);

        MyNoise.SetNoiseType(FastNoiseSIMD.NoiseType.SimplexFractal);
        MyNoise.SetFractalType(FastNoiseSIMD.FractalType.FBM);
        MyNoise.SetFrequency(0.01f);
        var ShortPeriod = MyNoise.GetSampledNoiseSet(_position.x, 0, _position.z, BlockProperties.ChunkSizeP2, 1,
                                                     BlockProperties.ChunkSizeP2, 1);

        MyNoise.SetNoiseType(FastNoiseSIMD.NoiseType.SimplexFractal);
        MyNoise.SetFractalType(FastNoiseSIMD.FractalType.Billow);
        MyNoise.SetFrequency(0.01f);
        MyNoise.SetAxisScales(1, 1, 2f);
        var CavePeriod = MyNoise.GetNoiseSet(_position.x, _position.z, _position.y, BlockProperties.ChunkSizeP2,
                                             BlockProperties.ChunkSizeP2, BlockProperties.ChunkSizeP2, 1f);

        MyNoise.SetNoiseType(FastNoiseSIMD.NoiseType.SimplexFractal);
        MyNoise.SetFractalType(FastNoiseSIMD.FractalType.Billow);
        MyNoise.SetFrequency(0.01f);
        MyNoise.SetAxisScales(2, 2, 2);
        var OutCroppingsPeriod = MyNoise.GetNoiseSet(_position.x, _position.z, _position.y, BlockProperties.ChunkSizeP2,
                                                     BlockProperties.ChunkSizeP2, BlockProperties.ChunkSizeP2, 1f);

        var index = 0;
        //Cave Information
        var index2 = 0;
        var index3 = 0;

        for (var X = 0; X < BlockProperties.ChunkSizeP2; X++)
        {
            for (var Z = 0; Z < BlockProperties.ChunkSizeP2; Z++)
            {
                var LP = LongPeriod[index];
                var SP = ShortPeriod[index];
                index++;

                //Set Altitude
                for (var Y = 0; Y < BlockProperties.ChunkSizeP2; Y++)
                {
                    var WPt = _position + new Vector3(X, Y, Z);
                    var B   = new BlockClass(BlockClass.BlockType.Grass, WPt);
                    if (WPt.y == 0)
                    {
                        B = new BlockClass(BlockClass.BlockType.BedRock, WPt);
                    }
                    else
                    {
                        var i = LP * 32f + SP * 16f; // Number 0 to 128
                        i = i + BlockProperties.chunkDistance.y * 8 - WPt.y;
                        if (i < 0)
                        {
                            B = new BlockClass(BlockClass.BlockType.Air, WPt);
                        }
                        else if (i > 1)
                        {
                            B = new BlockClass(BlockClass.BlockType.Dirt, WPt);
                        }

                        B.Data.Density =
                            (sbyte)Mathf.Clamp(i * sbyte.MaxValue, sbyte.MinValue, sbyte.MaxValue);
                        if (OutCroppingsPeriod[index3++] > .4f)
                        {
                            B = new BlockClass(BlockClass.BlockType.BedRock, WPt);
                        }

                        if (CavePeriod[index2++] > .3f)
                        {
                            B = new BlockClass(BlockClass.BlockType.Air, WPt);
                        }

                        if ((WPt.y < 30) & (B.Data.Type == BlockClass.BlockType.Air))
                        {
                            B = new BlockClass(BlockClass.BlockType.Water, WPt);
                            B.Data.Density = (sbyte)Mathf.Clamp(i * sbyte.MaxValue, sbyte.MinValue,
                                                                sbyte.MaxValue);
                        }
                    }

                    /*
                     *  if (B.Data.Density <= 0)
                     *      B.Data.Density = (sbyte) Mathf.Clamp(B.Data.Density, -126f, -1f);
                     *  if (B.Data.Density >= 0)
                     *      B.Data.Density = (sbyte)Mathf.Clamp(B.Data.Density, 1f, 127f);*/
                    B.Data.Blockiness = 1;
                    Blocks[X][Y][Z]   = B;
                }
            }
        }
    }
 public BlockClassState(BlockClass blockClass, bool enabled)
 {
     BlockClass = blockClass;
     Enabled    = enabled;
 }
Example #11
0
    public void OnCollision2D(Collision2D collision2D)
    {
        int damage;

        switch (collision2D.gameObject.tag)
        {
        case "Vegetable":
            VegetableClass vegatable = collision2D.gameObject.GetComponent <VegetableController>().Vegetable;
            damage = Mathf.RoundToInt(Damage *
                                      (vegatable.CurrentGameObject.GetComponent <VegetableController>().Rigidbody2D.velocity.magnitude +
                                       CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude) * PhysicsConstants.MagnitudeCoefficient *
                                      CurrentGameObject.GetComponent <Rigidbody2D>().mass *PhysicsConstants.MassCoefficient);
            vegatable.Health -= damage;

            Debug.Log("Meat to vegetable   :   " + damage);

            vegatable.CheckHealth();
            break;

        case "Block":
            BlockClass block = collision2D.gameObject.GetComponent <BlockController>().Block;
            damage = Mathf.RoundToInt(Damage *
                                      (block.CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude +
                                       CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude) * PhysicsConstants.MagnitudeCoefficient *
                                      CurrentGameObject.GetComponent <Rigidbody2D>().mass *PhysicsConstants.MassCoefficient);
            block.Health -= damage;

            Debug.Log("Meat to block   :   " + damage);

            block.CheckHealth();
            break;

        case "Meat":
            MeatClass meat = collision2D.gameObject.GetComponent <MeatController>().Meat;
            damage = Mathf.RoundToInt(Damage *
                                      (meat.CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude +
                                       CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude) * PhysicsConstants.MagnitudeCoefficient *
                                      CurrentGameObject.GetComponent <Rigidbody2D>().mass *PhysicsConstants.MassCoefficient);
            Health -= damage;

            Debug.Log("Meat to meat   :   " + damage);

            CheckHealth();
            break;

        default:
            if (wasFirstCollision)
            {
                damage = Mathf.RoundToInt(PhysicsConstants.DefaultDamage *
                                          CurrentGameObject.GetComponent <Rigidbody2D>().velocity.magnitude *PhysicsConstants.MagnitudeCoefficient *
                                          CurrentGameObject.GetComponent <Rigidbody2D>().mass *PhysicsConstants.MassCoefficient);
                Health -= damage;

                Debug.Log("Default to meat   :   " + damage);

                CheckHealth();
            }
            else
            {
                wasFirstCollision = true;
            }
            break;
        }
    }
Example #12
0
    private void FixedUpdate()
    {
        var Results        = MyPhysics.PhysicsUpdate();
        var NewBlocks      = new List <GameObject>();
        var EffectedChunks = new List <GameObject>();

        for (var i = 0; i < Results.Count; i++)
        {
            var tempBlock = GetBlockMesh(Results[i]); // Extract Block from Mesh
            tempBlock.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.None;
            NewBlocks.Add(tempBlock);
            var LastB = GetBlock(Results[i]);
            var NewB  = new BlockClass(BlockClass.BlockType.Air, LastB.Position);
            NewB.Data.Blockiness   = LastB.Data.Blockiness;
            NewB.Data.Density      = LastB.Data.Density;
            NewB.Data.ControlPoint = LastB.Data.ControlPoint;
            var R = SetBlock(Results[i], NewB);
            foreach (var GO in R)
            {
                if (!EffectedChunks.Contains(GO))
                {
                    EffectedChunks.Add(GO);
                }
            }
        }

        for (var i = 0; i < NewBlocks.Count; i++)
        {
            for (var i2 = i + 1; i2 < NewBlocks.Count; i2++)
            {
                if ((NewBlocks[i].transform.position - NewBlocks[i2].transform.position).sqrMagnitude < 1.1)
                {
                    var FJ = NewBlocks[i].AddComponent <FixedJoint>();
                    FJ.breakForce    = 1000;
                    FJ.breakTorque   = 1000;
                    FJ.connectedBody = NewBlocks[i2].GetComponent <Rigidbody>();
                }
            }
        }

        foreach (var GO in EffectedChunks)
        {
            GO.GetComponent <ChunkObject>().Mesh();
            GO.GetComponent <ChunkObject>().PostMesh();
        }

        RaycastHit RH;
        var        FPC = _player.transform.GetChild(0);

        if (FPC != null)
        {
            var BC = GetBlock(FPC.position);
            Physics.Raycast(FPC.position, FPC.forward, out RH); //Layer mask 0 for chunk
            if (RH.distance < 10)
            {
                // If it hit a chunk object, something meshed into the world
                if (RH.collider)
                {
                    if (RH.collider.gameObject)
                    {
                        if (RH.collider.name == "Solid")
                        {
                            InteractWithChunk(RH);
                        }
                        else if (RH.collider.gameObject.GetComponent <LooseBlockScript>())
                        {
                            InteractWithLoose(RH);
                        }
                    }
                    else
                    {
                        _target.SetActive(false);
                    }
                }
                else
                {
                    _target.SetActive(false);
                }
            }
            else
            {
                _target.SetActive(false);
            }
        }
        else
        {
            _target.SetActive(false);
        }
    }
Example #13
0
    private void InteractWithChunk(RaycastHit RH)
    {
        if (Input.GetMouseButtonDown(0))
        {
            MouseState[0] = true;
        }
        if (Input.GetMouseButtonDown(1))
        {
            MouseState[1] = true;
        }
        var CP = new Vector3Int();

        if (Input.GetMouseButton(1)) // Build Block
        {
            _target.SetActive(true);
            _target.transform.SetPositionAndRotation(Vector3Int.RoundToInt(RH.point + RH.normal * .5f),
                                                     Quaternion.identity);
            CP = Vector3Int.FloorToInt(_target.transform.position);
            _target.GetComponent <LooseBlockScript>().InitBlockFromCube(CP);
            _target.GetComponent <MeshRenderer>().material = TargetMaterial2;
        }
        else if (Input.GetMouseButton(0)) // Build Block
        {
            _target.SetActive(true);
            _target.transform.SetPositionAndRotation(Vector3Int.RoundToInt(RH.point - RH.normal * .5f),
                                                     Quaternion.identity);
            CP = Vector3Int.FloorToInt(_target.transform.position);
            _target.GetComponent <LooseBlockScript>().InitBlockFromWorld(CP);
            _target.GetComponent <MeshRenderer>().material = TargetMaterial1;
        }
        else
        {
            _target.SetActive(false);
        }

        if (Input.GetMouseButtonUp(0)) // Destroy Block
        {
            if (MouseState[0])
            {
                _target.transform.SetPositionAndRotation(Vector3Int.RoundToInt(RH.point - RH.normal * .5f), Quaternion.identity);
                var LastB = GetBlock(_target.transform.position);
                LastB.ChangeTypeTo(BlockClass.BlockType.Air);
                var results = SetBlock(_target.transform.position, LastB);
                foreach (var GO in results)
                {
                    GO.GetComponent <ChunkObject>().RefreshRequired = ChunkObject.RemeshEnum.Face;
                    //GO.GetComponent<ChunkObject>().Face();
                    //GO.GetComponent<ChunkObject>().PostMesh();
                }
                MyPhysics.PhysicsPrefab = _physicsPrefab;
                MyPhysics.RebuildModel(LastB.Position);
            }

            MouseState[0] = false;
        }

        if (Input.GetMouseButtonUp(1)) // Create Block
        {
            if (MouseState[1])
            {
                _target.transform.SetPositionAndRotation(Vector3Int.RoundToInt(RH.point + RH.normal * .5f),
                                                         Quaternion.identity);
                var NewB    = new BlockClass(ActiveWorld.ActiveBlockType, _target.transform.position);
                var results = SetBlock(_target.transform.position, NewB);
                foreach (var GO in results)
                {
                    GO.GetComponent <ChunkObject>().RefreshRequired = ChunkObject.RemeshEnum.Mesh;
                    //GO.GetComponent<ChunkObject>().Mesh();
                    //GO.GetComponent<ChunkObject>().PostMesh();
                }

                MyPhysics.PhysicsPrefab = _physicsPrefab;
                MyPhysics.RebuildModel(_target.transform.position);
            }

            MouseState[1] = false;
        }
    }
Example #14
0
 private void LinkBlocks()
 {
     Pos = Position;
     P   = new Vector3Int();
     for (int y = 0; y < Globals.ChunkSize; y++)
     {
         for (int z = 0; z < Globals.ChunkSize; z++)
         {
             for (int x = 0; x < Globals.ChunkSize; x++)
             {
                 P.x = x;
                 P.y = y;
                 P.z = z;
                 for (int i = 0; i < Globals.NeighborOffset.Length; i++)
                 {
                     V  = Globals.NeighborOffset[i];
                     B0 = Blocks[x][y][z];
                     R  = P + V;
                     if (R.x >= 0 && R.y >= 0 && R.z >= 0 && R.x < Globals.ChunkSize && R.y < Globals.ChunkSize && R.z < Globals.ChunkSize)
                     {
                         B0.Neighbor[V.x + 1, V.y + 1, V.z + 1] = Blocks[V.x + x][V.y + y][V.z + z];
                         //B0.SetNeighbor(Blocks[R.x][R.y][R.z]);
                     }
                     else
                     {
                         ChunkOffset = new Vector3Int(1, 1, 1);
                         if (R.x < 0)
                         {
                             ChunkOffset.x = 0;
                         }
                         if (R.y < 0)
                         {
                             ChunkOffset.y = 0;
                         }
                         if (R.z < 0)
                         {
                             ChunkOffset.z = 0;
                         }
                         if (R.x >= Globals.ChunkSize)
                         {
                             ChunkOffset.x = 2;
                         }
                         if (R.y >= Globals.ChunkSize)
                         {
                             ChunkOffset.y = 2;
                         }
                         if (R.z >= Globals.ChunkSize)
                         {
                             ChunkOffset.z = 2;
                         }
                         if (Neighbor[ChunkOffset.x, ChunkOffset.y, ChunkOffset.z] != null)
                         {
                             N = new Vector3Int(R.x, R.y, R.z);
                             while (N.x < 0)
                             {
                                 N.x += Globals.ChunkSize;
                             }
                             while (N.y < 0)
                             {
                                 N.y += Globals.ChunkSize;
                             }
                             while (N.z < 0)
                             {
                                 N.z += Globals.ChunkSize;
                             }
                             while (N.x >= Globals.ChunkSize)
                             {
                                 N.x -= Globals.ChunkSize;
                             }
                             while (N.y >= Globals.ChunkSize)
                             {
                                 N.y -= Globals.ChunkSize;
                             }
                             while (N.z >= Globals.ChunkSize)
                             {
                                 N.z -= Globals.ChunkSize;
                             }
                             B0.SetNeighbor(Neighbor[ChunkOffset.x, ChunkOffset.y, ChunkOffset.z].Blocks[N.x][N.y][N.z], V);
                         }
                     }
                 }
             }
         }
     }
 }
Example #15
0
    public void InitBlockFromWorld(Vector3 Pnt)
    {
        gameObject.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
        var ChunkPos = Vector3Int.FloorToInt(Pnt / 16f) * 16;
        var BlockPos = Vector3Int.FloorToInt(Pnt) - ChunkPos;

        //Test that block is contained in Block
        if ((BlockPos.x >= 0) & (BlockPos.y >= 0) & (BlockPos.z >= 0) & (BlockPos.x < BlockProperties.ChunkSize) &
            (BlockPos.y < BlockProperties.ChunkSize) &
            (BlockPos.z < BlockProperties.ChunkSize))
        {
            GameObject tempChunk;
            if (WorldScript.ActiveWorld.Chunks.TryGetValue(ChunkPos, out tempChunk))
            {
                var CO = tempChunk.GetComponent <ChunkObject>();
                Block = CO.GetBlock(BlockPos);
                for (var i = 0; i < 8; i++)
                {
                    Corners.Add(CO.GetBlock(BlockPos + BlockProperties.FacePts[i]).Data.ControlPoint +
                                BlockProperties.FacePts[i]);
                }
                chunkVertices.Clear();
                chunkTriangles.Clear();
                chunkUV.Clear();
                //####################################
                for (byte Dir = 0; Dir < 6; Dir++)
                {
                    var DirUV = Dir;
                    var B0    = (byte)(Block.Data.Occlude & (1 << Dir));
                    if (B0 > 0)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            chunkVertices.Add(BlockProperties.FacePts[BlockProperties.BlockFaces[Dir, i]] +
                                              CO.GetBlock(
                                                  BlockPos + BlockProperties.FacePts[
                                                      BlockProperties.BlockFaces[Dir, i]]).Data.ControlPoint);
                        }
                        var V1 = chunkVertices[chunkVertices.Count - 1] - chunkVertices[chunkVertices.Count - 2];
                        var V2 = chunkVertices[chunkVertices.Count - 3] - chunkVertices[chunkVertices.Count - 2];
                        var N  = Vector3.Cross(V1, V2).normalized;
                        if (N.y > .3)
                        {
                            DirUV = (byte)BlockClass.Direction.Up;
                        }
                        var sc = chunkVertices.Count - 4;
                        chunkTriangles.Add(sc);
                        chunkTriangles.Add(sc + 1);
                        chunkTriangles.Add(sc + 3);
                        chunkTriangles.Add(sc + 1);
                        chunkTriangles.Add(sc + 2);
                        chunkTriangles.Add(sc + 3);
                        var UV = Block.GetTex();
                        var uv = new Vector2(UV[DirUV].x / 16f, (15 - UV[DirUV].y) / 16f);
                        chunkUV.Add(uv);
                        chunkUV.Add(new Vector2(uv.x + BlockProperties.TUnit, uv.y));
                        chunkUV.Add(new Vector2(uv.x + BlockProperties.TUnit, uv.y + BlockProperties.TUnit));
                        chunkUV.Add(new Vector2(uv.x, uv.y + BlockProperties.TUnit));
                    }
                }

                //####################################
                // Generate Mesh
                Mesh         Rmesh;
                MeshCollider Rcol;
                Rmesh = GetComponent <MeshFilter>().mesh;
                Rcol  = GetComponent <MeshCollider>();
                Rmesh.Clear();
                Rmesh.vertices  = chunkVertices.ToArray();
                Rmesh.triangles = chunkTriangles.ToArray();
                Rmesh.uv        = chunkUV.ToArray();
                Rmesh.RecalculateNormals();
                Rcol.sharedMesh = Rmesh;
            }
        }
    }
Example #16
0
    public void MeldBlockIntoWorld()
    {
        var affectedChunks = new List <GameObject>();
        var pnt            = transform.position;
        var pntI           = Vector3Int.RoundToInt(pnt);

        // Apply the blocks resulting transformation to each point of the original block
        var T   = transform.rotation;
        var vec = transform.eulerAngles;

        vec.x = Mathf.RoundToInt(vec.x / 90) * 90;
        vec.y = Mathf.RoundToInt(vec.y / 90) * 90;
        vec.z = Mathf.RoundToInt(vec.z / 90) * 90;
        //transform.eulerAngles = vec;
        Quaternion euler = Quaternion.Euler(vec);

        //var T = Quaternion; transform.rotation;
        if (Corners.Count < 8)
        {
            return;
        }
        for (var i = 0; i < Corners.Count; i++)
        {
            Corners[i] = T * Corners[i];
        }

        List <Vector3Int> newPositions = new List <Vector3Int>();
        int anchor = 0;

        for (var i = 0; i < BlockProperties.FaceBlocksCenter.Length; i++)
        {
            newPositions.Add(Vector3Int.FloorToInt(euler * BlockProperties.FaceBlocksCenter[i]));
            if (newPositions[i].x >= 0 & newPositions[i].y >= 0 & newPositions[i].z >= 0)
            {
                anchor = i;
            }
        }


        var Delta = transform.position - pntI;

        for (var i = 0; i < newPositions.Count; i++)
        {
            Vector3Int newPos = newPositions[i] + pntI;
            BlockClass B      = WorldScript.ActiveWorld.GetBlock(newPos);
            if (B == null)
            {
                B = new BlockClass(BlockClass.BlockType.Grass, this.Block.Position);
            }
            if (i == anchor)
            {
                B = Block;
            }
            Vector3 newCp = Corners[i] - newPositions[i] + Delta;
            if (B.Data.CpLocked)
            {
                B.Data.ControlPoint = (newCp + B.Data.ControlPoint) / 2f;
            }
            else
            {
                B.Data.ControlPoint = newCp;
            }

            B.Data.ControlPoint.x = Mathf.Clamp01(B.Data.ControlPoint.x);
            B.Data.ControlPoint.y = Mathf.Clamp01(B.Data.ControlPoint.y);
            B.Data.ControlPoint.z = Mathf.Clamp01(B.Data.ControlPoint.z);
            B.Data.CpLocked       = true;
            affectedChunks.AddRange(WorldScript.ActiveWorld.SetBlock(newPos, B));
        }
        foreach (var go2 in affectedChunks)
        {
            go2.GetComponent <ChunkObject>().RefreshRequired = ChunkObject.RemeshEnum.Mesh;
        }
        DestroyBlock();
    }
Example #17
0
    public List <GameObject> SetBlock(Vector3 Pnt, BlockClass B)
    {
        var Affected = new List <GameObject>();
        var PS       = BlockProperties.GetPosition(Pnt);
        var BlockPos = PS.BlockInChunk;

        //Test that block is contained in Block
        if ((BlockPos.x >= 0) & (BlockPos.y >= 0) & (BlockPos.z >= 0) & (BlockPos.x < BlockProperties.ChunkSize) &
            (BlockPos.y < BlockProperties.ChunkSize) &
            (BlockPos.z < BlockProperties.ChunkSize))
        {
            GameObject tempChunk;
            if (Chunks.TryGetValue(PS.ChunkInWorld, out tempChunk))
            {
                tempChunk.GetComponent <ChunkObject>().Blocks[BlockPos.x + 1][BlockPos.y + 1][BlockPos.z + 1] = B;
                Affected.Add(tempChunk);
                var ChunkOffset = new Vector3Int();
                if (BlockPos.x == 0)
                {
                    ChunkOffset.x--;
                }
                if (BlockPos.y == 0)
                {
                    ChunkOffset.y--;
                }
                if (BlockPos.z == 0)
                {
                    ChunkOffset.z--;
                }
                if (BlockPos.x == BlockProperties.ChunkSize - 1)
                {
                    ChunkOffset.x++;
                }
                if (BlockPos.y == BlockProperties.ChunkSize - 1)
                {
                    ChunkOffset.y++;
                }
                if (BlockPos.z == BlockProperties.ChunkSize - 1)
                {
                    ChunkOffset.z++;
                }
                if (ChunkOffset.x != 0)
                {
                    if (Chunks.TryGetValue(PS.ChunkInWorld + new Vector3Int(ChunkOffset.x * 16, 0, 0), out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>()
                        .Blocks[BlockPos.x + 1 - ChunkOffset.x * BlockProperties.ChunkSize][
                            BlockPos.y + 1][BlockPos.z + 1] = B;
                        Affected.Add(tempChunk);
                    }
                }

                if (ChunkOffset.y != 0)
                {
                    if (Chunks.TryGetValue(PS.ChunkInWorld + new Vector3Int(0, ChunkOffset.y * 16, 0), out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>().Blocks[BlockPos.x + 1][
                            BlockPos.y + 1 - ChunkOffset.y * BlockProperties.ChunkSize][BlockPos.z + 1] = B;
                        Affected.Add(tempChunk);
                    }
                }

                if (ChunkOffset.z != 0)
                {
                    if (Chunks.TryGetValue(PS.ChunkInWorld + new Vector3Int(0, 0, ChunkOffset.z * 16), out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>().Blocks[BlockPos.x + 1][BlockPos.y + 1][
                            BlockPos.z + 1 - ChunkOffset.z * BlockProperties.ChunkSize] = B;
                        Affected.Add(tempChunk);
                    }
                }

                if ((ChunkOffset.x != 0) & (ChunkOffset.y != 0))
                {
                    if (Chunks.TryGetValue(PS.ChunkInWorld + new Vector3Int(ChunkOffset.x * 16, ChunkOffset.y * 16, 0),
                                           out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>()
                        .Blocks[BlockPos.x + 1 - ChunkOffset.x * BlockProperties.ChunkSize][
                            BlockPos.y + 1 - ChunkOffset.y * BlockProperties.ChunkSize][BlockPos.z + 1] = B;
                        Affected.Add(tempChunk);
                    }
                }

                if ((ChunkOffset.x != 0) & (ChunkOffset.z != 0))
                {
                    if (Chunks.TryGetValue(PS.ChunkInWorld + new Vector3Int(ChunkOffset.x * 16, 0, ChunkOffset.z * 16),
                                           out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>()
                        .Blocks[BlockPos.x + 1 - ChunkOffset.x * BlockProperties.ChunkSize]
                        [BlockPos.y + 1][BlockPos.z + 1 - ChunkOffset.z * BlockProperties.ChunkSize] = B;
                        Affected.Add(tempChunk);
                    }
                }

                if ((ChunkOffset.y != 0) & (ChunkOffset.z != 0))
                {
                    if (Chunks.TryGetValue(PS.ChunkInWorld + new Vector3Int(0, ChunkOffset.y * 16, ChunkOffset.z * 16),
                                           out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>().Blocks[BlockPos.x + 1][
                            BlockPos.y + 1 - ChunkOffset.y * BlockProperties.ChunkSize][
                            BlockPos.z + 1 - ChunkOffset.z * BlockProperties.ChunkSize]
                            = B;
                        Affected.Add(tempChunk);
                    }
                }

                if ((ChunkOffset.x != 0) & (ChunkOffset.y != 0) & (ChunkOffset.z != 0))
                {
                    if (Chunks.TryGetValue(
                            PS.ChunkInWorld + new Vector3Int(ChunkOffset.x * 16, ChunkOffset.y * 16, ChunkOffset.z * 16),
                            out tempChunk))
                    {
                        tempChunk.GetComponent <ChunkObject>()
                        .Blocks[BlockPos.x + 1 - ChunkOffset.x * BlockProperties.ChunkSize][
                            BlockPos.y + 1 - ChunkOffset.y * BlockProperties.ChunkSize][
                            BlockPos.z + 1 - ChunkOffset.z * BlockProperties.ChunkSize]
                            = B;
                        Affected.Add(tempChunk);
                    }
                }
            }
        }

        return(Affected);
    }