Example #1
0
 /// <summary>
 /// Initializes a new instance of the ChasingMovement class.
 /// </summary>
 /// <param name="unit">The owner of the generator instance.</param>
 public ChasingMovement(Unit unit)
     : base(unit)
 {
     this.pathFindingTimer = PATHFINDING_TIME;
     this.state = MotionStates.None;
     this.victim = World.PlayForm.Player;
     this.pathFinder = new PathFinder(Map.Instance.GetCell(this.mover.Position.Location),
         Map.Instance.GetCell(this.victim.Position.Location));
 }
Example #2
0
        public override void Collided(PhysicsObject collider, Edge collidingEdge, Vector2 projectionVector, float deltaTime)
        {
            if (!IsActive)
            {
                return;
            }

            if (collider is CharacterObject)
            {
                return;
            }

            if (IsEthereal || collider.IsEthereal)
            {
                return;
            }

            _projectionVector = projectionVector;
            // After collision project out of colliding surface.

            Position -= projectionVector;

            if (projectionVector.Length() == 0.0f)  // Return when no projection.
            {
                return;
            }
            // Split reflection velocity into friction and bounce vector.
            var surfaceNorm = Vector2.Normalize(projectionVector);
            var surfaceDir  = new Vector2(surfaceNorm.Y, -surfaceNorm.X);

            _surfaceNormal = surfaceNorm;

            Vector2 bounce   = surfaceNorm * Vector2.Dot(Velocity, surfaceNorm) * Restitution * -1;
            Vector2 friction = surfaceDir * Vector2.Dot(Velocity, surfaceDir) * (1 - Friction);

            if (Vector2.Dot(surfaceNorm, new Vector2(0, 1)) > 0.3f && bounce.Length() < 0.5f)
            {
                MotionState = MotionStates.MS_LANDED;
                Velocity    = friction;
            }
            else
            {
                _bounceVector   = bounce;
                _frictionVector = friction;

                // Add up to reflection vector.
                _reflectionVector = bounce + friction;
                Velocity          = _reflectionVector;
            }
        }
Example #3
0
 private void Awake()
 {
     MotionState = MotionStates.Default;
     IsTalking   = false;
     IsCarrying  = false;
 }
Example #4
0
    void Update()
    {
        if(networkView.isMine){
            inputAxis1 = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
            inputAxis2 = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

            UpadateMotionState();

            transform.Rotate(0, inputAxis2.x * mouseXSpeed, 0);
            lookAngle -= inputAxis2.y * mouseYSpeed;
            lookAngle = Utils.ClampAngle(lookAngle, -35.0f, 35.0f);

            //activeWeaponID = activeWeapon != null ? activeWeapon.info.id : 0;

            if(activeWeaponController && !IsAnimationEnabled("BLEND_Recoil_" + activeWeaponController.info.type) &&
                                      !IsAnimationEnabled("BLEND_Reload_" + activeWeaponController.info.type) &&
                                              weaponDD.ammoInClip != activeWeaponController.info.clipSize && weaponDD.hasAmmo > 0){
                if(Input.GetKeyDown(KeyCode.R) ||  weaponDD.ammoInClip<=0){
                    int needBullets = activeWeaponController.info.clipSize - weaponDD.ammoInClip;
                    if(weaponDD.hasAmmo >= needBullets){
                        weaponDD.ammoInClip += needBullets;
                        //hasAmmo[weaponIndex] -= needBullets;
                    }
                    else{
                        weaponDD.ammoInClip += weaponDD.hasAmmo;
                        weaponDD.hasAmmo = 0;
                    }

                    ReloadAnimation();// RPC
                }
            }

            if(pickUp && Input.GetKeyDown(cInput.KEY_USE)){
                if(activeWeaponController){
                    if(activeWeaponController.info != pickUp.info){
                        credits -= pickUp.weaponCost;
                        SetWeapon(pickUp.info, pickUp.ammoToAdd);
                    }
                    else{
                        credits -= pickUp.ammoCost;
                        weaponDD.hasAmmo += 100;
                    }
                }
                else{
                    credits -= pickUp.weaponCost;
                    SetWeapon(pickUp.info, pickUp.ammoToAdd);
                }
            }
             // && credits >= pickUp.weaponCost
            if(Input.GetKeyDown(KeyCode.H)){
                ApplyDamage(Random.Range(10, 30));
            }

            if(activeWeaponController && Input.GetKeyDown(cInput.KEY_DROP) && !IsAnimationEnabled("BLEND_Reload_" + activeWeaponController.info.type)){
                RemoveWeapon();
            }

            /*
            if(Input.GetKeyDown(KeyCode.Q) && weaponIDs.Count > 1 && !IsAnimationEnabled("BLEND_Reload_" + activeWeapon.info.type)){
                aiming = false;
                if(weaponIndex < weaponIDs.Count - 1)
                    weaponIndex++;
                else
                    weaponIndex = 0;
                SelectWeapon(weaponIndex);
            }
            */

            if(Input.GetMouseButtonDown(1) && activeWeaponController)
                aiming = true;

            if(Input.GetMouseButtonUp(1) || !activeWeaponController)
                aiming = false;

            if(Input.GetMouseButton(0) && aiming && !IsAnimationEnabled("BLEND_Reload_" + activeWeaponController.info.type)){
                if(Time.time <= activeWeaponController.rateTime || weaponDD.ammoInClip <= 0)
                    return;
                weaponDD.ammoInClip--;

                RecoilAnimation();// RPC
                activeWeaponController.Attack();
            }
        }
        /*
        if (lastActiveWeaponID != activeWeaponID){
            lastActiveWeaponID = activeWeaponID;
            ApplyAnimationState();
        }
        */

        if(lastMotionState != motionState){
            lastMotionState = motionState;
            ApplyAnimationState();
        }
    }
Example #5
0
    void UpadateMotionState()
    {
        if(_grounded){
            if(inputAxis1.y == 0 && inputAxis1.x == 0)
                motionState = aiming ? MotionStates.Idle_Aim : MotionStates.Idle_Simple;

            if(inputAxis1.y == 0 && inputAxis1.x > 0)
                motionState = aiming ? MotionStates.Walk_Right : MotionStates.Run_Right;

            if(inputAxis1.y == 0 && inputAxis1.x < 0)
                motionState = aiming ? MotionStates.Walk_Left : MotionStates.Run_Left;

            if(inputAxis1.y > 0 && inputAxis1.x == 0)
                motionState = aiming ? MotionStates.Walk_Forward : MotionStates.Run_Forward;

            if(inputAxis1.y < 0 && inputAxis1.x == 0)
                motionState = aiming ? MotionStates.Walk_Back : MotionStates.Run_Back;

            if(inputAxis1.y > 0 && inputAxis1.x > 0)
                motionState = aiming ? MotionStates.Walk_Forward_Right : MotionStates.Run_Forward_Right;

            if(inputAxis1.y < 0 && inputAxis1.x < 0)
                motionState = aiming ? MotionStates.Walk_Back_Left : MotionStates.Run_Back_Left;

            if(inputAxis1.y < 0 && inputAxis1.x > 0)
                motionState = aiming ? MotionStates.Walk_Back_Right : MotionStates.Run_Back_Right;

            if(inputAxis1.y > 0 && inputAxis1.x < 0)
                motionState = aiming ? MotionStates.Walk_Forward_Left : MotionStates.Run_Forward_Left;
            }
        else{
            motionState = MotionStates.Jump;
        }
    }
Example #6
0
 void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
 {
     Vector3 t_pos = Vector3.zero;
     Quaternion t_rot = Quaternion.identity;
     float t_la = 0.0f;
     int t_ms = 0;
     //int t_wi = 0;
     bool t_aiming = false;
     float t_hitPoints = 0;
     if (stream.isWriting) {
         t_pos = transform.position;
         t_rot = transform.rotation;
         t_la = lookAngle;
         t_ms = (int)motionState;
         //t_wi = activeWeaponID;
         t_aiming = aiming;
      	t_hitPoints = hitPoints;
         stream.Serialize(ref t_pos);
         stream.Serialize(ref t_rot);
         stream.Serialize(ref t_la);
         stream.Serialize(ref t_ms);
         //stream.Serialize(ref t_wi);
         stream.Serialize(ref t_aiming);
         stream.Serialize(ref t_hitPoints);
     }
     else {
         stream.Serialize(ref t_pos);
         stream.Serialize(ref t_rot);
         stream.Serialize(ref t_la);
         stream.Serialize(ref t_ms);
         //stream.Serialize(ref t_wi);
         stream.Serialize(ref t_aiming);
         stream.Serialize(ref t_hitPoints);
         transform.position = t_pos;
         transform.rotation = t_rot;
         lookAngle = t_la;
         motionState = (MotionStates)t_ms;
         //activeWeaponID = t_wi;
         aiming = t_aiming;
         hitPoints = t_hitPoints;
     }
 }
Example #7
0
 public override void StartMotion()
 {
     this.state = MotionStates.StandingBy;
     base.StartMotion();
 }
Example #8
0
        private void FindPath()
        {
            // when there is no victim unit
            if (this.victim == null)
                return;

            Cell currentCell = Map.Instance.GetCell(mover.Position.Location);

            // Stop pursuit
            // when victim is not available
            if (!this.victim.IsAlive || this.victim.IsAtHome || !this.victim.IsVisible)
            {
                state = MotionStates.ReturningHome;
            }
            // otherwise stop motion when returning home
            // after a time the (pathFindingTimer) the pursuit of the victim will be continued
            else if (state == MotionStates.ReturningHome)
                state = MotionStates.StandingBy;

            bool isHome = state == MotionStates.ReturningHome;

            // Generate new path:
            // if ReturningHome - path to the respawn location
            // else - path to the victim location
            this.pathFinder.GeneratePath(currentCell,
                isHome ? Map.Instance.GetCell(mover.Home) : Map.Instance.GetCell(this.victim.Position.Location));

            // PathFinding failed or victim and mover are at the same location
            if (this.pathFinder.Path.Count == 0)
            {
                state = MotionStates.StandingBy;
            }
            else
            {
                state = isHome ? MotionStates.ReturningHome : MotionStates.Chasing;

                if (this.pathFinder.Path.Contains(currentCell))
                {
                    // Index number of the current Cell in Path
                    int index = this.pathFinder.Path.IndexOf(currentCell);

                    GridLocation nextGPS = mover.Position.Location;
                    Cell nextCell;

                    if (index > 0)
                    {
                        nextCell = this.pathFinder.Path[index - 1];

                        // Difine coords difference between current location and next one.
                        int shiftX = nextCell.Location.X - currentCell.Location.X;
                        int shiftY = nextCell.Location.Y - currentCell.Location.Y;

                        // shiftX or shiftY == 0 -> moving is unidirectional
                        // shiftX == -1 -> Left; == 1 -> Right;
                        // shiftY == -1 -> Up; == 1 -> Down;
                        switch (shiftX * shiftY)
                        {
                            case -1:
                                if (shiftX == -1)
                                    CurrentDirection = new Direction(Directions.Left, Directions.Down);
                                else
                                    CurrentDirection = new Direction(Directions.Right, Directions.Up);
                                break;
                            case 0:
                                switch (shiftX)
                                {
                                    case -1:
                                        CurrentDirection = new Direction(Directions.Left);
                                        break;
                                    case 0:
                                        if (shiftY == -1)
                                            CurrentDirection = new Direction(Directions.Up);
                                        else
                                            CurrentDirection = new Direction(Directions.Down);
                                        break;
                                    case 1:
                                        CurrentDirection = new Direction(Directions.Right);
                                        break;
                                }
                                break;
                            case 1:
                                if (shiftX == 1)
                                    CurrentDirection = new Direction(Directions.Right, Directions.Down);
                                else
                                    CurrentDirection = new Direction(Directions.Left, Directions.Up);
                                break;
                        }
                    }
                    else
                        // TODO: know why we are doing that code
                        nextCell = this.pathFinder.Path[index];

                    DefineNextGPS();
                }
            }
        }
Example #9
0
 //determine the current stage of movement (mostly for acceleration/decceleration smoothing)
 void getMovementStage()
 {
     //to accelerate into moving (with acceleration) or start moving constantly (no acceleration)
     if (isInMotionState == MotionStates.idle && isIndicatingMovement)
     {
         if (didAccelerrationEnable)
         {
             isInMotionState = MotionStates.accelerating;
         }
         else
         {
             isInMotionState = MotionStates.moving;
         }
         //cleanup
         timeAccelerating  = 0f;
         timeMoving        = 0f;
         timeDeccelerating = 0f;
     }
     //to transform from movement acceleration into to a constant pace
     else if (isInMotionState == MotionStates.accelerating && didAccelerrationEnable)
     {
         timeAccelerating += Time.deltaTime;
         if (timeAccelerating >= accelerateTime)
         {
             isInMotionState = MotionStates.moving;
         }
     }
     //to keep moving at a constant pace
     else if (isInMotionState == MotionStates.moving && isIndicatingMovement)
     {
         timeMoving += Time.deltaTime;
         //in case of limited movement time type, cut it short if movement time out
         if (motionType == MotionTypes.limitedTime && timeMoving >= motionLimitedTime)
         {
             if (didAccelerrationEnable)
             {
                 isInMotionState = MotionStates.deccelerating;
             }
             else
             {
                 isInMotionState = MotionStates.idle;
                 isFinishedWithCurrentMovement = true;
             }
         }
         else
         {
             //isInMotionState = MotionStates.moving; //keep going...
         }
     }
     //to deccelerate from moving (with accelecration), or to just stop (no acceleration)
     else if (isInMotionState == MotionStates.moving && !isIndicatingMovement)
     {
         if (didAccelerrationEnable)
         {
             isInMotionState = MotionStates.deccelerating;
         }
         else
         {
             isInMotionState = MotionStates.idle;
             isFinishedWithCurrentMovement = true;
         }
     }
     //to stop from decceleration to idle
     else if (isInMotionState == MotionStates.deccelerating && didAccelerrationEnable)
     {
         timeDeccelerating += Time.deltaTime;
         if (timeDeccelerating >= accelerateTime)
         {
             isInMotionState = MotionStates.idle;
             isFinishedWithCurrentMovement = true;
             //cleanup
             timeAccelerating  = 0f;
             timeMoving        = 0f;
             timeDeccelerating = 0f;
         }
     }
 }