public bool OnScreen(Location loc){ return loc.X > 100 && loc.Y > 100 && loc.X < MAX_WIDTH-100 && loc.Y < MAX_HEIGHT-100; }
public Location GetNewCharacterLocation(GameState currGameState){ Location loc = new Location(); Random rand = new Random (); bool foundGoodLoc = false; int tries = 0; while(!foundGoodLoc && tries<1000){ loc.X = (float)rand.Next(0,(int)MAX_WIDTH); loc.Y = (float)rand.Next (0, (int)MAX_HEIGHT); foreach(Character c in currGameState.Characters){ if(GetDist(c,loc)<MIN_DIST){ float diffX = loc.X-c.X; float diffY = loc.Y-c.Y; float sqrmag = (diffX*diffX + diffY*diffY); diffX/=sqrmag; diffX*=MIN_DIST; diffY/=sqrmag; diffY*=MIN_DIST; loc.X=c.X+diffX; loc.Y=c.Y+diffY; } } if(OnScreen(loc)){ foundGoodLoc = true; } tries++; } return loc; }
//Placer is the character that needs the item public Location GetNewItemLocation(GameState currGameState, Character placer){ Location loc = new Location(); loc.X = 0; loc.Y = 0; Random rand = new Random (); bool placed=false; List<int> characterIndexes = new List<int> (); for (int i = 0; i<currGameState.Characters.Count; i++) { characterIndexes.Add(i); } while(characterIndexes.Count>0 && !placed ){ int charIndex = characterIndexes[rand.Next(0,characterIndexes.Count)]; if(currGameState.Characters[charIndex].X!=placer.X && currGameState.Characters[charIndex].Y!=placer.Y){ loc.X=currGameState.Characters[charIndex].X; loc.Y=currGameState.Characters[charIndex].Y; loc.X+=rand.Next((int)(-1*100),(int)(100)); loc.Y+=rand.Next((int)(-1*100),(int)(100)); if(OnScreen(loc)){ placed = true; } } characterIndexes.Remove(charIndex); } if (!placed) { loc =GetNewCharacterLocation(currGameState); } return loc; }
public float GetDist(Character character, Location loc){ return Abs(SqrDist (character.X, character.Y, loc.X, loc.Y)); }