public void Use()
        {
            if (_physics == null)
            {
                throw new ArgumentNullException("ExorciseAbility needs a physics component");
            }

            // Player should always face cursor when using an ability.
            if (GameObject == SceneManagement.SceneInfo.Player)
            {
                _physics.FaceCursor();
            }

            if (!_isMovementLocked)
            {
                SendMessage("LockMovement");
                _isMovementLocked = true;
            }

            float rotation = _physics.ViewRotation;

            var distance = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * Range;

            _start = GameObject.Position + new Vector2(Math.Abs(Offset.X) * _physics.Facing, Offset.Y);
            Vector2 end = GameObject.Position + _physics.ViewPoint + distance;

            _isCasting      = true;
            _collisionPoint = end;

            // Perform raycast and add damage.
            var world = GameObject.Scene.WorldInfo;

            world.RayCast((po, p, n) =>
            {
                _collisionPoint = ConvertUnits.ToDisplayUnits(p);
                if (po.GameObject != null)
                {
                    po.GameObject.SendMessage("TakeDamage", new object[] { Damage, _damageType });
                }
                return(false);
            }, ConvertUnits.ToSimUnits(_start), ConvertUnits.ToSimUnits(end));
        }
Exemple #2
0
        public void Use()
        {
            if (!IsActive)
            {
                return;
            }

            if (_physics == null)
            {
                throw new ArgumentNullException("PossessionAbility needs a physics component");
            }

            SendMessage("LockMovement");

            // Player should always face cursor when using an ability.
            if (GameObject == SceneManagement.SceneInfo.Player)
            {
                _physics.FaceCursor();
            }

            float rotation = _physics.ViewRotation;

            var     distance = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * Range;
            Vector2 start    = GameObject.Position + new Vector2(Math.Abs(Offset.X) * _physics.Facing, Offset.Y);
            Vector2 end      = GameObject.Position + _physics.ViewPoint + distance;

            _isCasting      = true;
            _start          = start;
            _collisionPoint = end;
            _progress       = 0.0f;

            // Perform raycast and add damage.
            var world = GameObject.Scene.WorldInfo;

            world.RayCast((po, p, n) =>
            {
                _collisionPoint = ConvertUnits.ToDisplayUnits(p);
                if (po.GameObject != null)
                {
                    GameObject victim       = po.GameObject;
                    Possessable possessInfo = victim.GetComponent <Possessable>();

                    if (possessInfo != null)
                    {
                        if (victim != _victim)
                        {
                            _timeElapsed = 0;
                            _victim      = victim;
                        }

                        if (_timeElapsed >= possessInfo.PossessTime)
                        {
                            _timeElapsed = 0.0f;
                            _progress    = 1f;
                            SendMessage("UnlockMovement");

                            // Deactivate player.
                            GameObject.IsActive = false;
                            GameObject.SendMessage("SetCharacterPhysicsActive", new object[] { false });

                            // Toggle animations.
                            _victim.GetComponents <AnimationComponent>().ForEach((c) => { c.IsActive = !c.IsActive; });

                            // Activate life drain.
                            _victim.GetComponent <LifeDrainAbility>().IsActive = true;

                            // Set player controller to victim to gain control.
                            _victim.SendMessage("DeactivateAI");
                            _victim.SendMessage("DeactivateSensitivity");
                            _victim.SendMessage("SetDrainer", new object[] { GameObject });
                            _victim.MoveComponent(GetComponent <PlayerInput>());
                            _victim.MoveComponent(GameObject.GetComponent <Camera>());
                            _victim.SendMessage("BlockPrimary");
                            possessInfo.IsActive = false;
                        }
                        else
                        {
                            _timeElapsed += Time.DeltaTime;
                            _progress     = _timeElapsed / possessInfo.PossessTime;
                        }
                    }
                    else
                    {
                        _victim      = null;
                        _timeElapsed = 0;
                    }
                }
                else
                {
                    _victim      = null;
                    _timeElapsed = 0;
                }
                return(false);
            }, ConvertUnits.ToSimUnits(start), ConvertUnits.ToSimUnits(end));
        }