/// <summary> /// Checks if the bounding box intersects with another bounding box. /// </summary> /// <param name="box">The bounding box to check for an intersection.</param> /// <returns>True if an intersection occurs, false if not.</returns> public bool Intersects(GenAABB box) { if ((box._maxX < _minX) || (box._minX > _maxX)) return false; if ((box._maxY < _minY) || (box._minY > _maxY)) return false; return true; }
/// <summary> /// Gets the positive or negative distances between the edges of two bounding boxes along the x-axis and y-axis. /// </summary> /// <param name="box">The bounding box to get the distance from.</param> /// <returns>A <c>Vector2</c> containing the positive or negative distances between the edges of two bounding boxes along the x-axis and y-axis.</returns> public Vector2 GetDistanceAABB(GenAABB box) { Vector2 distance = Vector2.Zero; distance.X = Math.Abs(_midpointX - box.MidpointX) - (_halfWidth + box.HalfWidth); distance.Y = Math.Abs(_midpointY - box.MidpointY) - (_halfHeight + box.HalfHeight); return distance; }
/// <summary> /// Gets the x and y intersection depths of two bounding boxes. /// </summary> /// <param name="box">The bounding box to check against for an intersection.</param> /// <returns>A <c>Vector2</c> containing the x and y intersection depths, or a zero vector if no intersection occurs.</returns> public Vector2 GetIntersectDepthAABB(GenAABB box) { // Check for intersection along the x-axis first. float distanceX = _midpointX - box.MidpointX; float minDistanceX = _halfWidth + box.HalfWidth; // If the horizontal distance between the midpoints is less than the sum of the widths, continue on the y-axis. if (Math.Abs(distanceX) < minDistanceX) { float distanceY = _midpointY - box.MidpointY; float minDistanceY = _halfHeight + box.HalfHeight; // If the vertical distance between the midpoints is less than the sum of the heights, there is an intersection. if (Math.Abs(distanceY) < minDistanceY) { Vector2 intersection = Vector2.Zero; // Get the intersection depths. intersection.X = (distanceX > 0) ? minDistanceX - distanceX : -minDistanceX - distanceX; intersection.Y = (distanceY > 0) ? minDistanceY - distanceY : -minDistanceY - distanceY; return intersection; } } // Return a zero vector if there is no intersection. return Vector2.Zero; }
/// <summary> /// Checks if the bounding box entirely contains another bounding box. /// </summary> /// <param name="box">The bounding box to check against.</param> /// <returns>True if the bounding box is entirely contained, false if not.</returns> public bool Contains(GenAABB box) { if ((box._minX <= _minX) || (box._maxX >= _maxX)) return false; if ((box._minY <= _minY) || (box._maxY >= _maxY)) return false; return true; }
/// <summary> /// A physical object that can be moved around in screen space. /// </summary> /// <param name="x">The x position of the top-left corner of the object.</param> /// <param name="y">The y position of the top-left corner of the object.</param> /// <param name="width">The width of the object.</param> /// <param name="height">The height of the object.</param> public GenObject(float x, float y, float width, float height) { _position = new Vector2(x, y); _oldPosition = _position; _hasMoved = false; _bounds = new GenAABB(x, y, width, height); _centerPosition = new Vector2(x + _bounds.HalfWidth, y + _bounds.HalfHeight); _origin = new Vector2(_bounds.HalfWidth, _bounds.HalfHeight); _originPosition = new Vector2(x + _origin.X, y + _origin.Y); _boundingRect = new Rectangle(0, 0, (int)width, (int)height); _rotation = 0f; RotationSpeed = 0f; _moveDistance = Vector2.Zero; _moveBounds = new GenAABB(x, y, width, height); Immovable = false; Solid = true; Mass = 1f; Velocity = Vector2.Zero; OldVelocity = Velocity; Acceleration = Vector2.Zero; Deceleration = Vector2.Zero; MaxVelocity = Vector2.Zero; _facing = Direction.None; OldTouching = Direction.None; Touching = Direction.None; Parent = null; ParentMode = ParentType.None; ParentOffset = Vector2.Zero; IsPlatform = false; Platform = null; OldPlatform = null; Path = null; PathNodeIndex = 0; PathDirection = 1; PathSpeed = 0f; PathType = GenPath.Type.Clockwise; PathAxis = GenMove.Axis.Both; PathMovement = GenPath.Movement.Instant; }
/// <summary> /// A camera occupies a space on the game screen to draw game objects. /// An error will occur if the camera is positioned outside of the game window. /// </summary> /// <param name="x">The x position of the camera.</param> /// <param name="y">The y position of the camera.</param> /// <param name="width">The width of the camera.</param> /// <param name="height">The height of the camera.</param> /// <param name="zoom">The scale at which to draw objects in the camera.</param> public GenCamera(float x, float y, int width, int height, float zoom) { _cameraRect = new Rectangle(0, 0, width, height); _cameraView = new GenAABB(0f, 0f, width, height); _centerPosition = new Vector2(_cameraView.MidpointX, _cameraView.MidpointY); _up = new Vector2(0f, -1f); RenderTarget = new RenderTarget2D(GenG.GraphicsDevice, width, height); PixelRenderTarget = new RenderTarget2D(GenG.GraphicsDevice, width, height); _backgroundTexture = GenU.MakeTexture(Color.White, 1, 1); Color = Color.White; BgColor = Color.Transparent; _scroll = Vector2.Zero; _oldScroll = _scroll; _velocity = Vector2.Zero; _rotation = 0f; Origin = new Vector2(width * 0.5f, height * 0.5f); Scale = Vector2.One; _drawPosition = new Vector2(x + Origin.X, y + Origin.Y); _initialZoom = zoom; Zoom = _initialZoom; CameraFollowType = FollowType.LockOn; _followTargets = new List<GenObject>(); FollowPosition = Vector2.Zero; FollowLeading = new Vector2(50, 30); _followStrength = 1f; MinZoom = _initialZoom; MaxZoom = 4f; _shakeOffset = Vector2.Zero; _shakeIntensity = 0f; _shakeDecreasing = false; _shakeTimer = new GenTimer(0f, null); SpriteEffect = SpriteEffects.None; _cameraEffect = new GenScreenEffect(_cameraRect); Transform = Matrix.Identity; // Set up the default camera blend state as alpha blend. BlendState = new BlendState(); BlendState.ColorSourceBlend = Blend.One; BlendState.ColorDestinationBlend = Blend.InverseSourceAlpha; BlendState.ColorBlendFunction = BlendFunction.Add; BlendState.AlphaSourceBlend = Blend.One; BlendState.AlphaDestinationBlend = Blend.InverseSourceAlpha; BlendState.AlphaBlendFunction = BlendFunction.Add; }
/// <summary> /// Gets the index of the leaf node that entirely contains the given bounding box. /// </summary> /// <param name="box">The bounding box used to search the leaf nodes.</param> /// <returns>The index of the leaf node that entirely contains the given bounding box. Otherwise, -1 is returned.</returns> internal int GetIndex(GenAABB box) { if (Contains(box)) { if (box.Bottom < _midpointY) { if (box.Right < _midpointX) // Top-left leaf node. return 0; else if (box.Left > _midpointX) // Top-right leaf node. return 1; } else if (box.Top > _midpointY) { if (box.Right < _midpointX) // Bottom-left leaf node. return 2; else if (box.Left > _midpointX) // Bottom-right leaf node. return 3; } } // Return -1 if the given bounding box does not fit within any leaf node. return -1; }
/// <summary> /// A particle emitter that emits particles from a point, /// </summary> /// <param name="x">The x position of the top-left corner of the emitter.</param> /// <param name="y">The y position of the top-left corner of the emitter.</param> public GenEmitter(float x, float y) { _position = new Vector2(x, y); _bounds = new GenAABB(x, y, 0f, 0f); _boundingRect = Rectangle.Empty; MinParticleSpeedX = -100; MaxParticleSpeedX = 100; MinParticleSpeedY = -100; MaxParticleSpeedY = 100; MinRotation = 0; MaxRotation = 0; MinRotationSpeed = 0; MaxRotationSpeed = 0; Colors = new List<Color>(); StartAlpha = 1f; EndAlpha = 1f; StartScale = 1f; EndScale = 1f; Explode = true; EmitQuantity = 10; _emitTimer = new GenTimer(0.1f, EmitParticles); _emitTimer.IsLooping = true; InheritVelocity = false; Parent = null; ParentOffset = Vector2.Zero; }