/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // Load the font _renderer.Font = Content.Load<SpriteFont>("Courier New"); // Load the background _background = new Background(new Sprite(Content.Load<Texture2D>("background-1"))); // Create the scenegraph _scene = new SceneNode(null, "_root_"); // Load the player // Load the player animations int fps = 6; Dictionary<String, FrameSet> playerAnimations = new Dictionary<string, FrameSet>(); List<Tuple<int, int>> idleFrames = new List<Tuple<int, int>>(); idleFrames.Add(new Tuple<int, int>(0, 0)); idleFrames.Add(new Tuple<int, int>(0, 1)); playerAnimations.Add("idle", new FrameSet(idleFrames, true, fps)); List<Tuple<int, int>> walkFrames = new List<Tuple<int, int>>(); walkFrames.Add(new Tuple<int, int>(0, 0)); playerAnimations.Add("walk", new FrameSet(walkFrames, true, fps)); List<Tuple<int, int>> crouchFrames = new List<Tuple<int, int>>(); crouchFrames.Add(new Tuple<int, int>(0, 1)); playerAnimations.Add("crouch", new FrameSet(crouchFrames, true, fps)); List<Tuple<int, int>> jumpFrames = new List<Tuple<int, int>>(); jumpFrames.Add(new Tuple<int, int>(0, 2)); playerAnimations.Add("jump", new FrameSet(jumpFrames, true, fps)); List<Tuple<int, int>> interactionFrames = new List<Tuple<int, int>>(); interactionFrames.Add(new Tuple<int, int>(0, 2)); playerAnimations.Add("interact", new FrameSet(interactionFrames, true, fps)); AnimatedSprite playerSprite = new AnimatedSprite(Content.Load<Texture2D>("player-ss"), 32, 64, playerAnimations); // Load the player sounds Dictionary<String, SoundEffect> playerSoundFX = new Dictionary<string, SoundEffect>(); playerSoundFX.Add("jump", Content.Load<SoundEffect>("jump")); playerSoundFX.Add("run", Content.Load<SoundEffect>("run")); playerSoundFX.Add("walk", Content.Load<SoundEffect>("walk")); _player = new Player(_scene, "Player", playerSprite, 100.0f, playerSoundFX); // Load the enemy Sprite enemySprite = new Sprite(Content.Load<Texture2D>("enemy")); _enemy = new Enemy(_scene, "Enemy-1", enemySprite, 20.0f); _enemy.Position = new Vector2(300.0f, 20.0f); // Load the crate WorldObject crate = new WorldObject(_scene, "Crate-1", new Sprite(Content.Load<Texture2D>("crate"))); crate.Position = new Vector2(200.0f, 200.0f); crate.IsElevated = true; WorldObject crate2 = new WorldObject(_scene, "Crate-2", new Sprite(Content.Load<Texture2D>("crate"))); crate2.Position = new Vector2(400.0f, 400.0f); }
/// <summary> /// Updates the player when the player is running /// </summary> /// <param name="gameTime">Game time</param> /// <param name="keys">The state of the keyboard</param> /// <param name="elapsedSeconds">Number of elapsed seconds</param> protected void UpdateCrouching(GameTime gameTime, KeyboardState keys, float elapsedSeconds) { float crouchSlowdown = 0.5f; // Slowdow factor to use when the player is crouching // Move the player Vector2 distance = Vector2.Zero; if (keys.IsKeyDown(Keys.Up)) { distance.Y += 1.0f; } if (keys.IsKeyDown(Keys.Down)) { distance.Y -= 1.0f; } if (keys.IsKeyDown(Keys.Left)) { distance.X -= 1.0f; } if (keys.IsKeyDown(Keys.Right)) { distance.X += 1.0f; } // If the player moved, adjust the player's position if (distance != Vector2.Zero) { distance.Normalize(); // Normalize the distance to the player into just a direction distance *= crouchSlowdown * WalkSpeed * elapsedSeconds; this.Adjust(distance); _noiseLevel = 0.3f; // If we're not playing the looping crouch noise, play it now if (null == _crouchSound) { SoundEffect crouchSound; _soundFX.TryGetValue("walk", out crouchSound); _crouchSound = crouchSound.CreateInstance(); _crouchSound.IsLooped = true; _crouchSound.Play(); _crouchSound.Volume /= 4.0f; } } else { // If we're not playing the looping crouch noise, play it now if (null != _crouchSound) { _crouchSound.Stop(); _crouchSound = null; } _noiseLevel = 0.0f; } // Check to see if we're crouching under anything if (_objectUnder != null && !this.CollisionAABB.DoesCollide(_objectUnder.CollisionAABB)) { _objectUnder = null; } }
/// <summary> /// Processes a collision with the given collider /// </summary> /// <param name="collider"></param> public virtual void ProcessCollision(WorldObject collider) { this.Position -= Displacement; }
/// <summary> /// Processes a collision with the given collider /// </summary> /// <param name="collider"></param> public override void ProcessCollision(WorldObject collider) { // Ignore collisions in certain cases if (collider.IsElevated && State == PlayerStateType.Crouching) { _objectUnder = collider; return; } else if (!collider.IsHigh && State == PlayerStateType.Jumping) { _objectUnder = null; return; } else { _objectUnder = null; this.Position -= Displacement; } }
/// <summary> /// Processes an interaction from another object /// </summary> /// <param name="interactor"></param> public virtual void OnInteract(WorldObject interactor) { }