First i need a class which holds the position information.
public class BossMovement
{
public Vector2 pos { get; set; }
public float speed { get; set; }
public float waitTime { get; set; }
public BossMovement(Vector2 pPos, float pSpeed, float pWaitTime)
{
pos = pPos;
speed = pSpeed;
waitTime = pWaitTime;
}
}
And generate positions.
List<BossMovement> movements = new List<BossMovement>();
movements.Add(new BossMovement(new Vector2(300f, 200f), 10f, 4f));
movements.Add(new BossMovement(new Vector2(50f, 200f), 7f, 6f));
movements.Add(new BossMovement(new Vector2(50f, 400f), 5f, 6f));
movements.Add(new BossMovement(new Vector2(300f, 350f), 10f, 6f));
movements.Add(new BossMovement(new Vector2(300f, -32f), 10f, 6f));
movements.Add(new BossMovement(new Vector2(50f, 32f), 8f, 6f));
So, i can manage movements.
public override void Update(GameTime gameTime)
{
if (!IsNasted)
{
//Wait for a while
if (waitTime > 0)
{
waitTime = Math.Max(0.0f, waitTime - Scene.Elapsed);
if (waitTime <= 0.0f)
{
waitTime = 0f;
}
}
else
{
//Is the sprite arrives the target position?
if (Vector2.Distance(ConvertUnits.ToDisplayUnits(movements[minx].pos), ConvertUnits.ToDisplayUnits(body.Position)) < 50)
{
//Change the current position index
waitTime = movements[minx].waitTime;
if (minx == movements.Count - 1)
minx = 0;
else
minx++;
//Stop the movement
body.LinearVelocity = Vector2.Zero;
}
else
{
//Move the boss to the specific position
Vector2 v = (ConvertUnits.ToDisplayUnits(movements[minx].pos) - ConvertUnits.ToDisplayUnits(body.Position));
v.Normalize();
body.LinearVelocity = ConvertUnits.ToSimUnits(v * (movements[minx].speed * (float)gameTime.ElapsedGameTime.TotalMilliseconds) );
}
}
}
base.Update(gameTime);
}
Hiç yorum yok:
Yorum Gönder