I am making a game in which monsters spawn randomly and begin to move around randomly. I am fairly new at programming but not at game development. Through using a few tutorials I was able to make 1 creature spawn randomly and move randomly (with animations!) in a way that seemed organic instead of robotic.
So, I moved on to the next step of doing this to several creatures. However, when I executed this, I found that instead of executing their own instructions, the rest of the monsters simply mimic the relative movements of 1 central guy. Everyone moves in sync like some North Korean military parade!
I am using foreach loops and random numbers to choose their directions. Since my game is a top down 2d isometric, I am using a dictionary to store 4 directions. I have made some changes based on the suggestions here, and now all the creatures animate! But they still don't change directions or do things on their own.
How do I make them each have their own unique set of instructions instead of everyone just following the dear leader?
Below is a walkthrough of my code:
Monster Initialization:
public void AddCreatures()
{
NewMonster = new Monster(Texture);
NewMonster.AddAnimation("northeast", 0, 96, 48, 48, 8, 0.2f);
NewMonster.AddAnimation("northwest", 0, 144, 48, 48, 8, 0.2f);
NewMonster.AddAnimation("southeast", 0, 240, 48, 48, 8, 0.2f);
NewMonster.AddAnimation("southwest", 0, 288, 48, 48, 8, 0.2f);
NewMonster.AddAnimation("hoon", 144, 336, 48, 48, 1, 1.0f);
NewMonster.Position = RandomPos();
NewMonster.DrawOffset = new Vector2(49, 80);
NewMonster.CurrentAnimation = "southwest";
NewMonster.IsAnimating = false;
creatures.Add(NewMonster);
}
Spawn:
for (int v = 0; v < 12; v++)
{
AddCreatures();
}
Randomize spawn Position:
private Vector2 RandomPos()
{
Vector2 location = Vector2.Zero;
location.X = rng.Next(300,900);
location.Y = rng.Next(200, 700);
return location;
}
Movements:
public void Update(GameTime gameTime)
{
timenow += (float)gameTime.ElapsedGameTime.TotalSeconds;
foreach (Monster mon in creatures)
{
if (mon.IsAnimating)
{
if (timenow - (float)gameTime.ElapsedGameTime.TotalSeconds > 0.5f)
{
mon.Velocity = directions[rng.Next(1, 5)];
ChooseAnim(mon);
mon.Velocity.Normalize();
speed = (float)(rng.Next(0, 3));
diff = new Vector2(mon.Position.X - mon.GetLastPosition.X, mon.Position.Y - mon.GetLastPosition.Y);
if (Math.Abs(diff.X) >= 4 && Math.Abs(diff.Y) >= 4)
{
mon.Velocity = directions[rng.Next(1, 5)];
ChooseAnim(mon);
mon.Velocity.Normalize();
}
mon.Velocity.X *= speed;
mon.Velocity.Y *= speed;
timenow = 0f;
}
mon.MoveBy(mon.Velocity);
}
DoNotExit();
mon.Update(gameTime);
}
}
Animation choosing method:
private void ChooseAnim(Monster mon)
{
if (mon.Velocity == directions[1])
mon.CurrentAnimation = "northeast";
if (mon.Velocity == directions[2])
mon.CurrentAnimation = "northwest";
if (mon.Velocity == directions[3])
mon.CurrentAnimation = "southeast";
if (mon.Velocity == directions[4])
mon.CurrentAnimation = "southwest";
}
foreach (Monster NewMonster in creatures)toforeach (Monster monster in creatures)Then rename all the instances ofNewMonsterinside that loop tomonster– Byte56 Apr 18 '12 at 20:39