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