Exemple #1
0
        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>();

            _personSoundContentStorage = serviceScope.GetRequiredService <IPersonSoundContentStorage>();

            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
            };

            _gameObjectsViewModel = new GameObjectsViewModel(gameObjectParams);

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

            var commandPool  = serviceScope.GetRequiredService <ICommandPool>();
            var commandInput =
                new CommandInput(_uiState, commandPool, _camera, Sector, _viewModelContext, commandFactory);

            _commandInput = commandInput;
        }
        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)
            };
        }