Example #1
0
        public override void Update()
        {
            if (GetStrengthOfMotion() == 0)
            {
                isMoving = false;
            }
            else
            {
                isMoving = true;
            }

            if (isMoving)
            {
                movingTime++;
            }

            if (movingTime > maxMovingTime)
            {
                v          = new Velocity(0, 0);
                movingTime = 0;
            }

            //execute jump
            if (!isMoving)
            {
                isSquishing = true;

                if (squishCooldown > 0)
                {
                    squishCooldown--;
                }

                if (squishCooldown <= 0) //if cooldown just ran out
                {
                    isMoving       = true;
                    isSquishing    = false;
                    squishCooldown = maxSquishCooldown;

                    Entity nearestGoal = GetNearestGoal();

                    if (nearestGoal == null)
                    {
                        return;
                    }

                    //calculate aim angle
                    aimAngle = GameMath.GetAngleBetweenPoints(x, y, nearestGoal.x, nearestGoal.y);

                    //move towards target
                    AddForce(jumpForce, aimAngle);
                }
            }

            //handle tounge
            if (!tounge.isOut)
            {
                //update position
                tounge.x = x;
                tounge.y = y;

                //search for something to grab
                foreach (Entity e in EntityManager.GetEntities())
                {
                    if (e.GetType() == typeof(Pickup) && GameMath.GetDistanceBetweenPoints(x, y, e.x, e.y) < toungeRange)
                    {
                        Pickup p = (Pickup)e;

                        tounge.SetTarget(new Vector2(p.x, p.y), p);
                        tounge.isOut = true;
                    }
                }
            }

            //update tounge if out
            if (tounge.isOut)
            {
                tounge.Update();
            }

            UpdateSprite();

            base.Update();
        }