Back to project page candy-drop.
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.
package com.gregfmartin.facetapper.entities; //from w ww.j av a2s.c om 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 com.badlogic.gdx.scenes.scene2d.InputListener; import com.gregfmartin.facetapper.screens.ArenaScreen; import com.gregfmartin.facetapper.utils.MathEngine; import java.util.Locale; /** * A representation of a candy drop. Any of the textures that are used for these entities are from * candy-modX-root. * <p/> * Rules for instantiation * <p/> * score can't exceed 10 but can't be zero * fallingSpeed can't exceed 20.0 but can't be less than 3.0 * rotationSpeed can't exceed 14.0 but can't be less than 1.0 * scale can't exceed 0.6 but can't be less than 0.4 * * @author Gregory Martin */ public class CandyDrop extends CandyBase { /** * The highest possible score the player could get for tapping on a Candy Drop. */ static final private int SCORE_LIMIT_UPPER = 10; /** * The lowest possible score the player could get for tapping on a Candy Drop. */ static final private int SCORE_LIMIT_LOWER = 1; private float imageWidth; private float imageHeight; private float startYPos; private float startXPos; protected CandyDrop() { super((int)(Math.random() * 10) + 1, (float)((Math.random() * 20) + 3), (float)((Math.random() * 14) + 1), (float)((Math.random() * 0.6F) + 0.4F)); setInitialStartPosition(); } /** * This will most likely be the ctor that's used going forward. * * @param level The current level the player is on */ protected CandyDrop(int level) { super((((int)Math.random() * SCORE_LIMIT_UPPER) + SCORE_LIMIT_LOWER), (BASE_FALLING_SPEED * MathEngine.getInstance().getDifficultyForLevel(level)), (((float)Math.random() * BASE_ROTATION_SPEED_LOWER) + BASE_ROTATION_SPEED_UPPER), (((float)Math.random() * BASE_SCALE_LOWER) + BASE_SCALE_UPPER)); setInitialStartPosition(); } protected CandyDrop(int score, float fallingSpeed, float rotationSpeed, float scale) { super(score, fallingSpeed, rotationSpeed, scale); setInitialStartPosition(); } static public CandyDrop obtain() { return new CandyDrop(); } /** * This will probably be the preferred obtain method going forward. * * @param level The current level the player is on * @return A CandyDrop with reference to the level */ static public CandyDrop obtain(int level) { return new CandyDrop(level); } static public CandyDrop obtain(int score, float fallingSpeed, float rotationSpeed, float scale) { return new CandyDrop(score, fallingSpeed, rotationSpeed, scale); } @Override public void generateTexture() { int candyId = (int)(Math.random() * 6) + 1; String candyTexFilename = String.format(Locale.US, "images/candy/candy-mod%s-root.png", String.valueOf(candyId)); mTexture = new TextureRegion(new Texture(Gdx.files.internal(candyTexFilename))); } @Override public void act(float delta) { super.act(delta); translate(0.0F, -getFallingSpeed()); rotate(getRotationSpeed()); // Check to see if the candy fell outside the lower boundary (plus padding) - remove if we did if(checkForFallout()) { // The player missed the candy - remove it from the stage but don't add to the score getStage().getRoot().removeActor(this); setAlive(false); } } @Override public int clipScore(int score) { if(score > 10) { score = 10; } else if(score <= 0) { score = 1; } return score; } @Override public float clipFallingSpeed(float fallingSpeed) { if(fallingSpeed > 10.0F) { fallingSpeed = 10.0F; } // else if(fallingSpeed < 3.0F) { // fallingSpeed = 3.0F; // } return fallingSpeed; } @Override public float clipRotationSpeed(float rotationSpeed) { // Upper = 14.0 Lower = 1.0 /* if(rotationSpeed > 14.0F) { rotationSpeed = 14.0F; } else if(rotationSpeed < 1.0F) { rotationSpeed = 1.0F; } */ return (rotationSpeed % 14.0F) + 1.0F; } @Override public float clipScalar(float scale) { // Upper = 0.6 Lower = 0.4 if(scale > 0.6F) { scale = 0.6F; } else if(scale < 0.4F) { scale = 0.4F; } return scale; } @Override public boolean handleTouchDown(InputEvent event, float x, float y, int pointer, int button) { // Play the tap sound mTapSound.play(); // Add to the score MathEngine.getInstance().addToScore(mScore); // Add to the current tap count MathEngine.getInstance().addToTotalTapped(1); // Kill the instance mAlive = false; // TESTING ArenaScreen.STATUS_TEXT_DIRTY = true; // Remove this from the stage getStage().getRoot().removeActor(this); return false; } @Override public void handleTouchUp(InputEvent event, float x, float y, int pointer, int button) { } 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 / 2))) { /* * 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 * 2); } // 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); } private boolean checkForFallout() { if(getY() < -(getHeight() + 10.0F)) { return true; } else { return false; } } }