private void SetupStaticObjects() { Random rn = new Random(); for (int i = 0; i < 20; i++) { int id = AddEntity(); CImportedModel modelComponent = new CImportedModel(); modelComponent.fileName = "flossy"; modelComponent.model = Game1.Inst.Content.Load <Model>("Models/" + modelComponent.fileName); AddComponent <C3DRenderable>(id, modelComponent); int x = (int)(rn.NextDouble() * 200); int z = (int)(rn.NextDouble() * 200); float y = 0; CTransform transformComponent = new CTransform(); transformComponent.Position = new Vector3(x, y, z); transformComponent.Rotation = Matrix.CreateFromAxisAngle(Vector3.UnitY, (float)(Math.PI * (rn.NextDouble() * 2))); float scale = 1; transformComponent.Scale = new Vector3(scale, scale, scale); AddComponent(id, transformComponent); AddComponent(id, new CBody { InvMass = 1, Aabb = new BoundingBox(new Vector3(0, 0, 0), new Vector3(1, 1, 1)), LinDrag = 5f, Velocity = Vector3.Zero, Radius = 1f, SpeedMultiplier = 0.5f, MaxVelocity = 5 }); AddComponent(id, new CAI()); } }
public void Load() { Model model = null; foreach (var renderable in Game1.Inst.Scene.GetComponents <C3DRenderable>()) { if (renderable.Value.GetType() != typeof(CHeightmap)) { continue; } CHeightmap heightmap = (CHeightmap)renderable.Value; // heightmap.HeightData // Create vertices Random rnd = new Random(1990); var terrainHeight = 1081; var terrainWidth = 1081; // int counter = 0; indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6]; vertices = new VertexPositionNormalColor[terrainWidth * terrainHeight]; vibirations = new int[terrainWidth * terrainHeight]; // Create vertices for (int x = 0; x < terrainWidth; x++) { for (int y = 0; y < terrainHeight; y++) { vertices[x + y * terrainWidth].Position = new Vector3(x, heightmap.LowestPoint + 10, y); var color = Color.Blue; color.A = 100; vertices[x + y * terrainWidth].Color = color; // Randomly set the direction of the vertex up or down vibirations[x + y * terrainWidth] = rnd.Next(0, 100) > 50 ? -1 : 1; } } for (int y = 0; y < terrainHeight - 1; y++) { for (int x = 0; x < terrainWidth - 1; x++) { int topLeft = x + y * terrainWidth; int topRight = (x + 1) + y * terrainWidth; int lowerLeft = x + (y + 1) * terrainWidth; int lowerRight = (x + 1) + (y + 1) * terrainWidth; indices[counter++] = (int)topLeft; indices[counter++] = (int)lowerRight; indices[counter++] = (int)lowerLeft; indices[counter++] = (int)topLeft; indices[counter++] = (int)topRight; indices[counter++] = (int)lowerRight; } } // Calculate normals for (int i = 0; i < vertices.Length; i++) { vertices[i].Normal = new Vector3(0, 0, 0); } for (int i = 0; i < indices.Length / 3; i++) { int index1 = indices[i * 3]; int index2 = indices[i * 3 + 1]; int index3 = indices[i * 3 + 2]; Vector3 side1 = vertices[index1].Position - vertices[index3].Position; Vector3 side2 = vertices[index1].Position - vertices[index2].Position; Vector3 normal = Vector3.Cross(side1, side2); vertices[index1].Normal += normal; vertices[index2].Normal += normal; vertices[index3].Normal += normal; } var vertexBuffer = new VertexBuffer(mGraphicsDevice, VertexPositionNormalColor.VertexDeclaration, vertices.Length, BufferUsage.None); vertexBuffer.SetData(vertices); var indexBuffer = new IndexBuffer(mGraphicsDevice, typeof(int), indices.Length, BufferUsage.None); indexBuffer.SetData(indices); var bones = new List <ModelBone>(); var meshes = new List <ModelMesh>(); List <ModelMeshPart> parts = new List <ModelMeshPart>(); ModelMeshPart meshPart = new ModelMeshPart(); meshPart.VertexBuffer = vertexBuffer; meshPart.IndexBuffer = indexBuffer; meshPart.NumVertices = indices.Length; meshPart.PrimitiveCount = indices.Length / 3; parts.Add(meshPart); ModelMesh mesh = new ModelMesh(mGraphicsDevice, parts); meshPart.Effect = bEffect; mesh.Name = "water"; ModelBone bone = new ModelBone(); bone.Name = "Water"; bone.AddMesh(mesh); bone.Transform = Matrix.Identity; mesh.ParentBone = bone; bones.Add(bone); meshes.Add(mesh); model = new Model(Game1.Inst.GraphicsDevice, bones, meshes); } int id = Game1.Inst.Scene.AddEntity(); Game1.Inst.Scene.AddComponent(id, new CTransform() { Position = new Vector3(-590, -900, -590) * 0.01f, Orientation = Quaternion.CreateFromRotationMatrix(Matrix.Identity), Scale = new Vector3(0.01f) }); CModel = new CImportedModel() { model = model }; Game1.Inst.Scene.AddComponent <C3DRenderable>(id, CModel); }
public static void CreateAnimals(int numFlocks, int worldsize) { var currentScene = Game1.Inst.Scene; int membersPerFlock = (int)(rnd.NextDouble() * 10) + 10; var flockRadius = membersPerFlock; for (int f = 0; f < numFlocks; f++) { int flockId = currentScene.AddEntity(); CFlock flock = new CFlock { Radius = 20, SeparationDistance = 3, AlignmentFactor = 0.1f, CohesionFactor = 0.5f, SeparationFactor = 100.0f, PreferredMovementSpeed = 150f }; double animal = rnd.NextDouble(); string flockAnimal = animal > 0.66 ? "flossy" : animal > 0.33 ? "goose" : "hen"; string deathSound = string.Format("Sounds/Effects/{0}", (flockAnimal == "flossy" ? "SheepDeath" : flockAnimal == "goose" ? "GooseDeath" : "DeathChicken")); int flockX = (int)(rnd.NextDouble() * worldsize * 2 - worldsize); int flockZ = (int)(rnd.NextDouble() * worldsize * 2 - worldsize); CTransform flockTransform = new CTransform { Position = new Vector3(flockX, 0, flockZ) }; for (int i = 0; i < membersPerFlock; i++) { int id = currentScene.AddEntity(); var npcAnim = wiggleAnimation(id); if (flockAnimal.Equals("hen")) { // TODO: Make animals have different animations based on state CAnimation normalAnimation = new CHenNormalAnimation { animFn = npcAnim }; // Set a random offset to animation so not all animals are synced normalAnimation.CurrentKeyframe = rnd.Next(normalAnimation.Keyframes.Count - 1); // Random animation speed between 0.8-1.0 normalAnimation.AnimationSpeed = (float)rnd.NextDouble() * 0.2f + 0.8f; currentScene.AddComponent <C3DRenderable>(id, normalAnimation); } else { CImportedModel modelComponent = new CImportedModel { animFn = npcAnim }; modelComponent.fileName = flockAnimal; modelComponent.model = Game1.Inst.Content.Load <Model>("Models/" + modelComponent.fileName); currentScene.AddComponent <C3DRenderable>(id, modelComponent); } float memberX = flockTransform.Position.X + (float)rnd.NextDouble() * flockRadius * 2 - flockRadius; float memberZ = flockTransform.Position.Z + (float)rnd.NextDouble() * flockRadius * 2 - flockRadius; float y = flockTransform.Position.Y; CTransform transformComponent = new CTransform(); transformComponent.Position = new Vector3(memberX, y, memberZ); transformComponent.Rotation = Matrix.CreateFromAxisAngle(Vector3.UnitY, (float)(Math.PI * (rnd.NextDouble() * 2))); float size = 0.5f; transformComponent.Scale = new Vector3(1f); currentScene.AddComponent(id, transformComponent); currentScene.AddComponent(id, new CBody { InvMass = 0.05f, Aabb = new BoundingBox(-size * new Vector3(1.0f, 2.0f, 1.0f), size * new Vector3(1.0f, 0.0f, 1.0f)), LinDrag = 0.8f, Velocity = Vector3.Zero, Radius = 1f, SpeedMultiplier = size, MaxVelocity = 4, Restitution = 0 }); // health value of npcs, maybe change per species/flock/member? var npcHealth = 1; currentScene.AddComponent(id, new CHealth { MaxHealth = npcHealth, Health = npcHealth, DeathSound = deathSound }); currentScene.AddComponent(id, new CAI { Flock = flockId }); currentScene.AddComponent(id, new CSyncObject()); flock.Members.Add(id); } currentScene.AddComponent(flockId, flock); currentScene.AddComponent(flockId, flockTransform); } }