Android Open Source - candy-drop Sucker






From Project

Back to project page candy-drop.

License

The source code is released under:

Copyright (c) 2014, Gregory Martin All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project candy-drop listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gregfmartin.facetapper.entities;
/*from   w  w  w .  j ava2s .  c  o m*/
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;

import java.util.Locale;

/**
 * A sucker/alternative to candy drop. Usually worth more points but they have higher falling speeds.
 * <p/>
 * Score - Upper: 25, Lower: 10
 * Falling Speed - Upper: 28.0, Lower: 6.0
 * Rotation Speed - Upper: 14.0, Lower: 1.0
 * Scale - Upper: 0.6, Lower: 0.4
 *
 * @author Gregory Martin
 */
public class Sucker extends CandyBase {
    private float imageWidth;
    private float imageHeight;
    private float startYPos;
    private float startXPos;

    /**
     * Default constructor. Makes a 100% random instance.
     */
    private Sucker() {
        super((int)(Math.random() * 25) + 11, (float)((Math.random() * 28) + 7), (float)((Math.random() * 14) + 2), (float)((Math.random() * 0.6F) + 0.4F));

        setInitialStartPosition();
    }

    private Sucker(int score, float fallingSpeed, float rotationSpeed, float scale) {
        super(score, fallingSpeed, rotationSpeed, scale);

        setInitialStartPosition();
    }

    @Override public void generateTexture() {
        int suckerStyleId = (int)((Math.random() * 6) + 1);
        String suckerFilename = String.format(Locale.US, "images/candy/sucker-mod%s-root.png", String.valueOf(suckerStyleId));

        mTexture = new TextureRegion(new Texture(Gdx.files.internal(suckerFilename)));
    }

    @Override public int clipScore(int score) {
        if(score > 25) { score = 25; } else if(score < 10) { score = 10; }

        return score;
    }

    @Override public float clipFallingSpeed(float fallingSpeed) {
        if(fallingSpeed > 14.0F) { fallingSpeed = 14.0F; } else if(fallingSpeed < 6.0F) { fallingSpeed = 6.0F; }

        return fallingSpeed;
    }

    @Override public float clipRotationSpeed(float rotationSpeed) {
        if(rotationSpeed > 14.0F) { rotationSpeed = 14.0F; } else if(rotationSpeed < 1.0F) { rotationSpeed = 1.0F; }

        return rotationSpeed;
    }

    @Override public float clipScalar(float scalar) {
        if(scalar > 0.6F) { scalar = 0.6F; } else if(scalar < 0.4F) { scalar = 0.4F; }

        return scalar;
    }

    @Override public boolean handleTouchDown(InputEvent event, float x, float y, int pointer, int button) {
        return false;
    }

    @Override public void handleTouchUp(InputEvent event, float x, float y, int pointer, int button) {

    }

    @Override public void act(float delta) {
        super.act(delta);

        // Make the sucker fall down perpetually
        translate(0.0F, -(getFallingSpeed()));
        rotate(getRotationSpeed());
    }

    /**
     * @return - A "pure" random instance of Sucker
     */
    static public Sucker obtain() {
        return new Sucker();
    }

    /**
     * @param score         User-defined score
     * @param fallingSpeed  User-defined falling speed
     * @param rotationSpeed User-defined rotation speed
     * @param scalar        User-defined scalar
     * @return - A "somewhat" random instance of Sucker
     */
    static public Sucker obtain(int score, float fallingSpeed, float rotationSpeed, float scalar) {
        return new Sucker(score, fallingSpeed, rotationSpeed, scalar);
    }

    /**
     * The start position for suckers is the nearly the same as it is for candy drops - above the screen with some
     * padding and a random point along the x-axis so long as that point is not outside or near the screen boundaries.
     */
    private void setInitialStartPosition() {
        imageWidth = getTexture().getRegionWidth() * getScalar();
        imageHeight = getTexture().getRegionHeight() * getScalar();
        startYPos = (Gdx.graphics.getHeight() + imageHeight) + (float)((Math.random() * 20) + 1);
        startXPos = (float)Math.random() * Gdx.graphics.getWidth();

        // Ensure that the start x position is sane
        if(startXPos <= 0.0F) {
            startXPos = imageWidth;
        } else if(startXPos >= (Gdx.graphics.getWidth() - imageWidth)) {
            /*
             * This conditional is slightly modified from what it was originally due to some inconsistencies in
             * operation. Originally, the check was against the width of the screen. While the checks were technically
             * correct, it was still possible for the candy to be created right at the edge of the screen, within
             * literally pixels, and still pass the check. However this isn't good for players since you'd never be
             * able to see it. So the check is a little more liberal on the right side subtracting half the image
             * width from the width of the screen as the fail zone.
             */
            startXPos = (Gdx.graphics.getWidth() - imageWidth);
        }

        // Manually setup required information about the actor since this isn't automatically done
        setScale(getScalar());
        setWidth(imageWidth);
        setHeight(imageHeight);
        setOrigin(getWidth() / 2, getHeight() / 2);
        setPosition(startXPos, startYPos);
    }
}




Java Source Code List

com.gregfmartin.facetapper.GameCore.java
com.gregfmartin.facetapper.Main.java
com.gregfmartin.facetapper.entities.CandyBase.java
com.gregfmartin.facetapper.entities.CandyDrop.java
com.gregfmartin.facetapper.entities.Pulse.java
com.gregfmartin.facetapper.entities.Screen.java
com.gregfmartin.facetapper.entities.SplashImage.java
com.gregfmartin.facetapper.entities.Sucker.java
com.gregfmartin.facetapper.screens.ArenaScreen.java
com.gregfmartin.facetapper.screens.AssociationScreen.java
com.gregfmartin.facetapper.screens.SplashScreen.java
com.gregfmartin.facetapper.screens.TitleScreen.java
com.gregfmartin.facetapper.utils.Colour.java
com.gregfmartin.facetapper.utils.CoreUtil.java
com.gregfmartin.facetapper.utils.DevSettings.java
com.gregfmartin.facetapper.utils.Gl20Utils.java
com.gregfmartin.facetapper.utils.MathEngine.java
com.gregfmartin.facetapper.utils.StagePrep.java