コード例 #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()))
                        );
                }
            );
        }
コード例 #2
0
        public Program(IHTMLElement placeholder)
        {
            // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
            // http://www.quirksmode.org/js/doctypes.html
            // http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/index.html

            // we do not want to see those scrollbars
            Native.Document.body.style.overflow = IStyle.OverflowEnum.hidden;

            // Preload Images
            foreach (var v in Frames.Harvester1)
            {
                var dummy = v.Image;
            }

            //Console.EnableActiveXConsole();

            var MyDudeHotRange = 24;
            var MyDudes = new List<Dude2>();
            var MySelectedDudes = new Dude2[] { };

            Session = new MySession();

            this.Session.IServer_EnterLobby(
                delegate(string e)
                {
                    var TheWorld = new MyGameWorld();

                    #region supporting user chat

                    var ChatBox = new LayeredTextBox();

                    Native.Document.onkeypress +=
                        delegate(IEvent ev)
                        {
                            if (ev.KeyCode == 'q') Console.WriteLine("Q");
                            if (ev.KeyCode == 'w') Console.WriteLine("W");
                            if (ev.KeyCode == 'e') Console.WriteLine("E");
                            if (ev.KeyCode == 'r')
                            {
                                var random_spawn_position = new __Type1
                                                            {
                                                                x = new System.Random().NextDouble() * 600,
                                                                y = new System.Random().NextDouble() * 400,
                                                            };

                                Console.WriteLine("random_spawn_position: " + random_spawn_position);

                                // Lets Spawn Something into the world
                            }

                            //Console.Log("onkeypress: " + new { KeyCode = ev.KeyCode });

                            if (!ChatBox.IsVisible)
                            {
                                if (ev.KeyCode == 't')
                                {
                                    ev.PreventDefault();

                                    Timer.DoAsync(
                                        delegate
                                        {
                                            ChatBox.ShowAndFocus();
                                        }
                                    );
                                }
                            }
                        };

                    #endregion

                    this.Session.ClientName = e;


                    var a = new ArenaControl();
                    var m = new ArenaMinimapControl();

                    a.Control.AttachToDocument();

                    a.Layers.Canvas.style.backgroundColor = Color.FromRGB(0, 0x80, 0);

                    // set the map to be somewhere left
                    a.SetLocation(Rectangle.Of(32, 32, 640, 480));


                    // set tha map canvas size to be something big
                    a.SetCanvasSize(new Point(8000, 8000));

                    #region DrawTextWithTimeout
                    System.Action<string, Color> DrawTextWithTimeout =
                                   delegate(string text, Color color)
                                   {
                                       var z = new IHTMLDiv(new ITextNode(text));

                                       z.style.color = color;
                                       z.style.backgroundColor = Color.Black;

                                       a.Layers.Info.appendChild(z);

                                       this.SessionTimer[TimerEvent.DelayOnce(9000)] =
                                           delegate
                                           {
                                               z.Dispose();
                                           };
                                   };
                    #endregion

                    this.SessionTimer[TimerEvent.DelayOnce(1000)] =
                       delegate
                       {
                           a.DrawTextToInfo("just some data", new Point(46, 246), Color.Black);
                           a.DrawTextToInfo("just some data", new Point(45, 245), Color.Yellow);

                           DrawTextWithTimeout("hello world", Color.Red);
                       };



                    var data = new List<Pair<Rectangle, Color>>();

                    #region minimap


                    m.Zoom.Validate += delegate
                    {
                        if (a.CurrentCanvasSize.X > a.CurrentCanvasSize.Y)
                        {
                            var w = m.CurrentLocation.Width / a.CurrentCanvasSize.X;

                            if (m.Zoom.Value < w)
                                m.Zoom.Value = w;
                        }
                        else
                        {
                            var h = m.CurrentLocation.Height / a.CurrentCanvasSize.Y;

                            if (m.Zoom.Value < h)
                                m.Zoom.Value = h;
                        }
                    };


                    m.Zoom.Changed += delegate
                    {
                        m.Layers.Canvas.removeChildren();

                        m.SetCanvasSize(a.CurrentCanvasSize * m.Zoom.Value);
                        m.SetSelectionLocation(a.CanvasView * m.Zoom.Value);
                        m.MakeSelectionVisible();


                        var data_array = data.ToArray();

                        foreach (var v in data_array)
                        {
                            var r = v.A;
                            var c = v.B;

                            m.DrawRectangleToCanvas(r * m.Zoom.Value, c);
                        }
                    };

                    m.Control.AttachToDocument();

                    m.SetLocation(Rectangle.Of(690, 50, 200, 200));


                    #endregion

                    #region DrawRectangle
                    Action<Rectangle, Color> DrawRectangleLocal =
                        delegate(Rectangle r, Color c)
                        {
                            var p = new Pair<Rectangle, Color>(r, c);

                            data.Add(p);

                            a.DrawRectangleToCanvas(r, c);
                            m.DrawRectangleToCanvas(r * m.Zoom.Value, c);
                        };



                    this.Session.OnIClient_DrawRectangle += delegate(Message._IClient_DrawRectangle p)
                    {
                        var r = new Rectangle
                                {
                                    Left = p.rect.Left,
                                    Top = p.rect.Top,
                                    Width = p.rect.Width,
                                    Height = p.rect.Height,
                                };

                        DrawRectangleLocal(r, p.color);
                    };


                    Action<Rectangle, Color> DrawRectangle =
                        delegate(Rectangle r, Color c)
                        {
                            DrawRectangleLocal(r, c);

                            this.Session.IServer_DrawRectangle(r, c);
                        };

                    #endregion

                    #region SpawnHarvester
                    Action<Point, double> SpawnHarvester_Local =
                        delegate(Point Location, double Direction)
                        {
                            // add new dude
                            var dude = CreateHarvester();

                            dude.Direction = Direction;
                            dude.Zoom.StaticZoom = 1;
                            dude.TeleportTo(Location.X, Location.Y);

                            dude.Control.AttachTo(a.Layers.Canvas);

                            MyDudes.Add(dude);
                        };

                    this.Session.OnIClient_SpawnHarvester += 
                        ev => SpawnHarvester_Local(ev.Location, (ev.Direction / 32d));

                    Action<Point, double> SpawnHarvester =
                        (Location, Direction) =>
                        {
                            SpawnHarvester_Local(Location, Direction);
                            this.Session.IServer_SpawnHarvester(Location, (Direction * 32).ToInt32());
                        };

                    //(Location, Direction) =>
                    //{
                    //    SpawnHarvester_Local(Location, Direction);
                    //};
                    #endregion

                    a.SelectionClick += delegate(Point p, IEvent ev)
                    {
                        Console.WriteLine("SelectionClick_1");

                        if (ev.ctrlKey)
                        {
                            SpawnHarvester(p, (Math.PI * 2).Random());

                            // DrawRectangle(p.WithMargin(a.SelectionMinimumSize * 2), Color.Green);
                        }
                        else
                        {
                            var Selection = MyDudes.Where(v => v.CurrentLocation.GetRange(p) < MyDudeHotRange).ToArray();

                            if (Selection.Length == 0)
                            {
                                MySelectedDudes.ForEach(v => v.WalkTo(p));
                            }
                            else
                            {
                                MySelectedDudes.ForEach(v => v.IsSelected = false);
                                MySelectedDudes = Selection;
                                MySelectedDudes.ForEach(v => v.IsSelected = true);
                            }

                        }
                    };

                    a.MouseMove +=
                        point =>
                        {
                            foreach (var v in MyDudes)
                            {
                                v.IsHot = v.CurrentLocation.GetRange(point) < MyDudeHotRange;
                            }
                        };

                    a.ApplySelection += delegate(Rectangle r, IEvent ev)
                    {
                        if (ev.ctrlKey)
                        {
                            DrawRectangle(r, RandomColor);
                        }
                        else
                        {
                            MySelectedDudes.ForEach(v => v.IsSelected = false);
                            MySelectedDudes = MyDudes.Where(v => r.Contains(v.CurrentLocation)).ToArray();
                            MySelectedDudes.ForEach(v => v.IsSelected = true);
                        }
                    };

                    a.CanvasViewChanged += delegate(Rectangle p)
                    {
                        m.SetSelectionLocation(p * m.Zoom.Value);
                        m.MakeSelectionVisible();
                    };

                    a.SetCanvasPosition(Point.Zero);

                    m.SelectionCenterChanged += delegate(Point p)
                    {
                        a.SetCanvasViewCenter(p / m.Zoom.Value);
                    };

                    m.Zoom.SetValue(0);

                    //chatbox.style.SetLocation(32, 480 - 32, 650, 22);
                    //chatbox.style.height = "1em";
                    //chatbox.attachToDocument();



                    ChatBox.SetLocation(Rectangle.Of(0, 440, 640, 22));
                    a.Layers.Info.appendChild(ChatBox.Control);

                    ChatBox.Layers.Canvas.style.backgroundColor = Color.Yellow;
                    ChatBox.Layers.Canvas.style.Opacity = 0.8;

                    ChatBox.Layers.Text.style.color = Color.White;

                    ChatBox.Hide();



                    ChatBox.Send += delegate(string text)
                    {
                        DrawTextWithTimeout(text, Color.White);

                        this.Session.IServer_TalkToOthers(text);
                    };



                    this.Session.OnIClient_DisplayNotification += x => DrawTextWithTimeout(x.text, x.color);


                    // put some elements on the canvas
                    //DrawRectangleLocal(Rectangle.Of(48, 48, 128, 64), Color.Green);
                    //DrawRectangleLocal(Rectangle.Of(48, 128, 128, 64), Color.Gray);
                    //DrawRectangleLocal(Rectangle.Of(400, 300, 128, 64), Color.Black);
                    //DrawRectangleLocal(Rectangle.Of(400, 500, 128, 64), 0xff5566);
                    //DrawRectangleLocal(Rectangle.Of(700, 600, 128, 64), 0x3f5466);


                    #region game units

                    var ai = new ArenaInfo();

                    ai.Arena = a;
                    ai.Minimap = m;

                    #region hidden
                    /*
                    {
                        var mcy = new ArenaUnit();

                        mcy.Color = Color.Red;
                        mcy.Owner = ai;
                        mcy.Title = "My Construction Yard Unit";


                        mcy.SetLocation(new Point(64, 64));
                        mcy.SetSize(new Point(72, 72));

                        // put the unit on the canvas
                        ai.Arena.Layers.Canvas.appendChild(mcy.Control);

                        // update its location
                        mcy.UpdateControlLocation();

                        // remeber that we have such a unit
                        ai.Units.Add(mcy);
                    }
                    */

                    /*

                    {

                        var mcy = new ArenaUnit();

                        mcy.Health.Max = 2000;
                        mcy.Health.Current = 1500;


                        mcy.Color = Color.Red;
                        mcy.Owner = ai;
                        mcy.Title = "My Construction Yard Unit 3";

                        mcy.IsSelectable = true;


                        mcy.SetLocation(new Point(250, 200));
                        var mfx = fx.Settings.ConstructionYard;

                        mcy.SetSize(mfx.Size);

                        mfx.ShowFrame(mcy.Control, 12);


                        // put the unit on the canvas
                        ai.Arena.Layers.Canvas.appendChild(mcy.Control);

                        // update its location
                        mcy.UpdateControlLocation();

                        // remeber that we have such a unit
                        ai.Units.Add(mcy);
                    }


                    {

                        var mcy = new ArenaUnit();

                        mcy.Health.Max = 2000;
                        mcy.Health.Current = 1500;


                        mcy.Color = Color.Red;
                        mcy.Owner = ai;
                        mcy.Title = "My Construction Yard Unit 3";

                        mcy.IsSelectable = true;


                        mcy.SetLocation(new Point(250, 300));
                        var mfx = fx.Settings.ConstructionYard;

                        mcy.SetSize(mfx.Size);

                        mfx.ShowFrame(mcy.Control, 12);


                        // put the unit on the canvas
                        ai.Arena.Layers.Canvas.appendChild(mcy.Control);

                        // update its location
                        mcy.UpdateControlLocation();

                        // remeber that we have such a unit
                        ai.Units.Add(mcy);
                    }*/
                    #endregion

                    m.Zoom.Changed +=
                        delegate
                        {
                            // redraw all units to the minimap
                            ai.DrawToMinimap();
                        };

                    #endregion

                    a.SelectionClick +=
                        delegate(Point p, IEvent ev)
                        {
                            ai.SelectUnits(p);
                        };

                    a.ApplySelection +=
                        delegate(Rectangle r, IEvent ev)
                        {
                            ai.SelectUnits(r);
                        };

                    ai.DrawToMinimap();
                }
            );
        }