MoveTo trouble
In my head I thought this would work. For MoveTo.cs you would check if it's 1 square away, then once that's completed you would check for a smaller double (ex: 0.5). Didn't go as planned, they would jitter across the screen and i'd say 75% of the time they would be 0.1 squares away from the xy coordinate. Honstly if someone had a better idea than this that would be great! PM me instead of a post so leachers don't leach (that's if you want to).
Note: the code below is messy and is not stable, don't use it.
PS. this code is just a test and the function "once" is not functional.
Note: the code below is messy and is not stable, don't use it.
PS. this code is just a test and the function "once" is not functional.
Code:
using Mono.Game;
using System;
using wServer.realm;
namespace wServer.logic.behaviors
{
public class MoveTo : CycleBehavior
{
private readonly float speed;
private readonly float speed2;
private readonly float baseX;
private readonly float baseY;
private readonly bool isMapPosition;
private readonly bool instant;
private bool once;
private bool returned;
private float X;
private float Y;
public MoveTo(float X, float Y, double speed = 2, bool once = false, bool isMapPosition = false, bool instant=false)
{
this.isMapPosition = isMapPosition;
this.speed = (float)speed;
this.speed2 = (float)speed / 8;
this.once = once;
this.instant = instant;
baseX = X;
baseY = Y;
}
protected override void OnStateEntry(Entity host, RealmTime time, ref object state)
{
if (!isMapPosition)
{
X = baseX + host.X;
Y = baseY + host.Y;
}
else
{
X = baseX;
Y = baseY;
}
if (instant)
{
host.Move(X, Y);
host.UpdateCount++;
}
}
protected override void TickCore(Entity host, RealmTime time, ref object state)
{
if (instant) return;
if (!returned)
{
if (host.HasConditionEffect(ConditionEffectIndex.Paralyzed)) return;
var spd = host.GetSpeed(speed) * (time.thisTickTimes / 1000f);
var spd2 = host.GetSpeed(speed2) * (time.thisTickTimes / 1000f);
if (Math.Abs(X - host.X) > 0.5 || Math.Abs(Y - host.Y) > 0.5)
{
Vector2 vect = new Vector2(X, Y) - new Vector2(host.X, host.Y);
vect.Normalize();
vect *= spd;
host.ValidateAndMove(host.X + vect.X, host.Y + vect.Y);
host.UpdateCount++;
if (host.X == X && host.Y == Y)
{
if (Math.Abs(X - host.X) > 0.4 || Math.Abs(Y - host.Y) > 0.4)
{
Vector2 vect0 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
vect0.Normalize();
vect0 *= spd2;
host.ValidateAndMove(host.X + vect0.X, host.Y + vect0.Y);
host.UpdateCount++;
if (host.X == X && host.Y == Y)
{
if (Math.Abs(X - host.X) > 0.3 || Math.Abs(Y - host.Y) > 0.3)
{
Vector2 vect1 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
vect1.Normalize();
vect1 *= spd2;
host.ValidateAndMove(host.X + vect1.X, host.Y + vect1.Y);
host.UpdateCount++;
if (host.X == X && host.Y == Y)
{
if (Math.Abs(X - host.X) > 0.2 || Math.Abs(Y - host.Y) > 0.2)
{
Vector2 vect2 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
vect2.Normalize();
vect2 *= spd2;
host.ValidateAndMove(host.X + vect2.X, host.Y + vect2.Y);
host.UpdateCount++;
if (host.X == X && host.Y == Y)
{
if (Math.Abs(X - host.X) > 0.1 || Math.Abs(Y - host.Y) > 0.1)
{
Vector2 vect3 = new Vector2(X, Y) - new Vector2(host.X, host.Y);
vect3.Normalize();
vect3 *= spd2;
host.ValidateAndMove(host.X + vect3.X, host.Y + vect3.Y);
host.UpdateCount++;
}
}
}
}
}
}
}
}
}
}
}
}
}
