/// <summary> /// This constructor is only for serialisation. /// </summary> public CollisionArea() { _type = CollisionAreaType.Common; _name = "dummyarea"; _collisionMaterial = CollisionMaterialType.Regular; _area = new Circle(Vector2.Zero, 10); }
public CollisionArea(string name, IGeomPrimitive area, Gob owner, CollisionAreaType type, CollisionMaterialType collisionMaterial) { _type = type; _collisionMaterial = collisionMaterial; _name = name; _area = area; _owner = owner; if (owner != null && owner.IsRegistered) Initialize(); }
/// <summary> /// This constructor is only for serialisation. /// </summary> public SpawnPlayer() { _spawnArea = new AW2.Helpers.Geometric.Rectangle(Vector2.Zero, new Vector2(1000, 1000)); }
/// <summary> /// This constructor is only for serialisation. /// </summary> public SpawnGob() { _spawnArea = new Circle(new Vector2(100, 100), 100); _spawnInterval = 20; _spawnTypes = new[] { new SpawnType() }; }
/// <summary> /// Returns true iff the point is contained in the geometric primitive. /// </summary> public static bool Intersect(Vector2 point, IGeomPrimitive prim) { if (prim is Circle) return Intersect(point, (Circle)prim); if (prim is Rectangle) return Intersect(point, (Rectangle)prim); if (prim is Triangle) return Intersect(point, (Triangle)prim); if (prim is Polygon) return Intersect(point, (Polygon)prim); throw new NotImplementedException("Geometry.Intersect not implemented for Vector2 and " + prim.GetType().Name); }
/// <summary> /// Returns a random location inside an area. /// </summary> /// <param name="prim">The area.</param> /// <returns>A random location inside the area.</returns> public static Vector2 GetRandomLocation(IGeomPrimitive prim) { var type = prim.GetType(); if (type == typeof(Circle)) return GetRandomLocation((Circle)prim); if (type == typeof(Rectangle)) { var rectangle = (Rectangle)prim; return RandomHelper.GetRandomVector2(rectangle.Min, rectangle.Max); } // For any other geometric primitive, randomise points in its bounding box // until we hit inside the primitive. This is generic but can be increasingly // inefficient with certain primitives. var boundingBox = prim.BoundingBox; for (int i = 0; i < 1000000; ++i) { var pos = RandomHelper.GetRandomVector2(boundingBox.Min, boundingBox.Max); if (Intersect(pos, prim)) return pos; } throw new NotImplementedException("GetRandomLocation not properly implemented for " + type.Name); }
public void TestGeneralDistance() { var prims = new IGeomPrimitive[] { new Circle(new Vector2(30, 40), 50), new Rectangle(60, 70, 80, 90), new Triangle(new Vector2(10, 60), new Vector2(-90, -10), new Vector2(-30, 20)), new Polygon(new[] { new Vector2(15, 25), new Vector2(35, 45), new Vector2(55, 65) }), }; foreach (var prim2 in prims) Assert.LessOrEqual(0, Geometry.Distance(new Vector2(10, 20), prim2)); }
/// <summary> /// Is an area free of physical gobs. If <paramref name="filter"/> returns false for /// a CollisionArea, it is not considered as an obstacle. /// </summary> public bool IsFreePosition(IGeomPrimitive area, Func<CollisionArea, bool> filter = null) { if (!BoundedAreaNormal.Contains(area.BoundingBox)) return false; return PhysicsHelper.IsFreePosition(_world, area, filter); }
/// <summary> /// Tries to return a position in the arena where there is no physical obstacles in a radius. /// </summary> /// <param name="radius">The radius of requested free space.</param> /// <param name="area">The area where to look for a position.</param> /// <param name="result">Best try for a position in the area where the gob is overlap consistent.</param> /// <returns>true if <paramref name="result"/> is legal and overlap consistent, /// false if the search failed.</returns> public bool GetFreePosition(float radius, IGeomPrimitive area, out Vector2 result) { // Iterate in the area for a while, looking for a free position. // Ultimately give up and return something that may be bad. for (int attempt = 1; attempt < FREE_POS_MAX_ATTEMPTS; ++attempt) { var tryPos = Geometry.GetRandomLocation(area); if (IsFreePosition(new Circle(tryPos, radius))) { result = tryPos; return true; } } result = Geometry.GetRandomLocation(area); return false; }
/// <summary> /// Tries to return a position in the arena where there is no physical obstacles in a radius. /// </summary> /// <param name="area">The area where to look for a position.</param> /// <returns>A position in the area where a gob might be overlap consistent.</returns> public Vector2 GetFreePosition(float radius, IGeomPrimitive area) { Vector2 result; GetFreePosition(radius, area, out result); return result; }