public static Scene MakeManySpritesScene() { var tex1 = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/slime_green_frames.png", false) , new Vector2i(4, 4)); var scene = new Scene() { Name = "many SpriteUV scene" }; Bounds2 bounds = scene.Camera.CalcBounds(); Vector2i num_cells = new Vector2i(8, 8); Vector2 cell_size = bounds.Size / num_cells.Vector2(); Math.RandGenerator rgen = new Math.RandGenerator(); System.Random random = new System.Random(); for (int y = 0; y < num_cells.Y; ++y) { for (int x = 0; x < num_cells.X; ++x) { Vector2 uv = new Vector2((float)x, (float)y) / num_cells.Vector2(); var sprite = new SpriteTile() { TextureInfo = tex1 , Color = Math.SetAlpha(new Vector4(0.5f) + rgen.NextVector4(Math._0000, Math._1111) * 0.5f, 0.75f) //, Color = Math.SetAlpha( new Vector4(0.5f), 1f/*0.75f*/ ) , BlendMode = BlendMode.None , TileIndex2D = new Vector2i(random.Next(0, 4), random.Next(0, 4)) }; Vector2 p = bounds.Min + bounds.Size * uv; // init position/size sprite.Position = p; sprite.Quad.S = cell_size; // note: we don't want to touch the Node.Scale here sprite.Pivot = sprite.Quad.S * 0.5f; sprite.Schedule((dt) => { sprite.Rotation = sprite.Rotation.Rotate(Math.Deg2Rad(1.0f)); sprite.Rotation = sprite.Rotation.Normalize(); if (Common.FrameCount % 8 == 0) { sprite.TileIndex1D = (sprite.TileIndex1D + 1) % 16; } }); scene.AddChild(sprite); } } scene.RegisterDisposeOnExit((System.IDisposable)tex1); return(scene); }
public static Scene MakeManySpritesInSpriteListScene() { var tex1 = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/water_puddle.png", false) , new Vector2i(6, 1)); var scene = new Scene() { Name = "many SpriteTile in SpriteList scene" }; Bounds2 bounds = scene.Camera.CalcBounds(); Vector2i num_cells = new Vector2i(16, 8); Vector2 cell_size = bounds.Size / num_cells.Vector2(); Math.RandGenerator rgen = new Math.RandGenerator(); System.Random random = new System.Random(); SpriteList sprite_list = new SpriteList(tex1) { BlendMode = BlendMode.None }; sprite_list.EnableLocalTransform = true; scene.AddChild(sprite_list); for (int y = 0; y < num_cells.Y; ++y) { for (int x = 0; x < num_cells.X; ++x) { Vector2 uv = new Vector2((float)x, (float)y) / num_cells.Vector2(); var sprite = new SpriteTile() { // all those properties (TextureInfo, Color, BlendMode) will be ignored // if sprite is used in SpriteList TextureInfo = tex1 , Color = Math.SetAlpha(new Vector4(0.8f) + rgen.NextVector4(Math._0000, Math._1111) * 0.2f, 0.75f) , BlendMode = BlendMode.Normal // TileIndex2D can still be set per sprite , TileIndex2D = new Vector2i(random.Next(0, 6), 0) }; Vector2 p = bounds.Min + bounds.Size * uv; // init position/size if (sprite_list.EnableLocalTransform) { // use .Position as we did in MakeManySpritesScene sprite.Position = p; } else { // .Position will be ignored, so use the geometry directly sprite.Quad.T = p; } sprite.Quad.S = cell_size; // note: we don't want to touch the Node.Scale here sprite.Pivot = sprite.Quad.S * 0.5f; sprite.Schedule((dt) => { sprite.Rotation = sprite.Rotation.Rotate(Math.Deg2Rad(1.0f)); sprite.Rotation = sprite.Rotation.Normalize(); // if you turn EnableLocalTransform off for performances, you can // still move the sprite freely by changing its geometry directly: // sprite.Quad.R = sprite.Quad.R.Rotate( Math.Deg2Rad( 1.0f ) ); // sprite.Quad.R = sprite.Quad.R.Normalize(); // if (Common.FrameCount % 7 == 0) { sprite.TileIndex1D = (sprite.TileIndex1D + 1) % 6; } }); sprite_list.AddChild(sprite); } } scene.RegisterDisposeOnExit((System.IDisposable)tex1); return(scene); }
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); }