Exemple #1
0
        public void FixedUpdate()
        {
            //Input and physics controls

            _doJump     = Input.GetAxisRaw(_verticalAxis) > 0;
            _goSideways = Input.GetAxisRaw(_horizontalAxis);
            _isWalking  = Math.Abs(_goSideways) > _moveThresh;

            float newVelocity = 0f;

            if (_isWalking &&
                (_goSideways > 0 || !_contactsPuller._hasLeftContact) &&
                (_goSideways < 0 || !_contactsPuller._hasRightContact))
            {
                newVelocity = (_goSideways < 0 ? -1 : 1) * Time.fixedDeltaTime * _view.Speed;
            }

            _view.Rigidbody2D.velocity = _view.Rigidbody2D.velocity.Change(x: newVelocity);

            if (_doJump && _contactsPuller._hasBottomContact && Math.Abs(_view.Rigidbody2D.velocity.y) < _jumpThresh)
            {
                _view.Rigidbody2D.AddForce(Vector2.up * _view.JumpForce, ForceMode2D.Impulse);
            }

            //Animation control

            AnimTrack animationTrack;

            if (_goSideways < 0)
            {
                _view.SpriteRenderer.flipX = true;
            }
            else if (_goSideways > 0)
            {
                _view.SpriteRenderer.flipX = false;
            }

            if (_contactsPuller._hasBottomContact)
            {
                animationTrack = _goSideways == 0 ? AnimTrack.Idle : AnimTrack.Run;
            }
            else
            {
                animationTrack = AnimTrack.Jump;
            }
            _spriteAnimator.StartAnimation(_view.SpriteRenderer, animationTrack, true);
        }
Exemple #2
0
        private void Awake()
        {
            _playerAnimatorConfig = Resources.Load <SpriteAnimatorConfig>("PlayerAnimatorConfig");
            _gumboAnimatorConfig  = Resources.Load <SpriteAnimatorConfig>("GumboAnimatorConfig");
            _boxAnimatorConfig    = Resources.Load <SpriteAnimatorConfig>("BoxAnimatorConfig");
            _coinAnimatorConfig   = Resources.Load <SpriteAnimatorConfig>("CoinAnimatorConfig");
            _UIAnimatorConfig     = Resources.Load <UIAnimatorConfig>("UISpriteAnimatorConfig");

            _playerAnimator = new SpriteAnimator(_playerAnimatorConfig);
            _gumboAnimator  = new SpriteAnimator(_gumboAnimatorConfig);
            _boxAnimator    = new SpriteAnimator(_boxAnimatorConfig);
            _coinAnimator   = new SpriteAnimator(_coinAnimatorConfig);
            _UIAnimator     = new SpriteAnimator(_UIAnimatorConfig);

            _playerContactsPuller     = new ContactsPuller(_playerView.Collider2D);
            _playerController         = new PlayerController(_playerView, _playerAnimator, _playerContactsPuller);
            _gunController            = new Gun(_gunView, _bulletView, _playerView);
            _flagController           = new Flag(_playerView, _flagView);
            _UIController             = new UIController(_UIView, _UIAnimator, _UIAnimatorConfig);
            _levelGeneratorController = new LevelGeneratorController(_levelGeneratorView);

            ///
            _levelGeneratorController.Awake();
            ///

            foreach (Transform gumbo in _gumbos.GetComponentInChildren <Transform>())
            {
                _gumboAnimator.StartAnimation(gumbo.gameObject.GetComponent <SpriteRenderer>(), AnimTrack.Run, true, 5);
            }

            foreach (Transform box in _boxes.GetComponentInChildren <Transform>())
            {
                _boxAnimator.StartAnimation(box.gameObject.GetComponent <SpriteRenderer>(), AnimTrack.Idle, true);
            }

            _coinsView = new List <LevelObjectView>(_coins.transform.childCount);
            foreach (Transform coin in _coins.GetComponentInChildren <Transform>())
            {
                _coinsView.Add(coin.gameObject.GetComponent <LevelObjectView>());
                _coinAnimator.StartAnimation(coin.gameObject.GetComponent <SpriteRenderer>(), AnimTrack.Idle, true, 5);
            }
            _coinController = new Coin(_playerView, _coinsView, _coinAnimator, _UIController);
        }