Android Open Source - android_game_engine Generic Sprite






From Project

Back to project page android_game_engine.

License

The source code is released under:

GNU General Public License

If you think the Android project android_game_engine 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 org.es.engine.graphics.sprite;
/*from  w ww  .  j a  v a 2s .c  om*/
import android.graphics.Canvas;
import android.graphics.RectF;

import org.es.engine.graphics.animation.Animation;
import org.es.engine.graphics.utils.DrawingParam;

import java.util.EnumMap;

/**
 * A sprite is a simple animated element.<br />
 * Plays a bitmap list in loop.
 *
 * @author Cyril Leroux
 *         Created on 25/09/13.
 */
public class GenericSprite<AnimationId extends Enum<AnimationId>> implements Sprite<AnimationId> {

    protected final EnumMap<AnimationId, Animation> mAnimations;
    /** The current animation state. */
    protected AnimationId mAnimationId = null;
    protected RectF mBoundingRect;

    private int mOldSurfaceW = 0;
    private int mOldSurfaceH = 0;

    /**
     * Sprite constructor.
     * @param animations
     * @param startAnimationId
     */
    public GenericSprite(EnumMap<AnimationId, Animation> animations, AnimationId startAnimationId) {
        mAnimations = animations;
        mAnimationId = startAnimationId;
        mBoundingRect = new RectF();
    }

    @Override
    public void onUpdateSurfaceSize(int surfaceWidth, int surfaceHeight) {
//
//        if (mOldSurfaceW != 0 && mOldSurfaceH != 0) {
//            // Get old position
//            final float oldCenterX = mPosition.x + getWidth() / 2;
//            final float oldCenterY = mPosition.y + getHeight() / 2;
//
//            // Set new position
//            mPosition.x = oldCenterX / mOldSurfaceW * surfaceWidth  - getWidth()  / 2;
//            mPosition.y = oldCenterY / mOldSurfaceH * surfaceHeight - getHeight() / 2;
//        }
//
//        mOldSurfaceW = surfaceWidth;
//        mOldSurfaceH = surfaceHeight;
    }

    /**
     * The sprite is already resized. No need to apply drawingParam.
     * @param canvas
     * @param drawingParam
     */
    @Override
    public void draw(Canvas canvas, DrawingParam drawingParam) {
        getAnimation().draw(canvas, mBoundingRect);
    }

    /** Set the destination size of the bitmap to draw. */
    public void setDimensions(float left, float top, float right, float bottom) {
        mBoundingRect.set(left, top, right, bottom);
    }

    @Override
    public float getPosX() { return mBoundingRect.left; }

    @Override
    public float getPosY() { return mBoundingRect.top; }

    @Override
    public void setPosition(float x, float y) {
        mBoundingRect.set(x, y, x + mBoundingRect.width(), y + mBoundingRect.height());
    }

    @Override
    public void offsetPosition(float dx, float dy) {
        mBoundingRect.offset(dx, dy);
    }

    @Override
    public float getWidth() { return mBoundingRect.width(); }

    @Override
    public float getHeight() { return mBoundingRect.height(); }

    @Override
    public void startAnimation() { getAnimation().start(); }

    @Override
    public void stopAnimation() { getAnimation().stop(); }

    @Override
    public boolean updateAnimationFrame() { return getAnimation().updateFrame(); }

    @Override
    public AnimationId getAnimationId() { return mAnimationId; }

    @Override
    public void setAnimationId(AnimationId animationId) { mAnimationId = animationId; }

    @Override
    public Animation getAnimation() { return getAnimation(mAnimationId); }

    @Override
    public Animation getAnimation(AnimationId animationId) { return mAnimations.get(animationId); }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.google.example.games.basegameutils.ScreenUtils.java
org.es.engine.audio.Sound.java
org.es.engine.game_mechanic.DrawingThread.java
org.es.engine.game_mechanic.DrawingView.java
org.es.engine.graphics.animation.AnimationCallback.java
org.es.engine.graphics.animation.Animation.java
org.es.engine.graphics.animation.BitmapAnimation.java
org.es.engine.graphics.animation.SpriteSheetAnimation.java
org.es.engine.graphics.drawable.DrawableElement.java
org.es.engine.graphics.sprite.GenericSprite.java
org.es.engine.graphics.sprite.SpriteSheet.java
org.es.engine.graphics.sprite.Sprite.java
org.es.engine.graphics.utils.DrawTextUtils.java
org.es.engine.graphics.utils.DrawingParam.java
org.es.engine.hud.Button.java
org.es.engine.hud.Control.java
org.es.engine.hud.HUD.java
org.es.engine.hud.Text.java
org.es.engine.hud.ToggleButton.java
org.es.engine.toolbox.pathfinding.Node.java
org.es.engine.toolbox.pathfinding.ShortestPath.java