• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.replica.replicaisland;
18 
19 import java.util.Arrays;
20 
21 /**
22  * Describes a single animation for a sprite.
23  */
24 public class SpriteAnimation extends PhasedObject {
25     private final static int LINEAR_SEARCH_CUTOFF = 16;
26 
27     private FixedSizeArray<AnimationFrame> mFrames;
28     private float[] mFrameStartTimes;
29     private boolean mLoop;
30     private float mLength;
31 
SpriteAnimation(int animationId, int frameCount)32     public SpriteAnimation(int animationId, int frameCount) {
33         super();
34         mFrames = new FixedSizeArray<AnimationFrame>(frameCount);
35         mFrameStartTimes = new float[frameCount];
36         mLoop = false;
37         mLength = 0.0f;
38         setPhase(animationId);
39     }
40 
getFrame(float animationTime)41     public AnimationFrame getFrame(float animationTime) {
42         AnimationFrame result = null;
43         final float length = mLength;
44         if (length > 0.0f) {
45         	final FixedSizeArray<AnimationFrame> frames = mFrames;
46         	assert frames.getCount() == frames.getCapacity();
47             final int frameCount = frames.getCount();
48             result = frames.get(frameCount - 1);
49 
50             if (frameCount > 1) {
51 	            float currentTime = 0.0f;
52 	            float cycleTime = animationTime;
53 	            if (mLoop) {
54 	                cycleTime = animationTime % length;
55 	            }
56 
57 	            if (cycleTime < length) {
58 	            	// When there are very few frames it's actually slower to do a binary search
59 	            	// of the frame list.  So we'll use a linear search for small animations
60 	            	// and only pull the binary search out when the frame count is large.
61 	            	if (mFrameStartTimes.length > LINEAR_SEARCH_CUTOFF) {
62 		            	int index = Arrays.binarySearch(mFrameStartTimes, cycleTime);
63 		            	if (index < 0) {
64 		            		index = -(index + 1) - 1;
65 		            	}
66 		            	result = frames.get(index);
67 	            	} else {
68 		                for (int x = 0; x < frameCount; x++) {
69 		                    AnimationFrame frame = frames.get(x);
70 		                    currentTime += frame.holdTime;
71 		                    if (currentTime > cycleTime) {
72 		                        result = frame;
73 		                        break;
74 		                    }
75 		                }
76 	            	}
77 	            }
78 	        }
79         }
80         return result;
81     }
82 
addFrame(AnimationFrame frame)83     public void addFrame(AnimationFrame frame) {
84     	mFrameStartTimes[mFrames.getCount()] = mLength;
85     	mFrames.add(frame);
86         mLength += frame.holdTime;
87     }
88 
getLength()89     public float getLength() {
90         return mLength;
91     }
92 
setLoop(boolean loop)93     public void setLoop(boolean loop) {
94         mLoop = loop;
95     }
96 
getLoop()97     public boolean getLoop() {
98         return mLoop;
99     }
100 }
101