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); } }
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()); } } }
public override bool Equals(object obj) { if (!(obj is TriPos)) { return(false); } TriPos pos = (TriPos)obj; return(pos.x == x && pos.y == y); }
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)); }
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); }
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); } }
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); }
public Block GetBlock(TriPos triPos) { Chunk containerChunk = GetChunk(triPos); if (containerChunk != null) { Block block = containerChunk.GetBlock(triPos); return(block); } else { return(new BlockAir()); } }
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()); } }