public void InputManagerOnMouseDrag(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // When the mouse is dragged, we want to adjust the offset to move the rendered map/stuff.
                if (_lastMouseLocationDrag.X != 0)
                {
                    int dx = e.Location.X - _lastMouseLocationDrag.X;
                    int dy = e.Location.Y - _lastMouseLocationDrag.Y;

                    _bus.Publish(new MessageMouseDragged
                    {
                        ChangeX = dx,
                        ChangeY = dy
                    });
                }
                _lastMouseLocationDrag = e.Location;
            }
        }
 private void InitGame()
 {
     _applicationLogger.LogInfo("Initializing...");
     _phaseManager.Init();
     _bus.Publish(new MessageWindowResized(DisplayRectangle));
 }
        public void KeyReleased(Keys key)
        {
            _keyStates[key] = false;

            _bus.Publish(new KeyReleased(key));
        }
Exemple #4
0
        public GameLevel(
            GameLevelSettings settings,
            InputManager inputManager,
            TowerFactory towerFactory,
            GraphicsTracker graphicsTracker,
            MouseDragControl mouseDragControl,
            FontsAndColors fontsAndColors,
            GameBus bus,
            GameMapOverlay gameMapOverlay,
            ApplicationLogger logger,
            EnemyFactory enemyFactory)
        {
            _settings           = settings;
            _towerFactory       = towerFactory;
            _graphicsTracker    = graphicsTracker;
            _fontsAndColors     = fontsAndColors;
            _gameMapOverlay     = gameMapOverlay;
            _logger             = logger;
            _enemyFactory       = enemyFactory;
            _stringFormatCenter = new StringFormat
            {
                LineAlignment = StringAlignment.Center,
                Alignment     = StringAlignment.Center
            };
            _gameMapOverlay.SetMap(settings.Map);

            inputManager.OnMouseDragged  += mouseDragControl.InputManagerOnMouseDrag;
            inputManager.OnMouseReleased += mouseDragControl.InputManagerOnMouseRelease;
            _enemySpawner = new EnemySpawner(_time, settings.SpawnFrequency);

            bus.Subscribe <EnemyReachedGoal>(message =>
            {
                if (IsVisible)
                {
                    _gameState = GameState.Lost;
                }
            });

            bus.Subscribe <GameStateChange>(message =>
            {
                if (IsVisible && message.GameLevel == this)
                {
                    _gameState = message.GameState;
                }
            });

            bus.Subscribe <EnemyDespawned>(message =>
            {
                if (IsVisible)
                {
                    CurrentEnemiesNew.Remove(message.Enemy);
                    if (CurrentEnemiesNew.Count == 0 && _monstersLeftToSpawn.Count == 0)
                    {
                        _gameState = GameState.Won;
                    }
                }
            });

            bus.Subscribe <MouseClicked>(message =>
            {
                if (IsVisible && message.EventArgs.Button == MouseButtons.Left)
                {
                    if (_towerBeingPlaced != null)
                    {
                        // There is a tower being placed and mouse was clicked => place it.
                        PlaceTower(_towerBeingPlaced);
                    }
                }
            });

            bus.Subscribe <KeyReleased>(message =>
            {
                if (IsVisible)
                {
                    if (_gameState == GameState.Won)
                    {
                        bus.Publish(new GameStateChange(GameState.Won, this));
                        CurrentEnemiesNew.Clear();
                        return;
                    }
                    if (_gameState == GameState.Lost)
                    {
                        bus.Publish(new GameStateChange(GameState.Lost, this));
                        CurrentEnemiesNew.Clear();
                        return;
                    }

                    switch (message.Key)
                    {
                    case Keys.Space:
                        TogglePause();
                        break;

                    case Keys.D1:
                        StartPlacingTower();
                        break;
                    }
                }
            });
        }
        public override void Update(TimeSpan timeDelta)
        {
            if (!IsVisible)
            {
                return;
            }

            if (!IsAlive)
            {
                AgonyPeriod -= timeDelta;
                if (AgonyPeriod.Ticks < 0)
                {
                    IsVisible = false;
                    _bus.Publish(new EnemyDespawned(this));
                }
                return;
            }

            if (_currentWaypoint == 0)
            {
                Point mapLocation = ActualLocationByMap(_waypoints[_currentWaypoint].X, _waypoints[_currentWaypoint].Y);
                _currentX = mapLocation.X;
                _currentY = mapLocation.Y;

                _currentWaypoint = 1;
            }
            else
            {
                if (_waypoints.Count <= _currentWaypoint)
                {
                    FoundPointG = true;
                    _bus.Publish(new EnemyReachedGoal(this));
                    return;
                }

                Point currentWaypointMap = _waypoints[_currentWaypoint];
                Point currentWaypoint    = ActualLocationByMap(currentWaypointMap.X, currentWaypointMap.Y);

                // From current position to the current waypoint.
                Vector2 toWaypoint = new Vector2(currentWaypoint.X - _currentX, currentWaypoint.Y - _currentY);

                SpriteWithDirectionsRenderer.ChangeDirection(toWaypoint);

                // Distance traveled.
                float traveled = (float)timeDelta.TotalSeconds * speed;

                // From current position to the position where we should be (position delta)
                Vector2 traveledRelative = Vector2.Lerp(Vector2.Zero, toWaypoint, traveled / toWaypoint.Length());

                _currentX += traveledRelative.X;
                _currentY += traveledRelative.Y;


                if (Math.Abs(toWaypoint.X) <= 1 && Math.Abs(toWaypoint.Y) <= 1)
                {
                    _currentWaypoint++;
                }
            }

            LocationCenter = new PointF(_currentX - SpriteWithDirectionsRenderer.Sprite.Location.Width / 2,
                                        _currentY - SpriteWithDirectionsRenderer.Sprite.Location.Height / 2);
        }