Пример #1
0
        public Vertex(char label, Vector2f pos)
        {
            this.label = label;
            previous   = '0';
            distance   = uint.MaxValue;

            shape = new CircleShape(20);

            shape.FillColor        = new Color(55, 200, 255);  //invisible
            shape.OutlineThickness = 5;
            shape.OutlineColor     = new Color(255, 255, 255); //white

            shape.Position = pos + new Vector2f(5, 5);         //for outlinethickness

            shape.Origin = new Vector2f(shape.GetGlobalBounds().Width / 2, shape.GetGlobalBounds().Height / 2);

            center = new CircleShape(5);

            center.Origin = new Vector2f(center.GetGlobalBounds().Width / 2, center.GetGlobalBounds().Height / 2);

            center.FillColor = new Color(255, 0, 0);

            center.Position = pos;

            bounds = new RectangleShape(new Vector2f(shape.GetLocalBounds().Width, shape.GetLocalBounds().Height));

            bounds.Origin = new Vector2f(bounds.GetLocalBounds().Width / 2, bounds.GetLocalBounds().Height / 2);

            bounds.FillColor = new Color(0, 0, 0, 0);

            bounds.OutlineThickness = 2;
            bounds.OutlineColor     = new Color(0, 0, 0); //white

            bounds.Position = pos;

            Font font = new Font("resources/fonts/Font1.ttf");

            if (font == null)
            {
                System.Console.WriteLine("No Such Font");
            }

            labelchar = new Text();

            labelchar.Font            = font;
            labelchar.CharacterSize   = 40;
            labelchar.Color           = new Color(255, 255, 255);
            labelchar.DisplayedString = label + "";
            labelchar.Position        = pos + new Vector2f(-2, -12); //font offset

            labelchar.Origin = new Vector2f(labelchar.GetGlobalBounds().Width / 2, labelchar.GetGlobalBounds().Height / 2);
        }
Пример #2
0
        public Slider(Vector2D position, Vector2D size, Font font) : base(position, size, font)
        {
            _box           = new RectangleShape(size);
            _box.FillColor = Color.White;
            _box.Origin    = size / 2;
            _box.Position  = position;

            _drawables.Add(_box);

            double smallerOne;

            if (size.X < size.Y)
            {
                smallerOne = size.X;
            }
            else
            {
                smallerOne = size.Y;
            }

            _circle                  = new CircleShape((float)(smallerOne * 2));
            _circle.FillColor        = Color.Black;
            _circle.OutlineColor     = Color.White;
            _circle.OutlineThickness = 1.5f;
            _circle.Origin           = new Vector2D(smallerOne * 2, smallerOne / 2 - 1);
            //_circle.Position = position + new Vector2D(-_box.GetGlobalBounds().Width / 2, -_circle.GetGlobalBounds().Height / 2 + _box.GetGlobalBounds().Height / 2);
            //_circle.Position = position + new Vector2D(_box.GetGlobalBounds().Width / 2, -_circle.GetGlobalBounds().Height / 2 + _box.GetGlobalBounds().Height / 2);
            _circle.Position = position + new Vector2D(0, -_circle.GetGlobalBounds().Height / 2 + _box.GetGlobalBounds().Height / 2);

            _drawables.Add(_circle);
        }
Пример #3
0
 /// <summary>
 /// Adds a collision trigger to a circle.
 /// </summary>
 public static void AddCollisionTriggerCircle(CircleShape rect, CircleShape rect2)
 {
     if (rect.GetGlobalBounds().Intersects(rect2.GetGlobalBounds()))
     {
         Vector2f position = rect.Position;
         Vector2f obj      = rect2.Position;
         if (position.Y > obj.Y)
         {
             position.Y   += 1;
             rect.Position = position;
         }
         else if (position.Y < obj.Y)
         {
             position.Y   -= 1;
             rect.Position = position;
         }
         else if (position.X > obj.X)
         {
             position.X   += 1;
             rect.Position = position;
         }
         else if (position.X < obj.X)
         {
             position.X   -= 1;
             rect.Position = position;
         }
     }
 }
Пример #4
0
        public static bool Intersection(FloatRect floatRect, Vector2D position)
        {
            CircleShape mouseCircle = new CircleShape(2);

            mouseCircle.Origin   = new Vector2D(1, 1);
            mouseCircle.Position = position;

            return(mouseCircle.GetGlobalBounds().Intersects(floatRect));
        }
Пример #5
0
 public void SetupLongRangeIndicator()
 {
     longRange = new CircleShape(LongRangeDistance);
     longRange.OutlineColor     = new Color(255, 255, 255, 20);
     longRange.OutlineThickness = 1;
     longRange.FillColor        = Color.Transparent;
     longRange.Position         = Position;
     longRange.Origin           = new Vector2f(longRange.GetGlobalBounds().Width / 2, longRange.GetGlobalBounds().Height / 2);
 }
Пример #6
0
 /// <summary>
 /// Gets a collision trigger to a circle.
 /// </summary>
 public static bool CollisionTriggerCircle(CircleShape rect, CircleShape rect2)
 {
     if (rect.GetGlobalBounds().Intersects(rect2.GetGlobalBounds()))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #7
0
        public bool IsMouseOverPlanetoid()
        {
            Vector2i vecMousePosition         = Mouse.GetPosition(Engine.Instance.GameWindow);
            Vector2f vecRelativeMousePosition = (Vector2f)Engine.Instance.GameWindow.MapPixelToCoords(vecMousePosition, Engine.Instance.GameWindow.GetView());

            if (OutLine.GetGlobalBounds().Contains(vecRelativeMousePosition.X, vecRelativeMousePosition.Y))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #8
0
        private void Input()
        {
            if (Keyboard.IsKeyPressed(Keyboard.Key.Return) || Keyboard.IsKeyPressed(Keyboard.Key.Escape))
            {
                _window.Close();
            }

            if (Mouse.IsButtonPressed(Mouse.Button.Left))
            {
                _mouseCircle.Position = (Vector2f)Mouse.GetPosition(_window);

                for (int i = 0; i < _checkBoxes.Count; i++)
                {
                    if (_mouseCircle.GetGlobalBounds().Intersects(_checkBoxes[i].GetGlobalBounds()))
                    {
                        if (i % 2 == 0)
                        {
                            _xLeft.Position = _checkBoxes[i].Position;
                            _difficultyLeft = (Ai.Difficulty)(i / 2);
                            break;
                        }
                        else
                        {
                            _xRight.Position = _checkBoxes[i].Position;
                            _difficultyRight = (Ai.Difficulty)((i - 1) / 2);
                            break;
                        }
                    }
                }


                if (_mouseCircle.GetGlobalBounds().Intersects(_pressToContinue.GetGlobalBounds()))
                {
                    _window.Close();
                }
            }
        }
Пример #9
0
        public override void Touched(Vector2D position)
        {
            CircleShape mouseCircle = new CircleShape(2);

            mouseCircle.Origin   = new Vector2D(1, 1);
            mouseCircle.Position = position;

            if (_box.GetGlobalBounds().Intersects(mouseCircle.GetGlobalBounds()))
            {
                IsChecked = true;
            }
            else
            {
                IsChecked = false;
            }
        }
        public void TouchedOnce(Vector2D position)
        {
            CircleShape mouseCircle = new CircleShape(2);

            mouseCircle.Origin   = new Vector2D(1, 1);
            mouseCircle.Position = position;

            if (_controls.GetGlobalBounds().Intersects(mouseCircle.GetGlobalBounds()))
            {
                OnPlay("menuselect");
                Program.windowState     = Program.WindowState.PopUp;
                Program.lastWindowState = Program.WindowState.MainMenu;
                return;
            }

            for (int i = 0; i < _checkboxes.Count; i++)
            {
                if (_checkboxes[i].Touched(position) && _checkboxes[i].IsCheckable)
                {
                    OnPlay("menuselect");
                }

                if (_checkboxes[i].IsChecked)
                {
                    switch (i)
                    {
                    case 0:
                        _checkboxes[1].IsChecked = false;
                        break;

                    case 1:
                        _checkboxes[0].IsChecked = false;
                        break;

                    case 2:
                        _checkboxes[3].IsChecked = false;
                        break;

                    case 3:
                        _checkboxes[2].IsChecked = false;
                        break;

                    default: return;
                    }
                }
            }
        }
Пример #11
0
 public bool Intersects(CircleShape circle)
 {
     return(shape.GetGlobalBounds().Intersects(circle.GetGlobalBounds()));
 }
Пример #12
0
        public void loadNewDialogue(string speaker, string content)
        {
            currSpeaker = speaker;


            if (speaker == "alex")
            {
                dialogueBoxSprite          = spriteDict["right"];
                dialogueBoxSprite.Position = new Vector2f((float)(SCREEN_WIDTH * 0.5) - (dialogueBoxSprite.GetGlobalBounds().Width / 2), (float)(SCREEN_HEIGHT * 0.33) - (dialogueBoxSprite.GetGlobalBounds().Height / 2));

                name              = new Text(speaker.ToUpper(), speechFont, nameFontSize);
                name.Position     = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + ((118 * scale.X) - name.GetGlobalBounds().Width / 2), dialogueBoxSprite.GetGlobalBounds().Top + ((22 * scale.Y) - name.GetGlobalBounds().Height));
                dialogue          = new Text(content, speechFont, dialogueFontSize);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (uint)(SCREEN_WIDTH * 0.004), dialogueBoxSprite.GetGlobalBounds().Top + (uint)(SCREEN_HEIGHT * 0.04));
            }
            else if (speaker == "dad")
            {
                dialogueBoxSprite          = spriteDict["left"];
                dialogueBoxSprite.Position = new Vector2f((float)(SCREEN_WIDTH * 0.21) - (dialogueBoxSprite.GetGlobalBounds().Width / 2), (float)(SCREEN_HEIGHT * 0.23) - (dialogueBoxSprite.GetGlobalBounds().Height / 2));

                name              = new Text(speaker.ToUpper(), speechFont, nameFontSize);
                name.Position     = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + ((118 * scale.X) - name.GetGlobalBounds().Width / 2), dialogueBoxSprite.GetGlobalBounds().Top + ((22 * scale.Y) - name.GetGlobalBounds().Height));
                dialogue          = new Text(content, speechFont, dialogueFontSize);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (uint)(SCREEN_WIDTH * 0.004), dialogueBoxSprite.GetGlobalBounds().Top + (uint)(SCREEN_HEIGHT * 0.04));
            }
            else if (speaker == "mom")
            {
                dialogueBoxSprite          = spriteDict["right"];
                dialogueBoxSprite.Position = new Vector2f((float)(SCREEN_WIDTH * 0.77) - (dialogueBoxSprite.GetGlobalBounds().Width / 2), (float)(SCREEN_HEIGHT * 0.25) - (dialogueBoxSprite.GetGlobalBounds().Height / 2));

                name              = new Text(speaker.ToUpper(), speechFont, nameFontSize);
                name.Position     = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + ((118 * scale.X) - name.GetGlobalBounds().Width / 2), dialogueBoxSprite.GetGlobalBounds().Top + ((22 * scale.Y) - name.GetGlobalBounds().Height));
                dialogue          = new Text(content, speechFont, dialogueFontSize);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (uint)(SCREEN_WIDTH * 0.004), dialogueBoxSprite.GetGlobalBounds().Top + (uint)(SCREEN_HEIGHT * 0.04));
            }
            else if (speaker == "player")
            {
                dialogueBoxSprite          = spriteDict["player"];
                dialogueBoxSprite.Position = new Vector2f(0, (float)(SCREEN_HEIGHT * 0.74));

                name              = new Text("YOU", speechFont, nameFontSize);
                name.Position     = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + ((354 * scale.X) - name.GetGlobalBounds().Width / 2), dialogueBoxSprite.GetGlobalBounds().Top + ((32 * scale.Y) - name.GetGlobalBounds().Height));
                dialogue          = new Text(content, speechFont, dialogueFontSize);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (uint)(SCREEN_WIDTH * 0.004), dialogueBoxSprite.GetGlobalBounds().Top + (uint)(SCREEN_HEIGHT * 0.062));
            }
            else if (speaker == "tooltip1")
            {
                // Press Space to advance
                dialogueBoxSprite          = spriteDict["tooltiplefthover"];
                dialogueBoxSprite.Position = new Vector2f(SCREEN_WIDTH * 0.93f - dialogueBoxSprite.GetGlobalBounds().Width, SCREEN_HEIGHT * 0.954f - dialogueBoxSprite.GetGlobalBounds().Height / 2);

                infoSprite          = spriteDict["tooltipleft"];
                infoSprite.Position = dialogueBoxSprite.Position;

                // NOT USED FOR TOOLTIP - STILL NEEDED FOR DB LOGIC
                name          = new Text("", speechFont, nameFontSize);
                name.Position = dialogueBoxSprite.Position;
                // ~~~~~~~~~~~~~~~~~~~

                dialogue          = new Text(content, speechFont, dialogueFontSize);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (dialogueBoxSprite.GetGlobalBounds().Width * 0.44f) - dialogue.GetGlobalBounds().Width / 2, dialogueBoxSprite.GetGlobalBounds().Top + (dialogueBoxSprite.GetGlobalBounds().Height / 2) - (dialogue.GetGlobalBounds().Height));
            }
            else if (speaker == "tooltip2")
            {
                // Drag tone to your dialogue
                int buttonxpos = (int)(SCREEN_WIDTH / 4);
                dialogueBoxSprite          = spriteDict["tooltiprighthover"];
                dialogueBoxSprite.Position = new Vector2f(((buttonxpos / 2) + buttonxpos) - dialogueBoxSprite.GetGlobalBounds().Width / 2, (float)(SCREEN_HEIGHT - SCREEN_HEIGHT * 0.315));

                infoSprite          = spriteDict["tooltipright"];
                infoSprite.Position = dialogueBoxSprite.Position;

                // NOT USED FOR TOOLTIP - STILL NEEDED FOR DB LOGIC
                name          = new Text("", speechFont, nameFontSize);
                name.Position = dialogueBoxSprite.Position;
                // ~~~~~~~~~~~~~~~~~~~

                dialogue          = new Text(content, speechFont, dialogueFontSize - 3);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (dialogueBoxSprite.GetGlobalBounds().Width * 0.55f) - dialogue.GetGlobalBounds().Width / 2, dialogueBoxSprite.GetGlobalBounds().Top + (dialogueBoxSprite.GetGlobalBounds().Height / 2) - (dialogue.GetGlobalBounds().Height));
            }
            else if (speaker == "tooltip3")
            {
                // Click/Space to Speak
                dialogueBoxSprite          = spriteDict["tooltipsmallhover"];
                dialogueBoxSprite.Position = new Vector2f(SCREEN_WIDTH * 0.85f, SCREEN_HEIGHT * 0.842f - dialogueBoxSprite.GetGlobalBounds().Height / 2);

                infoSprite          = spriteDict["tooltipsmall"];
                infoSprite.Position = dialogueBoxSprite.Position;

                // NOT USED FOR TOOLTIP - STILL NEEDED FOR DB LOGIC
                name          = new Text("", speechFont, nameFontSize);
                name.Position = dialogueBoxSprite.Position;
                // ~~~~~~~~~~~~~~~~~~~

                dialogue          = new Text(content, speechFont, dialogueFontSize - 5);
                dialogue.Position = new Vector2f(dialogueBoxSprite.GetGlobalBounds().Left + (dialogueBoxSprite.GetGlobalBounds().Width * 0.55f) - dialogue.GetGlobalBounds().Width / 2, dialogueBoxSprite.GetGlobalBounds().Top + (dialogueBoxSprite.GetGlobalBounds().Height / 2) - (dialogue.GetGlobalBounds().Height));
            }

            // Cursor initializations
            if (tag != "tooltip")
            {
                if (tag == "AI")
                {
                    OGcursorX = dialogueBoxSprite.GetGlobalBounds().Left + dialogueBoxSprite.GetGlobalBounds().Width - (float)(dialogueBoxSprite.GetGlobalBounds().Width * 0.05);
                    OGcursorY = dialogueBoxSprite.GetGlobalBounds().Top + dialogueBoxSprite.GetGlobalBounds().Height - (float)(dialogueBoxSprite.GetGlobalBounds().Height * .3);
                }
                else if (tag == "PLAYER")
                {
                    OGcursorX = (float)(SCREEN_WIDTH * 0.95);
                    OGcursorY = (float)(SCREEN_HEIGHT * 0.95);
                }
                cursor.Position = new Vector2f(OGcursorX, OGcursorY);

                cursor.Origin = new Vector2f(cursor.GetGlobalBounds().Width / 2, cursor.GetGlobalBounds().Height / 2);
                FcursorX      = OGcursorX + 3;
                FcursorY      = OGcursorY + 3;
            }
            // ----------------------

            name.Color     = Color.White;
            dialogue.Color = Color.White;

            awaitInput   = false;
            active       = true;
            elementIndex = 0;

            renderDialogue(content);
        }
Пример #13
0
        public Grid(IEnumerable <GamePiece> tiles, Vector2u gridSize, Vector2f scale, uint gridLayers = 1, DragManager mouseManager = null, int dragLayer = -1, int dropLayer = -1, Vector2f dropScale = default, Vector2u cellSize = default)
        {
            if (dropScale == default)
            {
                dropScale = scale;
            }

            window = mouseManager.Window;
            if (tiles == null)
            {
                this.tiles = new List <GamePiece>();
            }
            else
            {
                this.tiles = tiles.ToList();
            }
            this.mapSize = gridSize;
            this.scale   = scale;
            mapLayers    = gridLayers;
            map          = new int[gridSize.X, gridSize.Y, gridLayers];
            if (cellSize == default)
            {
                tileSize = this.tiles[0].TextureRect.GetSize().ToVector2f();
            }
            else
            {
                tileSize = (Vector2f)cellSize;
            }

            // apply scale to tiles
            for (var i = 0; i < this.tiles.Count; i++)
            {
                this.tiles[i].Scale = scale;
            }

            size           = new Vector2f(gridSize.X * tileSize.X, gridSize.Y * tileSize.Y);
            scaledSize     = new Vector2f(Size.X * scale.X, Size.Y * scale.Y);
            scaledTileSize = new Vector2f(tileSize.X * scale.X, tileSize.Y * scale.Y);

            // init map
            for (var z = 0; z < mapLayers; z++)
            {
                for (var y = 0; y < mapSize.Y; y++)
                {
                    for (var x = 0; x < mapSize.X; x++)
                    {
                        map[x, y, z] = -1;
                    }
                }
            }

            // mouse
            if (mouseManager != null)
            {
                if (dragLayer != -1)
                {
                    GamePiece       picked      = null;
                    int             pickedIndex = -1;
                    Action <Sprite> dropHandled = null;
                    Vector2f        dragOffset  = default;
                    Vector2i        pickCoords  = default;

                    mouseManager.DragStart += (object sender, DragEventArgs e) =>
                    {
                        if (Enabled)
                        {
                            if (e.Button == Mouse.Button.Left &&
                                e.DragStart.X >= Rect.Left &&
                                e.DragStart.Y >= Rect.Top &&
                                e.DragStart.X < Rect.Left + Rect.Width &&
                                e.DragStart.Y < Rect.Top + Rect.Height)
                            {
                                picked = SpritePicker(e.DragStart, dragLayer, out pickCoords);

                                if (picked != null)
                                {
                                    moves         = GetMoves(picked, pickCoords);
                                    movesOccupied = GetMovesOccupied(picked, pickCoords);
                                    Debug.WriteLine($"[Grid] Picked {pickCoords.X},{pickCoords.Y}");

                                    pickedIndex = map[pickCoords.X, pickCoords.Y, dragLayer];
                                    dropHandled = sprite =>
                                    {
                                        //map[pickCoords.X, pickCoords.Y, dragLayer] = -1;
                                    };
                                    picked.Color         = new Color(255, 255, 255, 0);
                                    draggingSprite       = new GamePiece(picked);
                                    draggingSprite.Color = new Color(255, 255, 255, 200);
                                    dragOffset           = (e.DragStart.ToVector2f() - draggingSprite.Position) * -1;

                                    // empty origial area
                                    var subgridSize = getSubGridSizeOfTile(picked);
                                    for (var y = 0; y < subgridSize.Y; y++)
                                    {
                                        for (var x = 0; x < subgridSize.X; x++)
                                        {
                                            map[pickCoords.X + x, pickCoords.Y + y, dragLayer] = -1;
                                        }
                                    }
                                }
                            }
                        }
                    };
                    mouseManager.Dragging += (object sender, DragEventArgs e) =>
                    {
                        if (Enabled && draggingSprite != null)
                        {
                            var dp = e.DragCurrent.ToVector2f();
                            draggingSprite.Position = dp + dragOffset;
                        }
                    };
                    mouseManager.DragEnd += (object sender, DragEventArgs e) =>
                    {
                        if (Enabled)
                        {
                            if (picked != null)
                            {
                                picked.Color  = new Color(255, 255, 255, 255);
                                picked        = null;
                                moves         = null;
                                movesOccupied = null;
                            }
                            if (draggingSprite != null)
                            {
                                if (!mouseManager.Drop(this, draggingSprite, dropHandled, e))
                                {
                                    // if the drop was unhandled, restore the item to its original location
                                    var subgridSize = getSubGridSizeOfTile(draggingSprite);
                                    if (subgridSize.X > 1 || subgridSize.Y > 1)
                                    {
                                        for (var y = 0; y < subgridSize.Y; y++)
                                        {
                                            for (var x = 0; x < subgridSize.X; x++)
                                            {
                                                map[pickCoords.X + x, pickCoords.Y + y, dropLayer] = pickedIndex - int.MinValue;
                                            }
                                        }
                                    }
                                    map[pickCoords.X, pickCoords.Y, dropLayer] = pickedIndex;
                                }
                                draggingSprite = null;
                            }
                        }
                    };
                }
                if (dropLayer != -1)
                {
                    mouseManager.Dropped += (object sender, DropEventArgs e) =>
                    {
                        if (Enabled)
                        {
                            if (e.DragStop.X >= Rect.Left && e.DragStop.Y >= Rect.Top &&
                                e.DragStop.X < Rect.Left + Rect.Width &&
                                e.DragStop.Y < Rect.Top + Rect.Height)
                            {
                                var p = e.Sprite.GetGlobalBounds().GetCenter().ToVector2i()
                                        + new Vector2i(0, 32); // move the drop point down so it is centered on the base of the chess piece
                                                               //var p = (Vector2i)(e.Sprite.Position + (cellSize.ToVector2f() / 2.0f));
                                var tpi = ScreenToGridCoordinates(p);
                                Debug.WriteLine($"[Grid] Dropped {tpi.X},{tpi.Y}");

                                var subgridSize = getSubGridSizeOfTile(e.Sprite);
                                if (tpi.X >= 0 && tpi.X + (subgridSize.X - 1) < mapSize.X && tpi.Y >= 0 && tpi.Y + (subgridSize.Y - 1) < mapSize.Y)
                                {
                                    var obstacleFound = false;

                                    /*
                                     * for (var y=tpi.Y; y<tpi.Y+subgridSize.Y; y++){
                                     *  for (var x=tpi.X; x<tpi.X+subgridSize.X; x++){
                                     *      if (map[x, y, dropLayer] != -1){
                                     *          obstacleFound=true;
                                     *          break;
                                     *      }
                                     *  }
                                     * }
                                     */

                                    // check if move is valid according to board state
                                    if (!obstacleFound)
                                    {
                                        obstacleFound = !IsMoveAllowed(e.Sprite, ScreenToGridCoordinates(e.DragStart), tpi);
                                    }
                                    //var index = map[tpi.X, tpi.Y, dropLayer];
                                    //if (index == -1)
                                    if (!obstacleFound)
                                    {
                                        e.Sprite.Color = new Color(255, 255, 255, 255);
                                        if (!this.tiles.Contains(e.Sprite))
                                        {
                                            this.tiles.Add(e.Sprite);
                                        }

                                        var tileIndex = this.tiles.IndexOf(e.Sprite);

                                        // if this item is larger than 1x1 fill the other grid cells with "shadow" indeces
                                        // so that items will not be able to overlap

                                        if (subgridSize.X > 1 || subgridSize.Y > 1)
                                        {
                                            for (var y = 0; y < subgridSize.Y; y++)
                                            {
                                                for (var x = 0; x < subgridSize.X; x++)
                                                {
                                                    map[tpi.X + x, tpi.Y + y, dropLayer] = tileIndex - int.MinValue;
                                                }
                                            }
                                        }

                                        map[tpi.X, tpi.Y, dropLayer] = tileIndex;

                                        e.Sprite.Scale = dropScale;
                                        if (e.OnHandled != null)
                                        {
                                            e.OnHandled.Invoke(e.Sprite);
                                            e.Handled = true;
                                            OnMoved();
                                        }
                                    }
                                }
                            }
                        }
                    };
                }
            }

            moveMarker = new CircleShape(13);
            moveMarker.OutlineThickness = 1;
            moveMarker.FillColor        = new Color(0, 0, 0, 64);
            moveMarker.OutlineColor     = new Color(0, 0, 0, 32);
            moveMarker.Origin           = moveMarker.GetGlobalBounds().GetCenter();

            moveMarkerOccupied                  = new CircleShape(24);
            moveMarkerOccupied.FillColor        = new Color(0, 0, 0, 0);
            moveMarkerOccupied.OutlineThickness = 6;
            moveMarkerOccupied.OutlineColor     = new Color(0, 0, 0, 64);
            moveMarkerOccupied.Origin           = moveMarkerOccupied.GetGlobalBounds().GetCenter();
        }
Пример #14
0
        static void Main(string[] args)
        {
            using var window = new RenderWindow(new VideoMode(800, 600), "Hello SFML");
            window.Closed   += (s, e) => window.Close();

//            var shape = new RectangleShape(new Vector2f(20, 100));
//            shape.Position = new Vector2f(20, 300);
//            shape.FillColor = new Color(66, 121, 153);
//            shape.Origin = shape.Size * 0.5f;
//
//            var shape2 = new RectangleShape(new Vector2f(20, 100));
//            shape2.Position = new Vector2f(800 - 20, 300);
//            shape2.FillColor = new Color(66, 121, 153);
//            shape2.Origin = shape2.Size * 0.5f;

            var batLeft  = new Bat(new Vector2f(20, 300), Keyboard.Key.W, Keyboard.Key.S);
            var batRight = new Bat(new Vector2f(800 - 20, 300), Keyboard.Key.Up, Keyboard.Key.Down);

            batLeft.OnEnter(window);
            batRight.OnEnter(window);

            var ball = new CircleShape(10.0f);

            ball.Position  = new Vector2f(400, 300);
            ball.FillColor = new Color(189, 248, 255);
            ball.Origin    = new Vector2f(ball.Radius, ball.Radius);
            var ballVelocity = new Vector2f(200.0f, 150.0f);

            var gui = new Text();

            gui.Font            = new Font("Roboto-Regular.ttf");
            gui.CharacterSize   = 30;
            gui.DisplayedString = "Score: 0 / 0";
            //gui.FillColor = Color.White;
            gui.Style    = Text.Styles.Bold;
            gui.Position = new Vector2f(400.0f, 30.0f);
            gui.Origin   = new Vector2f(gui.GetGlobalBounds().Width, gui.GetGlobalBounds().Height) * 0.5f;

            bool moveUp = false, moveDown = false, move2Up = false, move2Down = false;

            window.KeyPressed += (s, e) =>
            {
                if (e.Code == Keyboard.Key.W)
                {
                    moveUp = true;
                }
                if (e.Code == Keyboard.Key.S)
                {
                    moveDown = true;
                }
                if (e.Code == Keyboard.Key.Up)
                {
                    move2Up = true;
                }
                if (e.Code == Keyboard.Key.Down)
                {
                    move2Down = true;
                }
            };

            window.KeyReleased += (s, e) =>
            {
                switch (e.Code)
                {
                case Keyboard.Key.W:
                    moveUp = false;
                    break;

                case Keyboard.Key.S:
                    moveDown = false;
                    break;

                case Keyboard.Key.Up:
                    move2Up = false;
                    break;

                case Keyboard.Key.Down:
                    move2Down = false;
                    break;
                }
            };

            var clock        = new Clock();
            var player1Score = 0;
            var player2Score = 0;

            while (window.IsOpen)
            {
                var deltaTime = clock.Restart().AsSeconds();

                window.DispatchEvents();
                window.Clear(new Color(131, 197, 235));

                batLeft.OnUpdate(deltaTime);
                batRight.OnUpdate(deltaTime);

                ball.Position += ballVelocity * deltaTime;
                if (batLeft.Intersects(ball) || batRight.Intersects(ball))
                {
                    ballVelocity.X *= -1.0f;
                }

                if (ball.GetGlobalBounds().Top < 0.0f ||
                    ball.GetGlobalBounds().Top + ball.GetGlobalBounds().Height > 600.0f)
                {
                    ballVelocity.Y *= -1.0f;
                }

                if (ball.Position.X < -100.0f || ball.Position.X > 900.0f)
                {
                    if (ball.Position.X > 400.0f)
                    {
                        player1Score++;
                    }
                    else
                    {
                        player2Score++;
                    }
                    gui.DisplayedString = $"Score: {player1Score} / {player2Score}";

                    ball.Position = new Vector2f(400.0f, 300.0f);
                    ballVelocity  = new Vector2f(150.0f, 150.0f);
                }

                batLeft.OnRender(window);
                batRight.OnRender(window);
                window.Draw(ball);
                window.Draw(gui);

                window.Display();
            }
        }