/// <summary> /// Update the particle effect. /// </summary> /// <param name="elapsedTime">The amount of elapsed time, in seconds.</param> public virtual void Update(float elapsedTime) { // update the position based on the follow-object, if any if (followObject != null) { if (followObject.Active) { Position = followObject.Position; } else { followObject = null; Stop(false); } } // update each system active = false; for (int i = 0; i < particleSystems.Count; ++i) { if (particleSystems[i].Active) { particleSystems[i].Update(elapsedTime); active = true; } } }
public void Attach(GameplayObject subject) { this.subject = subject; }
public void Detach() { this.subject = null; }
/// <summary> /// Spawn a new particle effect at a the position of a given gameplay object /// </summary> /// <param name="effectType">The effect in question.</param> /// <param name="actor">The gameplay object.</param> /// <returns>The new particle effect.</returns> public ParticleEffect SpawnEffect(ParticleEffectType effectType, GameplayObject gameplayObject) { // safety-check the parameter if (gameplayObject == null) { throw new ArgumentNullException("gameplayObject"); } return SpawnEffect(effectType, gameplayObject.Position, gameplayObject); }
/// <summary> /// Spawn a new particle effect at a given location and gameplay object /// </summary> /// <param name="effectType">The effect in question.</param> /// <param name="position">The position of the effect.</param> /// <param name="actor">The gameplay object.</param> /// <returns>The new particle effect.</returns> public ParticleEffect SpawnEffect(ParticleEffectType effectType, Vector2 position, GameplayObject gameplayObject) { ParticleEffect particleEffect = null; if (particleEffectCache.ContainsKey(effectType) == true) { List<ParticleEffect> availableSystems = particleEffectCache[effectType]; for (int i = 0; i < availableSystems.Count; ++i) { if (availableSystems[i].Active == false) { particleEffect = availableSystems[i]; break; } } if (particleEffect == null) { particleEffect = availableSystems[0].Clone(); particleEffect.Initialize(contentManager); availableSystems.Add(particleEffect); } } if (particleEffect != null) { particleEffect.Reset(); particleEffect.GameplayObject = gameplayObject; particleEffect.Position = position; activeParticleEffects.Add(particleEffect); } return particleEffect; }
/// <summary> /// A rectangle which is positioned in relation to the characters BoundingBox and velocity.X /// </summary> /// <param name="subject">The character</param> /// <param name="movement">Characters x-scale movement</param> /// <returns></returns> private static Rectangle CollisionAreaRectangleX(GameplayObject subject, float movement) { if (movement < 0) { collisionAreaRecrangleX.X = (int)(subject.BoundingBox.Left / resizeFactor + movement / resizeFactor); collisionAreaRecrangleX.Y = subject.BoundingBox.Y / resizeFactor; collisionAreaRecrangleX.Width = 1; collisionAreaRecrangleX.Height = subject.BoundingBox.Height / resizeFactor; } else { collisionAreaRecrangleX.X = (int)(subject.BoundingBox.Right / resizeFactor + movement / resizeFactor); collisionAreaRecrangleX.Y = subject.BoundingBox.Y / resizeFactor; collisionAreaRecrangleX.Width = 1; collisionAreaRecrangleX.Height = subject.BoundingBox.Height / resizeFactor; } return collisionAreaRecrangleX; }
/// <summary> /// A rectangle which is positioned in relation to the characters BoundingBox and velocity.Y /// </summary> /// <param name="subject">The character</param> /// <param name="movement">Characters y-scale movement</param> /// <returns></returns> private static Rectangle CollisionAreaRectangleY(GameplayObject subject, float movement) { if (movement < 0) { collisionAreaRectangleY.X = subject.BoundingBox.X / resizeFactor; collisionAreaRectangleY.Y = (int)(subject.BoundingBox.Top / resizeFactor + movement / resizeFactor); collisionAreaRectangleY.Width = subject.BoundingBox.Width / resizeFactor; collisionAreaRectangleY.Height = 1; } else { collisionAreaRectangleY.X = subject.BoundingBox.X / resizeFactor; collisionAreaRectangleY.Y = (int)(subject.BoundingBox.Bottom / resizeFactor); collisionAreaRectangleY.Width = subject.BoundingBox.Width / resizeFactor; collisionAreaRectangleY.Height = 1 + (int)movement / resizeFactor; } return collisionAreaRectangleY; }
public static void DrawCollisionRectangles(SpriteBatch spriteBatch, Texture2D texture, GameplayObject subject, Vector2 movement) { Rectangle xRect = CollisionAreaRectangleX(subject, movement.X); Rectangle yRect = CollisionAreaRectangleY(subject, movement.Y); xRect.X *= resizeFactor; xRect.Y *= resizeFactor; yRect.X *= resizeFactor; yRect.Y *= resizeFactor; xRect.Height *= resizeFactor; yRect.Width *= resizeFactor; spriteBatch.Draw(texture, xRect, Color.White); spriteBatch.Draw(texture, yRect, Color.White); }
/// <summary> /// Returns a point which indicates the amount the subject overlaps another character. /// </summary> /// <param name="subject"></param> /// <returns></returns> public static int OverlapsCharacter(GameplayObject subject) { int overlap = 0; for (int i = 0; i < gameObjects.Count; i++) { if (gameObjects[i] != subject) { if (gameObjects[i].BoundingBox.Intersects(subject.BoundingBox) && gameObjects[i].IsActive && gameObjects[i].TAG != "Ladder") { Rectangle a = subject.BoundingBox; Rectangle b = gameObjects[i].BoundingBox; if (a.Right > b.Left) { if (a.Right < b.Right) { return -a.Right + b.Left; } else { return b.Right - a.Left; } } } } } return overlap; }
/// <summary> /// Returns Left, Right, Top, Bottom of the subjects BoundingBox if a collision happens. The method creates 2 rectangles /// (CollisionRectangleY, CollisionRectangleX) which are positioned in relation to the characters BoundingBox and velocity. /// The method then chekcs if the rectangles intercect any existing characters BoundingBox. /// </summary> /// <param name="subject"></param> /// <param name="movement"></param> /// <returns></returns> public static ObjectCollisionEvent CollisionOccursWithObject(GameplayObject subject, Vector2 movement) { CleanInactives(); ObjectCollisionEvent collisionEvent = null; int collision = 0; Rectangle Y = CollisionAreaRectangleY(subject, movement.Y); Rectangle X = CollisionAreaRectangleX(subject, movement.X); for (int i = 0; i < gameObjects.Count; i++) { if (subject == gameObjects[i] || !gameObjects[i].IsActive) continue; Rectangle targetBox = new Rectangle(gameObjects[i].BoundingBox.X / resizeFactor, gameObjects[i].BoundingBox.Y / resizeFactor, gameObjects[i].BoundingBox.Width / resizeFactor, gameObjects[i].BoundingBox.Height / resizeFactor); Rectangle targetBoxScaled = gameObjects[i].BoundingBox; if (Y.Intersects(targetBox)) { if (gameObjects[i].CollisionMap == null) { if (Y.Y < targetBox.Y + targetBox.Height / 2) { collision = subject.BoundingBox.Bottom; } else { collision = subject.BoundingBox.Top; } } else { int x = X.X * resizeFactor - targetBoxScaled.X; int y = Y.Y * resizeFactor - targetBoxScaled.Y; if (gameObjects[i].CollisionMapData[x, y] != Color.Transparent && gameObjects[i].CollisionMapData[x, y].A == 255) { if (Y.Y * resizeFactor < subject.BoundingBox.Y) { collision = subject.BoundingBox.Top; } else { collision = subject.BoundingBox.Bottom; } } } } if (X.Intersects(targetBox)) { if (gameObjects[i].CollisionMap == null) { if (X.X < targetBox.X + targetBox.Width / 2) { collision += subject.BoundingBox.Right; } else { collision += subject.BoundingBox.Left; } } else { } } if (gameObjects[i].CollisionMap == null && subject.BoundingBox.Intersects(targetBoxScaled) && collision == 0) { if (subject.BoundingBox.X < targetBox.X + targetBox.Width / 2) collision = subject.BoundingBox.Right; else collision = subject.BoundingBox.Left; if (subject.BoundingBox.Y < targetBox.Y + targetBox.Height / 2) collision = subject.BoundingBox.Bottom; else collision = subject.BoundingBox.Top; } if (collision != 0) { collisionEvent = new ObjectCollisionEvent(subject, gameObjects[i], collision); return collisionEvent; } } return null; }
public static void AddObject(GameplayObject obj) { gameObjects.Add(obj); }
void Start() { obj = GetComponent <GameplayObject>(); level = LevelState.instance; }
public ObjectCollisionEvent(GameplayObject a, GameplayObject b, int collision_location) { this.a = a; this.b = b; this.collision_location = collision_location; }
/// <summary> /// Kills this object, in response to the given GameplayObject. /// </summary> /// <param name="source">The GameplayObject responsible for the kill.</param> /// <param name="cleanupOnly"> /// If true, the object dies without any further effects. /// </param> public override void Die(GameplayObject source, bool cleanupOnly) { if (active) { // display the laser explosion if (!cleanupOnly && (ParticleEffectManager != null)) { ParticleEffectManager.SpawnEffect(ParticleEffectType.LaserExplosion, Position); } } base.Die(source, cleanupOnly); }