Пример #1
0
        public Model(char[,] statics, char[,] mobiles, IModelBehaviour behaviourModel)
        {
            InputBuffer = new ControllerMessageMediator();
            var loader = new TileLoader();

            Statics = loader.Load(statics, StaticsMap.TileBuilder);

            Statics.AddCollisionRules(
                (mobile, tile) => !tile.IsTraversible);

            Mobiles = loader.Load(mobiles, MobilesMap.TileBuilder);

            Mobiles.AddCollisionRules(
                (mobile, tile) => !(mobile.CanPush && tile.CanBePushed),
                (mobile, tile) => !(mobile.CanKill && tile.CanBeKilled),
                (mobile, tile) => !(mobile.CanFall && tile.CanBeFellInto)
                );

            Width = statics.GetWidth();
            Height = statics.GetHeight();

            behaviourModel.SetupBehaviours(Mobiles, Statics ,_gameMessageReactor);

            _messageHandler = new MessageHandler(Mobiles, Statics);

            InputBuffer.Subscribe(GameConstants.PLAYER, _playerInputQueue);
            InputBuffer.Subscribe(GameConstants.GAME, _gameInputQueue);

            _playerInputQueue.MessageRecieved += ResolvePlayerInput;
            _gameInputQueue.MessageRecieved += ResolveGameMessage;
        }
        public void CanResolveMessage()
        {
            var loader = new TileLoader();
            var mobiles = loader.Load(_level, MobilesMap.TileBuilder);
            var messageHandler = new MessageHandler(mobiles, null);

            var player = mobiles.WithName("player");

            var previousPlayerLocation = player.Location;

            var message = new Message{
                Name = "player",
                Type = "movement",
                From = player.Location.ToString(),
                To = new Point(player.Location.X - 1, player.Location.Y).ToString()
            };

            messageHandler.Resolve(message);

            Assert.AreNotEqual(previousPlayerLocation,player.Location);
            Assert.AreEqual(previousPlayerLocation.X - 1, player.Location.X);

            messageHandler.Resolve(message.Invert());
            Assert.AreEqual(previousPlayerLocation, player.Location);
        }