public static void MakePatches() { // TODO Calculate here the area that needs to be clear of trees etc, and pass xmin xmax, zmin z max to Patchs foreach (TerrainInfo tI in patchList) { for (int i = 0; i < terrainPatchRes; i++) { patchQueue.Enqueue(new TerrainPatch(tI.globalX, tI.globalZ, tI.terrain, InfiniteTerrain.m_heightMapSize * i / terrainPatchRes, InfiniteTerrain.m_heightMapSize * (i + 1) / terrainPatchRes, tI.newPosition, tI)); } } foreach (TerrainInfo tI in patchList) { for (int i = 0; i < splatDetailPatchRes; i++) { patchQueue.Enqueue(new SplatDetailPatch(tI.globalX, tI.globalZ, tI.terrain, InfiniteTerrain.m_alphaMapSize * i / splatDetailPatchRes, InfiniteTerrain.m_alphaMapSize * (i + 1) / splatDetailPatchRes, tI)); } } foreach (TerrainInfo tI in patchList) { // todo reconsider if this shoul be done in one go for each terrain, not inside foreach loop three times AreaData aData = InfiniteTerrain.GetAreaData(tI.globalX, tI.globalZ); for (int i = 0; i < treePatchRes; i++) { patchQueue.Enqueue(new TreePatch(tI.globalX, tI.globalZ, tI.terrain, InfiniteTerrain.numOfTreesPerTerrain * i / treePatchRes, InfiniteTerrain.numOfTreesPerTerrain * (i + 1) / treePatchRes, tI)); } } patchList.Clear(); }
// 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); }
public IEnumerator placeSavedRessource() { Vector2 viewerPos = InfiniteTerrain._viewerPos; clearRessource(); InfiniteTerrain terrainManager = FindObjectOfType <InfiniteTerrain> (); for (int i = 0; terrainManager == null && i < 10; i++) { yield return(new WaitForSeconds(1f)); terrainManager = FindObjectOfType <InfiniteTerrain> (); } Debug.Log($"list save {listSavedRessource.Count}"); foreach (var item in listSavedRessource) { InfiniteTerrain.TerrainChunk chunk = terrainManager.getChunkOfPoint(item.pos); for (int i = 0; chunk == null && i < 10; i++) { yield return(new WaitForSeconds(.1f)); chunk = terrainManager.getChunkOfPoint(item.pos); } if (savableRessourceOfChunk.ContainsKey(chunk)) { savableRessourceOfChunk[chunk].Enqueue(item); } else { savableRessourceOfChunk.Add(chunk, new Queue <SavableRessource> ()); savableRessourceOfChunk[chunk].Enqueue(item); chunk.subscribeRequestCollider(onColliderRecived); } } }
// SETTERS // FACTORY // MONOBEHAIVIOUR void Awake() { if (instance == null) { instance = this; } else if (instance != this) { Destroy(this); } }
public int landmassTypes; // 1,2,4,8... public TerrainInfo(int globX, int globZ, Terrain ter, Vector3 newPos) { newPosition = newPos; globalX = globX; globalZ = globZ; terrain = ter; string key = globalX.ToString() + "_" + globalZ.ToString(); landmassTypes = InfiniteTerrain.GetOrAssignLandMassTypes(key); SetParameters(); }
public PlayScene(Game game, SpriteBatch spriteBatch) : base(game) { this.spriteBatch = spriteBatch; var tempTerrain = new Terrain( Game, spriteBatch, GraphicsDevice.Viewport.Bounds.Width / 3f, 50, 80, 1, 0, ColourSchemes.normRed, new Vector2(0, GraphicsDevice.Viewport.Bounds.Height * 0.75f)); terrain = new InfiniteTerrain(Game, spriteBatch, tempTerrain, 3, 3); Components.Add(terrain); }
// public static int HasLandmassType(string key, int typeInt) { int massType = InfiniteTerrain.GetOrAssignLandMassTypes(key); bool is1 = false; switch (typeInt) { case 1: if (((massType & 1) > 0)) // hills { is1 = true; } break; case 2: if (((massType & 2) > 0)) // mountains { is1 = true; } break; case 4: if (((massType & 4) > 0)) // ridged mountains { is1 = true; } break; case 8: if (((massType & 8) > 0)) /// plains { is1 = true; } break; } if (is1 == true) { return(1); } else { return(0); } }
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; } }
void Start() { Bump0 = Resources.Load("Textures/" + "rock_2048-norm") as Texture2D; InfiniteTerrain infTerrain = (InfiniteTerrain)GetComponent(typeof(InfiniteTerrain)); if (Bump0) { Shader.SetGlobalTexture("_BumpMap0", Bump0); } if (Bump1) { Shader.SetGlobalTexture("_BumpMap1", Bump1); } if (Bump2) { Shader.SetGlobalTexture("_BumpMap2", Bump2); } if (Bump3) { Shader.SetGlobalTexture("_BumpMap3", Bump3); } Shader.SetGlobalFloat("_Tile0", Tile0); Shader.SetGlobalFloat("_Tile1", Tile1); Shader.SetGlobalFloat("_Tile2", Tile2); Shader.SetGlobalFloat("_Tile3", Tile3); terrainSizeX = InfiniteLandscape.m_landScapeSize; terrainSizeZ = InfiniteLandscape.m_landScapeSize; Shader.SetGlobalFloat("multiplier", 4f); Shader.SetGlobalFloat("_TerrainX", terrainSizeX); Shader.SetGlobalFloat("_TerrainZ", terrainSizeZ); }
public MapChunk(Vector2 coordinates, int size, InfiniteTerrain infTerrain, Material material) { Size = size; Coordinates = coordinates; Position = coordinates * size; InfiniteTerrain = infTerrain; Bounds = new Bounds(Position, Vector2.one * Size); MeshObject = new GameObject("Map Chunk"); MeshObject.transform.parent = InfiniteTerrain.transform; MeshObject.transform.position = new Vector3(Position.x, 0, Position.y); MeshRenderer = MeshObject.AddComponent <MeshRenderer>(); MeshFilter = MeshObject.AddComponent <MeshFilter>(); MeshRenderer.material = material; SetVisible(false); InfiniteTerrain.MapGenerator.RequestMapData(OnMapDataReceived); }
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); }
void OnEnable() { infiniteTerrain = (InfiniteTerrain)target; }
private void FillTerrainPatch() { int hRes = InfiniteTerrain.m_heightMapSize; float ratio = (float)InfiniteTerrain.m_landScapeSize / (float)hRes; float z0 = (InfiniteLandscape.initialGlobalIndexZ * (InfiniteTerrain.m_heightMapSize - 1)) * ratio; float z1 = (InfiniteLandscape.initialGlobalIndexZ * (InfiniteTerrain.m_heightMapSize - 1)) * ratio + hRes * ratio; float y0 = 0.0f; float y1 = 1.0f; bool plainsExist = false; bool hillsExist = false; bool mountainsExist = false; bool ridgedMountainsExist = false; string key = globalTileX.ToString() + "_" + globalTileZ.ToString(); int massType = InfiniteTerrain.GetOrAssignLandMassTypes(key); if ((massType & 1) > 0) { hillsExist = true; } if ((massType & 2) > 0) { mountainsExist = true; } if ((massType & 4) > 0) { ridgedMountainsExist = true; } if ((massType & 8) > 0) { plainsExist = true; } for (int z = h0; z < h1; z++) { float worldPosZ = (z + globalTileZ * (InfiniteTerrain.m_heightMapSize - 1)) * ratio; float hx = Mathf.Clamp((y1 - y0) / (z1 - z0) * (worldPosZ - z0) + y1, -4, 8); for (int x = 0; x < hRes; x++) { float worldPosX = (x + globalTileX * (InfiniteTerrain.m_heightMapSize - 1)) * ratio; float sum = 0; /* // very small hills * if (hillsExist) * { * //float hills = m_mountainNoise.FractalNoise2D(worldPosX, worldPosZ, 1, 100, 0.02f) + 0.01f; // good small bumpy thing * // makes "brain" bumps, that look odd from top * //float hills = -(m_mountainNoiseRidged.FractalNoise2D(worldPosX, worldPosZ, 1, 100, 0.01f)) + 0.005f; // flipped small ridge * float hills = -(m_mountainNoiseRidged.FractalNoise2D(worldPosX, worldPosZ, 6, 250, 0.015f)); // flipped small ridge * //hills = 0.2f; // for testing falloff map ignore noise * hills = BlendLandmass(hills, x, z, key, 1); * sum += hills; * } */ // TEST - unnaturally steep transition, otherwise cool /* * if (hillsExist) * { * float hills = -(m_mountainNoiseRidged.FractalNoise2D(worldPosX, worldPosZ, 6, 250, 0.015f)); // flipped small ridge * * if(hills < testMin ) testMin = hills; * if (hills > testMax) testMax = hills; * * // varies somewhere between -20 and 10 when x 1000 * * if (hills*1000 < 0f) * { * hills = -20f/1000f; * } * hills = BlendLandmass(hills, x, z, key, 1); * sum += hills; * } */ // produces "fjord" like valleys with plateau tops if (hillsExist) { float hills = m_mountainNoise.FractalNoise2D(worldPosX, worldPosZ, 1, 500, 1); //if (hills < testMin) testMin = hills; //if (hills > testMax) testMax = hills; hills = InfiniteTerrain.StaticTestCurve.Evaluate(hills); hills /= 10; hills += 0.01f; // to rse it somewhat above sea level // small bumps float hills2 = -(m_mountainNoiseRidged.FractalNoise2D(worldPosX, worldPosZ, 6, 100, 0.002f)); // flipped small ridge hills += hills2; hills = BlendLandmass(hills, x, z, key, 1); sum += hills; } // exiting but unrealistic maze like formation // effect depend on the animation curve /* * if (hillsExist) * { * float hills = m_mountainNoise.FractalNoise2D(worldPosX, worldPosZ, 1, 100, 1); * if (hills < testMin) testMin = hills; * if (hills > testMax) testMax = hills; * * hills = InfiniteTerrain.StaticTestCurve.Evaluate(hills); * * hills /= 20; * * hills = BlendLandmass(hills, x, z, key, 1); * * sum += hills; * } */ if (mountainsExist) { float mountainsPerlin = m_mountainNoise.FractalNoise2D(worldPosX, worldPosZ, 4, 3000, 0.4f); //mountainsPerlin = 0.2f; // for testing falloff map ignore noise mountainsPerlin = BlendLandmass(mountainsPerlin, x, z, key, 2); sum += mountainsPerlin; } if (ridgedMountainsExist) { float mountainsRidged = m_mountainNoiseRidged.FractalNoise2D(worldPosX, worldPosZ, 4, 3000, 0.2f); //mountainsRidged = 0.2f; // for testing falloff map ignore noise float accentHills = m_mountainNoise.FractalNoise2D(worldPosX, worldPosZ, 1, 100, 0.005f); // good small bumpy thing //accentHills = 0f; // for testing falloff map ignore noise mountainsRidged += accentHills; mountainsRidged = BlendLandmass(mountainsRidged, x, z, key, 4); sum += mountainsRidged; } if (plainsExist) { float plains = m_mountainNoise.FractalNoise2D(worldPosX, worldPosZ, 4, 9000, 0.1f) + 0.05f; // plains = 0.01f; // for testing falloff map ignore noise plains = BlendLandmass(plains, x, z, key, 8); sum += plains; } // for debugging textures, create flat surfacce //sum = 0.04f; float height = (sum) + 0.003f * hx; InfiniteTerrain.m_terrainHeights[z, x] = height; } } //Debug.Log( testMin + " - " + testMax ); }
/// <summary> /// Creates the GameScene scene /// </summary> /// <param name="game">Current game reference</param> /// <param name="spriteBatch">spriteBatch to draw scene</param> public GameScene(Game game, SpriteBatch spriteBatch) : base(game) { this.spriteBatch = spriteBatch; inputNameString = new KbInputString(Game, spriteBatch, resources.MonoFont, new Vector2(GraphicsDevice.Viewport.Width / 2f, GraphicsDevice.Viewport.Height / 2f + resources.MonoFont.LineSpacing * 2), Color.Wheat, SimpleString.TextAlignH.Middle); // Add the infinite terrain (which also does gas cans) var tempTerrain = new Terrain(Game, spriteBatch, GraphicsDevice.Viewport.Bounds.Width / 3f, 50, 80, 1, 0, 4, ColourSchemes.brown, new Vector2(0, GraphicsDevice.Viewport.Bounds.Height * 0.75f)); Components.Add(terrain = new InfiniteTerrain(Game, spriteBatch, tempTerrain, 3, 3)); // Add our UFO this.Components.Add(ufo = new UFO(Game, spriteBatch, new Vector2(50, terrain.ExtremeHeightAt(25, 50, false)))); // Add distance string Components.Add( distance = new SimpleString(game, spriteBatch, resources.BoldFont, new Vector2(20, 20), "Distance: 0", ColourSchemes.pink)); // Create Speed meter & string Components.Add( meterSpeed = new MeterBar( new SimpleString(game, spriteBatch, resources.RegularFont, new Vector2(215, 70), "", Color.Black, SimpleString.TextAlignH.Right), new Rectangle(20, 70, 200, resources.RegularFont.LineSpacing), 0, ufo.MaxVelocity)); Components.Add(new SimpleString(game, spriteBatch, resources.RegularFont, new Vector2(25, 70), "Speed", Color.Black)); // Create Gas meter & string Components.Add( meterGas = new MeterBar( new SimpleString(game, spriteBatch, resources.RegularFont, new Vector2(215, 120), "", Color.Black, SimpleString.TextAlignH.Right), new Rectangle(20, 120, 200, resources.RegularFont.LineSpacing), 0, ufo.Gas)); Components.Add(new SimpleString(game, spriteBatch, resources.RegularFont, new Vector2(25, 120), "Gas: ", Color.Black)); // Create collision manager Components.Add(collisionManager = new CollisionManager(Game)); collisionManager.Add(ufo); // Create explosion explosion = new Explosion(game, spriteBatch, resources.Explosion, Vector2.Zero, 3); this.Components.Add(explosion); // Death sound deathSouthIns = resources.deathSound.CreateInstance(); deathSouthIns.Volume = .2f; }
protected override void Update() { base.Update(); /* * //-------Just debugging-------- * if(updateRound) * { * // previous was updateRound * float executionTime = Time.time - StartTime; * //Debug.Log(updatecounter + ": ----------------------------------------> Execution time " + executionTime); * updateRound = false; * } * updateRound = updateLandscape; * StartTime = Time.deltaTime; * //--------Just debugging ends ------- */ if (updateLandscape == true) { m_terrainGrid[curCyclicIndexX, curCyclicIndexZ].GetComponent <Collider>().enabled = true; //Slow operation // for displaying data in UI: Terrain current = m_terrainGrid[curCyclicIndexX, curCyclicIndexZ]; //Debug.Log("-------> entering " + current.name); terrainName.text = current.name; int massType = InfiniteTerrain.GetOrAssignLandMassTypes(current.name); hillsValue.text = (massType & 1) > 0 ? "yes": "no"; mountainsValue.text = (massType & 2) > 0 ? "yes": "no"; ridgedMountainsValue.text = (massType & 4) > 0 ? "yes" : "no"; plainsValue.text = (massType & 8) > 0 ? "yes" : "no"; m_terrainGrid[prevCyclicIndexX, prevCyclicIndexZ].GetComponent <Collider>().enabled = false; UpdateTerrainNeighbors(); UpdateTerrainPositions(); } if (PatchManager.patchQueue.Count != 0) { terrainIsFlushed = false; if (patchIsFilling == false) { patchToBeFilled = PatchManager.patchQueue.Dequeue(); StartCoroutine(CountdownForPatch()); } if (patchToBeFilled != null) { //float execTime = Time.time - oneUpdateTime; /* * if(patchToBeFilled is TerrainPatch) * Debug.Log( execTime + " Executed TerrainPatch..."); * if (patchToBeFilled is SplatDetailPatch) * Debug.Log(execTime + " Executed SplatDetailPatch..."); * if (patchToBeFilled is TreePatch) * Debug.Log(execTime + " Execute TreePatch..."); */ /* * if(execTime > 0.1f) * { * Debug.Log(Time.time + "------------------> exec time " + execTime); * } */ //for debugging //oneUpdateTime = Time.time; patchToBeFilled.ExecutePatch(); patchToBeFilled = null; } } else if (PatchManager.patchQueue.Count == 0 && terrainIsFlushed == false) { StartCoroutine(FlushTerrain()); terrainIsFlushed = true; } }
public void Start() { mapGenerator = FindObjectOfType <MapGenerator> (); infiniteTerrain = FindObjectOfType <InfiniteTerrain> (); shift = false; }