Beispiel #1
0
        private static ISelectableViewModel?GetViewModelByNode(SectorViewModelContext sectorViewModelContext,
                                                               ISelectableViewModel?currentSelectedViewModel,
                                                               HexNode?hoverNode)
        {
            if (hoverNode != null)
            {
                var actorsInThisNode =
                    sectorViewModelContext.GetActors().SingleOrDefault(x => ReferenceEquals(x.Node, hoverNode));
                if (actorsInThisNode is null)
                {
                    if (currentSelectedViewModel is null)
                    {
                        return(new NodeViewModel(hoverNode));
                    }

                    if (currentSelectedViewModel.Item != hoverNode)
                    {
                        return(new NodeViewModel(hoverNode));
                    }

                    return(currentSelectedViewModel);
                }

                var actorViewModel = sectorViewModelContext.GameObjects.OfType <IActorViewModel>()
                                     .SingleOrDefault(x => x.Actor == actorsInThisNode);

                return(actorViewModel);
            }

            return(null);
        }
        public SectorViewModel(Game game, Camera camera, SpriteBatch spriteBatch)
        {
            _camera      = camera;
            _spriteBatch = spriteBatch;

            var serviceScope = ((LivGame)game).ServiceProvider;

            _player  = serviceScope.GetRequiredService <IPlayer>();
            _uiState = serviceScope.GetRequiredService <ISectorUiState>();

            _intarectionBus = serviceScope.GetRequiredService <IActorInteractionBus>();

            _intarectionBus.NewEvent += IntarectionBus_NewEvent;

            var personVisualizationContentStorage =
                serviceScope.GetRequiredService <IPersonVisualizationContentStorage>();
            var personSoundContentStorage             = serviceScope.GetRequiredService <IPersonSoundContentStorage>();
            var gameObjectVisualizationContentStorage =
                serviceScope.GetRequiredService <IGameObjectVisualizationContentStorage>();

            var sector = GetPlayerSectorNode(_player).Sector;

            if (sector is null)
            {
                throw new InvalidOperationException();
            }

            Sector = sector;

            var playerActor = (from actor in Sector.ActorManager.Items
                               where actor.Person == _player.MainPerson
                               select actor).SingleOrDefault();

            _mapViewModel = new MapViewModel(game, _player, _uiState, Sector, spriteBatch);

            ViewModelContext = new SectorViewModelContext(sector);

            var gameObjectParams = new GameObjectParams
            {
                Game                                  = game,
                Camera                                = camera,
                UiState                               = _uiState,
                Player                                = _player,
                SpriteBatch                           = _spriteBatch,
                SectorViewModelContext                = ViewModelContext,
                PersonSoundStorage                    = personSoundContentStorage,
                PersonVisualizationContentStorage     = personVisualizationContentStorage,
                GameObjectVisualizationContentStorage = gameObjectVisualizationContentStorage
            };

            _gameObjectsViewModel = new GameObjectsViewModel(gameObjectParams);

            var commandFactory = new ServiceProviderCommandFactory(((LivGame)game).ServiceProvider);

            var commandPool      = serviceScope.GetRequiredService <ICommandPool>();
            var sectorInteractor =
                new SectorInteractor(_uiState, commandPool, _camera, Sector, ViewModelContext, commandFactory);

            _sectorInteractor = sectorInteractor;
        }
Beispiel #3
0
        public GameObjectsViewModel(GameObjectParams gameObjectParams)
        {
            _viewModelContext = gameObjectParams.SectorViewModelContext ??
                                throw new ArgumentException(
                                          $"{nameof(gameObjectParams.SectorViewModelContext)} is not defined.",
                                          nameof(gameObjectParams));
            _player = gameObjectParams.Player ??
                      throw new ArgumentException($"{nameof(gameObjectParams.Player)} is not defined.",
                                                  nameof(gameObjectParams));
            _camera = gameObjectParams.Camera ??
                      throw new ArgumentException($"{nameof(gameObjectParams.Camera)} is not defined.",
                                                  nameof(gameObjectParams));
            _game = gameObjectParams.Game ??
                    throw new ArgumentException($"{nameof(gameObjectParams.Game)} is not defined.",
                                                nameof(gameObjectParams));
            _spriteBatch = gameObjectParams.SpriteBatch ??
                           throw new ArgumentException($"{nameof(gameObjectParams.SpriteBatch)} is not defined.",
                                                       nameof(gameObjectParams));

            _uiState = gameObjectParams.UiState ?? throw new ArgumentException(
                                 $"{nameof(gameObjectParams.UiState)} is not defined.",
                                 nameof(gameObjectParams));

            foreach (var actor in _viewModelContext.Sector.ActorManager.Items)
            {
                var actorViewModel = new ActorViewModel(actor, gameObjectParams);

                if (actor.Person == _player.MainPerson)
                {
                    _uiState.ActiveActor = actorViewModel;
                }

                _viewModelContext.GameObjects.Add(actorViewModel);
            }

            foreach (var staticObject in _viewModelContext.Sector.StaticObjectManager.Items)
            {
                var staticObjectModel =
                    new StaticObjectViewModel(gameObjectParams.Game, staticObject, gameObjectParams.SpriteBatch);

                _viewModelContext.GameObjects.Add(staticObjectModel);
            }

            var sector = _viewModelContext.Sector;

            sector.ActorManager.Removed        += ActorManager_Removed;
            sector.StaticObjectManager.Added   += StaticObjectManager_Added;
            sector.StaticObjectManager.Removed += StaticObjectManager_Removed;
        }
Beispiel #4
0
 public CommandInput(
     ISectorUiState sectorUiState,
     ICommandPool commandPool,
     Camera camera,
     ISector sector,
     SectorViewModelContext sectorViewModelContext,
     ServiceProviderCommandFactory commandFactory)
 {
     _uiState                = sectorUiState;
     _commandPool            = commandPool;
     _camera                 = camera;
     _sector                 = sector;
     _sectorViewModelContext = sectorViewModelContext;
     _commandFactory         = commandFactory;
 }
        private void ProcessMouseCommands(SectorViewModelContext sectorViewModelContext)
        {
            var mouseState = Mouse.GetState();

            var inverseCameraTransform = Matrix.Invert(_camera.Transform);

            var mouseInWorld = Vector2.Transform(new Vector2(mouseState.X, mouseState.Y), inverseCameraTransform);

            var offsetMouseInWorld =
                HexHelper.ConvertWorldToOffset((int)mouseInWorld.X, (int)mouseInWorld.Y * 2,
                                               (int)(MapMetrics.UnitSize / 2));

            var map = _sector.Map;

            var hoverNodes = map.Nodes.OfType <HexNode>().Where(node => node.OffsetCoords == offsetMouseInWorld);
            var hoverNode  = hoverNodes.FirstOrDefault();

            _uiState.HoverViewModel =
                GetViewModelByNode(sectorViewModelContext, _uiState.HoverViewModel, hoverNode);

            if (!_leftMousePressed &&
                mouseState.LeftButton == ButtonState.Pressed &&
                _uiState.HoverViewModel != null &&
                _uiState.CanPlayerGivesCommand &&
                !BottomMenuPanel.MouseIsOver &&
                !PersonMarkersPanel.MouseIsOver)
            {
                _leftMousePressed = true;

                _uiState.SelectedViewModel = _uiState.HoverViewModel;
                var command = SelectCommandBySelectedViewModel(
                    _uiState.SelectedViewModel,
                    _commandFactory,
                    _uiState);

                if (command.CanExecute().IsSuccess)
                {
                    _commandPool.Push(command);
                }
            }

            if (_leftMousePressed && mouseState.LeftButton == ButtonState.Released)
            {
                _leftMousePressed = false;
            }
        }
Beispiel #6
0
        public void Update(SectorViewModelContext sectorViewModelContext)
        {
            if (!_uiState.CanPlayerGivesCommand)
            {
                return;
            }

            var wasHotKey = HandleHotKeys();

            if (wasHotKey)
            {
                return;
            }

            ProcessMouseCommands(sectorViewModelContext);

            ProcessKeyboardCommands(sectorViewModelContext);
        }
Beispiel #7
0
        private void ProcessKeyboardCommands(SectorViewModelContext sectorViewModelContext)
        {
            if (!_uiState.CanPlayerGivesCommand)
            {
                return;
            }

            var keyboardState = Keyboard.GetState();

            var map = _sector.Map;

            var actor = _uiState.ActiveActor?.Actor;

            if (actor is null)
            {
                return;
            }

            var actorNode = (HexNode)actor.Node;

            var currentCoords = actorNode.OffsetCoords;

            OffsetCoords?moveCoords = default;

            if (keyboardState.IsKeyDown(Keys.NumPad4))
            {
                moveCoords = currentCoords.Left();
            }

            if (keyboardState.IsKeyDown(Keys.NumPad1))
            {
                moveCoords = currentCoords.LeftDown();
            }

            if (keyboardState.IsKeyDown(Keys.NumPad7))
            {
                moveCoords = currentCoords.LeftUp();
            }

            if (keyboardState.IsKeyDown(Keys.NumPad6))
            {
                moveCoords = currentCoords.Right();
            }

            if (keyboardState.IsKeyDown(Keys.NumPad3))
            {
                moveCoords = currentCoords.RightDown();
            }

            if (keyboardState.IsKeyDown(Keys.NumPad9))
            {
                moveCoords = currentCoords.RightUp();
            }

            if (moveCoords == default)
            {
                return;
            }

            var moveNode = map.Nodes.OfType <HexNode>().FirstOrDefault(node => node.OffsetCoords == moveCoords);
            var nodeVm   = GetViewModelByNode(sectorViewModelContext, _uiState.HoverViewModel, moveNode);

            if (nodeVm is null)
            {
                return;
            }

            _uiState.HoverViewModel    = nodeVm;
            _uiState.SelectedViewModel = nodeVm;

            var command = SelectCommandBySelectedViewModel(
                nodeVm,
                _commandFactory,
                _uiState);

            if (command.CanExecute().IsSuccess)
            {
                _commandPool.Push(command);
            }
        }
        public ActorViewModel(
            IActor actor,
            GameObjectParams gameObjectParams)
        {
            Actor = actor;

            _game = gameObjectParams.Game ??
                    throw new ArgumentException($"{nameof(gameObjectParams.Game)} is not defined.",
                                                nameof(gameObjectParams));
            _sectorViewModelContext = gameObjectParams.SectorViewModelContext ??
                                      throw new ArgumentException(
                                                $"{nameof(gameObjectParams.SectorViewModelContext)} is not defined.",
                                                nameof(gameObjectParams));
            _personSoundStorage = gameObjectParams.PersonSoundStorage ??
                                  throw new ArgumentException(
                                            $"{nameof(gameObjectParams.PersonSoundStorage)} is not defined.",
                                            nameof(gameObjectParams));

            _gameObjectVisualizationContentStorage = gameObjectParams.GameObjectVisualizationContentStorage ??
                                                     throw new ArgumentException(
                                                               $"{nameof(gameObjectParams.GameObjectVisualizationContentStorage)} is not defined.",
                                                               nameof(gameObjectParams));

            _spriteBatch = gameObjectParams.SpriteBatch ??
                           throw new ArgumentException($"{nameof(gameObjectParams.SpriteBatch)} is not defined.",
                                                       nameof(gameObjectParams));

            if (gameObjectParams.PersonVisualizationContentStorage is null)
            {
                throw new ArgumentException(
                          $"{nameof(gameObjectParams.PersonVisualizationContentStorage)} is not defined.",
                          nameof(gameObjectParams));
            }

            var equipmentModule = Actor.Person.GetModuleSafe <IEquipmentModule>();

            var shadowTexture = _game.Content.Load <Texture2D>("Sprites/game-objects/simple-object-shadow");

            _rootSprite   = new SpriteContainer();
            _shadowSprite = new Sprite(shadowTexture)
            {
                Position = new Vector2(0, 0),
                Origin   = new Vector2(0.5f, 0.5f),
                Color    = new Color(Color.White, 0.5f)
            };

            _rootSprite.AddChild(_shadowSprite);

            var isHumanGraphics = Actor.Person is HumanPerson;

            if (isHumanGraphics)
            {
                if (equipmentModule is not null)
                {
                    var graphicsRoot = new HumanoidGraphics(equipmentModule,
                                                            gameObjectParams.PersonVisualizationContentStorage);

                    _rootSprite.AddChild(graphicsRoot);

                    _graphicsRoot = graphicsRoot;
                }
                else
                {
                    // There is no cases when human person hasn't equipment module.
                    // It can be empty module to show "naked" person in the future.
                    throw new InvalidOperationException("Person has no IEquipmentModule.");
                }
            }
            else
            {
                var             monsterPerson = Actor.Person as MonsterPerson;
                SpriteContainer?graphics      = null;
                switch (monsterPerson.Scheme.Sid)
                {
                case "predator":
                    graphics = new AnimalGraphics(gameObjectParams.PersonVisualizationContentStorage);
                    break;

                case "warthog":
                    graphics = new MonoGraphics("warthog", gameObjectParams.PersonVisualizationContentStorage);
                    break;

                case "predator-meat":
                    graphics = new AnimalGraphics(gameObjectParams.PersonVisualizationContentStorage);
                    break;

                case "skeleton":
                    graphics = new MonoGraphics("skeleton", gameObjectParams.PersonVisualizationContentStorage);
                    break;

                case "skeleton-equipment":
                    graphics = new MonoGraphics("skeleton-equipment",
                                                gameObjectParams.PersonVisualizationContentStorage);
                    break;

                case "gallbladder":
                    graphics = new MonoGraphics("gallbladder", gameObjectParams.PersonVisualizationContentStorage);
                    break;
                }

                _rootSprite.AddChild(graphics);

                _graphicsRoot = (IActorGraphics)graphics;
            }

            var hexSize = MapMetrics.UnitSize / 2;
            var playerActorWorldCoords = HexHelper.ConvertToWorld(((HexNode)Actor.Node).OffsetCoords);
            var newPosition            = new Vector2(
                (float)(playerActorWorldCoords[0] * hexSize * Math.Sqrt(3)),
                playerActorWorldCoords[1] * hexSize * 2 / 2
                );

            _rootSprite.Position = newPosition;

            Actor.Moved    += Actor_Moved;
            Actor.UsedProp += Actor_UsedProp;
            Actor.PropTransferPerformed        += Actor_PropTransferPerformed;
            Actor.BeginTransitionToOtherSector += Actor_BeginTransitionToOtherSector;
            if (Actor.Person.HasModule <IEquipmentModule>())
            {
                Actor.Person.GetModule <IEquipmentModule>().EquipmentChanged += PersonEquipmentModule_EquipmentChanged;
            }

            if (Actor.Person.HasModule <ISurvivalModule>())
            {
                Actor.Person.GetModule <ISurvivalModule>().Dead += PersonSurvivalModule_Dead;
            }

            _actorStateEngineList = new List <IActorStateEngine> {
                new ActorIdleEngine(_graphicsRoot.RootSprite)
            };
        }