• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
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.badlogic.gdx.maps.tiled.tiles;
18 
19 import com.badlogic.gdx.graphics.g2d.TextureRegion;
20 import com.badlogic.gdx.maps.MapProperties;
21 import com.badlogic.gdx.maps.tiled.TiledMapTile;
22 import com.badlogic.gdx.utils.Array;
23 import com.badlogic.gdx.utils.GdxRuntimeException;
24 import com.badlogic.gdx.utils.IntArray;
25 import com.badlogic.gdx.utils.TimeUtils;
26 
27 /** @brief Represents a changing {@link TiledMapTile}. */
28 public class AnimatedTiledMapTile implements TiledMapTile {
29 
30 	private static long lastTiledMapRenderTime = 0;
31 
32 	private int id;
33 
34 	private BlendMode blendMode = BlendMode.ALPHA;
35 
36 	private MapProperties properties;
37 
38 	private StaticTiledMapTile[] frameTiles;
39 
40 	private int[] animationIntervals;
41 	private int frameCount = 0;
42 	private int loopDuration;
43 	private static final long initialTimeOffset = TimeUtils.millis();
44 
45 	@Override
getId()46 	public int getId () {
47 		return id;
48 	}
49 
50 	@Override
setId(int id)51 	public void setId (int id) {
52 		this.id = id;
53 	}
54 
55 	@Override
getBlendMode()56 	public BlendMode getBlendMode () {
57 		return blendMode;
58 	}
59 
60 	@Override
setBlendMode(BlendMode blendMode)61 	public void setBlendMode (BlendMode blendMode) {
62 		this.blendMode = blendMode;
63 	}
64 
getCurrentFrameIndex()65 	public int getCurrentFrameIndex () {
66 		int currentTime = (int)(lastTiledMapRenderTime % loopDuration);
67 
68 		for (int i = 0; i < animationIntervals.length; ++i) {
69 			int animationInterval = animationIntervals[i];
70 			if (currentTime <= animationInterval) return i;
71 			currentTime -= animationInterval;
72 		}
73 
74 		throw new GdxRuntimeException(
75 			"Could not determine current animation frame in AnimatedTiledMapTile.  This should never happen.");
76 	}
77 
getCurrentFrame()78 	public TiledMapTile getCurrentFrame () {
79 		return frameTiles[getCurrentFrameIndex()];
80 	}
81 
82 	@Override
getTextureRegion()83 	public TextureRegion getTextureRegion () {
84 		return getCurrentFrame().getTextureRegion();
85 	}
86 
87 	@Override
setTextureRegion(TextureRegion textureRegion)88 	public void setTextureRegion (TextureRegion textureRegion) {
89 		throw new GdxRuntimeException("Cannot set the texture region of AnimatedTiledMapTile.");
90 	}
91 
92 	@Override
getOffsetX()93 	public float getOffsetX () {
94 		return getCurrentFrame().getOffsetX();
95 	}
96 
97 	@Override
setOffsetX(float offsetX)98 	public void setOffsetX (float offsetX) {
99 		throw new GdxRuntimeException("Cannot set offset of AnimatedTiledMapTile.");
100 	}
101 
102 	@Override
getOffsetY()103 	public float getOffsetY () {
104 		return getCurrentFrame().getOffsetY();
105 	}
106 
107 	@Override
setOffsetY(float offsetY)108 	public void setOffsetY (float offsetY) {
109 		throw new GdxRuntimeException("Cannot set offset of AnimatedTiledMapTile.");
110 	}
111 
getAnimationIntervals()112 	public int[] getAnimationIntervals () {
113 		return animationIntervals;
114 	}
115 
setAnimationIntervals(int[] intervals)116 	public void setAnimationIntervals (int[] intervals) {
117 		if (intervals.length == animationIntervals.length) {
118 			this.animationIntervals = intervals;
119 
120 			loopDuration = 0;
121 			for (int i = 0; i < intervals.length; i++) {
122 				loopDuration += intervals[i];
123 			}
124 
125 		} else {
126 			throw new GdxRuntimeException("Cannot set " + intervals.length
127 				+ " frame intervals. The given int[] must have a size of " + animationIntervals.length + ".");
128 		}
129 	}
130 
131 	@Override
getProperties()132 	public MapProperties getProperties () {
133 		if (properties == null) {
134 			properties = new MapProperties();
135 		}
136 		return properties;
137 	}
138 
139 	/** Function is called by BatchTiledMapRenderer render(), lastTiledMapRenderTime is used to keep all of the tiles in lock-step
140 	 * animation and avoids having to call TimeUtils.millis() in getTextureRegion() */
updateAnimationBaseTime()141 	public static void updateAnimationBaseTime () {
142 		lastTiledMapRenderTime = TimeUtils.millis() - initialTimeOffset;
143 	}
144 
145 	/** Creates an animated tile with the given animation interval and frame tiles.
146 	 *
147 	 * @param interval The interval between each individual frame tile.
148 	 * @param frameTiles An array of {@link StaticTiledMapTile}s that make up the animation. */
AnimatedTiledMapTile(float interval, Array<StaticTiledMapTile> frameTiles)149 	public AnimatedTiledMapTile (float interval, Array<StaticTiledMapTile> frameTiles) {
150 		this.frameTiles = new StaticTiledMapTile[frameTiles.size];
151 		this.frameCount = frameTiles.size;
152 
153 		this.loopDuration = frameTiles.size * (int)(interval * 1000f);
154 		this.animationIntervals = new int[frameTiles.size];
155 		for (int i = 0; i < frameTiles.size; ++i) {
156 			this.frameTiles[i] = frameTiles.get(i);
157 			this.animationIntervals[i] = (int)(interval * 1000f);
158 		}
159 	}
160 
161 	/** Creates an animated tile with the given animation intervals and frame tiles.
162 	 *
163 	 * @param intervals The intervals between each individual frame tile in milliseconds.
164 	 * @param frameTiles An array of {@link StaticTiledMapTile}s that make up the animation. */
AnimatedTiledMapTile(IntArray intervals, Array<StaticTiledMapTile> frameTiles)165 	public AnimatedTiledMapTile (IntArray intervals, Array<StaticTiledMapTile> frameTiles) {
166 		this.frameTiles = new StaticTiledMapTile[frameTiles.size];
167 		this.frameCount = frameTiles.size;
168 
169 		this.animationIntervals = intervals.toArray();
170 		this.loopDuration = 0;
171 
172 		for (int i = 0; i < intervals.size; ++i) {
173 			this.frameTiles[i] = frameTiles.get(i);
174 			this.loopDuration += intervals.get(i);
175 		}
176 	}
177 
getFrameTiles()178 	public StaticTiledMapTile[] getFrameTiles () {
179 		return frameTiles;
180 	}
181 }
182