Exemplo n.º 1
0
 public override void OnTick(ConnectionBuffer buffer)
 {
     if (Active)
     {
         buffer.Add(new PlayerUpdatePacket(-1, Health, _sprite.Position, _playerController.Velocity, _playerController.Direction));
     }
 }
Exemplo n.º 2
0
        public override void OnUpdate(float deltatime, EntityManager entityManager, ConnectionBuffer buffer, RenderWindow window)
        {
            Direction previousDirection = _currentDirection;

            // Update the player controller
            _playerController.Position = _sprite.Position;
            _playerController.Update(window, deltatime, Camera);
            _sprite.Position = _playerController.Position;

            // Update the direction
            float angle = (float)-Math.Atan2(_playerController.Direction.Y, _playerController.Direction.X);

            while (angle < 0.0f)
            {
                angle += 2.0f * (float)Math.PI;
            }

            while (angle > 2.0f * (float)Math.PI)
            {
                angle -= 2.0f * (float)Math.PI;
            }

            _currentDirection = Direction.RIGHT;
            if (angle > Math.PI * 0.25f)
            {
                _currentDirection = Direction.UP;
            }
            if (angle > Math.PI * 0.75f)
            {
                _currentDirection = Direction.LEFT;
            }
            if (angle > Math.PI * 1.25f)
            {
                _currentDirection = Direction.DOWN;
            }
            if (angle > Math.PI * 1.75f)
            {
                _currentDirection = Direction.RIGHT;
            }

            float speed = (float)Math.Sqrt(_playerController.Velocity.X * _playerController.Velocity.X +
                                           _playerController.Velocity.Y * _playerController.Velocity.Y);

            // Update animation
            if (_playerController.Velocity.X == 0.0f && _playerController.Velocity.Y == 0.0f)
            {
                _animations[(int)_currentDirection].SetFrame(0.0f);
            }
            else
            {
                if (_currentDirection != previousDirection)
                {
                    _animations[(int)_currentDirection].SetFrame(0.0f);
                }
                _animations[(int)_currentDirection].Update(deltatime * speed * 2.0f);
            }

            // Shoot a bullet
            if (_playerController.ShotOrigin.HasValue)
            {
                entityManager.ShootBullet(true, -1,
                                          new Bullet.Bullet(_playerController.ShotOrigin.Value, _playerController.ShotDirection), 800.0f);

                buffer.Add(new PlayerShootPacket(-1, _playerController.ShotOrigin.Value, _playerController.ShotDirection));
            }

            _sprite.TextureRect = _animations[(int)_currentDirection].GetShape();

            if (_playerController.DeletionMark)
            {
                MarkForDeletion();
            }

            if (!Active)
            {
                if (_playerController.Health != Health)
                {
                    Hit();
                }

                Health = _playerController.Health;
            }
        }