This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

click n drag arrow to select destination square

nathanjwtxnathanjwtx Posts: 30Member
edited September 2019 in 2D

I'm working on a square grid based game and want to have a "click n drag" arrow to select the destination square but I'm not sure where to start with that. Any ideas?


Tags :

Answers

  • MegalomaniakMegalomaniak Posts: 2,580Admin

    BTW, would love to see what solution you end up ultimately arriving at, and I'm sure there are plenty of others lurking around that would love to see it too! :)

  • nathanjwtxnathanjwtx Posts: 30Member
    edited September 2019

    :/

  • nathanjwtxnathanjwtx Posts: 30Member
    edited September 2019

    Gave up with trying to paste it in code tags :/

    Gist


    using Godot;
    using System;
    using System.Collections.Generic;
    
    public class Player : KinematicBody2D
    {
        private bool _dragged = false;
        private Vector2 _current;
        private List<Vector2> _moves = new List<Vector2>();
    
        public override void _Ready()
        {
            // set initial position of unit
            _current = new Vector2((float)(Math.Floor(this.GetPosition().x / 64)) * 64 + 32,
                (float)(Math.Floor(this.GetPosition().y / 64) * 64 + 32));
            _moves.Add(_current);
        }
    
        public override void _Process(float delta)
        {
            // SelectedTank();
        }
    
        public override void _Input(InputEvent @event)
        {
            if (@event is InputEventMouseButton mouseEvent)
            {
                if ((mouseEvent.GetPosition() - this.GetPosition()).Length() < 32)
                {
                    if (!_dragged & mouseEvent.ButtonMask == 1)
                    {
                        _dragged = true;
                    }
                    else
                    {
                        _dragged = false;
                    }
                }   
            }
            if (@event is InputEventMouseMotion mouseMoved)
            {
                if (_dragged)
                {
                    MoveTank(mouseMoved.GetPosition());
                }   
            }
        }
    
        private void MoveTank(Vector2 mousePos)
        {
            Vector2 tile = new Vector2((float)(Math.Floor(mousePos.x / 64)) * 64 + 32,
                (float)(Math.Floor(mousePos.y / 64) * 64 + 32));
            float priorX;
            float priorY;
            // GD.Print(tile);
            if (_moves.Count == 0)
            {
                _moves.Add(tile);
            }
            if (tile != _current)
            {
                _current = tile;
                priorX = _moves[_moves.Count - 1].x;
                priorY = _moves[_moves.Count - 1].y;
                this.Position = tile;
                if (_moves.IndexOf(_current) > -1)
                {
                    _moves.RemoveAt(_moves.Count - 1);
                }
                else
                {
                    _moves.Add(_current);
                }
                if (_moves.Count > 1)
                {
                    var norm = (_current - _moves[_moves.Count - 1]).Normalized();
                    GD.Print(norm);
                    if (_current.x < priorX)
                    {
                        this.SetRotationDegrees((float) 90);
                    }
                    else if (_current.x > priorX)
                    {
                        this.SetRotationDegrees((float)-90);
                    }
                    else if (_current.y > priorY)
                    {
                        this.SetRotationDegrees((float) 0);
                    }
                    else if (_current.y < priorY)
                    {
                        this.SetRotationDegrees((float) 180);
                    }
                }
            }
    
        }
    }
    
  • MegalomaniakMegalomaniak Posts: 2,580Admin

    Huh, might it be you are editing the post? I keep trying to edit it to fix the code formatting, but I keep also seeing unexpected changes again. :sweat_smile:

  • nathanjwtxnathanjwtx Posts: 30Member

    Actually I kinda gave up on the arrow idea and just drag the tank sprite for the time being. Seems simpler to implement.

  • MegalomaniakMegalomaniak Posts: 2,580Admin
    edited September 2019

    Just indent your whole code by one indentation, then copy and paste here. :)

    this
    is all
    on the
    same
    indentation
    of one level
    

    edit: well, hopefully I found the correct gist.

  • nathanjwtxnathanjwtx Posts: 30Member

    yep, looks like it :)

  • MegalomaniakMegalomaniak Posts: 2,580Admin

    Definitely good to have more C# code posted since most users have been using GDScript so far.

  • nathanjwtxnathanjwtx Posts: 30Member

    I toyed with going the GDScript route as I'm a huge Python fan but I use C# in my daily job and figured it would be best to stick with something I already know vs trying to learn something new.

  • nathanjwtxnathanjwtx Posts: 30Member

    How do you mark a post answered?

  • MegalomaniakMegalomaniak Posts: 2,580Admin

    I think you can figure it out just fine. The problem was the topic type was discussion and not a question.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.