Esempio n. 1
0
        public LoseState(ButtonFactory buttonFactory, TextFactory textFactory, IPostOfficeService postOfficeService, LeaderboardManager leaderboardManager)
        {
            _buttonFactory      = buttonFactory;
            _textFactory        = textFactory;
            _postOfficeService  = postOfficeService;
            _leaderboardManager = leaderboardManager;
            _playerScore        = 0L;

            // Check for 123456 because user cannot enter this
            _playername = "123456";

            // Ready to write to file.
            _ready = true;

            #region Button Stuff

            // Menu button stuff
            var menuArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Menu")
            };

            _menuButton = _buttonFactory.GenerateButton("Menu", 20, 400, menuArgs);

            #endregion
        }
Esempio n. 2
0
        public Button GenerateButton(string text, int x, int y, PostOfficeEventArgs args)
        {
            var position = new Vector2(x, y);
            var button   = new Button(text, position, _defaultTexture, _font, _postOffice, args);

            return(button);
        }
Esempio n. 3
0
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            switch (args.MessageName)
            {
            case "GetPlayerScore":

                // Get current score
                var currentScore = BitConverter.ToInt64(args.Data, 0);

                // Flip score
                currentScore *= -1;

                //Args to set score to 0
                var scoreArgs = new PostOfficeEventArgs()
                {
                    SendAddress = "PlayerMetaData",
                    MessageName = "AddScore",
                    Data        = BitConverter.GetBytes(currentScore)
                };

                _postOfficeService.SendMail(returnAddress, scoreArgs);

                break;
            }
        }
Esempio n. 4
0
        public Button(string text, Vector2 position, Texture2D buttonSprite, SpriteFont font, IPostOfficeService postOffice, PostOfficeEventArgs args)
        {
            // Initialise local variables
            _clickReady = false;
            _guid       = Guid.NewGuid();
            _canUpdate  = true;

            // Store local variables passed in
            _text         = text;
            _position     = position;
            _buttonSprite = buttonSprite;
            _font         = font;
            _postOffice   = postOffice;
            _args         = args;

            // Register at the post office
            _postOffice.RegisterClient(this, _guid.ToString());

            // Measure and center text
            var textRadii    = _font.MeasureString(_text) / 2;
            var buttonCenter = new Vector2(_buttonSprite.Width / 2, _buttonSprite.Height / 2);
            var textOffset   = buttonCenter - textRadii;

            _textPosition = _position + textOffset;

            // Set up rectangles for collision.
            _buttonCollisionRectangle = new Rectangle((int)_position.X, (int)_position.Y, _buttonSprite.Width, _buttonSprite.Height);
        }
Esempio n. 5
0
        public bool Initialise()
        {
            // Reset stuff
            _playerScore = 0L;
            _playername  = "123456";
            _ready       = true;

            #region Getting Player Meta Data

            var getNameArgs = new PostOfficeEventArgs()
            {
                SendAddress = "PlayerMetaData",
                MessageName = "RequestName",
                Data        = new byte[0]
            };

            var getScoreArgs = new PostOfficeEventArgs()
            {
                SendAddress = "PlayerMetaData",
                MessageName = "RequestScore",
                Data        = new byte[0]
            };

            _postOfficeService.SendMail(this.StateRegisterName, getNameArgs);
            _postOfficeService.SendMail(this.StateRegisterName, getScoreArgs);

            #endregion

            return(true);
        }
Esempio n. 6
0
        public NameInputState(ButtonFactory buttonFactory, TextFactory textFactory, IPostOfficeService postOfficeService, Rectangle bounds)
        {
            // Initialise fields
            StateRegisterName = "NameInput";
            _maxLen           = 5;

            // Set up buttons
            #region Buttons

            // Buttons
            PostOfficeEventArgs args;
            _buttons = new List <Button>();

            // Back button
            args = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Menu")
            };

            _buttons.Add(buttonFactory.GenerateButton("Back", 100, 100, args));

            // Play button
            var playArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("FlavourText")
            };

            _buttons.Add(buttonFactory.GenerateButton("Play", 100, 100, playArgs));

            // Get real values
            var seperation = 100;
            var totalWidth = (_buttons[0].Width * 2) + seperation;
            var leftSpace  = bounds.Width - totalWidth;
            var margin     = leftSpace / 2;

            var yValue = 300;

            // Set real values
            _buttons[0].Position = new Vector2(margin, yValue);
            _buttons[1].Position = new Vector2(margin + seperation + _buttons[0].Width, yValue);

            #endregion

            // Initialise more fields
            _nameHelper        = new NameInputHelper(_maxLen);
            _textFactory       = textFactory;
            _postOfficeService = postOfficeService;
            _name = _textFactory.GenerateText(_nameHelper.GetName(), margin, 100);
        }
Esempio n. 7
0
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            switch (args.MessageName)
            {
            // Disable the button if requested
            case "Disable":
                _canUpdate = false;
                break;
            }

            return;
        }
Esempio n. 8
0
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            switch (args.MessageName)
            {
            // Take health off the gate
            case "DamageHealth":

                var damage = BitConverter.ToInt32(args.Data, 0);
                _health -= damage;

                break;
            }
        }
Esempio n. 9
0
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            // All in and out messages
            switch (args.MessageName)
            {
            // external power changes the current name
            case "ChangeName":
                var newName = Encoding.ASCII.GetString(args.Data);

                _playerData.Name = newName;

                break;

            // External power adds value to the current score.
            case "AddScore":

                var newScore = BitConverter.ToInt32(args.Data, 0);

                _playerData.PlayerScore += newScore;

                break;

            // Sends out current name in letter
            case "RequestName":

                var reqArgs = new PostOfficeEventArgs()
                {
                    SendAddress = returnAddress,
                    MessageName = "GetPlayerName",
                    Data        = Encoding.ASCII.GetBytes(_playerData.Name)
                };

                _postOfficeService.SendMail(this.LetterboxName, reqArgs);

                break;

            // Sends out current score in letter
            case "RequestScore":

                var reqScoreArgs = new PostOfficeEventArgs()
                {
                    SendAddress = returnAddress,
                    MessageName = "GetPlayerScore",
                    Data        = BitConverter.GetBytes(_playerData.PlayerScore)
                };

                _postOfficeService.SendMail(this.LetterboxName, reqScoreArgs);

                break;
            }
        }
Esempio n. 10
0
        // Call letterbox function and pass any messages through
        public bool SendMail(string returnAddress, PostOfficeEventArgs args)
        {
            // Validate key is there
            var keyThere = _clients.Keys.Contains(args.SendAddress);

            if (!keyThere)
            {
                return(false);
            }

            _clients[args.SendAddress].LetterBox(returnAddress, args);

            return(true);
        }
Esempio n. 11
0
        // Get post office messages
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            switch (args.MessageName)
            {
            // Add money to the player
            case "AddMoney":

                var newMoney = BitConverter.ToInt32(args.Data, 0);

                _money += newMoney;

                break;
            }
        }
Esempio n. 12
0
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            switch (args.MessageName)
            {
            case "AddArcher":
                // Check money
                if (_moneyManager.Money >= 20)
                {
                    // Add archer
                    AddArcher();

                    _moneyManager.Money -= 20;
                }


                break;
            }
        }
Esempio n. 13
0
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            // Sort the messages for this state
            switch (args.MessageName)
            {
            case "GetPlayerName":

                var name = Encoding.ASCII.GetString(args.Data);
                _playername = name;

                break;

            case "GetPlayerScore":

                var score = BitConverter.ToInt64(args.Data, 0);
                _playerScore = score;

                break;
            }
        }
Esempio n. 14
0
        public MenuState(ButtonFactory buttonFactory, TextFactory textFactory)
        {
            StateRegisterName = "Menu";

            // Buttons
            _buttons = new List <Button>();

            // Play button
            var playArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("NameInput")
            };

            _buttons.Add(buttonFactory.GenerateButton("Play", 100, 100, playArgs));

            // Leaderboard
            var leaderboardArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Leaderboard")
            };

            _buttons.Add(buttonFactory.GenerateButton("Leaderboard", 100, 150, leaderboardArgs));


            // Exit
            var exitArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Exit")
            };

            _buttons.Add(buttonFactory.GenerateButton("Exit", 100, 200, exitArgs));
            _textFactory = textFactory;

            _title = _textFactory.GenerateText("Siege of Brightforest", 400, 100);
        }
Esempio n. 15
0
        public LeaderboardState(ButtonFactory buttonFactory, TextFactory textFactory, LeaderboardManager leaderboardManager)
        {
            StateRegisterName = "Leaderboard";

            // Factories and managers
            _buttonFactory      = buttonFactory;
            _textFactory        = textFactory;
            _leaderboardManager = leaderboardManager;

            // Lists
            _text    = new List <Text>();
            _buttons = new List <Button>();

            // Change the state to the menu
            var buttonArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Menu")
            };

            _buttons.Add(_buttonFactory.GenerateButton("Back", 550, 400, buttonArgs));
        }
Esempio n. 16
0
        public bool Stop()
        {
            // Send finished name off to the player meta data
            var args = new PostOfficeEventArgs()
            {
                SendAddress = "PlayerMetaData",
                MessageName = "ChangeName",
                Data        = Encoding.ASCII.GetBytes(FormatName(_nameHelper.GetName(), false))
            };

            // Start process to reset score to 0
            var scoreargs = new PostOfficeEventArgs()
            {
                SendAddress = "PlayerMetaData",
                MessageName = "RequestScore",
                Data        = new byte[10]
            };

            _postOfficeService.SendMail(this.StateRegisterName, args);
            _postOfficeService.SendMail(this.StateRegisterName, scoreargs);

            return(true);
        }
Esempio n. 17
0
        public FlavourTextState(ButtonFactory buttonFactory, TextFactory textFactory)
        {
            // Fields
            _textFactory = textFactory;

            #region ContinueButton

            var continueArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Play")
            };
            _continueButton = buttonFactory.GenerateButton("Continue", 20, 400, continueArgs);



            #endregion

            #region Text Stuff

            // Lines of flavour text
            var lines = new List <string>()
            {
                "The castle is under attack!",
                "The evil fire squirrels have beseiged Brightforest.",
                "Direct orders from the crown state you are in charge",
                "of defending as long as you can.",
                "Upgrades are M30 each, a new archer is M20.",
                "Good luck."
            };

            // Get the flavour text as Text objects
            _flavourText = GenerateTexts(lines);

            #endregion
        }
Esempio n. 18
0
        // All post office messages coming in and replies
        public void LetterBox(string returnAddress, PostOfficeEventArgs args)
        {
            // to disable a button
            var disableArgs = new PostOfficeEventArgs()
            {
                SendAddress = returnAddress,
                MessageName = "Disable",
                Data        = new byte[0]
            };

            // To reduce the player money
            var reduceMoneyArgs = new PostOfficeEventArgs()
            {
                SendAddress = "MoneyManager",
                MessageName = "AddMoney",
                Data        = BitConverter.GetBytes(-30)
            };

            switch (args.MessageName)
            {
            // Change damage multiplier
            case "ModifyArrowDamage":

                if (CanIncreaseDamage && _moneyManager.Money >= 30)
                {
                    _damageModifier++;     // Increases the damage output by 1x

                    if (!CanIncreaseDamage)
                    {
                        // Disable damage upgrade button
                        _postOfficeService.SendMail(this.LetterboxName, disableArgs);
                    }

                    // reduce the money
                    _postOfficeService.SendMail(this.LetterboxName, reduceMoneyArgs);
                }


                break;

            // Change rate of fire
            case "ModifyRateOfFire":

                if (CanHalfRof && _moneyManager.Money >= 30)
                {
                    _rateOfFire /= 2;     // Doubles the rate of fire of projectiles.
                    _timesHalvedRoF++;

                    if (!CanHalfRof)
                    {
                        // Disable the rof upgrade button
                        _postOfficeService.SendMail(this.LetterboxName, disableArgs);
                    }

                    // Reduce the money
                    _postOfficeService.SendMail(this.LetterboxName, reduceMoneyArgs);
                }


                break;
            }
        }
Esempio n. 19
0
        public void Update(MouseState mouseState, KeyboardState keyboardState)
        {
            // Update all the text
            _amountOfArchers.DisplayText = $"{_archers.Count.ToString()} Archer(s)";
            _rofLevel.DisplayText        = $"Level {_statsManager.RateOfFireLevel}";
            _damageLevel.DisplayText     = $"{_statsManager.DamageModifier}x Damage";
            _money.DisplayText           = $"Current Funds: M{_moneyManager.Money}";
            _waveText.DisplayText        = $"Wave {_wave}";
            _gateHealth.DisplayText      = $"Gate Health: {_gate.Health}";

            // Update all the upgrade buttons
            foreach (var upgradeButton in _upgradeButtons)
            {
                upgradeButton.Update(mouseState, keyboardState);
            }

            // Update the quit button
            _quitButton.Update(mouseState, keyboardState);

            // Update each archer
            foreach (var archer in _archers)
            {
                archer.Update(mouseState, keyboardState);
            }

            // If in a wave
            if (_inWave)
            {
                // Go through all enemies, update, if dead add to list
                var deadSquirrels = new List <Squirrel>();
                foreach (var squirrel in _enemies)
                {
                    squirrel.Update(mouseState, keyboardState);

                    if (!squirrel.Alive)
                    {
                        deadSquirrels.Add(squirrel);
                    }
                }

                // 15 money per squirrel
                var newMoneyArgs = new PostOfficeEventArgs()
                {
                    SendAddress = "MoneyManager",
                    MessageName = "AddMoney",
                    Data        = BitConverter.GetBytes(15 * deadSquirrels.Count)
                };
                _postOfficeService.SendMail(this.LetterboxName, newMoneyArgs);

                // Add points 10 points per squirrel
                var newScoreArgs = new PostOfficeEventArgs()
                {
                    SendAddress = "PlayerMetaData",
                    MessageName = "AddScore",
                    Data        = BitConverter.GetBytes(10 * deadSquirrels.Count)
                };
                _postOfficeService.SendMail(this.LetterboxName, newScoreArgs);

                foreach (var deadSquirrel in deadSquirrels)
                {
                    _enemies.Remove(deadSquirrel);
                }

                // If theres enemies
                if (_enemies.Count > 0)
                {
                    // order the squillows
                    var orderedSquirrels = _enemies.OrderBy(r => r.Position.X);

                    foreach (var archer in _archers)
                    {
                        // One of first 5
                        var firstX = 10;

                        var amount = orderedSquirrels.Count();
                        amount = amount >= firstX ? firstX : amount;

                        Random r     = new Random((int)DateTime.Now.Ticks);
                        var    index = r.Next(0, amount);

                        archer.FireArrow(orderedSquirrels.ToArray()[index]);
                    }
                }
            }

            // If all enemies are dead
            if (_enemies.Count == 0)
            {
                _wave++;
                _inWave = false;
                GenerateEnemies();
            }

            // If the gate is dead
            if (_gate.Health <= 0)
            {
                var changeStateArgs = new PostOfficeEventArgs()
                {
                    SendAddress = "StateManager",
                    MessageName = "ChangeState",
                    Data        = Encoding.ASCII.GetBytes("Lose")
                };
                _postOfficeService.SendMail(this.LetterboxName, changeStateArgs);
            }
        }
Esempio n. 20
0
        public PlayState(IPostOfficeService postOfficeService, SquirrelFactory squirrelFactory, ButtonFactory buttonFactory, TextFactory textFactory, StatsManager statsManager, MoneyManager moneyManager, Texture2D background, Rectangle bounds, Texture2D upgradebar, Texture2D stone, Gate gate)
        {
            _inWave  = true;
            _wave    = 0;
            _enemies = new List <Squirrel>();
            _archers = new List <Archer>();


            StateRegisterName  = "Play";
            _postOfficeService = postOfficeService;
            _squirrelFactory   = squirrelFactory;
            _buttonFactory     = buttonFactory;
            _textFactory       = textFactory;
            _statsManager      = statsManager;
            _moneyManager      = moneyManager;
            _background        = background;
            _stone             = stone;
            _gate   = gate;
            _bounds = new Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height - 100);

            _upgradesBar = new UpgradesBar(new Vector2(0, _bounds.Height), new Rectangle(0, 0, _bounds.Width, 100), upgradebar);


            #region UpgradeButtons

            _upgradeButtons = new List <Button>();

            // Add archer
            var addArcherArgs = new PostOfficeEventArgs()
            {
                SendAddress = "PlayState",
                MessageName = "AddArcher",
                Data        = new byte[0]
            };

            _upgradeButtons.Add(_buttonFactory.GenerateButton("New Archer", 30, _bounds.Height + 5, addArcherArgs));

            // Increase Rate of Fire

            var rateOfFireArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StatsManager",
                MessageName = "ModifyRateOfFire",
                Data        = new byte[0]
            };

            _upgradeButtons.Add(_buttonFactory.GenerateButton("RoF++", 230, _bounds.Height + 5, rateOfFireArgs));

            // Increase damage modifier

            var damageArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StatsManager",
                MessageName = "ModifyArrowDamage",
                Data        = new byte[0]
            };

            _upgradeButtons.Add(_buttonFactory.GenerateButton("Damage++", 430, _bounds.Height + 5, damageArgs));

            #endregion


            #region Text

            // Generate the text objects

            _amountOfArchers =
                _textFactory.GenerateText($"{_archers.Count.ToString()} Archer(s)", 30, _bounds.Height + 55);

            _rofLevel = _textFactory.GenerateText($"Level {_statsManager.RateOfFireLevel}", 230, _bounds.Height + 55);

            _damageLevel =
                _textFactory.GenerateText($"{_statsManager.DamageModifier}x Damage", 430, _bounds.Height + 55);

            _money = _textFactory.GenerateText($"Current Funds: M{_moneyManager.Money}", 20, 20);

            _waveText = _textFactory.GenerateText($"Wave {_wave}", 20, 70);

            _gateHealth = _textFactory.GenerateText($"Gate Health: {_gate.Health}", 20, 120);

            #endregion

            #region Quit Button

            var quitArgs = new PostOfficeEventArgs()
            {
                SendAddress = "StateManager",
                MessageName = "ChangeState",
                Data        = Encoding.ASCII.GetBytes("Menu")
            };

            _quitButton = buttonFactory.GenerateButton("Quit", 600, 20, quitArgs);

            #endregion
        }