/// <summary> /// Initializes the World object and everything in it. /// </summary> public void InitWorld() { //Change to public so it can be called from the gameplay screen. //the world wont exist till it is class from the GameplayScreen // Create the World object. Vector2 gravity = new Vector2(0f, 9.8f); _world = new World(gravity); // Make sure that the level has boundaries. CreateWalls(); // Make sure that bounces against walls are counted. OnWallCollision += IncrementBounces; // Create a launcher with a ball in it. _ball = new Ball(_ballTex); _ball.InitBody(_world); _launcher.LoadBall(_ball); // Add the goal to the world. _goal.InitBody(_world); // When the ball touches the goal, conclude the simulation phase. _goal.OnGoalCollision += Complete; // Add the platforms to the world. foreach (Platform plat in _platforms) { plat.InitBody(_world); plat.OnPlatformCollision += IncrementBounces; } foreach (Platform plat in _moveablePlatforms) { plat.InitBody(_world); plat.OnPlatformCollision += IncrementBounces; } // Add the treasures to the world. foreach (Treasure treasure in _treasures) { treasure.InitBody(_world); treasure.OnTreasureCollect += IncrementCollected; } // Add the death traps to the world. foreach (DeathTrap trap in _deathTraps) { trap.InitBody(_world); trap.OnTrapCollision += EndAttempt; } }
/// <summary> /// Loads a ball into the launcher. /// </summary> /// <param name="ball">The ball that will be eventually launched.</param> public void LoadBall(Ball newBall) { if (_ball != null) { throw new InvalidOperationException("There is already a ball that is ready to be launched."); } // The launcher now contains the ball, and the user can now aim it. _ball = newBall; _movable = true; // Make sure that it actually gets displayed. UpdateBallPos(); }
/// <summary> /// Constructs a Launcher object. /// </summary> /// <param name="textures">An array of textures that will be used to draw the launcher's /// parts. The texture in index 0 is the "stick", the texture in index 1 is the base, and /// the texture in index 2 is the power meter. The "stick" should be of length 80, the /// base should be 120x120, and the power meter should be 80x10.</param> /// <param name="position">The position of the launcher.</param> public Launcher(Texture2D[] textures, Vector2 position) { // Set the textures. if (textures != null) { _rotatorTex = textures[0]; _baseTex = textures[1]; _meterTex = textures[2]; } // TODO: add texture dimensions check if (position.X < 0 || position.Y < 0) { throw new ArgumentOutOfRangeException("The position should not be negative."); } // Set the launcher's position. _position = position; // At first, the launcher will be empty, and the launcher is only movable // when there's a ball in it. _movable = false; _ball = null; // Initialize the angle and magnitude, and calculate the initial position of the tip. _angle = (float)Math.PI / 4; // 45 degrees _magnitude = 10f; _tip = new Vector2(); CalculateNewTip(); // Position the ball. UpdateBallPos(); }
/// <summary> /// Launches the ball in the launcher. /// </summary> public void LaunchBall() { // Don't do anything if no ball is in the launcher. if (!_movable) { return; } // Stop the player from moving the launcher. _movable = false; // Calculate the velocity of the ball based on the launcher's angle and magnitude. float xVelocity = _magnitude * (float)Math.Cos(-1 * _angle); float yVelocity = _magnitude * (float)Math.Sin(-1 * _angle); _ball.Launch(xVelocity, yVelocity); // Call any methods that are waiting for the ball to launch. if (OnBallLaunch != null) { OnBallLaunch(); } // The launcher no longer owns the ball. _ball = null; }
/* Put more tests here */ /// <summary> /// Creates and returns a launcher. /// </summary> /// <param name="createBall">Whether a ball should be loaded in the launcher.</param> /// <returns>A Launcher object.</returns> private Launcher CreateLauncher(bool createBall) { // Create the launcher. // We don't need a texture for unit tests, so pass in null for the texture parameters. Launcher launcher = new Launcher(null, new Vector2(0, 0)); if (createBall) { // Give the launcher a ball; otherwise, it won't move. Ball ball = new Ball(null); launcher.LoadBall(ball); } return launcher; }