summaryrefslogtreecommitdiff
path: root/jeu-test/Lemmini/0.84/src/GameUtil/Sprite.java
blob: fbef9fa18a438f0e3fd511fdeae81bedccfefbb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package GameUtil;

import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

import Tools.ToolBox;

/*
 * Copyright 2009 Volker Oth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Simple sprite class.
 *
 * @author Volker Oth
 */
public class Sprite {

	/**
	 * Animation style.
	 */
	public static enum Animation {
		/** not animated */
		NONE,
		/** endless animation loop */
		LOOP,
		/** triggered animation */
		TRIGGERED,
		/** one animation cycle */
		ONCE
	};

	/** sprite width in pixels */
	protected int width;
	/** sprite height in pixels */
	protected int height;
	/** number of animation frames */
	private int numframes;
	/** index of current animation frame */
	private int frameIdx;
	/** animation mode */
	private Animation animMode;
	/** index for a sound */
	private int sound;
	/** boolean flag: animation was triggered */
	private boolean triggered;
	/** array of animation frames */
	private BufferedImage frames[];

	/**
	 * Constructor.
	 * @param sourceImg Image containing animation frames one above each other.
	 * @param animFrames number of frames.
	 */
	public Sprite(final Image sourceImg, final int animFrames) {
		init(sourceImg, animFrames);
	}

	/**
	 * Constructor. Create Sprite from other Sprite.
	 * @param src Sprite to clone.
	 */
	public Sprite(final Sprite src) {
		copyFrom(src);
	}

	/**
	 * Copy all class attributes from another Sprite to this one.
	 * @param src Sprite to copy from.
	 */
	private void copyFrom(final Sprite src) {
		numframes = src.numframes;
		width = src.width;
		height = src.height;
		frameIdx = src.frameIdx;
		animMode = src.animMode;
		sound = src.sound;
		triggered = false;
		frames = src.frames.clone();
	}

	/**
	 * Initialized sprite with new animation.
	 * @param sourceImg Image containing animation frames one above each other.
	 * @param animFrames number of frames.
	 */
	private void init(final Image sourceImg, final int animFrames) {
		numframes = animFrames;
		width = sourceImg.getWidth(null);
		height = sourceImg.getHeight(null)/numframes;
		frameIdx = 0;
		animMode = Animation.NONE;
		triggered = false;
		// animation frames stored one above the other - now separate them into single images
		frames = ToolBox.getAnimation(sourceImg, animFrames, Transparency.BITMASK);
	}

	/**
	 * Get given animation frame.
	 * @param idx index of animation frame
	 * @return animation frame at position idx.
	 */
	public BufferedImage getImage(final int idx) {
		return frames[idx];
	}

	/**
	 * Get current animation frame.
	 * @return current animation frame.
	 */
	public BufferedImage getImage() {
		return frames[frameIdx];
	}

	/**
	 * Replace one animation frame.
	 * Note: replacing with an image of different size will create problems.
	 * @param idx index of frame to replace
	 * @param img image to use for this animation frame
	 */
	public void setImage(final int idx, final BufferedImage img) {
		frames[idx] = img;
	}

	/**
	 * Get current animation frame and animate.
	 * @return current animation frame (before increasing the animation step).
	 */
	public BufferedImage getImageAnim() {
		BufferedImage i = frames[frameIdx];
		switch (animMode) {
			case LOOP:
				if(++frameIdx >= numframes)
					frameIdx = 0;
				break;
			case ONCE:
				if (frameIdx < numframes-1)
					frameIdx++;
				break;
			case TRIGGERED:
				if (triggered) {
					if (frameIdx < numframes-1)
						frameIdx++;
					else {
						triggered = false;
						frameIdx = 0;
					}
				} else frameIdx = 0;
				break;
		}
		return i;
	}

	/**
	 * Set Pixel in all animation frames.
	 * @param x x position
	 * @param y y position
	 * @param color color as TYPE_INT_ARGB
	 */
	public void setPixel(final int x, final int y, final int color) {
		if (x>=0 && x<width && y>=0 && y<height)
			for (int i=0; i<numframes; i++)
				frames[i].setRGB(x,y,color);
	}

	/**
	 * Get animation mode.
	 * @return animation mode.
	 */
	public Animation getAnimMode() {
		return animMode;
	}

	/**
	 * Set animation mode.
	 * @param mode animation mode
	 */
	public void setAnimMode(final Animation mode) {
		animMode = mode;
	}

	/**
	 * Check if the animation is a triggered type.
	 * @return true if the animation is Animation.TRIGGERED.
	 */
	public boolean canBeTriggered() {
		return (animMode == Animation.TRIGGERED);
	}

	/**
	 * Trigger a triggered animation
	 * @return true if not yet triggered, false if already triggered
	 */
	public boolean trigger() {
		if (triggered == true)
			return false;
		triggered = true;
		frameIdx = 1;
		return true;
	}

	/**
	 * Get current animation frame index.
	 * @return current animation frame index
	 */
	public int getFrameIdx() {
		return frameIdx;
	}

	/** Set current animation frame index.
	 * @param frameIdx current animation frame index
	 */
	public void setFrameIdx(final int frameIdx) {
		this.frameIdx = frameIdx;
	}

	/**
	 * Get number of animation frames.
	 * @return number of animation frames
	 */
	public int getNumFrames() {
		return numframes;
	}

	/**
	 * Get sound index.
	 * @return sound index
	 */
	public int getSound() {
		return sound;
	}

	/**
	 * Set sound index.
	 * @param s sound index
	 */
	public void setSound(final int s) {
		sound = s;
	}

	/**
	 * Get width of Sprite in pixels.
	 * @return width of Sprite in pixels
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * Get height of Sprite in pixels.
	 * @return height of Sprite in pixels
	 */
	public int getHeight() {
		return height;
	}

}