/// <summary> /// Asetetaan uusi suunta. Hakee olioita edestä ja sivuilta ja arpoo tyhjistä kohdista uuden suunnan. /// Jos eteen tai sivuille ei pääse, uusi suunta on taaksepäin. /// </summary> private void SetNextDirectionAndDestination() { PhysicsObject owner = this.Owner as PhysicsObject; if (owner == null || owner.Game == null) { return; } Game game = owner.Game; List <Vector> directions = new List <Vector> { direction, Vector.FromLengthAndAngle(tileSize, direction.Angle - Angle.RightAngle), Vector.FromLengthAndAngle(tileSize, direction.Angle + Angle.RightAngle) }; GameObject tile; double radius = tileSize / 5; while (directions.Count > 0) { Vector directionCandidate = RandomGen.SelectOne <Vector>(directions); directions.Remove(directionCandidate); if (labyrinthWallTag != null) { tile = game.GetObjectAt(owner.Position + directionCandidate, labyrinthWallTag, radius); } else { tile = game.GetObjectAt(owner.Position + directionCandidate, radius); } //PhysicsObjects only! if (tile == null || tile as PhysicsObject == null || (owner.CollisionIgnoreGroup != 0 && (tile as PhysicsObject).CollisionIgnoreGroup == owner.CollisionIgnoreGroup)) { direction = directionCandidate.Normalize() * tileSize; //direction.X = Math.Round(direction.X); //direction.Y = Math.Round(direction.Y); destination = owner.Position + direction; return; } } direction = -direction.Normalize() * tileSize; //direction.X = Math.Round(direction.X); //direction.Y = Math.Round(direction.Y); destination = owner.Position + direction; }