/// <summary> /// Generates the entities to be placed into a chunk /// </summary> /// <param name="chunkX"></param> /// <param name="chunkY"></param> /// <param name="surface"></param> public void GenerateEntities(int chunkX, int chunkY, SurfaceContainer surface) { Vector2 chunkPos = new Vector2(chunkX * Props.chunkSize * Props.tileSize, chunkY * Props.chunkSize * Props.tileSize); for (int i = 0; i < entitiesGenerated.Count; i++) { Vector2[] poissonDiscDistribution = PoissonDiscDistribution.GetDistribution(entitiesGenerated[i].density, (chunkX * (surfaceSize / Props.chunkSize) + chunkY) + seed, 10); for (int j = 0; j < poissonDiscDistribution.Length; j++) { Vector2 curPos = poissonDiscDistribution[j].VAdd(chunkPos); int nx = (int)(curPos.x) / Props.tileSize; int ny = (int)(curPos.y) / Props.tileSize; double elevation = 0.5 * elevationNoise.GetNoise(1 * nx, 1 * ny) + 0.25 * elevationNoise.GetNoise(2 * nx, 2 * ny) + 0.25 * elevationNoise.GetNoise(4 * nx, 4 * ny); double moisture = 0.5 * moistureNoise.GetNoise(1 * nx, 1 * ny) + 0.25 * moistureNoise.GetNoise(2 * nx, 2 * ny) + 0.25 * moistureNoise.GetNoise(4 * nx, 4 * ny); double temperature = 0.5 * temperatureNoise.GetNoise(1 * nx, 1 * ny) + 0.25 * temperatureNoise.GetNoise(2 * nx, 2 * ny) + 0.25 * temperatureNoise.GetNoise(4 * nx, 4 * ny); elevation = 3 * Math.Pow(elevation, elevationFactor); moisture = 3 * Math.Pow(moisture, moistureFactor); temperature = 3 * Math.Pow(temperature, temperatureFactor); float lowestAffinity = 2; int lowestPrototype = 0; for (int k = 0; k < entitiesGenerated[i].prototypeVars.Length; k++) { float entityProb = (float)(Math.Abs(entitiesGenerated[i].elevationAffinities[k] - elevation) + Math.Abs(entitiesGenerated[i].moistureAffinities[k] - moisture) + Math.Abs(entitiesGenerated[i].temperatureAffinities[k] - temperature)); if (entityProb < lowestAffinity) { lowestAffinity = entityProb; lowestPrototype = k; } } float moistureDiff = (float)(Math.Abs(entitiesGenerated[i].moistureAffinities[lowestPrototype] - moisture)); float elevationDiff = (float)(Math.Abs(entitiesGenerated[i].elevationAffinities[lowestPrototype] - elevation)); float temperatureDiff = (float)(Math.Abs(entitiesGenerated[i].temperatureAffinities[lowestPrototype] - temperature)); if (entitiesGenerated[i].moistureRange[lowestPrototype % entitiesGenerated[i].prototypeCount] > elevationDiff && entitiesGenerated[i].temperatureRange[lowestPrototype % entitiesGenerated[i].prototypeCount] > temperatureDiff && entitiesGenerated[i].elevationRange[lowestPrototype % entitiesGenerated[i].prototypeCount] > moistureDiff) { Entity prototype = entityCollection.GetPrototype(entitiesGenerated[i].prototypeVars[lowestPrototype]); if (prototype.tileAligned) { curPos = new Vector2(curPos.x - curPos.x % 32 + 16, curPos.y - curPos.y % 32 + 16); } if (prototype != null && !BoundingBox.CheckForPlacementCollision(prototype.collisionBox, curPos, surface, entitiesGenerated[i].placementMask)) { entityCollection.InstantiatePrototype(prototype.name, curPos, surface); } } } } }
public void RenderHeldDrawable(RenderWindow window, Camera camera, Player player, EntityCollection entityCollection, InputManager input) { window.SetView(camera.GetGameView()); if (player.heldItem != null && player.heldItem.item.placeResult != null) { Entity entity = entityCollection.GetPrototype(player.heldItem.item.placeResult); if (entity != null) { Vector2f mousePos; bool mouse = input.GetMousePosition(out mousePos); if (mouse) { for (int i = 0; i < entity.drawArray.Length; i++) { if (entity.drawArray[i].drawLayer == Drawable.DrawLayer.Shadow) { continue; } Sprite sprite = entity.drawArray[i].GetSprite(); Vector2f drawOffset = entity.drawArray[i].drawOffset; sprite.Scale = new Vector2f(1, 1); if (entity.tileAligned == true) { sprite.Position = new Vector2f((int)(mousePos.X - mousePos.X % Props.tileSize + (entity.tileWidth % 2) * 16), (int)(mousePos.Y - mousePos.Y % Props.tileSize + (entity.tileHeight % 2 + 1) * 16)) + drawOffset; } else { sprite.Position = mousePos + drawOffset; } if (player.placeable == true) { sprite.Color = new Color(0, 255, 0, 160); } else { sprite.Color = new Color(255, 0, 0, 160); } if (sprite.Origin.X == 0 && sprite.Origin.Y == 0) { sprite.Origin = new Vector2f(sprite.TextureRect.Width / 2, sprite.TextureRect.Height / 2); } window.Draw(sprite); } } } } }