示例#1
0
        public void squishHeightCenter(PositionComponent posComp, SquishComponent squishComp, float amt)
        {
            while (posComp.height - amt < squishComp.minHeight)
            {
                amt--;
            }

            level.getMovementSystem().changeHeight(posComp, posComp.height - amt);
        }
示例#2
0
        public void squishWidthCenter(PositionComponent posComp, SquishComponent squishComp, float amt)
        {
            while (posComp.width - amt < squishComp.minWidth)
            {
                amt--;
            }

            level.getMovementSystem().changeWidth(posComp, posComp.width - amt);
        }
示例#3
0
        public void squishRight(PositionComponent posComp, SquishComponent squishComp, float amt)
        {
            while (posComp.width - amt < squishComp.minWidth)
            {
                amt--;
            }

            level.getMovementSystem().changeWidth(posComp, posComp.width - amt);
            level.getMovementSystem().changePosition(posComp, posComp.x + amt / 2, posComp.y, true, false);
        }
示例#4
0
        public void squishUp(PositionComponent posComp, SquishComponent squishComp, float amt)
        {
            while (posComp.height - amt < squishComp.minHeight)
            {
                amt--;
            }


            level.getMovementSystem().changeHeight(posComp, posComp.height - amt);
            level.getMovementSystem().changePosition(posComp, posComp.x, posComp.y - amt / 2, false, true);
        }
示例#5
0
        //----------------------------------------------------------------------------------------------

        public void resizeEntityFromAcceleration(Entity e)
        {
            VelocityComponent velComp    = ( VelocityComponent )e.getComponent(GlobalVars.VELOCITY_COMPONENT_NAME);
            SquishComponent   squishComp = ( SquishComponent )e.getComponent(GlobalVars.SQUISH_COMPONENT_NAME);
            PositionComponent posComp    = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME);

            float xSpeed = velComp.x;
            float ySpeed = velComp.y;

            float xChange = Math.Abs(xSpeed) - Math.Abs(squishComp.prevXVelocity);
            float yChange = Math.Abs(ySpeed) - Math.Abs(squishComp.prevYVelocity);

            float xSquishAmount = (-xChange * squishComp.xDecSpeedStretchMultiplier);
            float ySquishAmount = (-yChange * squishComp.yDecSpeedStretchMultiplier);

            if (xChange > 0)
            {
                xSquishAmount = (-xChange * squishComp.xIncSpeedStretchMultiplier);
            }
            if (yChange > 0)
            {
                ySquishAmount = (-yChange * squishComp.yIncSpeedStretchMultiplier);
            }


            //---------------------------- RESTRICTION CALCULATIONS ----------------------
            float newWidth  = posComp.width - xSquishAmount;
            float newHeight = posComp.height - ySquishAmount;

            if (newWidth > squishComp.maxWidth)
            {
                newWidth = squishComp.maxWidth;
            }
            if (newHeight > squishComp.maxHeight)
            {
                newHeight = squishComp.maxHeight;
            }

            if (newWidth < squishComp.minWidth)
            {
                newWidth = squishComp.minWidth;
            }
            if (newHeight < squishComp.minHeight)
            {
                newHeight = squishComp.minHeight;
            }


            //Don't Go Over Max Surface Area
            if ((newWidth * newHeight) > squishComp.maxSurfaceArea)
            {
                //if only one has been changed, and the other is relatively normal - shrink the other
                if (Math.Abs(newWidth - posComp.width) < squishComp.xStretchThreshold)
                {
                    float mult = squishComp.maxSurfaceArea / (newWidth * newHeight);
                    newWidth = newWidth * mult;
                }
                else if (Math.Abs(newHeight - posComp.height) < squishComp.yStretchThreshold)
                {
                    float mult = squishComp.maxSurfaceArea / (newWidth * newHeight);
                    newHeight = newHeight * mult;
                }
                //If they've both been changed - take the ratio
                else
                {
                    float mult = squishComp.maxSurfaceArea / (newWidth * newHeight);
                    newWidth  = newWidth * ( float )Math.Sqrt(mult);
                    newHeight = newHeight * ( float )Math.Sqrt(mult);
                }
            }

            //Don't Go Under Min Surface Area
            if ((newWidth * newHeight) < squishComp.minSurfaceArea)
            {
                //if only one changed then expand the other
                if (Math.Abs(newWidth - posComp.width) < squishComp.xStretchThreshold)
                {
                    float mult = squishComp.minSurfaceArea / (newWidth * newHeight);
                    newWidth = newWidth * mult;
                }
                else if (Math.Abs(newHeight - posComp.height) < squishComp.yStretchThreshold)
                {
                    float mult = squishComp.minSurfaceArea / (newWidth * newHeight);
                    newHeight = newHeight * mult;
                }
                //If they've both been changed - take the ratio
                else
                {
                    float mult = squishComp.minSurfaceArea / (newWidth * newHeight);
                    newWidth  = newWidth * ( float )Math.Sqrt(mult);
                    newHeight = newHeight * ( float )Math.Sqrt(mult);
                }
            }

            //-------------------------------------------------------------------

            xSquishAmount = posComp.width - newWidth;
            ySquishAmount = posComp.height - newHeight;


            //if it going right, squish right.
            if (squishComp.prevXVelocity > 0 && Math.Abs(xSquishAmount) >= squishComp.xStretchThreshold)
            {
                squishRight(posComp, squishComp, xSquishAmount);
            }
            else if (squishComp.prevXVelocity < 0 && Math.Abs(xSquishAmount) >= squishComp.xStretchThreshold)
            {
                squishLeft(posComp, squishComp, xSquishAmount);
            }
            else if (Math.Abs(xSquishAmount) >= squishComp.xStretchThreshold)
            {
                squishWidthCenter(posComp, squishComp, xSquishAmount);
            }

            //If going down, squish down
            if (squishComp.prevYVelocity > 0 && Math.Abs(ySquishAmount) >= squishComp.yStretchThreshold)
            {
                squishDown(posComp, squishComp, ySquishAmount);
            }
            else if (velComp.y == 0 && squishComp.prevYVelocity == 0 && Math.Abs(ySquishAmount) >= squishComp.yStretchThreshold)
            {
                squishHeightCenter(posComp, squishComp, ySquishAmount);
            }
            else if (Math.Abs(ySquishAmount) >= squishComp.yStretchThreshold)       // && velComp.y < 0
            {
                squishDown(posComp, squishComp, ySquishAmount);
            }

            squishComp.prevXVelocity = xSpeed;
            squishComp.prevYVelocity = ySpeed;
        }
示例#6
0
        public void recoverSize(Entity e)
        {
            PositionComponent posComp    = ( PositionComponent )e.getComponent(GlobalVars.POSITION_COMPONENT_NAME);
            SquishComponent   squishComp = ( SquishComponent )e.getComponent(GlobalVars.SQUISH_COMPONENT_NAME);

            //Restore Width
            if (posComp.width < squishComp.baseWidth)
            {
                //Collisions on left or right side?
                List <Entity> leftCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x - posComp.width / 2 - 1,
                                                                                                   posComp.y - posComp.height / 2 - 1, posComp.x - posComp.width / 2 - 1, posComp.y + posComp.height / 2 - 1);
                List <Entity> rightCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x + posComp.width / 2 + 1,
                                                                                                    posComp.y - posComp.height / 2 - 1, posComp.x + posComp.width / 2 + 1, posComp.y + posComp.height / 2 - 1);

                bool leftSide  = false;
                bool rightSide = false;


                foreach (Entity ent in leftCollisions)
                {
                    if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME))
                    {
                        ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME);
                        if (squishStoppers.Contains(colComp.colliderType))
                        {
                            leftSide = true;
                            break;
                        }
                    }
                }

                foreach (Entity ent in rightCollisions)
                {
                    if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME))
                    {
                        ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME);
                        if (squishStoppers.Contains(colComp.colliderType))
                        {
                            rightSide = true;
                            break;
                        }
                    }
                }

                if (!leftSide && !rightSide)
                {
                    squishWidthCenter(posComp, squishComp, -squishComp.xEaseRate);
                }
                if (leftSide && !rightSide)
                {
                    squishRight(posComp, squishComp, -squishComp.xEaseRate);
                }
                if (!leftSide && rightSide)
                {
                    squishLeft(posComp, squishComp, -squishComp.xEaseRate);
                }

                //Don't over compensate
                if (posComp.width > squishComp.baseWidth)
                {
                    level.getMovementSystem().changeWidth(posComp, squishComp.baseWidth);
                }
            }
            else if (posComp.width > squishComp.baseWidth)
            {
                level.getMovementSystem().changeWidth(posComp, posComp.width - squishComp.xEaseRate);
                if (posComp.width < squishComp.baseWidth)
                {
                    level.getMovementSystem().changeWidth(posComp, squishComp.baseWidth);
                }
            }


            //Restore Height
            if (posComp.height < squishComp.baseHeight)
            {
                //Collisions on left or right side?
                List <Entity> upperCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x - posComp.width / 2 + 1,
                                                                                                    posComp.y + posComp.height / 2 + 1, posComp.x + posComp.width / 2 - 1, posComp.y + posComp.height / 2 + 1);
                List <Entity> lowerCollisions = level.getCollisionSystem().findObjectsBetweenPoints(posComp.x - posComp.width / 2 + 1,
                                                                                                    posComp.y - posComp.height / 2 - 1, posComp.x + posComp.width / 2 - 1, posComp.y - posComp.height / 2 - 1);

                bool upperSide = false;
                bool lowerSide = false;

                foreach (Entity ent in upperCollisions)
                {
                    if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME))
                    {
                        ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME);
                        if (squishStoppers.Contains(colComp.colliderType))
                        {
                            upperSide = true;
                            break;
                        }
                    }
                }
                foreach (Entity ent in lowerCollisions)
                {
                    if (ent.hasComponent(GlobalVars.COLLIDER_COMPONENT_NAME))
                    {
                        ColliderComponent colComp = ( ColliderComponent )ent.getComponent(GlobalVars.COLLIDER_COMPONENT_NAME);
                        if (squishStoppers.Contains(colComp.colliderType))
                        {
                            lowerSide = true;
                            break;
                        }
                    }
                }


                if (!upperSide && !lowerSide)
                {
                    squishHeightCenter(posComp, squishComp, -squishComp.xEaseRate);
                }
                if (upperSide && !lowerSide)
                {
                    squishDown(posComp, squishComp, -squishComp.xEaseRate);
                }
                if (!upperSide && lowerSide)
                {
                    squishUp(posComp, squishComp, -squishComp.xEaseRate);
                }

                //Don't overcompensate
                if (posComp.height > squishComp.baseHeight)
                {
                    level.getMovementSystem().changeHeight(posComp, squishComp.baseHeight);
                }
            }
            else if (posComp.height > squishComp.baseHeight)
            {
                level.getMovementSystem().changeHeight(posComp, posComp.height - squishComp.yEaseRate);
                if (posComp.height < squishComp.baseHeight)
                {
                    level.getMovementSystem().changeHeight(posComp, squishComp.baseHeight);
                }
            }
        }