Esempio n. 1
0
    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);
    }
Esempio n. 2
0
    public static Scene MakeTestActionsScene()
    {
        TextureInfo Characters = new TextureInfo(new Texture2D("/Application/Sample/GameEngine2D/FeatureCatalog/data/PlanetCute/Objects.png", false), new Vector2i(7, 3));

        float world_scale = 0.036f;

        var sprite = new SpriteTile();

        sprite.TextureInfo = Characters;
        sprite.TileIndex1D = 2;
        sprite.Quad.S      = sprite.CalcSizeInPixels() * world_scale;
        sprite.CenterSprite();
        sprite.Color     = new Vector4(1.0f, 1.0f, 1.0f, 0.75f);
        sprite.BlendMode = BlendMode.Normal;

        var scene = new Scene()
        {
            Name = "Action tests"
        };

        scene.AddChild(sprite);

        var pos = new Vector2(0.0f, 0.0f);

        int writing_color_tag    = 333;
        int writing_position_tag = 65406;

        AddButton(scene, "MoveTo (0,0)", ref pos, () => { sprite.RunAction(new MoveTo(new Vector2(0.0f, 0.0f), 0.1f)); });
        AddButton(scene, "MoveBy (3,0)", ref pos, () => { sprite.RunAction(new MoveBy(new Vector2(3.0f, 0.0f), 0.1f)); });
        AddButton(scene, "ScaleBy (2,2)", ref pos, () => { sprite.RunAction(new ScaleBy(new Vector2(2.0f, 2.0f), 0.1f)); });
        AddButton(scene, "ScaleBy (0.5,0.5)", ref pos, () => { sprite.RunAction(new ScaleBy(new Vector2(0.5f, 0.5f), 0.1f)); });

        AddButton(scene, "TintTo White", ref pos, () =>
        {
            // prevent from running an other action that writes the color
            if (sprite.GetActionByTag(writing_color_tag) != null)
            {
                sprite.StopActionByTag(writing_color_tag);
            }

            sprite.RunAction(new TintTo(Math.SetAlpha(Colors.White, 0.75f), 2.0f)
            {
                Tag = writing_color_tag
            });
        }
                  );

        AddButton(scene, "TintTo Blue", ref pos, () =>
        {
            // prevent from running an other action that writes the color
            if (sprite.GetActionByTag(writing_color_tag) != null)
            {
                sprite.StopActionByTag(writing_color_tag);
            }

            sprite.RunAction(new TintTo(Math.SetAlpha(Colors.Blue, 0.75f), 2.0f)
            {
                Tag = writing_color_tag
            });
        }
                  );

        AddButton(scene, "Pingpong colors", ref pos, () =>

        {
            // prevent from running an other action that writes the color
            if (sprite.GetActionByTag(writing_color_tag) != null)
            {
                sprite.StopActionByTag(writing_color_tag);
            }

            var action12 = new TintTo(Colors.Green, 0.5f)
            {
                Tween = (t) => FMath.Sin(t * Math.Pi * 0.5f),
            };

            var action13 = new TintTo(Colors.Magenta, 0.5f)
            {
                Tween = (t) => FMath.Sin(t * Math.Pi * 0.5f),
            };

            var seq = new Sequence();
            seq.Add(action12);
            seq.Add(action13);
            var repeat0 = new RepeatForever()
            {
                InnerAction = seq, Tag = writing_color_tag
            };
            sprite.RunAction(repeat0);
        }
                  );

        AddButton(scene, "Pingpong position", ref pos, () =>

        {
            // prevent from running the same action twice
            // (we could also just hold an Action object somewhere and re-run, so that this check wouldn't be needed)
            if (sprite.GetActionByTag(writing_position_tag) != null)
            {
                sprite.StopActionByTag(writing_position_tag);
            }

            var action12 = new MoveTo(new Vector2(-5, 0), 0.5f)
            {
                Tween = (t) => FMath.Sin(t * Math.Pi * 0.5f),
            };

            var action13 = new MoveTo(new Vector2(5, 0), 0.5f)
            {
                Tween = (t) => FMath.Sin(t * Math.Pi * 0.5f),
            };

            var seq = new Sequence();
            seq.Add(action12);
            seq.Add(action13);
            var repeat0 = new RepeatForever()
            {
                InnerAction = seq, Tag = writing_position_tag
            };

            sprite.RunAction(repeat0);
        }
                  );

        AddButton(scene, "StopAllActions", ref pos, () => sprite.StopAllActions());

        scene.RegisterDisposeOnExit((System.IDisposable)Characters);

        return(scene);
    }