コード例 #1
0
        public DeadlyObject(Level currentLevel, Rectangle initialBoundingBox, Vector2 initialVelocity, MovingObjectObserver myObserver)
            : base(currentLevel, initialBoundingBox, initialVelocity, myObserver)
        {
            beginFade = false;
            fadeDuration = TimeSpan.FromSeconds(1);

            // create an array of possible sprites
            sprites.Add("Weapons/anvil");
            //sprites.Add("mooglegrin");

            spriteMasses["Weapons/anvil"] = 400;

            string spriteName = (string)sprites[rand.Next(sprites.Count)];

            // choose a random sprite and set your bounding box accordingly
            sprite = currentLevel.Content.Load<Texture2D>(spriteName);

            // set the mass
            if (spriteMasses.ContainsKey(spriteName))
            {
                mass = spriteMasses[spriteName];
            }

            boundingRectangle.Height = sprite.Height;
            boundingRectangle.Width = sprite.Width;

            hitSFX = level.Content.Load<SoundEffect>("Audio/cannon");
        }
コード例 #2
0
 public MovingObject(Level currentLevel, Rectangle initialBoundingRectangle, Vector2 initialVelocity, MovingObjectObserver myObserver)
 {
     level = currentLevel;
     velocity = initialVelocity;
     observer = myObserver;
     boundingRectangle = initialBoundingRectangle;
 }
コード例 #3
0
ファイル: Level.cs プロジェクト: AmeliaMesdag/Sheep-Cannon
        /// <summary>
        /// Contructs a new level
        /// </summary>
        /// <param name="serviceProvider">The service provider that will be used to construct a ContentManager.</param>
        public Level(IServiceProvider serviceProvider)
        {
            // Create a new content manager to load content used just by this level.
            content = new ContentManager(serviceProvider, "Content");

            cannon = new Cannon(this);
            farmer = new Farmer(this, new Vector2(200, GAME_HEIGHT - 64));
            observer = new MovingObjectObserver(this);
            deadlyObjectFactory = new DeadlyObjectFactory(this);
            foregroundClouds = new CloudFactory(new Color(Color.White, .90f), 1f, 5, this);
            backgroundClouds = new CloudFactory(new Color(.75f, .75f, 1f, .95f), .50f, 17, this);
            score = 0;
            cannonCountdown = TimeSpan.FromSeconds(5.5);

            ground = new Rectangle(0, GAME_HEIGHT - 64, GAME_WIDTH, 64);

            // Load background layer textures.
            layers = new Texture2D[4];
            for (int i = 0; i < layers.Length; ++i)
            {
                // Choose a random segment for each background layer for level variety.
                int segmentIndex = 0; // random.Next(3);
                layers[i] = Content.Load<Texture2D>("Backgrounds/Layer" + i + "_" + segmentIndex);
            }
            ChangeWind();

            //Play BGM
            levelMusic = Content.Load<Song>("Audio/Music/GameMusic");
            MediaPlayer.Play(levelMusic);
        }
コード例 #4
0
ファイル: Sheep.cs プロジェクト: AmeliaMesdag/Sheep-Cannon
        public Sheep(Level currentLevel, Rectangle initialBoundingRectangle, Vector2 initialVelocity, MovingObjectObserver myObserver)
            : base(currentLevel, initialBoundingRectangle, initialVelocity, myObserver)
        {
            beginFade = false;
            sheepSprite = currentLevel.Content.Load<Texture2D>("Sheep/sheep");
            boundingRectangle.Width  = sheepSprite.Width;
            boundingRectangle.Height = sheepSprite.Height / 2;
            boundingRectangle.X -= boundingRectangle.Width;
            boundingRectangle.Y -= boundingRectangle.Height;
            mass = 900;

            sheepSFX = level.Content.Load<SoundEffect>("Audio/sheep");
        }
コード例 #5
0
        // update function generates falling objects, samples the farmer's location
        public void Update(GameTime gameTime, MovingObjectObserver observer)
        {
            FARMER_SAMPLE_INTERVAL -= gameTime.ElapsedGameTime;

            if (FARMER_SAMPLE_INTERVAL <= TimeSpan.Zero)
            {
                arrFarmerPositions[nextIntervalPosition++] = level.Farmer.Position;
                if (nextIntervalPosition == FARMER_SAMPLE_NUM)
                    nextIntervalPosition = 0;
            }

            // update the local elapsed time
            elapsedTime += gameTime.ElapsedGameTime;

            // check to see if we should sample the farmer
            if (elapsedTime > DEADLY_OBJECT_SPAWN_INTERVAL)
            {
                // move to the next interval
                elapsedTime -= DEADLY_OBJECT_SPAWN_INTERVAL;
                if (DEADLY_OBJECT_SPAWN_INTERVAL > TimeSpan.FromMilliseconds(155))
                    DEADLY_OBJECT_SPAWN_INTERVAL -= TimeSpan.FromMilliseconds(8);
                observer.SpawnDeadlyObject(new Vector2(rand.Next(Level.GAME_WIDTH), -100), new Vector2(0, 1));
            }
        }
コード例 #6
0
ファイル: Cannon.cs プロジェクト: AmeliaMesdag/Sheep-Cannon
        public void Update(GameTime gameTime, MovingObjectObserver observer)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            float minFireDelay = initialMinFireDelay / fireDelayDivisor;
            float maxFireDelay = initialMaxFireDelay / fireDelayDivisor;
            TimeSpan timeSinceFiring = (gameTime.TotalGameTime - lastFiring);
            TimeSpan timeSinceSpeedIncrease = (gameTime.TotalGameTime - lastSpeedIncrease);

            // Get input state.
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.Two);
            MouseState mouseState = Mouse.GetState();
            Point mousePosition = new Point(mouseState.X - cannonOrigin.X, mouseState.Y - cannonOrigin.Y);
            //Mouse position relative to the cannon

            //Default to no slope
            cannonAngle = 0f;

            //If mouse is in valid range, point cannon toward cursor
            if ((mousePosition.X < 0) && (mousePosition.Y < 0))
                cannonAngle = Math.Atan((float)mousePosition.Y / (float)mousePosition.X);
            else if ((mousePosition.X > 0) && (mousePosition.Y < 0)) //If mouse forms obtuse angle, go to vertical
                cannonAngle = Math.PI / 2f;

            // If analog stick is detected, override mouse and point in the direction it points.
            if ((gamePadState.ThumbSticks.Left.X < 0) && (gamePadState.ThumbSticks.Left.Y > 0))
                cannonAngle = Math.Atan(gamePadState.ThumbSticks.Left.Y / gamePadState.ThumbSticks.Left.X);

            // Fire cannon if mouse clicked or A face button pressed and cannon is ready, or max fire time has passed
            if ((((mouseState.LeftButton == ButtonState.Pressed) || gamePadState.IsButtonDown(Buttons.A))
                      && (timeSinceFiring > TimeSpan.FromSeconds(minFireDelay)))
                             || (timeSinceFiring > TimeSpan.FromSeconds(maxFireDelay)))
            {
                cannonSFX.Play();
                observer.SpawnSheep(new Vector2((float)cannonOrigin.X + 88, (float)cannonOrigin.Y),
                                    new Vector2(System.Convert.ToSingle((-launchVelocity * Math.Cos(cannonAngle))),
                                                System.Convert.ToSingle((-launchVelocity * Math.Sin(cannonAngle)))));
                lastFiring = gameTime.TotalGameTime;
            }

            //Increment speed if speedIncreaseInterval has passed
            if (timeSinceSpeedIncrease.TotalSeconds >= (double)speedIncreaseInterval)
            {
                fireDelayDivisor++;
                lastSpeedIncrease = gameTime.TotalGameTime;
            }
        }