<commit_before>package org.anddev.andengine.entity.sprite;

import java.util.Arrays;

import org.anddev.andengine.opengl.texture.TiledTextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.constants.TimeConstants;

/**
 * @author <NAME>
 * @since 15:25:46 - 10.03.2010
 */
public class AnimatedSprite extends TiledSprite implements TimeConstants {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean mAnimationRunning;
	private long mFrameStart;

	private int[] mFrameEndsInMilliseconds;
	private long mAnimationDuration;

	private int mFirstTileIndex;
	private long mAnimationStart;
	private boolean mLoop;

	// ===========================================================
	// Constructors
	// ===========================================================

	public AnimatedSprite(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public boolean isAnimationRunning() {
		return this.mAnimationRunning;
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		if(this.mAnimationRunning) {
			final long now = System.nanoTime();
			final long elapsedSinceFrameStart = now - this.mFrameStart;
			final long elapsedNanosecondsInFrame = elapsedSinceFrameStart % this.mAnimationDuration;
			final int currentFrameIndex = this.determineCurrentFrameIndex((int)(elapsedNanosecondsInFrame / NANOSECONDSPERMILLISECOND));
			this.setCurrentTileIndex(this.mFirstTileIndex + currentFrameIndex);

			this.mFrameStart = now - elapsedNanosecondsInFrame;
			if(!this.mLoop) {
				this.stopAnimationIfDurationExceeded(now);
			}
		}
	}

	// ===========================================================
	// Methods
	// ===========================================================

	private void stopAnimationIfDurationExceeded(final long now) {
		if(now - this.mAnimationStart > this.mAnimationDuration){
			this.mAnimationRunning = false;
		}
	}

	private int determineCurrentFrameIndex(final int pElapsedRelativeInFrame) {
		final int[] frameEnds = this.mFrameEndsInMilliseconds;<commit_msg>Animation now stoppable.<commit_after>package org.anddev.andengine.entity.sprite;

import java.util.Arrays;

import org.anddev.andengine.opengl.texture.TiledTextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.constants.TimeConstants;

/**
 * @author <NAME>
 * @since 15:25:46 - 10.03.2010
 */
public class AnimatedSprite extends TiledSprite implements TimeConstants {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================

	private boolean mAnimationRunning;
	private long mFrameStart;

	private int[] mFrameEndsInMilliseconds;
	private long mAnimationDuration;

	private int mFirstTileIndex;
	private long mAnimationStart;
	private boolean mLoop;

	// ===========================================================
	// Constructors
	// ===========================================================

	public AnimatedSprite(final float pX, final float pY, final TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
	}

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public boolean isAnimationRunning() {
		return this.mAnimationRunning;
	}

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		if(this.mAnimationRunning) {
			final long now = System.nanoTime();
			final long elapsedSinceFrameStart = now - this.mFrameStart;
			final long elapsedNanosecondsInFrame = elapsedSinceFrameStart % this.mAnimationDuration;
			final int currentFrameIndex = this.determineCurrentFrameIndex((int)(elapsedNanosecondsInFrame / NANOSECONDSPERMILLISECOND));
			this.setCurrentTileIndex(this.mFirstTileIndex + currentFrameIndex);

			this.mFrameStart = now - elapsedNanosecondsInFrame;
			if(!this.mLoop) {
				this.stopAnimationIfDurationExceeded(now);
			}
		}
	}

	// ===========================================================
	// Methods
	// ===========================================================
	
	public void stopAnimation() {
		this.mAnimationRunning = false;
	}
	
	public void stopAnimation(final int pTileIndex) {
		this.mAnimationRunning = false;
		this.setCurrentTileIndex(pTileIndex);
	}

	private void stopAnimationIfDurationExceeded(final long now) {
		if(now - this.mAnimationStart > this.mAnimationDuration){
			this.mAnimationRunning = false;
		}
	}

	private int determineCurrentFrameIndex(final int pElapsedRelativeInFrame) {
		final int[] frameEnds = this.mFrameEndsInMilliseconds;
{'fraction_non_alphanumeric': 0.3224373778646296, 'fraction_numerical': 0.004974240540060401, 'mean_word_length': 6.1811224489795915, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4262021', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1388, 'n_words': 260}