Ejemplo n.º 1
0
    public void ApplayVelocity(Vector2 velocity)
    {
        var newVelocity = velocity;

        if (IsCollisionTerrain)
        {
            newVelocity = Collision.ChackVelocity(position, velocity, itemWidth, itemHeight);
        }
        position += newVelocity;
    }
Ejemplo n.º 2
0
    public void Update()
    {
        if (Sleep || Collision == null)
        {
            return;
        }

        /*
         * if (trace) {
         *  position = oldPos;
         *  velocity = oldVel;
         *  trace = false;
         *  return;
         * }*/

        var offset = TileDataProvider.WorldPosToOffsetTile(position);
        //GetBottomPos

        bool isWater      = MapGenerator.GetTile(offset).IsWater2();
        var  offsetBottom = TileDataProvider.WorldPosToOffsetTile(GetBottomPos());
        bool isStairs     = MapGenerator.GetTile(offsetBottom).IsStairs();



        float d = isWater ? 2 : 1;

        oldVel = velocity;

        //Эффект поплавка например не тонуть  в воде
        //d = gravity.y;

        if (isWater && velocity.y >= -5)
        {
            velocity += gravity / d;
        }
        else
        {
            if (!isWater)
            {
                velocity += gravity / d;
            }

            if (isWater && velocity.y < -5)
            {
                velocity = new Vector2(velocity.x, -5);
            }
        }

        velocity = new Vector2(velocity.x * momentVector.x, velocity.y * momentVector.y);

        if (isStairs)
        {
            bool isStairsCenter = MapGenerator.GetTile(offset).IsStairs();

            if (!isStairsCenter && velocity.y < 0)
            {
                velocity = new Vector2(velocity.x, 0);
            }
            if (isStairsCenter)
            {
                velocity = new Vector2(velocity.x, 0);
            }
        }

        //урезаем
        oldVelocity = velocity;


        var newVelocity = velocity;

        if (IsCollisionTerrain)
        {
            newVelocity = Collision.ChackVelocity(position, velocity, itemWidth, itemHeight);
        }

        if (velocity != newVelocity)
        {
            OnTerrainCollision.TryCall();
        }

        velocity  = newVelocity;
        oldPos    = position;
        position += velocity;
        if (ReboundFactor != 0)
        {
            if (Math.Abs(oldVelocity.x - velocity.x) > 0.1f)
            {
                velocity = new Vector2(-oldVelocity.x * ReboundFactor, velocity.y);
            }

            if (Math.Abs(oldVelocity.y - velocity.y) > 0.1f)
            {
                velocity = new Vector2(velocity.x, -oldVelocity.y * ReboundFactor);
            }
        }

        PhysicsManager.ChackRect(this);

        if (Vector2.Distance(oldPos, position) == 0)
        {
            countTrySleep++;
            if (countTrySleep > 10)
            {
                Sleep = true;
            }
        }
        else
        {
            countTrySleep = 0;
        }

        /*
         * var offset = TileDataProvider.WorldPosToOffsetTile(position);
         * if (!MapGenerator.GetMap()[offset.x, offset.y].IsEmpty()) {
         *  if (View.name.IndexOf("block") != -1) {
         *      Debug.LogError("type != 0" + View.name + "oldPos = " + oldPos + " position = " + position);
         *      trace = true;
         *  }
         * }*/
    }