Beispiel #1
0
        public static DrawableGameObject Create(GameWorld world, EnemyCreationParams param)
        {
            DrawableGameObject result = null;

            switch (param.Type)
            {
            case ENEMY_TP_SHOOTER:
                result = new ArcherEnemy(world, param.Position, Vector2.UnitX * param.Direction);
                break;

            case ENEMY_TP_CANNON:

                result = new CannonEnemy(
                    world,
                    param.Position,
                    new CannonEnemyParams
                {
                    Direction      = Vector2.UnitX * param.Direction,
                    DistanceOfShot = param.Force,
                    ThrowFrequency = TimeSpan.FromSeconds(param.Frequency)
                });
                break;

            case ENEMY_TP_ROTATE:
                result = new RotatingPost(world, param.Position, param.Speed.X);
                break;

            case ENEMY_TP_WANDERER:
            default:
                result = new WandererEnemy(world, param.Position, Vector2.UnitX * param.Direction);
                break;
            }

            return(result);
        }
Beispiel #2
0
        public void RemoveGameObject(GameObject gameObject)
        {
            allObjects.Remove(gameObject);

            DrawableGameObject drawableGameObject = gameObject as DrawableGameObject;

            if (drawableGameObject != null)
            {
                drawableObjects.Remove(drawableGameObject);
            }
        }
Beispiel #3
0
        public void AddGameObject(GameObject gameObject)
        {
            allObjects.Add(gameObject);

            DrawableGameObject drawableGameObject = gameObject as DrawableGameObject;

            if (drawableGameObject != null)
            {
                drawableObjects.Add(drawableGameObject);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Применение игровых событий к снимку игрового мира и
        /// перемешение его в будущее
        /// </summary>
        /// <param name="gameEvents">Новые игровые события</param>
        public void ApplyEvents(IEnumerable <AGameEvent> gameEvents)
        {
            foreach (var gameEvent in gameEvents)
            {
                #region моделирование вперед

                // моделируем мир ко времени GameEvent'а
                long elapsedTime = gameEvent.TimeStamp - Time;
                // Debug.Assert(elapsedTime >= 0);

                ComputeMovement(elapsedTime);
                Time = gameEvent.TimeStamp;

                #endregion

                #region применение произошедшего события

                if (gameEvent.Type == EventType.NewObjectEvent)
                {
                    Debug.Assert((gameEvent as NewObjectEvent) != null);
                    var serverGameObject = (gameEvent as NewObjectEvent).NewGameObject;

                    DrawableGameObject newDrawableGameObject = GameFactory.CreateClientGameObject(serverGameObject);
                    AddGameObject(newDrawableGameObject);

                    //					// todo починить
                    //					if (newDrawableGameObject.Is(AGameObject.EnumObjectType.Explosion))
                    //					{
                    //						_explosions.Add(newDrawableGameObject, DateTime.Now.Ticks / 10000);
                    //					}
                }
                else
                {
                    Debug.Assert(gameEvent.GameObjectId.HasValue);

                    DrawableGameObject drawableGameObject = _gameObjects[gameEvent.GameObjectId.Value];

                    gameEvent.UpdateMob(drawableGameObject);
                    if (drawableGameObject.IsActive == false)
                    {
                        //						todo починить
                        //						// if object is explosion, just ignore his deletion
                        //						if (drawableGameObject.Is(AGameObject.EnumObjectType.Explosion))
                        //						{
                        //							drawableGameObject.IsActive = true;
                        //						}
                        _gameObjects.Remove(drawableGameObject.Id);
                    }
                }

                #endregion
            }
        }
Beispiel #5
0
        public GameSnapshot(SynchroFrame serverSynchroFrame, GameLevel gameLevel)
        {
            _gameObjects = new Dictionary <Guid, DrawableGameObject>();
            foreach (AGameObject serverGameObject in serverSynchroFrame)
            {
                DrawableGameObject newDrawableGameObject = GameFactory.CreateClientGameObject(serverGameObject);
                _gameObjects.Add(newDrawableGameObject.Id, newDrawableGameObject);
            }

            _gameLevel = gameLevel;
            Time       = serverSynchroFrame.Time;
        }
Beispiel #6
0
        public DrawableGameObject[] ExtrapolateTo(long gameTime)
        {
            Trace.WriteLine("Extrapolation time = " + (gameTime - Time));

            var result = new DrawableGameObject[_gameObjects.Count];
            int index  = 0;

            foreach (DrawableGameObject gameObject in _gameObjects.Values)
            {
                DrawableGameObject futureObject = result[index] = new DrawableGameObject();
                futureObject.Copy(gameObject);
                futureObject.Coordinates = futureObject.ComputeMovement(gameTime - Time, _gameLevel);
                index++;
            }
            return(result);
        }
        /// <summary>
        /// Poistaa olion listasta ja allobjects listasta jos sitä ei poisteta heti alussa.
        /// </summary>
        private void RemoveFromLists <T>(IList list, T gameObject) where T : GameObject
        {
            list.Remove(gameObject);

            // Koska ylempi listä voi olla allobjects lista, katsotaan ollaanko siitä jo poistettu.
            if (list != allObjects)
            {
                allObjects.Remove(gameObject);
            }
            if (list != drawableObjects)
            {
                DrawableGameObject drawableObject = gameObject as DrawableGameObject;
                if (drawableObject != null)
                {
                    drawableObjects.Remove(drawableObject);
                }
            }
        }
        /// <summary>
        /// Lisää olion listaan ja allobjects listaan jos sitä ei lisätä heti alussa.
        /// </summary>
        private void AddToLists <T>(IList list, T gameObject) where T : GameObject
        {
            // Ei lisätä samaa olio viitettä uudestaan.
            if (list.Contains(gameObject))
            {
                return;
            }

            list.Add(gameObject);

            // Koska ylempi listä voi olla allobjects lista, katsotaan ollaanko siihen jo lisätyy.
            if (list != allObjects)
            {
                allObjects.Add(gameObject);
            }
            if (list != drawableObjects)
            {
                DrawableGameObject drawableObject = gameObject as DrawableGameObject;
                if (drawableObject != null)
                {
                    drawableObjects.Add(drawableObject);
                }
            }
        }
Beispiel #9
0
 private void AddGameObject(DrawableGameObject drawableGameObject)
 {
     Debug.Assert(!_gameObjects.ContainsKey(drawableGameObject.Id));
     _gameObjects.Add(drawableGameObject.Id, drawableGameObject);
 }
Beispiel #10
0
        public void HandleInput(Controller controller)
        {
            DrawableGameObject player = _gameModel.GetGameObject(MyId);

            // current RunVector
            Vector2?currentRunVector = controller.RunVector;

            if (currentRunVector.HasValue)
            {
                Move(currentRunVector.Value);
                player.RunVectorM = currentRunVector.Value;
            }

            // переключение оружия на цифры и колесико мыши(todo)
            if (controller is KeyboardAndMouse)
            {
                var keyboardAndMouse = controller as KeyboardAndMouse;

                bool weaponSwitched = false;

                foreach (var weapon in _weaponKeys)
                {
                    if (keyboardAndMouse.IsNewKeyPressed(weapon.Key))
                    {
                        _weaponIndex   = weapon.Key - Keys.D1;
                        weaponSwitched = true;
                    }
                }

                if ((DateTime.Now - _lastWeaponChanged).Milliseconds > 100)
                {
                    int mouseWheelValue = keyboardAndMouse.MouseWheelValue;

                    if (mouseWheelValue < 0)
                    {
                        _weaponIndex++;
                        if (_weaponIndex == _weaponKeys.Count)
                        {
                            _weaponIndex = 0;
                        }
                        weaponSwitched = true;
                    }

                    if (mouseWheelValue > 0)
                    {
                        _weaponIndex--;
                        if (_weaponIndex == -1)
                        {
                            _weaponIndex = _weaponKeys.Count - 1;
                        }
                        weaponSwitched = true;
                    }
                    _lastWeaponChanged = DateTime.Now;
                }

                if (weaponSwitched)
                {
                    ConnectionManager.Instance.ChangeWeapon(CurrentWeapon);
                }

                // todo remove this
                if (keyboardAndMouse.IsNewKeyPressed(Keys.D7))
                {
                    ConnectionManager.Instance.ChangeWeapon(WeaponType.MobGenerator);
                }
            }

            Vector2 mouseCoordinates = controller.SightPosition;

            player.ShootVectorM = mouseCoordinates - _gameModel.Camera2D.ConvertToLocal(player.CoordinatesM);
            if (player.ShootVector.Length() > 0)
            {
                player.ShootVector.Normalize();
            }

            if (controller.ShootButton == ButtonState.Pressed)
            {
                if ((DateTime.Now - _dateTime).Milliseconds > Constants.SHOOT_RATE)
                {
                    _dateTime = DateTime.Now;
                    Shoot(player.ShootVectorM);
                }
            }
        }