private void CreateRandomHouses(int nHouses, Texture2D wall1, Texture2D roof1, Texture2D wall2, Texture2D roof2) { // Max and min values short maxScale = 110; short minScale = 70; short minX = 100; short maxX = 950; short minZ = -900; short maxZ = -100; for (int i = 0; i < nHouses; ++i) { var s = rnd.Next(minScale, maxScale); Vector3 scale = new Vector3(s += rnd.Next(0, 20), s + rnd.Next(0, 20), s + rnd.Next(0, 20)); Vector3 pos = new Vector3(rnd.Next(minX, maxX), scale.Y / 2 + 20, rnd.Next(minZ, maxZ)); var houses = (EntityComponentManager.GetManager().GetComponentByType(typeof(HouseComponent)).Values); bool ok = true; foreach (var h in houses) { var v = (HouseComponent)h; if (Vector3.Distance(v.Position, pos) < 150) { ok = false; break; } } if (ok) { var wall = wall1; var roof = roof1; if (rnd.NextDouble() < .5) { wall = wall2; roof = roof2; shader.RealisticSettings(); } HouseComponent house = new HouseComponent(scale, pos, Matrix.CreateRotationY((float)(rnd.NextDouble() * (Math.PI * 2))), wall, roof); int hid = EntityComponentManager.GetManager().CreateNewEntityId(); EntityComponentManager.GetManager().AddComponentToEntity(hid, house); HouseBoundingBox hBox = new HouseBoundingBox() { BoundingBox = new BoundingBox(pos, pos + scale) }; EntityComponentManager.GetManager().AddComponentToEntity(hid, hBox); } else { --i; } } }
private void RenderHouses(HouseComponent h, VertexBuffer vb, IndexBuffer ib, Texture2D texture) { game.GraphicsDevice.SetVertexBuffer(vb); game.GraphicsDevice.Indices = ib; var effect = new BasicEffect(game.GraphicsDevice); effect.View = ((HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).First().Value).ViewMatrix; effect.Projection = ((HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).First().Value).ProjectionMatrix; effect.VertexColorEnabled = false; effect.TextureEnabled = true; effect.EnableDefaultLighting(); effect.LightingEnabled = false; var objectWorld = Matrix.CreateScale(h.Scale) * h.Rotation * Matrix.CreateTranslation(h.Position); effect.World = objectWorld * ((HeightMapCameraComponent)EntityComponentManager.GetManager().GetComponentByType(typeof(HeightMapCameraComponent)).First().Value).TerrainMatrix; effect.Texture = texture; foreach (EffectPass ep in effect.CurrentTechnique.Passes) { ep.Apply(); game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 18); } }