/// <summary> /// Removes the given sprite as a child from this sprite. The given sprite's Parent property must be set to the sprite this method is called on. /// </summary> /// <param name="sprite">The sprite to remove as a child of this sprite. The sprite's Parent property must be set to the sprite this method is called on.</param> protected void removeChild(Sprite sprite) { if (sprite.Parent != this) throw new InvalidOperationException("The given sprite is not a child of this sprite."); children.Remove(sprite); sprite.Parent = null; }
/// <summary> /// Adds the given sprite as a child to this sprite. The given sprite's Parent property must be set to null. /// </summary> /// <param name="sprite">The sprite to make a child of this sprite. The sprite's Parent property must be set to null.</param> protected void addChild(Sprite sprite) { if (sprite == null) throw new ArgumentNullException(nameof(sprite)); if (sprite.Parent != null) throw new InvalidOperationException("The given sprite already has a parent."); sprite.Parent = this; children.Add(sprite); }
public void AddChild(Sprite child) { addChild(child); }
public void RemoveChild(Sprite child) { removeChild(child); }
#pragma warning restore CS1591 private void recursiveCollisionCheck(Sprite sprite, double x, double y, Func<Sprite, bool> predicate, Action<Sprite> whenCollides, Action<Sprite> whenNotCollides = null) { if (!predicate(sprite)) return; if (sprite.CheckCollision(x, y)) { whenCollides(sprite); foreach (Sprite child in sprite.Children) { // translate coordinates to childs coordinate system for next check. double newX = x, newY = y; newX -= child.X; newY -= child.Y; newX /= child.SizeX; newY /= child.SizeY; double cosVal = Math.Cos(-(child.Rotation * (Math.PI / 180))); double sinVal = Math.Sin(-(child.Rotation * (Math.PI / 180))); double origX = newX; newX = newX * cosVal - newY * sinVal; newY = origX * sinVal + newY * cosVal; newX -= child.OriginX; newY -= child.OriginY; recursiveCollisionCheck(child, newX, newY, predicate, whenCollides, whenNotCollides); } } else if (whenNotCollides != null) { whenNotCollides(sprite); foreach (Sprite child in sprite.AllChildren) whenNotCollides(child); } }