Exemple #1
0
    //Creates a new chunk at given position
    public Chunk CreateChunk(int x, int y, int z)
    {
        WorldPos worldPos = new WorldPos(x, y, z);

        //Instantiate the chunk at the cordinates using chunk prefab
        GameObject newChunkObject = Instantiate(chunkPrefab, new Vector3(x, y, z), Quaternion.Euler(Vector3.zero)) as GameObject;
        Chunk      newChunk       = newChunkObject.GetComponent <Chunk>();

        newChunk.pos   = worldPos;
        newChunk.world = this;
        //Adds it to chunk dictonary
        chunks.Add(worldPos, newChunk);

        //Terrain Generation
        TerrainGen terrainGen = new TerrainGen();

        newChunk = terrainGen.ChunkGen(newChunk);

        //Sets the generated blocks to unmodified and tries to load any modified blocks from the save file
        if (isClient)
        {
            NetworkBlocksClient.RequestChuckData(newChunk.pos);
        }
        else
        {
            newChunk.SetBlocksUnmodified();
            Serialization.Load(newChunk);
        }

        //newChunk.SetBlocksUnmodified();
        //Serialization.Load(newChunk);

        return(newChunk);
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            RaycastHit hit;
            if (Physics.Raycast(transform.position, transform.forward, out hit, 100))
            {
                if (World.singleton.isClient)
                {
                    NetworkBlocksClient.SetBlock(hit, new BlockAir());
                }
                else
                {
                    EditTerrain.SetBlock(hit, new BlockAir());
                }
            }
        }

        rot = new Vector2(
            rot.x + Input.GetAxis("Mouse X") * 2,
            rot.y + Input.GetAxis("Mouse Y") * 2);

        transform.localRotation  = Quaternion.AngleAxis(rot.x, Vector3.up);
        transform.localRotation *= Quaternion.AngleAxis(rot.y, Vector3.left);

        transform.position += transform.forward * 2 * Input.GetAxis("Vertical");
        transform.position += transform.right * 2 * Input.GetAxis("Horizontal");
    }
 private void Awake()
 {
     if (singleton == null)
     {
         singleton = this;
         DontDestroyOnLoad(gameObject);
     }
     else
     {
         Destroy(gameObject);
     }
 }