Пример #1
0
    public void MovementUpdate()
    {
        lastOnGround = onGround;
        onGround     = checkIfGrounded();

        if (onGround && velocity.y <= 0)
        {
            velocity.y  = 0;
            velocity.x *= (1 - groundFriction);
        }
        else
        {
            velocity.x *= (1 - airFriction);
        }

        if (velocity.y > 0 && hittingCeiling())
        {
            movementControllerScript.Move(new Vector2(0, velocity.y));
            velocity.y = 0;
        }

        if (!onGround && lastOnGround)
        {
            foreach (GameObject go in movementControllerScript.collisionState.thingsIWasStandingOn)
            {
                MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
                if (mcs != null)
                {
                    velocity += mcs.amountMovedLastFrame;
                }
            }
        }
        velocity.y -= gravity;
    }
Пример #2
0
 // Use this for initialization
 void Start()
 {
     physicsScript            = GetComponent <PhysicsScript>();
     sprite                   = GetComponent <SpriteRenderer>();
     pusherScript             = GetComponent <PusherScript>();
     movementControllerScript = GetComponent <MovementControllerScript>();
     facing                   = 1;
 }
Пример #3
0
    // Use this for initialization

    void Start()
    {
        physicsScript            = GetComponent <PhysicsScript>();
        sprite                   = GetComponent <SpriteRenderer>();
        graberScript             = GetComponent <GraberScript>();
        movementControllerScript = GetComponent <MovementControllerScript>();
        inputScript              = GetComponent <KeyboardInputScript>();
        facing                   = 1;
    }
Пример #4
0
    public Vector2 MoveWithGameObjects(Vector2 delta, List <GameObject> attachedObjects)
    {
        gameObjectsToIgnoreCollisonsWith.AddRange(attachedObjects);
        Vector2 newDelta = new Vector2(delta.x, delta.y);

        if (delta.x != 0)
        {
            newDelta.x = CalculateMoveX(delta);
        }
        if (delta.y != 0)
        {
            newDelta.y = CalculateMoveY(delta);
        }

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            mcs.gameObjectsToIgnoreCollisonsWith.Add(gameObject);
            Vector2 objectDelta = new Vector2(0, 0);
            if (delta.x != 0)
            {
                objectDelta.x = mcs.CalculateMoveX(delta);
            }
            if (delta.y != 0)
            {
                objectDelta.y = mcs.CalculateMoveY(delta);
            }

            if (Mathf.Abs(objectDelta.x) < Mathf.Abs(newDelta.x))
            {
                newDelta.x = objectDelta.x;
            }
            if (Mathf.Abs(objectDelta.y) < Mathf.Abs(newDelta.y))
            {
                newDelta.y = objectDelta.y;
            }
        }

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            mcs.gameObjectsToIgnoreCollisonsWith.Remove(gameObject);
            go.transform.Translate(newDelta);
        }

        gameObjectsToIgnoreCollisonsWith = gameObjectsToIgnoreCollisonsWith.Except <GameObject>(attachedObjects).ToList <GameObject>();
        transform.Translate(newDelta);
        return(newDelta);
    }
    public Vector2 CalculateMoveWithGameObjects(Vector2 delta, List <GameObject> attachedObjects)
    {
        List <GameObject> tmpList = new List <GameObject>(gameObjectsToIgnoreCollisonsWith);

        gameObjectsToIgnoreCollisonsWith.AddRange(attachedObjects);
        Vector2 newDelta = new Vector2(delta.x, delta.y);

        if (delta.x != 0)
        {
            newDelta.x = CalculateMoveX(delta);
        }
        if (delta.y != 0)
        {
            newDelta.y = CalculateMoveY(delta);
        }

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs      = go.GetComponent <MovementControllerScript>();
            List <GameObject>        tmpList2 = new List <GameObject>(mcs.gameObjectsToIgnoreCollisonsWith);
            mcs.gameObjectsToIgnoreCollisonsWith.Add(gameObject);
            mcs.gameObjectsToIgnoreCollisonsWith.AddRange(attachedObjects);

            Vector2 objectDelta = new Vector2(0, 0);
            if (delta.x != 0)
            {
                objectDelta.x = mcs.CalculateMoveX(delta);
            }
            if (delta.y != 0)
            {
                objectDelta.y = mcs.CalculateMoveY(delta);
            }

            if (Mathf.Abs(objectDelta.x) < Mathf.Abs(newDelta.x))
            {
                newDelta.x = objectDelta.x;
            }
            if (Mathf.Abs(objectDelta.y) < Mathf.Abs(newDelta.y))
            {
                newDelta.y = objectDelta.y;
            }
            mcs.gameObjectsToIgnoreCollisonsWith = tmpList2;
        }

        gameObjectsToIgnoreCollisonsWith = tmpList;
        return(newDelta);
    }
Пример #6
0
    public GameObject GetFirstPushable(List <GameObject> gameObjects)
    {
        GameObject pushable = null;

        foreach (GameObject go in gameObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            if (mcs != null)
            {
                if (mcs.pushableLevel.x < pusherLevel)
                {
                    return(mcs.gameObject);
                }
            }
        }
        return(pushable);
    }
    public float CalculateMoveWithGameObjectsY(Vector2 delta, List <GameObject> attachedObjects)
    {
        float distance = CalculateMoveY(delta, attachedObjects);

        List <GameObject> objectsToIgnore = new List <GameObject>();

        objectsToIgnore.Add(gameObject);

        foreach (GameObject go in attachedObjects)
        {
            MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
            if (mcs != null)
            {
                float testDistance = mcs.CalculateMoveY(delta, objectsToIgnore);
                if (Mathf.Abs(testDistance) < Mathf.Abs(distance))
                {
                    distance = testDistance;
                }
            }
        }
        return(distance * Mathf.Sign(delta.y));
    }
Пример #8
0
 public void StartPushing(List <GameObject> objects)
 {
     StopPushing();
     foreach (GameObject go in objects)
     {
         MovementControllerScript mcs = go.GetComponent <MovementControllerScript>();
         if (mcs != null)
         {
             if (mcs.basePushableLevel != -1)
             {
                 {
                     if (!movementControllerScript.attachedObjects.Contains(go))
                     {
                         thingIAmPushing = go;
                         mcs.collisionState.beingPushed = true;
                         movementControllerScript.attachedObjects.Add(go);
                         break;
                     }
                 }
             }
         }
     }
 }
Пример #9
0
 // Use this for initialization
 void Start()
 {
     collider = GetComponent <BoxCollider2D>();
     movementControllerScript = GetComponent <MovementControllerScript>();
     currentWaypointIndex     = 0;
 }
Пример #10
0
 // Use this for initialization
 void Start()
 {
     movementControllerScript = GetComponent <MovementControllerScript>();
 }
Пример #11
0
 // Use this for initialization
 void Start()
 {
     physicsScript            = GetComponent <PhysicsScript>();
     movementControllerScript = GetComponent <MovementControllerScript>();
 }
    public Vector2 Move(Vector2 delta, Vector2 sourcePusherLevel, int depthCount)
    {
        depthCount++;
        if (depthCount > 10)
        {
            return(new Vector2(0, 0));
        }
        else
        {
            //Move attempts to move in the x dimension and then the y
            //it only moves us as far as we can move, and returns however far that was

            float   direction        = 0;
            Vector2 amountLeftToMove = delta;
            Vector2 totalAmountMoved = new Vector2();

            List <RaycastHit2D> currentPushHits  = new List <RaycastHit2D>();
            List <RaycastHit2D> currentCarryHits = new List <RaycastHit2D>();

            List <MovementControllerScript> hitThingsXmcs = new List <MovementControllerScript>();
            List <MovementControllerScript> hitThingsYmcs = new List <MovementControllerScript>();

            for (int i = 0; i < numberOfPassengerMovementPasses; i++)
            {
                Vector2 amountMoved = new Vector2(0, 0);

                if (Mathf.Abs(amountLeftToMove.x) > 0)
                {
                    direction       = Mathf.Sign(amountLeftToMove.x);
                    currentPushHits = HorizontalRaycastHits(MIN_DISTANCE * direction);
                    RaycastHit2D closestHit = GetClosestHit(currentPushHits);
                    if (closestHit.transform != null)
                    {
                        MovementControllerScript mcs = closestHit.transform.gameObject.GetComponent <MovementControllerScript>();
                        if (mcs != null)
                        {
                            bool          shouldGetPushed = (mcs.pushableLevel.x < sourcePusherLevel.x);
                            PhysicsScript physicsScript   = mcs.GetComponent <PhysicsScript>();
                            if (physicsScript != null)
                            {
                                if (physicsScript.onGround == false || physicsScript.velocity.y > 0)
                                //this is a convulted way of checking if I'm in the air and therefore should be pushed
                                {
                                    shouldGetPushed = true;
                                }
                            }
                            if (shouldGetPushed)
                            {
                                hitThingsXmcs.Add(mcs);
                                mcs.Move(new Vector2(amountLeftToMove.x, 0), sourcePusherLevel, depthCount);
                            }
                        }
                    }

                    amountMoved.x = CalculateMoveX(amountLeftToMove);
                    List <GameObject> carryObjects = new List <GameObject>();

                    currentCarryHits = VerticalRaycastHits(MIN_DISTANCE);
                    foreach (RaycastHit2D hit in currentCarryHits)
                    {
                        if (hit.transform != null)
                        {
                            if (!carryObjects.Contains(hit.transform.gameObject))
                            {
                                if (hit.transform.gameObject.GetComponent <PhysicsScript>() != null)
                                {
                                    carryObjects.Add(hit.transform.gameObject);
                                }
                            }
                        }
                    }
                    if (carryObjects.Count > 0 && amountMoved.x != 0)
                    {
                        List <GameObject> carryObjectsSorted = SortGameObjectsByPositionX(carryObjects);

                        int iStart;
                        int iDirection;
                        int iEnd;
                        if (amountMoved.x < 0)
                        {
                            iStart     = 0;
                            iDirection = 1;
                            iEnd       = carryObjectsSorted.Count;
                        }
                        else
                        {
                            iStart     = carryObjectsSorted.Count - 1;
                            iDirection = -1;
                            iEnd       = -1;
                        }
                        for (int j = iStart; j != iEnd; j += iDirection)
                        {
                            GameObject carryObject       = carryObjectsSorted[j];
                            MovementControllerScript mcs = carryObject.GetComponent <MovementControllerScript>();
                            if (mcs != null)
                            {
                                if (!mcs.beenCarriedThisFrame)
                                {
                                    mcs.beenCarriedThisFrame = true;
                                    mcs.Move(new Vector2(amountMoved.x, 0));
                                }
                            }
                        }
                    }
                }

                TranslateAndRecord(amountMoved);
                if (Mathf.Abs(amountLeftToMove.y) > 0)
                {
                    direction       = Mathf.Sign(amountLeftToMove.y);
                    currentPushHits = VerticalRaycastHits(MIN_DISTANCE * direction);

                    List <GameObject> hitObjects = new List <GameObject>();
                    foreach (RaycastHit2D hit in currentPushHits)
                    {
                        if (hit.transform != null)
                        {
                            if (!hitObjects.Contains(hit.transform.gameObject))
                            {
                                hitObjects.Add(hit.transform.gameObject);
                            }
                        }
                    }
                    List <GameObject> pushedObjects = new List <GameObject>();
                    foreach (GameObject hitObject in hitObjects)
                    {
                        MovementControllerScript hitMCS = hitObject.GetComponent <MovementControllerScript>();
                        if (hitMCS != null)
                        {
                            if (hitMCS.pushableLevel.y < sourcePusherLevel.y)
                            {
                                if (!pushedObjects.Contains(hitMCS.gameObject))
                                {
                                    pushedObjects.Add(hitMCS.gameObject);
                                }
                            }
                        }
                    }

                    float   minDistanceMoved = Mathf.Infinity;
                    float[] distancesMoved   = new float[pushedObjects.Count];

                    for (int j = 0; j < pushedObjects.Count; j++)
                    {
                        GameObject pushedObject         = pushedObjects[j];
                        MovementControllerScript hitMCS = pushedObject.GetComponent <MovementControllerScript>();
                        distancesMoved[j] = hitMCS.Move(new Vector2(0, amountLeftToMove.y), sourcePusherLevel, depthCount).y;

                        if (distancesMoved[j] < minDistanceMoved)
                        {
                            minDistanceMoved = distancesMoved[j];
                        }
                    }

                    for (int j = 0; j < pushedObjects.Count; j++)
                    {
                        GameObject pushedObject = pushedObjects[j];
                        if (distancesMoved[j] > minDistanceMoved)
                        {
                            MovementControllerScript mcs = pushedObject.GetComponent <MovementControllerScript>();
                            if (mcs != null)
                            {
                                float diff = distancesMoved[j] - minDistanceMoved;
                                mcs.Move(new Vector2(0, diff * direction * -1));
                            }
                        }
                    }

                    List <GameObject> carryObjects = new List <GameObject>();

                    if (direction < 0)
                    {
                        currentCarryHits = VerticalRaycastHits(MIN_DISTANCE);
                        foreach (RaycastHit2D hit in currentCarryHits)
                        {
                            if (hit.transform != null)
                            {
                                if (!carryObjects.Contains(hit.transform.gameObject))
                                {
                                    carryObjects.Add(hit.transform.gameObject);
                                }
                            }
                        }
                    }

                    amountMoved.y = CalculateMoveY(amountLeftToMove);
                    TranslateAndRecord(new Vector2(0, amountMoved.y));
                    if (direction < 0)
                    {
                        foreach (GameObject carryObject in carryObjects)
                        {
                            MovementControllerScript mcs           = carryObject.GetComponent <MovementControllerScript>();
                            PhysicsScript            physicsScript = carryObject.GetComponent <PhysicsScript>();
                            //TODO: this is not the right way to prevent things from simultaneously pulling objects down and being pushed by them
                            if (physicsScript != null)
                            {
                                if (mcs.pushableLevel.y <= pusherLevel.y)
                                {
                                    mcs.Move(new Vector2(0, amountMoved.y));
                                }
                            }
                        }
                    }
                }
                amountLeftToMove -= amountMoved;
                totalAmountMoved += amountMoved;
            }
            return(totalAmountMoved);
        }
    }
Пример #13
0
    public Vector2 Move(Vector2 delta)
    {
        Vector2 amountMoved;

        if (attachedObjects.Count > 0)
        {
            amountMoved = MoveWithGameObjects(delta, attachedObjects);
        }
        else
        {
            //Move attempts to move in the x dimension and then the y
            //it only moves us as far as we can move, and returns however far that was

            Vector2 newDelta = new Vector2(delta.x, delta.y);
            if (delta.x != 0)
            {
                float slopeAngle = getSlopeUpAngle(Mathf.Sign(delta.x));
                if (slopeAngle < maxAscentAngle)
                {
                    float slopeY = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(delta.x);
                    Move(new Vector2(0, slopeY));
                    Debug.Log("up is" + slopeAngle);
                    ascendingSlope = true;
                }

                else
                {
                    ascendingSlope = false;
                }


                UpdateCollisionState();
                newDelta.x = CalculateMoveX(delta);
            }
            transform.Translate(new Vector2(newDelta.x, 0));
            if (delta.y != 0)
            {
                float slopeAngle = getSlopeDownAngle(Mathf.Tan(delta.y));
                if (delta.y != 0)
                {
                    float slopeX = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * (delta.y);
                    Move(new Vector2(-slopeX, 0));
                    Debug.Log("down is" + slopeAngle);
                    decendingSlope = true;
                }
                else
                {
                    decendingSlope = false;
                }
                UpdateCollisionState();
                newDelta.y = CalculateMoveY(delta);
            }
            amountMoved = newDelta;
            transform.Translate(new Vector2(0, newDelta.y));
        }

        foreach (GameObject passenger in passengers)
        {
            MovementControllerScript mcs = passenger.GetComponent <MovementControllerScript>();
            if (mcs != null)
            {
                mcs.Move(amountMoved);
            }
        }
        return(amountMoved);
    }