public static Scene MakeForestScene() { // System.Console.WriteLine( "MakeForestScene" ); var scene = new Scene() { Name = "Forest" }; TextureInfo ObjectsMap = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/PlanetCute/Objects.png", false), new Vector2i(7, 3)); Vector2i[] tree_indexes = new Vector2i[3]; tree_indexes[0] = PlanetCute.TreeShort; tree_indexes[1] = PlanetCute.TreeTall; tree_indexes[2] = PlanetCute.TreeUgly; Math.RandGenerator rgen = new Math.RandGenerator(); Bounds2 bounds = scene.Camera2D.CalcBounds(); Vector2i numcells = new Vector2i(7, 4); Vector2 cellsize = bounds.Size / numcells.Vector2(); System.Random rnd1 = new System.Random(); SpriteList sprite_list = new SpriteList(ObjectsMap); // we are going to put a tree at a random location inside each cell of a regular grid for (int x = 0; x < numcells.X; ++x) { // generate rows of tree top to bottom, for draw order for (int y = numcells.Y - 1; y >= 0; --y) { Vector2 cellindexf = new Vector2((float)x, (float)y); var sprite = new SpriteTile(); // sprite.TextureInfo = tree_tex[rnd1.Next(3)]; sprite.TextureInfo = ObjectsMap; sprite.TileIndex2D = tree_indexes[rnd1.Next(3)]; // bounds for one cell Bounds2 gen_bounds = new Bounds2(bounds.Min + cellindexf * cellsize, bounds.Min + cellindexf * cellsize + cellsize); // scale gen_bounds to countrols irregularity gen_bounds = gen_bounds.Scale(new Vector2(0.6f), gen_bounds.Center); // pick up a random point in that cell sprite.Position = gen_bounds.Min + gen_bounds.Size * rgen.NextVector2(Math._00, Math._11); // make the size of the tree match the size of cells, preserve texture ratio Vector2 aspect = sprite.CalcSizeInPixels() / sprite.CalcSizeInPixels().X; sprite.Quad.S = cellsize.X * aspect; // make the sprite bottom center point be the new pivot (for Skew) sprite.Quad.Centering(TRS.Local.BottomCenter); // make the trees move in the wind sprite.Schedule((dt) => { int hc = sprite.GetHashCode(); System.Random rnd2 = new System.Random(hc); sprite.Skew = new Vector2(Math.Deg2Rad(1.0f) * FMath.Sin((float)Director.Instance.DirectorTime * 1.0f * rnd2.Next(4096) / 4096.0f), Math.Deg2Rad(2.0f) * FMath.Sin((float)Director.Instance.DirectorTime * 3.0f * rnd2.Next(4096) / 4096.0f)); }); sprite_list.AddChild(sprite); } } scene.Camera2D.Center += new Vector2(0.0f, 2.0f); scene.AddChild(sprite_list); scene.RegisterDisposeOnExit((System.IDisposable)ObjectsMap); return(scene); }