예제 #1
0
        private void slingHandler()
        {
            //Animation Handler
            if (slingInit)
            {
                animation = "jump";

                shouldSlamTimer++;

                if (shouldSlamTimer >= shouldSlamTimerMax)
                {
                    shouldSlam = true;
                }

                if (((Gravity)getTrait("gravity")).grounded)
                {
                    if (shouldSlam)
                    {
                        PlayerSprites.fall.Play();
                        //Camera.Shake(100, 15);
                        //ParticleHandler.particles.Add(new PlayerSmash(x, y, isFacingRight));
                    }
                    slingInit = false;
                }
            }
            else
            {
                shouldSlamTimer = 0;
                shouldSlam      = false;
            }


            //Handles Trajectory Calculations
            if (!isSlinging || isAttacking)
            {
                return;
            }

            projections = new Projection[projectionRange / toProjectModifier];
            int projected = 0;

            float ghostX = x;
            float ghostY = y;

            float xPower = (startingX - mouse.X) * dragPower;
            float yPower = (startingY - mouse.Y) * dragPower;

            //Sets ghost delta position to power or to max power
            float ghostDx = (xPower > maxSlingPowerX || xPower < -maxSlingPowerX) ? xPower > 0 ? maxSlingPowerX : -maxSlingPowerX : xPower;
            float ghostDy = (yPower > maxSlingPowerY || yPower < -maxSlingPowerY) ? yPower > 0 ? maxSlingPowerY : -maxSlingPowerY : yPower;


            float ghostWeight = ((Gravity)getTrait("gravity")).weight;

            float ghostFriction = ((Friction)getTrait("friction")).airCoefficient;

            List <Hitbox> ghostHitboxes;

            ghostHitboxes = ((Rigidbody)getTrait("rigidbody")).hitboxes;

            //Temporary variable to detect when projection has hit an object
            Boolean detected = false;

            //Applies ground friction to first itteration for more accurate projection
            ghostDx -= ghostDx - (ghostDx) / ((Friction)getTrait("friction")).coefficient;

            //  Simulate Trajectory  //
            for (int i = 0; i < projectionRange; i++)
            {
                //Apply Gravity
                ghostDy += ghostWeight; // * parent.tm

                //Apply Friction
                ghostDx -= ghostDx - (ghostDx) / ghostFriction;

                //Detect If Colliding
                for (int j = 0; j < EntityHandler.entities.Count; j++)
                {
                    Entity entity = EntityHandler.entities[j];
                    if (entity.classId == "player" || !entity.hasTrait("rigidbody"))
                    {
                        continue;
                    }

                    for (int k = 0; k < ghostHitboxes.Count; k++)
                    {
                        Hitbox    ghostHitbox     = ghostHitboxes[k];
                        Rigidbody entityRigidbody = ((Rigidbody)entity.getTrait("rigidbody"));
                        for (int l = 0; l < entityRigidbody.hitboxes.Count; l++)
                        {
                            Hitbox entityHitbox = entityRigidbody.hitboxes[l];
                            //Rectangle collision on entities.  If true, end loop and tell where is
                            if (Utils.rectCollision(ghostX - ghostHitbox.diffX, ghostY - ghostHitbox.diffY, ghostHitbox.width, ghostHitbox.height, entityHitbox.x, entityHitbox.y, entityHitbox.width, entityHitbox.height))
                            {
                                detected = true;
                                break;
                            }
                        }
                        if (detected)
                        {
                            break;
                        }
                    }
                    if (detected)
                    {
                        break;
                    }
                }

                //Settle expected landing position if detected, then break
                if (detected)
                {
                    projections[projections.Length - 1] = new Projection(ghostX, ghostY);
                    break;
                }

                //Draws Projections In Series
                if ((projectionRange % toProjectModifier) == 0 && projected != projections.Length - 1)
                {
                    projections[projected] = new Projection(ghostX, ghostY);
                    projected++;
                }

                //Move Ghost
                ghostX += ghostDx;
                ghostY += ghostDy;
            }
        }
예제 #2
0
        public override void Draw(SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
        {
            //Draw Hitbox
            Texture2D texture = new Texture2D(graphicsDevice, 1, 1, false, SurfaceFormat.Color);

            texture.SetData <Color>(new Color[] { Color.White });
            spriteBatch.Begin(samplerState: SamplerState.PointClamp);

            if (showHitbox)
            {
                for (int i = 0; i < ((Rigidbody)getTrait("rigidbody")).hitboxes.Count; i++)
                {
                    Hitbox hitbox = ((Rigidbody)getTrait("rigidbody")).hitboxes[i];
                    spriteBatch.Draw(texture, new Rectangle((int)(hitbox.x - Camera.X), (int)(hitbox.y - Camera.Y), (int)(hitbox.width), (int)(hitbox.height)), Color.White);
                }
            }

            //Console.WriteLine(Camera.gameScale);
            float scale = 4 * Camera.gameScale;

            Hitbox myHitbox = ((Rigidbody)getTrait("rigidbody")).hitboxes[0];

            //Draw Frog Sprites
            Rectangle DR = new Rectangle(
                (int)(x - PlayerSprites.frogSize.X * scale / 2 - Camera.X),
                (int)(((y + height / 2) - (PlayerSprites.frogSize.Y - 12) * scale) - Camera.Y),
                (int)(PlayerSprites.frogSize.X * scale),
                (int)(PlayerSprites.frogSize.Y * scale));

            int trueAnimator = (int)(animator / aniMod);

            switch (animation)
            {
            case "neutral":
                if (isFacingRight)
                {
                    spriteBatch.Draw(PlayerSprites.frog,
                                     DR,
                                     sourceRectangle: PlayerSprites.idle[trueAnimator],
                                     color: Color.White);
                    break;
                }
                spriteBatch.Draw(PlayerSprites.frog,
                                 destinationRectangle: DR,
                                 sourceRectangle: PlayerSprites.idle[trueAnimator],
                                 effects: SpriteEffects.FlipHorizontally,
                                 color: Color.White);
                break;

            case "jump":
                if (isFacingRight)
                {
                    spriteBatch.Draw(PlayerSprites.frog,
                                     DR,
                                     sourceRectangle: PlayerSprites.jump[trueAnimator],
                                     color: Color.White);
                    break;
                }
                spriteBatch.Draw(PlayerSprites.frog,
                                 destinationRectangle: DR,
                                 sourceRectangle: PlayerSprites.jump[trueAnimator],
                                 effects: SpriteEffects.FlipHorizontally,
                                 color: Color.White);
                break;

            case "openMouth":
                if (isFacingRight)
                {
                    spriteBatch.Draw(PlayerSprites.frog,
                                     DR,
                                     sourceRectangle: PlayerSprites.openMouth[trueAnimator],
                                     color: Color.White);
                    break;
                }
                spriteBatch.Draw(PlayerSprites.frog,
                                 destinationRectangle: DR,
                                 sourceRectangle: PlayerSprites.openMouth[trueAnimator],
                                 effects: SpriteEffects.FlipHorizontally,
                                 color: Color.White);
                break;

            case "swing":
                if (isFacingRight)
                {
                    spriteBatch.Draw(PlayerSprites.frog,
                                     destinationRectangle: DR,
                                     sourceRectangle: PlayerSprites.swing[trueAnimator],
                                     color: Color.White);
                    break;
                }
                spriteBatch.Draw(PlayerSprites.frog,
                                 destinationRectangle: DR,
                                 sourceRectangle: PlayerSprites.swing[trueAnimator],
                                 effects: SpriteEffects.FlipHorizontally,
                                 color: Color.White);
                break;
            }

            //Draw Cursor moved to cursor handler


            spriteBatch.End();


            spriteBatch.Begin(samplerState: SamplerState.PointClamp);

            if (isSwinging)
            {
                //Draw Tongue
                Utils.DrawLine(spriteBatch,
                               new Vector2((int)(sox - Camera.X), (int)(soy - Camera.Y)),
                               new Vector2((int)(x - Camera.X), (int)(y - 45 * Camera.gameScale - Camera.Y)),
                               new Color(70, 135, 143),
                               5f);

                //Draw End Of Tongue
                spriteBatch.Draw(PlayerSprites.frog,
                                 new Rectangle((int)(sox - 80 * scale / 2 - Camera.X),
                                               (int)(((soy + height / 2) - (PlayerSprites.frogSize.Y - 6) * scale) - Camera.Y),
                                               (int)(80 * scale),
                                               (int)(60 * scale)),
                                 sourceRectangle: PlayerSprites.tongueEnd[0],
                                 color: Color.White);
            }


            if (isSlinging)
            {
                //Shows projection dots of estemated trajectory
                for (int i = 0; i < projections.Count(); i++)
                {
                    Projection projection = projections[i];
                    if (projection == null)
                    {
                        continue;
                    }

                    //Draws Projection Dots
                    spriteBatch.Draw(texture,
                                     new Rectangle((int)(projection.x - Camera.X), (int)(projection.y - Camera.Y), 5, 5),
                                     new Color(226, 243, 228));
                }

                //Shows line from starting sling to current mouse position
                Utils.DrawLine(spriteBatch,
                               new Vector2((int)(mouse.X), (int)(mouse.Y)),
                               new Vector2((int)(startingX), (int)(startingY)),
                               Color.Black);
            }

            spriteBatch.End();
            texture.Dispose();
        }