Exemple #1
0
        private static Dude2 CreateDude(DudeAnimationInfo LoadedCharacter)
        {
            var r = new Dude2();

            r.Frames = LoadedCharacter.Frames_Stand;

            r.AnimationInfo.Frames_Stand = LoadedCharacter.Frames_Stand;
            r.AnimationInfo.Frames_Walk  = LoadedCharacter.Frames_Walk;

            r.Zoom.DynamicZoomFunc = a => (a + 400) / (800);
            r.Zoom.StaticZoom      = 1.75;

            r.SetSize(48, 72);
            r.TeleportTo(100, 100);

            r.Control.className = "cursorred";
            r.TargetLocationDistanceMultiplier = 1;


            r.Direction = Math.PI.Random() * 2;
            return(r);
        }
        private static Dude2 CreateHarvester()
        {
            var dude = new Dude2();

            System.Console.WriteLine("new dude");

            dude.Frames = Frames.Harvester1;



            dude.AnimationInfo.Frames_Stand = dude.Frames;
            dude.AnimationInfo.Frames_Walk = new[] { dude.Frames };

            dude.Zoom.StaticZoom = 1;
            dude.Zoom.DynamicZoom = 1;
            dude.Zoom.DynamicZoomFunc = i => 1;
            //dude.RawWalkSpeed = 0.4;

            dude.SetSize(48, 48);

            dude.TargetLocationDistanceMultiplier = 1;


            //dude.Direction = System.Math.PI.Random() * 2;
            //dude.Direction = 0;

            //dude.Control.style.border = "1px solid red";

            //dude.Control.AttachToDocument();
            return dude;
        }
        private static void BuildStage()
        {
            var stage = new IHTMLDiv { className = "stage" };

            stage.AttachToDocument();

            IStyleSheet.Default.AddRule(".stage",
                r =>
                {
                    r.style.backgroundColor = Color.White;

                    r.style.position = IStyle.PositionEnum.absolute;
                    r.style.left = "0px";
                    r.style.top = "0px";
                    r.style.right = "0px";
                    r.style.bottom = "0px";
                    r.style.Opacity = 0.0;
                }
            );



            Action<Dude2, IEvent> WheelAction =
                (d, ev) =>
                    d.Zoom.StaticZoom += ev.WheelDirection * 0.2;

            var list = new List<Dude2>();

            var Selection = default(Dude2);

            Action<Dude2> SelectDude = i =>
            {
                if (Selection != null)
                    Selection.IsSelected = false;

                Selection = i;

                if (Selection != null)
                    Selection.IsSelected = true;
            };

            Action<Dude2> BindSelectDude =
                i => i.Control.onclick += delegate { SelectDude(i); };

            Func<FrameInfo[], int, int, Dude2> SpawnLookingDude =
            (f, x, y) =>
            {
                var r = new Dude2
                {
                    Frames = f,
                };

                //BindSelectDude(r);

                r.AnimationInfo.Frames_Stand = f;

                r.Zoom.DynamicZoomFunc = a => (a + 400) / (800);
                r.Zoom.StaticZoom = 1;

                r.SetSize(48, 72);
                r.TeleportTo(x, y);

                r.Control.className = "cursorred";

                r.Control.AttachToDocument();

                r.Direction = Math.PI.Random() * 2;


                r.Control.onmousewheel +=
                    (ev) =>
                    {
                        WheelAction(r, ev);
                    };

                list.Add(r);

                return r;
            };

            Func<int> r400 = () => 400.Random() + 100;


            SpawnLookingDude(Frames.PigCop, r400(), r400()).LookAtMouse(stage);
            SpawnLookingDude(Frames.Trooper, r400(), r400()).LookAtMouse(stage);

            #region wolf3d
            // say hello to wolf3d
            // video: http://www.youtube.com/watch?v=ohDAKEEKyP8
            // http://verdammt.ionichost.com/

            var w1 = SpawnLookingDude(Frames.WolfSoldier, r400(), r400());
            w1.Zoom.StaticZoom = 1.75;
            //w1.LookAtMouse(stage);
            w1.AnimationInfo.Frames_Walk = Frames.WolfSoldier_Walk;
            //w1.RawWalkSpeed = 0.01;
            BindSelectDude(w1);


            var w2 = SpawnLookingDude(Frames.WolfSoldier, r400(), r400());
            w2.Zoom.StaticZoom = 1.75;
            w2.AnimationInfo.Frames_Walk = Frames.WolfSoldier_Walk;
            w2.WalkTo(new Point(r400(), r400()));
            BindSelectDude(w2);

            #endregion

            var runner = SpawnLookingDude(Frames.Duke, r400(), r400());

            runner.AnimationInfo.Frames_Walk = Frames.Duke_Walk;
            runner.WalkTo(new Point(r400(), r400()));
            BindSelectDude(runner);

            stage.onmousemove +=
                ev =>
                {
                    try
                    {
                        if (Selection != null)
                            if (!Selection.IsWalking)
                                Selection.LookAt(ev.CursorPosition);
                    }
                    catch
                    {

                    }
                    /*
                    foreach (var v in list.Where(i => i.AnimationInfo.Frames_Stand == Frames.Duke))
                        v.WalkTo(ev.CursorPosition);
                    */
                };



            stage.onclick +=
                (ev) =>
                {
                    if (ev.ctrlKey)
                    {
                        var n = SpawnLookingDude(Frames.Duke, ev.CursorX, ev.CursorY);

                        n.AnimationInfo.Frames_Walk = Frames.Duke_Walk;

                        BindSelectDude(n);
                        SelectDude(n);

                        return;
                    }

                    if (Selection != null)
                        Selection.WalkTo(ev.CursorPosition);

                    //SelectDude(null);

                    /*
                    var n = SpawnLookingDude(Frames.Duke, ev.CursorX, ev.CursorY);

                    n.AnimationInfo.Frames_Walk = Frames.Duke_Walk;*/
                };

            stage.onmousewheel +=
                (ev) =>
                {
                    foreach (var v in list)
                    {
                        WheelAction(v, ev);
                    }
                };
        }
        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();
                }
            );
        }