Beispiel #1
0
        public MouseInput(Core core)
        {
            this.core = core;

            selectionRectangle = Rectangle.Empty;
            gridRectangle = Rectangle.Empty;
        }
Beispiel #2
0
        public Camera(Core core)
        {
            this.core = core;

            Zoom = 1.0f;
            Rotation = 0.0f;
            Position = Vector2.Zero;
            Speed = 300;
        }
Beispiel #3
0
        public Interface(Core core)
        {
            this.core = core;

            UIParent = new Frame();
            UIParent.Width = GameServices.GetService<GraphicsDevice>().Viewport.Width;
            UIParent.Height = GameServices.GetService<GraphicsDevice>().Viewport.Height;

            // Selection rectangle
            var selectionFrame = new Frame(UIParent);
            selectionFrame.Texture = Utility.GetSolidTexture();
            selectionFrame.Color = Color.Red * 0.5f;
            selectionFrame.Visible = false;
            UIParent["selectionFrame"] = selectionFrame;

            // OnPress
            UIParent.OnPress += delegate(Frame frame, GUIManager.MouseEventArgs args)
            {
                //KeyboardState ks = Keyboard.GetState();
                MouseState ms = args.MouseState;
                var mousePos = new Point(ms.X, ms.Y);
                var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1);
                var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y));
                var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);

                // Show the selection frame
                if (args.LeftButton == ButtonState.Pressed)
                {
                    var selectFrame = (GUI.Frame)frame["selectionFrame"];
                    if (!selectFrame.Visible)
                    {
                        selectFrame.X = mousePos.X;
                        selectFrame.Y = mousePos.Y;
                        selectFrame.Visible = true;
                    }
                }

                if (args.RightButton == ButtonState.Pressed)
                {
                    // Unit rotation
                    foreach (var unit in core.Squad)
                    {
                        if (unit.Selected && mouseWorldRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
                        {
                            checkingDirection = true;
                            directionStart = unit.Transform.World + unit.Transform.World.Origin;
                        }
                    }
                }
            };

            // OnRelease
            UIParent.OnRelease += delegate(Frame frame, GUIManager.MouseEventArgs args)
            {
                KeyboardState ks = Keyboard.GetState();
                MouseState ms = args.MouseState;
                var mousePos = new Point(ms.X, ms.Y);

                var mouseRectangle = new Rectangle(mousePos.X, mousePos.Y, 1, 1);
                var mouseWorldPos = core.Camera.ScreenToWorld(new Vector2(mousePos.X, mousePos.Y));
                var mouseGridPos= Grid.WorldToGrid(mouseWorldPos);
                var mouseWorldRectangle = new Rectangle((int)mouseWorldPos.X, (int)mouseWorldPos.Y, 1, 1);

                if (args.LeftButton == ButtonState.Pressed)
                {
                    // Deselect all units
                    if (!ks.IsKeyDown(Keys.LeftControl))
                    {
                        foreach (var unit in core.Squad)
                            unit.Selected = false;

                        foreach (var unitFrame in UnitFrames)
                            unitFrame.Color = Color.Black;
                    }

                    // Select all soldier units inside the selection rectangle
                    var selectFrame = (GUI.Frame)frame["selectionFrame"];
                    if (selectFrame.Visible)
                    {
                        Console.WriteLine("Before: " + selectFrame.AbsoluteRectangle.X + ", " + selectFrame.AbsoluteRectangle.Y + ", " + selectFrame.AbsoluteRectangle.Width + ", " + selectFrame.AbsoluteRectangle.Height);

                        var intersectionRectangle = core.Camera.ScreenToWorld(selectFrame.AbsoluteRectangle);

                        Console.WriteLine("After: " + intersectionRectangle.X + ", " + intersectionRectangle.Y + ", " + intersectionRectangle.Width + ", " + intersectionRectangle.Height);

                        // Select all units in the rectangle
                        foreach (var soldier in core.Squad)
                        {
                            if (intersectionRectangle.Intersects(soldier.GetComponent<Component.Collision>().Rectangle))
                            {
                                soldier.Selected = true;
                                foreach (var unitFrame in UnitFrames)
                                {
                                    if (unitFrame["soldier"] == soldier)
                                        unitFrame.Color = Color.Blue;
                                }
                            }
                        }

                        selectFrame.Visible = false;
                    }
                }

                // Send orders with right click
                if (args.RightButton == ButtonState.Pressed)
                {
                    if (checkingDirection)
                    {
                        checkingDirection = false;
                    }
                    else
                    {
                        Debug.WriteLine("Right button pressed on UIParent");
                        Point mainpath = Point.Zero;
                        Stack<Soldier> soldierStack = new Stack<Soldier>();
                        //core.Squad.Where(a=>a.Selected).ToList().ForEach(a => soldierStack.Push(a));
                        /*foreach (var soldier in core.Squad)
                        {
                            if (soldier.Selected)
                            {
                                soldierStack.Push(soldier);
                            }
                        }*/

                        // Don't move into other soldiers
                        bool blocked = false;
                        foreach (var soldier in core.Squad)
                            if (soldier.Position == mouseGridPos)
                                blocked = true;

                        if (!blocked)
                        {
                            byte[,] collisionMap = Core.Map.GetCollisionMap();

                            // Try pathfinding
                            foreach (var unit in core.Squad)
                            {
                                if (unit.Selected)
                                {
                                    if (mainpath == Point.Zero)
                                    {
                                        mainpath = unit.Position;
                                    }
                                    Vector2 fromMain = new Vector2((mainpath.X - unit.Position.X)*Grid.TileSize,
                                                                   (mainpath.Y - unit.Position.Y)*Grid.TileSize);
                                    var goal = Grid.WorldToGrid(mouseWorldPos - fromMain);
                                    Console.WriteLine("First " + unit.AP);
                                    unit.GetComponent<Component.PathFinder>().Goal = goal;
                                    var length = unit.GetComponent<Component.PathFinder>().Length - 1;
                                    if (unit.AP >= length)
                                    {

                                        if (unit.GetComponent<Component.PathFinder>().IsMoving == false)
                                        {
                                            // If a path was not found, add the unit to the stack
                                            soldierStack.Push(unit);
                                        }
                                        else
                                        {
                                            unit.AP -= length;
                                            Console.WriteLine("then " + unit.AP);
                                            collisionMap[goal.X, goal.Y] = 0;
                                        }
                                    }
                                    else
                                    {
                                        unit.GetComponent<Component.PathFinder>().Stop();
                                    }

                                }
                            }

                            // Loop in a spiral and assign the soldiers in the stack their own unblocked positions
                            int X = 10;
                            int Y = 10;
                            int x, y, dx, dy;
                            x = y = dx = 0;
                            dy = -1;
                            int t = (int) MathHelper.Max(X, Y);
                            int maxI = t*t;
                            for (int i = 0; i < maxI; i++)
                            {
                                if ((-X/2 < x && x <= X/2) && (-Y/2 < y && y <= Y/2))
                                {
                                    Point point = new Point(x + mouseGridPos.X, y + mouseGridPos.Y);
                                    if (collisionMap[point.X, point.Y] == 1)
                                    {
                                        if (soldierStack.Count == 0)
                                        {
                                            break;
                                        }
                                        var soldier = soldierStack.Pop();

                                        if (soldier == null)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            Console.WriteLine("before " + soldier.AP);

                                            soldier.GetComponent<Component.PathFinder>().Goal = point;
                                            var length = soldier.GetComponent<Component.PathFinder>().Length - 1;

                                            if (soldier.GetComponent<Component.PathFinder>().IsMoving == true)
                                            {
                                                if (soldier.AP >= length)
                                                {

                                                    soldier.AP -= length;

                                                    Console.WriteLine("then " + soldier.AP);

                                                }
                                                else
                                                {
                                                    soldier.GetComponent<Component.PathFinder>().Stop();
                                                }
                                            }
                                        }
                                    }

                                }
                                if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1 - y)))
                                {
                                    t = dx;
                                    dx = -dy;
                                    dy = t;
                                }
                                x += dx;
                                y += dy;
                            }
                        }
                    }
                }
            };

            hitChance = new Text(UIParent);
            hitChance.SetFont("Arial");
            hitChance.Value = "";

            var button = new GUI.Button(UIParent);
            button.SetTexture("images/Enter");
            button.X = 200;
            button.Y = 100;
            button.Width = 55;
            button.Height = 40;

            // Test GUI frames
            /*var frame = new GUI.Frame()
            {
                Y = 100
            };

            var button = new GUI.Button(frame)
            {
                Width = 48,
                Height = 23,
                Texture = core.Content.Load<Texture2D>("images/Enter"),
                OnClick = delegate(Point clickPos)
                {
                    Debug.WriteLine("That ugly button was clicked at (" + clickPos.X + "," + clickPos.Y + ")");
                }
            };

            var text = new GUI.Text(frame);
            text.SetFont("Arial");
            text.Value = "Hello world!";*/

            // Create some unit frames
            /*var panicFrame = CreatePanicFrame();
            var unit1 = CreateUnitFrame("Flight captain Rainbow");
            unit1.X = panicFrame.X;
            unit1.Y = panicFrame.Y + panicFrame.Height;
            unit1.OnClick += delegate(Frame frame, Frame.MouseEventArgs args)
            {
                frame.Color = Color.Red;
            };
            var unit2 = CreateUnitFrame("Mr. Sparkle");
            unit2.X = unit1.X;
            unit2.Y = unit1.Y + unit1.Height;
            unit2.OnClick += delegate(Frame frame, Frame.MouseEventArgs args)
            {
                frame.Color = Color.Red;
            };*/

            /*var frame1 = new GUI.Frame();
            frame1.Layer = 0;
            frame1.SetTexture("images/Enter");
            frame1.Color = Color.Red;
            frame1.Width = 100;
            frame1.Height = 100;
            frame1.OnClick += delegate(Frame frame, Frame.MouseEventArgs args)
            {
                if (frame.Color == Color.Red)
                    frame.Color = Color.Green;
                else
                    frame.Color = Color.Red;
            };
                var frame1_child = new GUI.Frame(frame1);
                frame1_child.SetTexture("images/Enter");
                frame1_child.Color = Color.Purple;
                frame1_child.Width = 60;
                frame1_child.Height = 60;
                frame1_child.X = 20;
                frame1_child.Y = 20;

            var frame2 = new GUI.Frame();
            frame2.Layer = 0;
            frame2.SetTexture("images/Enter");
            frame2.Color = Color.Blue;
            frame2.Width = 100;
            frame2.Height = 100;
            frame2.X = 50;
            frame2.Y = 50;
            frame2.OnClick += delegate(Frame frame, Frame.MouseEventArgs args)
            {
                if (frame.Color == Color.Blue)
                    frame.Color = Color.Yellow;
                else
                    frame.Color = Color.Blue;
            };*/
        }
Beispiel #4
0
        public void ParseObjects(Core core)
        {
            foreach (var layer in Layers)
            {
                var objectLayer = layer as MapObjectLayer;

                if (objectLayer == null)
                    continue;

                foreach (var mapObject in objectLayer.Objects)
                {
                    if (mapObject.Type == "SquadStart")
                    {
                        var soldier = new Soldier(ref core.Squad);
                        soldier.Name = "Derp";
                        // HACK: For some reason, the tile object coordinates are offset by one tile on the Y-axis in the Tiled map file (https://github.com/bjorn/tiled/issues/91)
                        var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
                        var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
                        soldier.X = mapObjectGridPos.X;
                        soldier.Y = mapObjectGridPos.Y;

                        core.Squad.Add(soldier);
                    }
                    if (mapObject.Type == "MonsterSpawn" || mapObject.Type == "EnemySpawn")
                    {
                        var enemy = new MonsterSpawn("Monster", 14, 0, ref core.Swarm);
                        var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
                        var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
                        enemy.X = mapObjectGridPos.X;
                        enemy.Y = mapObjectGridPos.Y;
                    }
                    if (mapObject.Type == "AmbushMonsterSpawn" || mapObject.Type == "AmbushEnemySpawn")
                    {
                        var enemy = new MonsterSpawn("Monster", 2, 0, ref core.Swarm);
                        var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
                        var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
                        enemy.X = mapObjectGridPos.X;
                        enemy.Y = mapObjectGridPos.Y;
                    }
                    if (mapObject.Type == "DelayedMonsterSpawn" || mapObject.Type == "DelayedEnemySpawn")
                    {
                        var enemy = new MonsterSpawn("Monster", 200, 4, ref core.Swarm);
                        var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
                        var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
                        enemy.X = mapObjectGridPos.X;
                        enemy.Y = mapObjectGridPos.Y;
                    }
                    if (mapObject.Type == "BossSpawn")
                    {
                        var enemy = new MonsterSpawn("BUB", 12, 0, ref core.Swarm);
                        var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize);
                        var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
                        enemy.X = mapObjectGridPos.X;
                        enemy.Y = mapObjectGridPos.Y;
                    }
                    if (mapObject.Type == "Door")
                    {
                        var door = new Door(true);
                        var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y);
                        var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos);
                        door.X = mapObjectGridPos.X;
                        door.Y = mapObjectGridPos.Y;
                    }
                }
            }
        }
 public KeyboardInput(Core core)
 {
     this.core = core;
 }