// METHODS // PUBLIC public void UpdateTerrainChunk() { if (!mapDataRequested) { if (InfiniteTerrain.GetInstance().GetTerrainChunk(coordinates + Vector2.up).mapDataRecieved&& InfiniteTerrain.GetInstance().GetTerrainChunk(coordinates + Vector2.down).mapDataRecieved&& InfiniteTerrain.GetInstance().GetTerrainChunk(coordinates + Vector2.left).mapDataRecieved&& InfiniteTerrain.GetInstance().GetTerrainChunk(coordinates + Vector2.right).mapDataRecieved) { mapGenerator.RequestMapData(OnMapDataRecieved, coordinates); mapDataRequested = true; } } float viewerDistanceFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition)); if (colliders.Count != 0 && !collidable && viewerDistanceFromNearestEdge <= maxColliderDistance) { foreach (Collider2D collider in colliders) { collider.enabled = true; } collidable = true; } if (colliders.Count != 0 && collidable && viewerDistanceFromNearestEdge > maxColliderDistance) { foreach (Collider2D collider in colliders) { collider.enabled = false; } collidable = false; } bool visible = viewerDistanceFromNearestEdge <= maxViewDistance; SetVisible(visible); }
void UpdateTile(Vector2 coords) { int x = (int)coords.X; int y = (int)coords.Y; int configuration = GetConfiguration(coords); GameObject temp = tiles[x, y]; temp.GetComponent <SpriteRenderer>().sprite = InfiniteTerrain.GetInstance().tiles[configuration]; if (configuration != 15) { if (temp.GetComponent <EdgeCollider2D>() == null) { colliders.Add(temp.AddComponent <EdgeCollider2D>()); ((EdgeCollider2D)colliders[colliders.Count - 1]).points = GenerateEdgeCollider2DPoints(configuration); } temp.GetComponent <EdgeCollider2D>().points = GenerateEdgeCollider2DPoints(configuration);; // ((EdgeCollider2D)colliders[colliders.Count-1]); // colliders[colliders.Count-1].enabled = false; } }
GameObject CreateTile(Vector2 coords) { int configuration = 0; int x = (int)coords.X; int y = (int)coords.Y; if (x == 0 || mapData.cellMap[x - 1, y] != 0) { configuration += 8; } if (y == mapData.cellMap.GetLength(1) - 1 || mapData.cellMap[x, y + 1] != 0) { configuration += 4; } if (x == mapData.cellMap.GetLength(0) - 1 || mapData.cellMap[x + 1, y] != 0) { configuration += 2; } if (y == 0 || mapData.cellMap[x, y - 1] != 0) { configuration += 1; } GameObject temp = new GameObject(x + "/" + y); temp.tag = "Tile"; temp.transform.parent = chunkObject.transform; temp.transform.localPosition = new Vector3(x - size / 2, y - size / 2, 0); temp.AddComponent <SpriteRenderer>().sprite = InfiniteTerrain.GetInstance().tiles[configuration]; if (configuration != 15) { colliders.Add(temp.AddComponent <EdgeCollider2D>()); ((EdgeCollider2D)colliders[colliders.Count - 1]).points = GenerateEdgeCollider2DPoints(configuration); colliders[colliders.Count - 1].enabled = false; } return(temp); }