Esempio n. 1
0
 void OnCollisionEnter2D(Collision2D coll)
 {
     Debug.Log("touch");
     if (coll.gameObject.tag == "breakDirt")
     {
         foreach (ContactPoint2D contact in coll.contacts)
         {
             WorldPos hitPos = new WorldPos(0, 0);
             for (float dX = -0.01f; dX < 0.02f; dX += 0.01f)
             {
                 for (float dY = -0.01f; dY < 0.02f; dY += 0.01f)
                 {
                     hitPos.x = contact.point.x + dX;
                     hitPos.y = contact.point.y + dY;
                     TriPos triPos = hitPos.toTriPos();
                     SetBlock(triPos, new BlockAir());
                 }
             }
             Debug.DrawRay(new Vector3(hitPos.x, hitPos.y, 0),
                           new Vector3(0.1f, 0.1f), Color.white, 10f, false);
             Debug.DrawRay(new Vector3(hitPos.x + 0.1f, hitPos.y, 0),
                           new Vector3(-0.1f, 0.1f), Color.white, 10f, false);
         }
         Object.Destroy(coll.gameObject);
     }
 }
Esempio n. 2
0
    public void CreateChunk(ChunkPos chunkPos)
    {
        WorldPos worldPos = chunkPos.toWorldPos();

        //Instantiate the chunk at its world coordinates
        GameObject newChunkObject = Instantiate(
            chunkPrefab, new Vector3(worldPos.x, worldPos.y),
            Quaternion.Euler(Vector3.zero)
            ) as GameObject;

        Chunk newChunk = newChunkObject.GetComponent <Chunk>();

        newChunk.pos   = chunkPos;
        newChunk.world = this;

        //Add it to the chunks dictionary with the position as the key
        chunks.Add(chunkPos, newChunk);

        TriPos startTriPos = chunkPos.toTriPos();

        for (int xi = 0; xi < chunkXSize; xi++)
        {
            for (int yi = 0; yi < chunkYSize; yi++)
            {
                TriPos triPos = new TriPos(startTriPos.x + xi, startTriPos.y + yi);
                SetBlock(triPos, new Block());
            }
        }
    }
Esempio n. 3
0
    public override bool Equals(object obj)
    {
        if (!(obj is TriPos))
        {
            return(false);
        }

        TriPos pos = (TriPos)obj;

        return(pos.x == x && pos.y == y);
    }
Esempio n. 4
0
 public Block GetBlock(TriPos triPos)
 {
     if (baseTriPos == null)
     {
         baseTriPos = pos.toTriPos();              //todo: cache better
     }
     if (InRangeX(triPos.x) && InRangeY(triPos.y))
     {
         return(blocks[baseTriPos.x - triPos.x, baseTriPos.y - triPos.y]);
     }
     return(world.GetBlock(triPos));
 }
Esempio n. 5
0
    public Chunk GetChunk(TriPos triPos)
    {
        ChunkPos pos            = triPos.toChunkPos();
        Chunk    containerChunk = null;

        chunks.TryGetValue(pos, out containerChunk);
        if (containerChunk == null)
        {
            Debug.Log("GetChunk: No chunk at " + pos.ToString());
        }
        return(containerChunk);
    }
Esempio n. 6
0
 public void SetBlock(TriPos triPos, Block block)
 {
     if (InRangeX(triPos.x) && InRangeY(triPos.y))
     {
         blocks[triPos.x - baseTriPos.x, triPos.y - baseTriPos.y] = block;
         update = true;
     }
     else
     {
         Debug.Log("SetBlock on wrong chunk " + pos + " with tri " + triPos);
         //world.SetBlock(triPos, block);
     }
 }
Esempio n. 7
0
    public bool InRangeX(int index)
    {
        if (baseTriPos == null)
        {
            baseTriPos = pos.toTriPos();              //todo: cache better
        }
        if (index < baseTriPos.x || index >= baseTriPos.x + chunkXSize)
        {
            return(false);
        }

        return(true);
    }
Esempio n. 8
0
    public Block GetBlock(TriPos triPos)
    {
        Chunk containerChunk = GetChunk(triPos);

        if (containerChunk != null)
        {
            Block block = containerChunk.GetBlock(triPos);
            return(block);
        }
        else
        {
            return(new BlockAir());
        }
    }
Esempio n. 9
0
    public void SetBlock(TriPos triPos, Block block)
    {
        Chunk chunk = GetChunk(triPos);

        if (chunk != null)
        {
            chunk.SetBlock(triPos, block);
            chunk.update = true;
        }
        else
        {
            Debug.Log("SetBlock: No chunk at " + triPos.toChunkPos().ToString());
        }
    }