using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; /* * For any questions or help, contact us: * Zepharis Lubrano, 3861368 * zepharislubrano@hotmail.com * Luciano van der Veekens, 3784827 * luciano_vd_veekens@live.nl */ class Duck : AnimatedGameObject { TimeSpan vliegtimer = new TimeSpan(0, 0, 0); //de timer TimeSpan stuitertimer = new TimeSpan(0, 0, 0); // timer voor lucht stuiteren Random random; float randomvelx, randomvely, randomposx, randomposy; //Random X and Y coordinates for the Velocity and Position bool duckAlive; //Bool value to see if the Duck is still alive int timer = 0; //timer that starts whenever the duck gets shot dead. bool duckMoving; // Bool value to check if the Duck is moving or not (shot dead) bool shooting; //Bool value to check if we're allowed to shoot or not. SoundEffect flyingDuck, hitsGrass, squeck; //soundeffect variable for when the duck's flying. SoundEffect buzz; //declares the squecking sound when you hit a duck and the buzz sound when you've missed him after three shots. SoundEffectInstance instance; //needed for looping an effect bool soundReady = true; //bool value for checking the soundeffect of the duck hitting the grass bool targetHit; bool respawnDuck; //To know whether the duck has been respawned at the server's side. public bool Mirror { get { return animPlayer.Mirror; } set { animPlayer.Mirror = value; } } public AnimationPlayer AnimPlayer { get { return animPlayer; } } public Duck(GameWorld gameWorld, int layer = 0, string id = "") : base(layer, id) { //Load all the animations this.LoadAnimation(gameWorld, "Sprites/Duck/Idle", "idle", true); this.LoadAnimation(gameWorld, "Sprites/Duck/Die", "die", false); this.LoadAnimation(gameWorld, "Sprites/Duck/Falling", "falling", true); this.LoadAnimation(gameWorld, "Sprites/Duck/Flying", "flying", true); flyingDuck = gameWorld.ResourceManager.getSound("Sounds/Duckflying"); //flying duck sound effect instance = flyingDuck.CreateInstance(); //needed to create an instance, otherwise the effect wouldn't loop. instance.IsLooped = true; //setting the bool value to true, so it will loop hitsGrass = gameWorld.ResourceManager.getSound("Sounds/HitsBottom"); squeck = gameWorld.ResourceManager.getSound("Sounds/Squeckingduck"); buzz = gameWorld.ResourceManager.getSound("Sounds/Buzz"); random = new Random(); Randomness(gameWorld); Reset2(gameWorld); } public SoundEffectInstance FlyingDuck { get { return instance; } } //Random numbers public void Randomness(GameWorld gameWorld) { GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; if (gmHandler.CurrentGameMode != GameModeHandler.GameMode.Join) { randomposx = random.Next(55, 1170); //random spawn pos.X randomposy = random.Next(400, 450); // random spawn pos.Y randomvelx = random.Next(-5, 5); //Random X coordinate (velocity) randomvely = random.Next(-5, 0); //Random Y coordinate (velocity) } } //Resetting values to standard values public void Reset2(GameWorld gameWorld) { GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; if (gmHandler.CurrentGameMode != GameModeHandler.GameMode.Join) { Position = new Vector2(randomposx, randomposy); Velocity = new Vector2(randomvelx, randomvely); } duckAlive = true; shooting = true; soundReady = true; targetHit = false; vliegtimer = new TimeSpan(0, 0, 0); } public override void HandleInput(InputHelper inputHelper, GameWorld gameWorld) { GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; if (shooting && inputHelper.MouseBoundingRectangle.Intersects(this.BoundingRectangle) && inputHelper.MouseLeftButtonPressed()) { duckAlive = false; duckMoving = false; instance.Pause(); targetHit = true; } else targetHit = false; if (!DuckAlive && Shooting && inputHelper.MouseLeftButtonPressed()) //Plays squeeking sound squeck.Play(); //plays the squecking sound ( got it from freesound.org ) base.HandleInput(inputHelper, gameWorld); } //Normal duck movement, so call the "idle" animation and take care of boundaries. public void DuckMovement() { instance.Play(); //playing the looped instance this.PlayAnimation("idle"); animPlayer.Mirror = Velocity.X < 0; // mirrors the animation if the velocity becomes under zero. Position += Velocity; //Duck boundaries if (Position.Y < 0 + BoundingRectangle.Height || Position.Y > 550) { velocity.Y *= -1; stuitertimer = new TimeSpan(0, 0, 0); // reset de stuitertijd zodat ie niet spastisch doet bij de muren } if (Position.X < 0 + BoundingRectangle.Width || Position.X > 1280 - BoundingRectangle.Width) { velocity.X *= -1; stuitertimer = new TimeSpan(0, 0, 0);// reset de stuitertijd zodat ie niet spastisch doet bij de muren } //Preventing the duck from moving linear. while (Velocity.X == 0 || Velocity.Y == 0) { velocity.X = random.Next(-5, 5); velocity.Y = random.Next(-5, 0); } } //when the Duck isn't alive, the duck will stop and fall out of the sky when he dies. public void DuckDeath() { if (duckMoving == false) //duck will stop moving { shooting = false; // you can't shoot the duck any longer, it died. Velocity = Vector2.Zero; this.PlayAnimation("die"); timer++; //if timer hits 50, the duck will move again (down the birdy goes!), so bool value changes to true if (timer > 50) { DuckMoving = true; timer = 0; } } else //so if the Duck is moving again (falling out of sky) { this.PlayAnimation("falling"); Velocity = new Vector2(0, 10); Position += Velocity; } } public override void Update(GameTime gameTime, GameWorld gameWorld) { Player player = gameWorld.Find("Cursor") as Player; Player player2 = gameWorld.Find("Cursor2") as Player; GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) { vliegtimer += gameTime.ElapsedGameTime; stuitertimer += gameTime.ElapsedGameTime; if (stuitertimer.Seconds >= 1 && stuitertimer.Milliseconds>50) { int willekeur = random.Next(0,100); if (willekeur >= 40) { if (velocity.X > 0) { velocity.X = random.Next(-5, 0); } else if (velocity.X < 0) { velocity.X = random.Next(0, 5); } velocity.Y = random.Next(-5, 0); stuitertimer = new TimeSpan(0, 0, 0); } } if (vliegtimer.Seconds >= 10) { player.ShotCount = 3; } } if (duckAlive) //call normal duck movement when it's alive. { if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) // resets the duck after he flies out of the screen. { if (player.ShotCount < 3) //only when you still got bullets left, the duck will fly normally. { DuckMovement(); instance.Resume(); //resuming the sound, to create random squeaking per duck. } else //or else he'll fly away { // resets the duck after he flies out of the screen. animPlayer.Mirror = Velocity.X < 0; // mirrors the animation if the velocity becomes under zero. this.PlayAnimation("flying"); Velocity = new Vector2(10, -10); Position += Velocity; if (shooting) buzz.Play(); //plays the buzz sound when you're letting the duck escape ( got it from freesound.org and its edited with audacity) shooting = false; SmallDucks smallducks = gameWorld.Find("SmallDucks") as SmallDucks; if (Position.Y < -300) // resets the duck after he flies out of the screen. { if (!smallducks.Filled) //if there are less than 10 ducks, they should respawn. { LevelGameObject levelGameObject = gameWorld.Find("level") as LevelGameObject; Randomness(gameWorld); Reset2(gameWorld); //applies speedscaling according to the current level (Rob) if (velocity.X > 0) { velocity.X += levelGameObject.Level * 2; } else if (velocity.X < 0) { velocity.X += levelGameObject.Level * -2; } if (velocity.Y > 0) { velocity.Y += levelGameObject.Level * 2; } else if (velocity.Y < 0) { velocity.Y += levelGameObject.Level * -2; } } player.Reset(); } } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host || gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) // resets the duck after he flies out of the screen. { player.Reset(); //if it's Game Over, let the duck fly away TimerGameObject timerGameObject = gameWorld.Find("Timer") as TimerGameObject; if (timerGameObject.GameOver) { animPlayer.Mirror = Velocity.X < 0; // mirrors the animation if the velocity becomes under zero. this.PlayAnimation("flying"); Velocity = new Vector2(10, -10); Position += Velocity; if (shooting) buzz.Play(); //plays the buzz sound when you're letting the duck escape ( got it from freesound.org and its edited with audacity) shooting = false; } else //else just let the duck move normally { DuckMovement(); instance.Resume(); } } } else { DuckDeath(); if (Position.Y > 420) { if (soundReady) hitsGrass.Play(); // a sound will be played when the ducks hits the bottom. soundReady = false; } if (Position.Y > 700 && gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) //The duck will be generated at a new position, resetting all the values in the player and duck class and making random numbers. { SmallDucks smallducks = gameWorld.Find("SmallDucks") as SmallDucks; if (!smallducks.Filled) //if there are less than 10 ducks, they should respawn. { LevelGameObject levelGameObject = gameWorld.Find("level") as LevelGameObject; Randomness(gameWorld); Reset2(gameWorld); if (velocity.X > 0) { velocity.X += levelGameObject.Level * 2; } else if (velocity.X < 0) { velocity.X += levelGameObject.Level * -2; } if (velocity.Y > 0) { velocity.Y += levelGameObject.Level * 2; } else if (velocity.Y < 0) { velocity.Y += levelGameObject.Level * -2; } } player.Reset(); } if (Position.Y > 700 && gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host) { Randomness(gameWorld); respawnDuck = true; Reset2(gameWorld); } if (Position.Y > 700 && gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) Reset2(gameWorld); } animPlayer.Update(gameTime); } public bool Shooting //property for using the shooting variable { get { return shooting; } } public bool DuckAlive //property to know whether the Duck is alive or not. { get { return duckAlive; } set { duckAlive = value; } } public bool DuckMoving //property to know whether the Duck is alive or not. { get { return duckMoving; } set { duckMoving = value; } } public bool RespawnDuck { get { return respawnDuck; } set { respawnDuck = value; } } public SoundEffectInstance Instance { get { return instance; } set { instance = value; } } public bool TargetHit { get { return targetHit; } set { targetHit = value; } } public TimeSpan VliegTimer { get { return vliegtimer; } set { vliegtimer = value; } } public float StartPositionX { get { return randomposx; } set { randomposx = value; } } public float StartPositionY { get { return randomposy; } set { randomposy = value; } } public float VelocityX { get { return randomvelx; } set { randomvelx = value; } } public float VelocityY { get { return randomvely; } set { randomvely = value; } } }