public Body ClosestTarget(Entity e) { AI a = e.GetComponent<AI>(); PhysicsBody pb = world.GetClosestBody(e.GetComponent<ITransform>().Position, a.HostileGroup); Body b = new Body(world, pb.UserData as Entity); return b; }
public AI(Body target, Func<Body, bool> behavior, string targetGroup, float searchRadius) { this.Target = target; this.Targeting = Targeting.Closest; this.TargetGroup = targetGroup; this.SearchRadius = searchRadius; this.Behavior = behavior; Notify = (x, y) => { return; }; }
public void CallChildren(Body b) { foreach (Entity e in children) { if (e != null && e.HasComponent<Function>()) { Function f = e.GetComponent<Function>(); f.Behavior.Invoke(b); } } }
/// <summary> /// Creates a sprite with a spritesheet and abody. /// </summary> /// <param name="spriteSheet"></param> /// <param name="spriteKey"></param> /// <param name="body"></param> /// <param name="scale"></param> /// <param name="color"></param> /// <param name="layer"></param> public Sprite(SpriteSheet spriteSheet, string spriteKey, Body body, float scale, Color color, float layer) : this(spriteSheet, spriteKey, AssetCreator.CalculateOrigin(body) / scale, scale, color, layer) { }
/// <summary> /// Creates a new sprite with a source and a texture using a body. /// </summary> /// <param name="spriteSheet"></param> /// <param name="source"></param> /// <param name="body"></param> /// <param name="scale"></param> /// <param name="color"></param> /// <param name="layer"></param> public Sprite(Texture2D spriteSheet, Rectangle source, Body body, float scale, Color color, float layer) : this(spriteSheet, source, AssetCreator.CalculateOrigin(body) / scale, scale, color, layer) { }
public AI(Body target, Func<Body, bool> behavior, string targetGroup, bool recalculate) : this(target, behavior, targetGroup, 200f) { Recalculate = recalculate; }
public AI(Body target, Func<Body, bool> behavior) : this(target, behavior, "", 200f) { }
public static Func<Body, bool> CreateWarMachine(Entity ent, float speed, Body bitch, float sideTime, float shootTime, Sprite s, EntityWorld _World) { float shotTime = 0f; float time = 0f; return (target) => { if (time > sideTime) time = 0f; Body b = ent.GetComponent<Body>(); float x = (float)(ConvertUnits.ToSimUnits(ScreenHelper.Viewport.Width / 2) * (Math.Cos(MathHelper.ToRadians(360 / sideTime) * time))); if (shotTime > shootTime) { shotTime = 0f; Vector2 velocity1 = new Vector2(0, 1); velocity1 *= 8f; SoundManager.Play("Shot2"); _World.CreateEntity("ExplosiveBullet", bitch.Position, velocity1, 1, "reddownmissile").Refresh(); } if (!ent.HasComponent<Slow>()) { b.Position = new Vector2(x, b.Position.Y + speed * _World.Delta / 1000); shotTime += (float)_World.Delta / 1000; time += (float)_World.Delta / 1000; } else { handleSlow(ent, _World); } return false; }; }
public AI(Body target, Func<Body, bool> behavior, string targetGroup) : this(target, behavior, targetGroup, 200f) { }
public AI(Body target, Func<Body, bool> behavior, float searchRadius) : this(target, behavior, "", searchRadius) { }
public static Func<Body, bool> CreateFlamer(Entity ent, float speed, Body bitch, Sprite s, EntityWorld _World) { int times = 0; return (target) => { ++times; Body b = ent.GetComponent<Body>(); Vector2 distance = target.Position - b.Position; if (distance != Vector2.Zero) distance.Normalize(); distance *= speed; if (target != null && target.LinearVelocity != distance && !ent.HasComponent<Slow>()) { b.LinearVelocity = distance; } if (times % 10 == 0) { int range = s.CurrentRectangle.Width / 2; float posx = -range; Vector2 pos1 = bitch.Position + ConvertUnits.ToSimUnits(new Vector2(posx, 0)); Vector2 pos2 = bitch.Position - ConvertUnits.ToSimUnits(new Vector2(posx, 0)); float x = posx / range; float y = 1; Vector2 velocity1 = new Vector2(x, y); velocity1.Normalize(); velocity1 *= 7; Vector2 velocity2 = new Vector2(-velocity1.X, velocity1.Y); _World.CreateEntity("Fire", pos1, velocity1).Refresh(); _World.CreateEntity("Fire", pos2, velocity2).Refresh(); } return false; }; }
/// <summary> /// Finds a new target for AI component /// </summary> /// <param name="ai"></param> /// <param name="location"></param> /// <returns></returns> private Body _FindNewTarget(AI ai, Body location) { //find all fixtures in world around the location. AABB aabb = new AABB(location.Position, ai.SearchRadius, ai.SearchRadius); #if WINDOWS HashSet<PhysicsBody> bodies = new HashSet<PhysicsBody>(); #else List<PhysicsBody> bodies = new List<PhysicsBody>(); #endif world.QueryAABB(x => { if (x.Body.BodyId != location.BodyId) { if ((string.IsNullOrEmpty(ai.TargetGroup) || ai.TargetGroup.Equals((x.Body.UserData as Entity).Group)) && !(world.GetEntity((x.Body.UserData as Entity).Id) == null || ( world.GetEntity((x.Body.UserData as Entity).Id) != null && world.GetEntity((x.Body.UserData as Entity).Id).GetComponent<Body>() != null && !world.GetEntity((x.Body.UserData as Entity).Id).GetComponent<Body>().Equals(x.Body)))) { bodies.Add(x.Body); } } return true; }, ref aabb); if (bodies.Count > 0) //IF BODIES IN SEARCH RADIUS. { PhysicsBody[] list = new PhysicsBody[bodies.Count]; bodies.CopyTo(list); Entity ent = (location.UserData as Entity); //SORT BY TARGETING TYPE. switch (ai.Targeting) { case Targeting.Closest: return ClosestEntity(location.Position, list).GetComponent<Body>(); case Targeting.Strongest: return StrongestEntity(list).GetComponent<Body>(); case Targeting.Weakest: return WeakestEntity(list).GetComponent<Body>(); case Targeting.None: return null; default: return null; } } else return null; }