/// <summary> /// Verifies if a specified entity can be used in the game. /// </summary> /// <param name="camera">Current game camera.</param> /// <param name="entity">The entity to be checked.</param> /// <returns>True if the entity can be used in the game, otherwise False.</returns> bool IsValidEntity(CameraInfo camera, GamePlayEntity entity) { const int maxDistanceSqr = 1000000; // Check the entity distance from the screen. var distance = entity.Position - camera.Position; if (distance.LengthSquared() > maxDistanceSqr) { RemoveEntity(entity); return(false); } return(true); }
protected GameEntityBehavior(GamePlayEntity entity) : base(entity) { }
public EntityEventArgs(GamePlayEntity entity, GameTime gameTime, GamePlay level) : base(gameTime, level) { Entity = entity; }
/// <summary> /// Removes an entity from the game. /// </summary> /// <param name="entity">Entity to be removed.</param> public void RemoveEntity(GamePlayEntity entity) { _entities.Remove(entity); }
/// <summary> /// Adds an entity to the game. /// </summary> /// <param name="entity">Entity to be added.</param> public void AddEntity(GamePlayEntity entity) { _entities.Add(entity); }
/// <summary> /// Transfers energy from one entity to another. /// </summary> /// <param name="a">Entity A</param> /// <param name="b">Entity B</param> /// <param name="gameTime">Current game time.</param> void ResolveCollision(GamePlayEntity a, GamePlayEntity b, GameTime gameTime) { var aDirection = Vector2.Normalize(a.Momentum); var bDirection = Vector2.Normalize(b.Momentum); var normal = a.Position - b.Position; var dist = normal; normal.Normalize(); // Calculate relative velocity Vector2 rv = b.Momentum - a.Momentum; // Calculate relative velocity in terms of the normal direction float velAlongNormal = Vector2.Dot(rv, normal); // Do not resolve if velocities are separating if (velAlongNormal < 0) { return; } // Calculate restitution (bounciness) float e = MathHelper.Min(a.Restitution, b.Restitution); // Calculate impulse scalar float j = -(1 + e) * velAlongNormal; j /= 1 / a.Mass + 1 / b.Mass; #region Apply impulse Vector2 impulse = j * normal; a.ApplyForce(-impulse, instantaneous: true); b.ApplyForce(impulse, instantaneous: true); #endregion #region Apply Rotation if (velAlongNormal > 2) { var collisionPlane = new Vector2(-normal.Y, normal.X); if (a.Momentum != Vector2.Zero) { var angle = Vector2.Dot(aDirection, collisionPlane); if (!b.RotateToMomentum) { b.ApplyRotation(angle * -j * a.Mass / b.Mass, isAcceleration: false, instantaneous: true); } } if (b.Momentum != Vector2.Zero) { var angle = -Vector2.Dot(bDirection, collisionPlane); if (!a.RotateToMomentum) { a.ApplyRotation(angle * -j * b.Mass / a.Mass, isAcceleration: false, instantaneous: true); } } } #endregion #region Stops overlapping var distLength = dist.Length(); if (distLength < 16) { a.Position += normal * (16 - distLength); } #endregion a.RaiseCollision(b, gameTime, this); b.RaiseCollision(a, gameTime, this); }
public TouchControlBehavior(GamePlayEntity parent) : base(parent) { }
public EntityCollisionEventArgs(GamePlayEntity entity, GamePlayEntity collidedWith, GameTime gameTime, GamePlay level) : base(entity, gameTime, level) { CollidedWith = collidedWith; }