public NpcController(GameView inGameView, ActorFacing inFacing) : base(inGameView)
        {
            // --------------------------------------------------------------------------------
            // Initializing
            // --------------------------------------------------------------------------------
            _animator = inGameView.GetComponent <GameAnimator>() ??
                        throw new Exception($"GameView [{inGameView.name}] must attached GameAnimator component.");

            _collider = inGameView.GetComponent <HitBox>() ??
                        throw new Exception($"GameView [{inGameView.name}] must attached GameCollider component.");

            _view = inGameView as EnemyView ?? throw new Exception($"NpcController can only attached to EnemyView");

            _lastAction    = ActorActions.None;
            _currentAction = ActorActions.None;
            _facing        = inFacing;
            _directionX    = GetIntDirection();
            _view.SetDirectionX(_directionX);

            // --------------------------------------------------------------------------------
            // Add Ray Sensor to detect player
            // --------------------------------------------------------------------------------
            var rayLength = (_view.GetSize().x / 2) + _view.Profile.sightDistance;

            _sightRay                 = gameView.AddComponent(new RaySensor(gameView, GetDirection(), rayLength, true));
            _sightRay.onHitEvent     += inO => { hasEnemy = true; };
            _sightRay.onHitLostEvent += () => { hasEnemy = false; };
            _sightRay.SetOffset(new Vector2(0, (_view.GetSize().y - 7)));  //set the origin ray as middle center

            SystemFacade.NPC.AddComponent(this);
        }
示例#2
0
        private void Move(float inHorizontal, float inSpeed)
        {
            if (_collider == null || _currentAction == ActorActions.Shoot)
            {
                return;
            }
            if ((int)inHorizontal != 0)
            {
                // --------------------------------------------------------------------------------
                // player is on move : play the desired animation
                // --------------------------------------------------------------------------------
                if (!_playerInTheAir)
                {
                    PlayAnimation(ActorActions.Move);
                }
                _directionX = inHorizontal < 0 ? -1 : 1;
                _facing     = inHorizontal < 0 ? ActorFacing.Left : ActorFacing.Right;
                _levelColliderSensor.SetDirection(GetDirection());
                gameView.SetDirectionX(_directionX);
                horizontalMove = inHorizontal;
            }
            else
            {
                // --------------------------------------------------------------------------------
                // Player is idle : play the desired animation
                // --------------------------------------------------------------------------------
                horizontalMove = 0;
                if (!_playerInTheAir && _currentAction != ActorActions.Shoot)
                {
                    PlayAnimation(ActorActions.Idle);
                }
            }

            // --------------------------------------------------------------------------------
            // Translate target view
            // --------------------------------------------------------------------------------
            if (_isBlocked)
            {
                return;
            }
            gameView.position = gameView.transform.position;
            gameView.transform.Translate(new Vector3(horizontalMove * (Time.fixedDeltaTime + inSpeed), 0, 0));
        }
示例#3
0
        public ControlView(GameView inGameView, PlayerProfile inProfile) : base(inGameView)
        {
            // --------------------------------------------------------------------------------
            // Initializing
            // --------------------------------------------------------------------------------
            _collider = inGameView.GetComponent <HitBox>() ??
                        throw new NullReferenceException($"Missing Component HitBox");

            _animator = inGameView.GetComponent <GameAnimator>() ??
                        throw new NullReferenceException($"Missing Component GameAnimator");

            _view = inGameView as PlayerView ?? throw new Exception($"ControlView must be attached on PlayerView");

            _facing        = ActorFacing.Right;
            _directionX    = 1;
            profile        = inProfile;
            _lastAction    = ActorActions.None;
            _currentAction = ActorActions.None;
            _isQuickMove   = false;
            _jumpRequest   = false;

            // --------------------------------------------------------------------------------
            // Add Ray Sensor to detect is level collider
            // --------------------------------------------------------------------------------
            var rayChild  = _view.CreateChild("CollisionRay");
            var rayLength = (_view.GetSize().x / 2) + profile.colliderBlockDistance;

            _levelColliderSensor                 = rayChild.AddComponent(new RaySensor(rayChild, GetDirection(), rayLength, true));
            _levelColliderSensor.onHitEvent     += inO => { _isBlocked = true; };
            _levelColliderSensor.onHitLostEvent += () => { _isBlocked = false; };
            _levelColliderSensor.SetOffset(new Vector2(0, (_view.GetSize().y / 2))); //set the origin ray as middle center
            // --------------------------------------------------------------------------------
            // Add Ray Sensor to detect is grounded
            // --------------------------------------------------------------------------------
            var rayGround = gameView.AddComponent(new RaySensor(gameView, Vector2.down, 5f, true));

            rayGround.onHitEvent     += inO => { _playerInTheAir = false; };
            rayGround.onHitLostEvent += () => { _playerInTheAir = true; };
        }
 private int InvertDirection()
 {
     _directionX *= -1;
     _facing      = (ActorFacing)_directionX;
     return(_directionX);
 }