- Edited
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?
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?
Hmm, starting point, draw method perhaps? https://docs.godotengine.org/en/3.1/classes/class_canvasitem.html#class-canvasitem-method-draw
Thinking about it more, you could use turtle as inspiration and implement a worker if you will that will follow the mouse selections to construct and draw the path for you/your player.
https://en.wikipedia.org/wiki/Turtle_graphics https://en.wikipedia.org/wiki/Logo_(programming_language) https://cs.nyu.edu/courses/spring16/CSCI-UA.0380-004/slides/02/turtle.html
https://docs.godotengine.org/en/3.1/tutorials/math/beziers_and_curves.html https://docs.godotengine.org/en/3.1/tutorials/math/vector_math.html
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! :)
:/
Gave up with trying to paste it in code tags :/
[Gist](http:// "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);
}
}
}
}
}
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:
Actually I kinda gave up on the arrow idea and just drag the tank sprite for the time being. Seems simpler to implement.
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.
yep, looks like it :)
Definitely good to have more C# code posted since most users have been using GDScript so far.
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.
How do you mark a post answered?
I think you can figure it out just fine. The problem was the topic type was discussion and not a question.