Beispiel #1
0
        public static void InitializeContent()
        {
            #region tutorial step 2
            #region arena
            var map = new Point(2000, 2000);

            var arena = new ArenaControl();

            arena.Layers.Canvas.style.backgroundColor =
                Color.FromGray(0xc0);
            arena.SetLocation(
                Rectangle.Of(0, 0, Native.window.Width, Native.window.Height));

            arena.SetCanvasSize(map);

            arena.Control.AttachToDocument();


            arena.DrawTextToInfo(Title, new Point(8, 8), Color.Blue);

            Native.window.onresize +=
                delegate
            {
                arena.SetLocation(
                    Rectangle.Of(0, 0, Native.window.Width, Native.window.Height));

                arena.SetCanvasPosition(
                    arena.CurrentCanvasPosition
                    );
            };
            #endregion
            #endregion


            var pending = default(Dude2);

            #region arsenal
            var arsenal = new Dictionary <string, DudeAnimationInfo>
            {
                { "Soldier",
                  new DudeAnimationInfo
                  {
                      Frames_Stand = Frames.WolfSoldier,
                      Frames_Walk  = Frames.WolfSoldier_Walk
                  } },
                { "Imp",
                  new DudeAnimationInfo
                  {
                      Frames_Stand = Frames.DoomImp,
                      Frames_Walk  = Frames.DoomImp_Walk
                  } }
            };
            #endregion


            #region tutorial step 3

            Func <DudeAnimationInfo, Point, Dude2> CreateActor =
                (_frames, _coords) =>
            {
                var actor = new Dude2();

                actor.Frames = _frames.Frames_Stand;
                actor.AnimationInfo.Frames_Stand = _frames.Frames_Stand;
                actor.AnimationInfo.Frames_Walk  = _frames.Frames_Walk;
                actor.Zoom.DynamicZoomFunc       = a => 1;
                actor.SetSize(48, 72);
                actor.TeleportTo(_coords.X, _coords.Y);
                actor.Zoom.StaticZoom = DefaultActiorZoom;
                actor.Direction       = Math.PI * 0.5;
                actor.Control.AttachTo(arena.Layers.Canvas);
                //actor.HasShadow = _frames.Frames_Stand.Length > 1;
                if (_frames.Frames_Stand.Length == 1)
                {
                    actor.Shadow.style.Opacity = 0.4;
                }
                actor.AnimationInfo.WalkAnimationInterval = 1000 / 30;
                return(actor);
            };

            var actors = new List <Dude2>
            {
            };

            var selection = from i in actors
                            where i.IsSelected
                            select i;

            arena.ApplySelection +=
                (rect, ev) =>
            {
                if (pending != null)
                {
                    return;
                }

                foreach (var v in actors)
                {
                    v.IsSelected = rect.Contains(v.CurrentLocation.ToInt32());
                }
            };

            var Argh        = new Argh();
            var Affirmative = new Affirmative();
            var ghoullaugh  = new ghoullaugh();
            var sheep       = new sheep();
            var pig         = new pig();
            var click       = new click().AttachToDocument();

            arena.SelectionClick +=
                (p, ev) =>
            {
                if (pending != null)
                {
                    return;
                }

                foreach (var v in selection)
                {
                    if (v.AnimationInfo.Frames_Stand[0].Source == MyFrames.ManWithHorns.Frames_Stand[0].Source)
                    {
                        ghoullaugh.play();
                        ghoullaugh = new ghoullaugh();
                    }
                    else if (v.AnimationInfo.Frames_Stand[0].Source == MyFrames.ThePig.Frames_Stand[0].Source)
                    {
                        pig.play();
                        pig = new pig();
                    }
                    else if (v.AnimationInfo.Frames_Stand[0].Source == MyFrames.TheSheep.Frames_Stand[0].Source)
                    {
                        sheep.play();
                        sheep = new sheep();
                    }
                    else if (v.AnimationInfo.Frames_Stand[0].Source == Frames.WolfSoldier[0].Source)
                    {
                        Affirmative.play();
                        Affirmative = new Affirmative();
                    }
                    else
                    {
                        Argh.play();
                        Argh = new Argh();
                    }



                    v.WalkTo(p.ToDouble());

                    // move in group formation
                    p.X += 16;
                    p.Y += 16;
                }
            };


            #endregion

            #region tutorial step 4



            #region CreateDialogAt
            var CreateDialogAt =
                new
            {
                //Dialog = default(IHTMLDiv),
                Content = default(IHTMLDiv),
                Width   = default(string)
            }
            .ToFunc(
                (Point pos, string width) =>
            {
                var f = new Form();

                f.Show();

                f.SizeTo(200, 200);


                // https://sites.google.com/a/jsc-solutions.net/work/knowledge-base/15-dualvr/20151115/audio
                //f.PopupInsteadOfClosing();

                f.MoveTo(pos.X, pos.Y);
                //f.SizeTo(


                //var dialog = new IHTMLDiv();

                //dialog.style.SetLocation(pos.X, pos.Y);

                //dialog.style.backgroundColor = Color.Gray;
                //dialog.style.padding = "1px";

                //var caption = new IHTMLDiv().AttachTo(dialog);

                //caption.style.backgroundColor = Color.Blue;
                //caption.style.width = width;
                //caption.style.height = "0.5em";
                //caption.style.cursor = IStyle.CursorEnum.move;

                //var drag = new DragHelper(caption);

                //drag.Position = pos;
                //drag.Enabled = true;
                //drag.DragMove +=
                //    delegate
                //    {
                //        dialog.style.SetLocation(drag.Position.X, drag.Position.Y);
                //    };

                var _content = new IHTMLDiv().AttachTo(f.GetHTMLTargetContainer());

                _content.style.textAlign       = IStyle.TextAlignEnum.center;
                _content.style.backgroundColor = Color.White;
                _content.style.padding         = "1px";

                //dialog.AttachToDocument();

                return(new
                {     //Dialog = dialog,
                    Content = _content,
                    Width = width
                });
            }
                );
            #endregion

            #region dialog
            var toolbar = CreateDialogAt(new Point(2, 2), "8em");

            var combo = new IHTMLSelect();
            var build = new IHTMLButton();

            build.style.SetSize(72, 72);
            build.style.padding = "0px";

            var avatar = new IHTMLImage().AttachTo(build);
            var remove = new IHTMLButton("Remove");


            combo.AttachTo(toolbar.Content);
            new IHTMLBreak().AttachTo(toolbar.Content);
            build.AttachTo(toolbar.Content);
            new IHTMLBreak().AttachTo(toolbar.Content);
            remove.AttachTo(toolbar.Content);
            new IHTMLBreak().AttachTo(toolbar.Content);
            #endregion


            #region GetSelectedArsenal
            Func <DudeAnimationInfo> GetSelectedArsenal =
                () =>
            {
                if (arsenal.ContainsKey(combo[combo.selectedIndex].value))
                {
                    return(arsenal[combo[combo.selectedIndex].value]);
                }

                return(null);
            };
            #endregion

            Action Refresh =
                delegate
            {
                var i = GetSelectedArsenal();

                if (i != null)
                {
                    avatar.src = i.Images.Random().src;
                }
            };

            combo.Add(arsenal.Keys.ToArray());
            Refresh();

            combo.onchange +=
                delegate
            {
                Refresh();



                click.play();
                click = new click().AttachToDocument();
            };

            #region pending actor

            arena.MouseMove +=
                p =>
            {
                if (pending == null)
                {
                    return;
                }

                pending.TeleportTo(p.X, p.Y);
            };

            arena.Layers.User.oncontextmenu +=
                e =>
            {
                e.preventDefault();

                if (pending != null)
                {
                    pending.Control.Orphanize();
                    pending = null;
                    arena.ShowSelectionRectangle = true;

                    return;
                }

                actors.ForEach(
                    k => k.IsSelected = false
                    );
            };

            arena.SelectionClick +=
                (p, ev) =>
            {
                if (pending == null)
                {
                    return;
                }

                pending.TeleportTo(p.X, p.Y);

                actors.Add(pending);

                pending.IsHot = false;


                var x = GetSelectedArsenal();
                pending = CreateActor(x,
                                      new Point(
                                          Native.window.Width / 2,
                                          Native.window.Height / 2
                                          )
                                      );

                pending.IsHot = true;


                click.play();
                click = new click().AttachToDocument();
            };

            build.onclick +=
                delegate
            {
                if (pending != null)
                {
                    pending.Control.Orphanize();
                    pending = null;

                    return;
                }

                var x = GetSelectedArsenal();

                pending = CreateActor(x,
                                      new Point(
                                          Native.window.Width / 2,
                                          Native.window.Height / 2
                                          )
                                      );

                pending.IsHot = true;
                arena.ShowSelectionRectangle = false;

                click.play();
                click = new click().AttachToDocument();
            };
            #endregion

            remove.onclick +=
                delegate
            {
                foreach (var v in selection.ToArray())
                {
                    v.Control.Orphanize();
                    actors.Remove(v);
                }


                click.play();
                click = new click().AttachToDocument();
            };

            #endregion

            if (FilterToImpAndSoldier)
            {
            }
            else
            {
                #region step 6

                {
                    var n = "NPC";

                    arsenal.Add(n, MyFrames.NPC3);
                    combo.Add(n);
                }

                #endregion


                {
                    var n = "ManWithHorns";

                    arsenal.Add(n, MyFrames.ManWithHorns);
                    combo.Add(n);
                }
                {
                    var n = "TheSheep";

                    arsenal.Add(n, MyFrames.TheSheep);
                    combo.Add(n);
                }
                {
                    var n = "ThePig";

                    arsenal.Add(n, MyFrames.ThePig);
                    combo.Add(n);
                }

                {
                    var n = "TheCactus";

                    arsenal.Add(n, MyFrames.TheCactus);
                    combo.Add(n);
                }
            }


            if (BeforeAddingDebris != null)
            {
                BeforeAddingDebris(arena.Layers.Canvas);
            }

            3.Times(
                delegate()
            {
                new DebrisImages().Images.ForEach(
                    img => img.AttachTo(arena.Layers.Canvas).style.SetLocation(map.X.Random(), map.Y.Random())
                    );
            }
                );

            16.Times(
                delegate()
            {
                actors.Add(
                    CreateActor(arsenal.Random().Value, new Point(map.X.Random(), map.Y.Random()))
                    );
            }
                );
        }
        /// <summary>
        /// Creates a new control
        /// </summary>
        /// <param name="DataElement">The hidden data element</param>
        public GameOfLife()
        {
            var MyContainer = new IHTMLDiv().AttachToDocument();

            MyContainer.style.position = IStyle.PositionEnum.absolute;
            MyContainer.style.left     = "0px";
            MyContainer.style.top      = "0px";
            MyContainer.style.right    = "0px";
            MyContainer.style.bottom   = "0px";

            var vv = new ArenaControl();



            var cx = 32;
            var cy = 32;

            var w = 32;
            var h = 32;

            vv.SetCanvasSize(new Point(cx * w, cy * h));


            vv.SetLocation(new Rectangle {
                Left = 0, Top = 0, Width = Native.window.Width, Height = Native.window.Height
            });

            Native.window.onresize +=
                delegate
            {
                Console.WriteLine("onresize");
                vv.SetLocation(new Rectangle {
                    Left = 0, Top = 0, Width = Native.window.Width, Height = Native.window.Height
                });
            };

            vv.Layers.Canvas.style.backgroundColor = Color.White;

            vv.Control.AttachTo(MyContainer);


            var buffer = new Array2D <LayeredControl.CanvasRectangle>(cx, cy);


            vv.Layers.Canvas.Hide();

            Action <string, Point, Color> DrawTextWithShadow =
                (text, pos, c) =>
            {
                vv.DrawTextToInfo(text, pos + new Point(2, 2), Color.Black);
                vv.DrawTextToInfo(text, pos, Color.White);
                vv.DrawTextToInfo(text, pos + new Point(1, 1), c);
            };

            //DrawTextWithShadow("Game Of Life - Use middle mouse button to drag map around", new Point(8, 8), Color.Red);

            int index = 0;

            var State = new __Type1
            {
                ColorDeath = Color.White,
                //ColorSurvival = Color.Gray,
                ColorBirth = Color.Black
            };

            Func <Color> RandomState = () => new System.Random().NextDouble() > 0.5 ? State.ColorDeath : State.ColorBirth;


            #region reset

            Action Reset =
                () =>
                buffer.ForEach(
                    (int x, int y) =>
            {
                var c = new LayeredControl.CanvasRectangle
                {
                    BackgroundColor = RandomState(),
                    Location        = new Rectangle {
                        Left = x * w + 1, Top = y * h + 1, Width = w - 2, Height = h - 2
                    }
                };

                vv.DrawRectangleToCanvas(c);

                buffer[x, y] = c;
            }
                    );

            Reset();

            #endregion

            vv.Layers.Canvas.Show();

            //var f = new IHTMLElement(IHTMLElement.HTMLElementEnum.fieldset);

            //var chk_enabled = new IHTMLInput(HTMLInputTypeEnum.checkbox);
            //var btn_reset = new IHTMLButton("Randomize");

            //btn_reset.onclick += (i) => Reset();

            //f.appendChild(btn_reset, chk_enabled, new IHTMLLabel("Activate", chk_enabled));

            //f.AttachToDocument();
            //f.style.SetLocation(500, 60);

            var NextEvolution = default(Action);

            Action <int> SleepAndEvolve =
                (timeout) => new Timer((t) => NextEvolution(), timeout, 0);

            #region NextEvolution
            NextEvolution =
                () =>
            {
                var nextframe = default(Action);
                var t         = IDate.Now.getTime();

                //if (chk_enabled.@checked)
                buffer.ForEach(
                    (int x, int y) =>
                {
                    Func <int, int, LayeredControl.CanvasRectangle> g =
                        (ox, oy) => buffer[x + ox, y + oy];

                    var u = g(0, 0);

                    var c = new[] {
                        g(-1, -1),
                        g(-1, 0),
                        g(-1, 1),
                        g(0, 1),
                        g(0, -1),
                        g(1, -1),
                        g(1, 0),
                        g(1, 1)
                    }.Count(i => i != null && i.BackgroundColor == State.ColorBirth);


                    var ncolor = default(Color);

                    var IsDeath = u.BackgroundColor == State.ColorDeath;
                    var IsBirth = u.BackgroundColor == State.ColorBirth;

                    var Is2    = c == 2;
                    var Is3    = c == 3;
                    var Is2or3 = Is2 || Is3;

                    if (IsDeath && Is3)
                    {
                        ncolor = State.ColorBirth;
                    }
                    else if (IsBirth && Is2or3)
                    {
                        ncolor = State.ColorBirth;
                    }
                    else
                    {
                        ncolor = State.ColorDeath;
                    }


                    nextframe += () =>
                    {
                        u.BackgroundColor = ncolor;
                        u.Update();
                    };
                }
                    );

                if (nextframe != null)
                {
                    nextframe();
                    nextframe = null;
                }

                int timeout = (int)(IDate.Now.getTime() - t) - 0;

                System.Console.WriteLine("time: " + timeout);

                SleepAndEvolve(timeout.Max(100));
            };
            #endregion

            SleepAndEvolve(1);
        }