Пример #1
0
        protected virtual StringShape CreatePackageDetailsMessage(IBabyPackageProvider babyPackage)
        {
            if (String.IsNullOrEmpty(babyPackage.Author) &&
                String.IsNullOrEmpty(babyPackage.Title) &&
                babyPackage.Website == null)
            {
                return(null);
            }

            var website = String.Empty;

            if (babyPackage.Website != null)
            {
                website = babyPackage.Website.ToString().TrimEnd('/').Replace(babyPackage.Website.Scheme + "://", String.Empty);
            }
            var helpText = new StringShape(this.Game);

            helpText.String       = String.Format("{0}\n{1}\n{2}", babyPackage.Title, String.IsNullOrEmpty(babyPackage.Author) ? String.Empty : "By " + babyPackage.Author, website);
            helpText.Colour       = Color.Black;
            helpText.ShadowColour = Color.White;
            helpText.ShadowOffset = 1f;
            helpText.Location     = new Vector2(10);
            helpText.SpinTime     = TimeSpan.Zero;
            helpText.FadeInTime   = TimeSpan.FromSeconds(0.4);
            helpText.OnScreenTime = TimeSpan.FromSeconds(10);
            helpText.FadeOutTime  = TimeSpan.FromSeconds(1.5);
            // TODO: load font sizes based on screen resolution and from a pool of fonts so I'm not newing one up here.
            helpText.Font = this.Game.Content.Load <SpriteFont>(@"DejaVu Sans 14");
            return(helpText);
        }
Пример #2
0
 protected virtual void DoEndOfLoop(GameTime gameTime, Configuration config, IBabyPackageProvider babyPackage, SoundService sound)
 {
     // Save key / button state pressed for the next game update.
     this.UpdateLastKeysPressed(Keyboard.GetState());
     this.UpdateLastButtonsPressed();
     this._LastMouseState = Mouse.GetState();
     this._FirstUpdate    = false;
 }
Пример #3
0
        protected virtual void DoIdleHandling(GameTime gameTime, Configuration config, IBabyPackageProvider babyPackage, SoundService sound)
        {
            // Idle timeout.
            if (gameTime.TotalGameTime > this._LastAction.Add(babyPackage.IdleTimeout))
            {
                // Get the idle timeout action.
                var timeoutAction = babyPackage.DoIdleTimeout(this._LastAction);
                if (timeoutAction != null)
                {
                    if (timeoutAction.Shape != null)
                    {
                        this._BabyShapes.Components.Add(timeoutAction.Shape);
                    }
                    if (timeoutAction.Sound != null)
                    {
                        sound.TryPlaySoundLong(timeoutAction.Sound, LongSoundOwner.Idle);
                    }
                }

                // Display the 'about' notice again (so parents know how to exit!).
                this._BabyShapes.Components.Add(this.Game.GetHelpText());

                this._LastAction = gameTime.TotalGameTime;      // Reset the timeout.
            }
        }
Пример #4
0
        protected virtual void DoGamepadHandling(GameTime gameTime, Configuration config, IBabyPackageProvider babyPackage, SoundService sound)
        {
            // Gamepads.
            var buttonPresses = this.GetButtonPresses();

            if (buttonPresses.Any())
            {
                this._LastAction = gameTime.TotalGameTime;
            }

            // Add buttons pressed to be drawn.
            foreach (var buttonAndPlayer in buttonPresses)
            {
                var buttonResult = babyPackage.MapControllerButton(buttonAndPlayer.Button, buttonAndPlayer.Player);
                this.HandleShapeAndSound(sound, buttonResult, SoundAction.Button);
            }

            // Deal with analogue inputs.
            for (PlayerIndex player = PlayerIndex.One; player <= PlayerIndex.Four; player++)
            {
                if (GamePad.GetState(player).IsConnected)
                {
                    var analogueInputs = new ControllerAnalogue(GamePad.GetState(player), ControllerReversal.LeftThumb);
                    if (!analogueInputs.IsZero)
                    {
                        this._LastAction = gameTime.TotalGameTime;
                        this._LastGamepadAnalogueMoveDetected = gameTime.TotalGameTime;
                    }

                    var analogueResult = babyPackage.DoControllerAnalogue(analogueInputs, player);
                    if (analogueResult != null && analogueResult.Any())
                    {
                        foreach (var action in analogueResult)
                        {
                            // Add the shape.
                            if (action.Shape != null && !this._BabyShapes.Components.Contains(action.Shape))
                            {
                                this._BabyShapes.Components.Add(action.Shape);
                            }

                            if (action.Sound != null && sound.LongSoundPlayingState == SoundState.Stopped)
                            {
                                // Play sound while analogue controls are being used.
                                sound.TryPlaySoundLong(action.Sound, LongSoundOwner.GamePadAnalogueInputs);
                            }
                        }
                    }

                    if (analogueResult == null || !analogueResult.Any() || analogueResult.All(a => a.Sound == null))
                    {
                        // No analogue action: stop playing sound after short delay.
                        if (gameTime.TotalGameTime > this._LastGamepadAnalogueMoveDetected.Add(GamepadAnalogueMoveGraceTime) &&
                            sound.LongSoundPlayingState == SoundState.Playing && sound.LongSoundOwner == LongSoundOwner.GamePadAnalogueInputs)
                        {
                            sound.StopPlayingLongSound();
                        }
                    }
                }
            }
        }
Пример #5
0
        protected virtual void DoMouseHandling(GameTime gameTime, Configuration config, IBabyPackageProvider babyPackage, SoundService sound)
        {
            var mouseState   = Mouse.GetState();
            var mouseButtons = mouseState.GetPressedButtons();

            // Mouse movement.
            var mouseMoveResult = babyPackage.DoMouseMovement(mouseState.X, mouseState.Y, this._LastMouseState.X, this._LastMouseState.Y, mouseState.AnyButtonPressed());

            if (mouseMoveResult != null)
            {
                this._LastAction = this._LastMouseMoveDetected = gameTime.TotalGameTime;

                // Add the shape for the mouse cursor.
                if (mouseMoveResult.Shape != null && !this._BabyShapes.Components.Contains(mouseMoveResult.Shape))
                {
                    this._BabyShapes.Components.Add(mouseMoveResult.Shape);
                }

                if (mouseMoveResult.Sound != null && sound.LongSoundPlayingState == SoundState.Stopped)
                {
                    // Play mouse sound while the mouse is moving.
                    sound.TryPlaySoundLong(mouseMoveResult.Sound, LongSoundOwner.MouseMovement);
                }
            }
            else if (mouseMoveResult == null || mouseMoveResult.Sound == null)
            {
                // Stop the sound after a short grace time.
                if (gameTime.TotalGameTime > this._LastMouseMoveDetected.Add(MouseMoveGraceTime) &&
                    sound.LongSoundPlayingState == SoundState.Playing && sound.LongSoundOwner == LongSoundOwner.MouseMovement)
                {
                    sound.StopPlayingLongSound();
                }
            }

            // Mouse buttons.
            foreach (var button in this.GetMouseButtonPresses(mouseButtons))
            {
                var buttonResult = babyPackage.MapMouseButton(button, mouseState.X, mouseState.Y);
                this.HandleShapeAndSound(sound, buttonResult, SoundAction.Button);
            }

            // Mouse wheel.
            var mouseWheelResult = babyPackage.DoMouseWheelMovement(mouseState.ScrollWheelValue, this._LastMouseState.ScrollWheelValue, mouseState.AnyButtonPressed());

            this.HandleShapeAndSound(sound, mouseWheelResult, SoundAction.Button);
        }
Пример #6
0
        protected virtual void DoKeyboardHandling(GameTime gameTime, Configuration config, IBabyPackageProvider babyPackage, SoundService sound)
        {
            // Keyboard handling.
            var keyboardState = Keyboard.GetState();

            // Idle timeout reset.
            if (keyboardState.GetPressedKeys().Length > 0)
            {
                this._LastAction = gameTime.TotalGameTime;
            }

            if (!this.IsBashingKeys(keyboardState))
            {
                // Add characters pressed to be drawn.
                foreach (var key in this.GetKeyPresses(keyboardState))
                {
                    var keyResult = babyPackage.MapKeyPress(key);
                    this.HandleShapeAndSound(sound, keyResult, SoundAction.Button);
                }
            }
            else
            {
                // Bashing detected, display something different and clear all other shapes / letters on screen.
                var bashingAction = babyPackage.KeyBashingDetected();
                if (bashingAction != null)
                {
                    foreach (var element in this._BabyShapes.Components.OfType <BabyShape>().ToArray())
                    {
                        this._BabyShapes.Components.Remove(element);
                    }

                    sound.StopPlayingAllSounds();
                    this.HandleShapeAndSound(sound, bashingAction, SoundAction.ForceButton);

                    this._BashingShape = bashingAction.Shape;
                    this._BabyShapes.Components.ComponentRemoved += new EventHandler <GameComponentCollectionEventArgs>(BabyShapes_BashingComponentRemoved);
                }
            }
        }
Пример #7
0
        protected virtual void DoFirstUpdate(GameTime gameTime, Configuration config, IBabyPackageProvider babyPackage, SoundService sound)
        {
            // Play the startup sound.
            this.HandleShapeAndSound(sound, babyPackage.Startup(), SoundAction.Analogue);

            if (!this._BabyShapes.Components.Contains(this._HelpText))
            {
                // Display the package details if the help message has gone.
                this.BabyShapes_HelpTextComponentRemoved(this, new GameComponentCollectionEventArgs(null));
            }
            else
            {
                // Wait for the help text to be removed.
                this._BabyShapes.Components.ComponentRemoved += new EventHandler <GameComponentCollectionEventArgs>(BabyShapes_HelpTextComponentRemoved);
            }
        }