Distance() public static method

Calculates the distance between two vectors.
public static Distance ( Vector2 value1, Vector2 value2 ) : float
value1 Vector2 Source vector.
value2 Vector2 Source vector.
return float
Beispiel #1
0
 public override void step()
 {
     Velocity.Y += FallSpeed;
     if (Collides(position + Vector2.UnitY))
     {
         Velocity.Y  = 0;
         position.Y  = (float)Math.Floor(position.Y);
         Velocity.X /= 1.2f;
     }
     Velocity.X /= 1.1f;
     if (Collides(position + Velocity))
     {
         Velocity = Vector2.Zero;
     }
     position += Velocity;
     if (Vector2.Distance(Player.LocalClient.position, position) <= 3)
     {
         position = Vector2.Lerp(position, Player.LocalClient.position, 0.1f);
     }
     if (Vector2.Distance(Player.LocalClient.position, position) <= 0.3f)
     {
         SoundManager.PlaySound(SoundManager.SfxCoinPickup);
         destroy();
     }
 }
Beispiel #2
0
 /// <summary>
 /// Конструктор анимации с постоянной скоростью.
 /// </summary>
 /// <param name="linkedObject">Привязанный объект на игровом поле.</param>
 /// <param name="speed">Скрость движения объекта.</param>
 /// <param name="beginPos">Позиция в начале анимации.</param>
 /// <param name="endPos">Позиция в конце анимации.</param>
 /// <param name="blocking">Блокирует ли анимация переход в следующее состояние игры.</param>
 public MoveAnimation(GenericObject linkedObject, double speed, Vector2 beginPos, Vector2 endPos, bool blocking = false, Action <GenericObject> finishedCallback = null)
 {
     this.linkedObject     = linkedObject;
     this.beginPos         = beginPos;
     this.endPos           = endPos;
     this.blocking         = blocking;
     this.finishedCallback = finishedCallback;
     duration   = Vector2.Distance(beginPos, endPos) / speed;
     timePassed = 0.0;
     active     = true;
 }
Beispiel #3
0
        public float Compare(RushFireWork f)
        {
            var d = Vector2.Distance(Location, f.Location);

            d = 1f - Math.Min(d, 200f) / 200f; //0-1
            var rd = 1f - Math.Abs(f.Color.R - Color.R) / 255f;
            var gd = 1f - Math.Abs(f.Color.G - Color.G) / 255f;
            var bd = 1f - Math.Abs(f.Color.B - Color.B) / 255f;
            var ca = (rd + gd + bd) / 3f;
            var sd = 1f - Math.Abs(Size - f.Size);

            return((d * .5f) + (sd * .2f) + (ca * .3f));
        }
Beispiel #4
0
            private float GetMetric()
            {
                switch (Count)
                {
                case 1:
                    return(0.0f);

                case 2:
                    return(LocalPoint.Distance(Vertices.Item1.VertexDelta, Vertices.Item2.VertexDelta));

                case 3:
                    return(Vector2Util.Cross(
                               Vertices.Item2.VertexDelta - Vertices.Item1.VertexDelta,
                               Vertices.Item3.VertexDelta - Vertices.Item1.VertexDelta));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
Beispiel #5
0
 public override void Update(GameTime gameTime, WorldHandler world)
 {
     timer += gameTime.ElapsedGameTime.Milliseconds;
     base.Update(gameTime, world);
     if (timer / 1000 >= 1)
     {
         UnitInteraction(world);
     }
     if (waypoints.Count() > 0 && Vector2.Distance(Position, TargetPosition + DistanceFromPosition) < stats[typeof(Range)].Value)
     {
         waypoints.Clear();
     }
     if (timer / 1000 >= 1)
     {
         if (Target != null && Target.Position.ToPoint() != TargetPosition.ToPoint()) // Keeps tracking the units
         {
             Move(Target.Position, world);
         }
         timer = 0;
     }
 }
Beispiel #6
0
        /// <summary>
        /// Interact with each tile based on what type of tile it is if the unit is within range of their target.
        /// </summary>
        private void UnitInteraction(WorldHandler world)
        {
            float dist = Vector2.Distance(TargetPosition + DistanceFromPosition, Position);

            if (Direction == zero && Target != null && !arrived && dist <= stats[typeof(Range)].Value * 1.5)
            {
                if (Target is Building && ((ModifiableTile)Target).TeamAssociation == this.TeamAssociation)
                {
                    ((Building)Target).GarrisonedUnits.Add(this);
                    if (this.UnitState == BaseUnitState.harvest)
                    {
                        if (((Building)Target).HasTag("Wood Collector") && ((Building)Target).HasTag("Iron Collector"))
                        {
                            ((Building)Target).Collect(this.unitWallet.Withdraw());
                        }
                        else if (((Building)Target).HasTag("Iron Collector"))
                        {
                            ((Building)Target).Collect(this.unitWallet.Withdraw(new Iron()));
                        }
                    }
                    arrived = true;
                    if (returnTarget != null)
                    {
                        Harvest(returnTarget, world);
                    }
                    else if (toBuild.Count > 0)
                    {
                        Build(toBuild.Pop(), world);
                    }
                    returnTarget = null;//Set to null after setting target...keeps the unit from going random places the player wouldn't expect.
                }
                else if (Target is IHarvestable)
                {
                    Type   type = ((HarvestableUnit)Target).type.GetType();
                    Wallet wal  = ((HarvestableUnit)Target).Harvest(stats[typeof(HarvestPower)].Value + teamStats[typeof(HarvestPower)].Value);
                    if (((HarvestableUnit)Target).State == tileState.dead) //When the source dies find a new thing to harvest
                    {
                        Harvest(world.FindNearest(((HarvestableUnit)Target).type.GetType().Name.ToString(), this.Position), world);
                    }
                    bool returnResources = unitWallet.Deposit(wal);
                    if (returnResources)//If the unit wallet is full return it to the nearest building that collects the type of the harvestable resource
                    {
                        ((HarvestableUnit)Target).Return(wal);
                        returnTarget = Target;
                        try
                        {
                            Garrison(world.FindNearest(type.Name.ToString() + " Collector", this.Position), world);
                        }
                        catch (NullReferenceException) { }
                    }
                }
                //Attack the unit if it isn't part of their team
                else if (((ModifiableTile)Target).TeamAssociation != this.TeamAssociation)
                {
                    Target.Damage(stats[typeof(MeleeDamage)].Value + teamStats[typeof(MeleeDamage)].Value);
                    if (((ModifiableTile)Target).State == tileState.dead)
                    {
                        Target = null;
                    }
                }
                timer = 0;// Only reset the timer if the unit does something that way the player instantly acts when they get to the position
            }
        }