-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimatedActor.java
117 lines (106 loc) · 2.86 KB
/
AnimatedActor.java
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class AnimatedActor here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class AnimatedActor extends Actor
{
public enum AnimationState
{
ANIMATING, FROZEN, RESTING, SPECIAL;
}
protected GreenfootImage[] images = null;
protected GreenfootImage specialImage = null;
protected int currentImage = 0;
private int animationCounter = 0;
private int delay = 0;
private AnimationState state;
protected boolean flipped = false;
public AnimatedActor()
{
super();
state = AnimationState.FROZEN;
}
public AnimatedActor(String basename, String suffix, int numImages, int delay)
{
super();
images = new GreenfootImage[numImages];
for (int i = 0; i < numImages; i++) {
images[i] = new GreenfootImage(basename + i + suffix);
}
setImage(images[currentImage]);
this.delay = delay;
this.state = AnimationState.ANIMATING;
}
/**
* Act - do whatever the AnimatedActor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (images != null)
{
if (state == AnimationState.ANIMATING)
{
if(animationCounter++ > delay)
{
animationCounter = 0;
currentImage = (currentImage + 1) % images.length;
setImage(images[currentImage]);
}
}
else if (state == AnimationState.SPECIAL)
{
setImage(specialImage);
}
else if (state == AnimationState.RESTING)
{
state = AnimationState.FROZEN;
animationCounter = 0;
currentImage = 0;
setImage(images[0]);
}
}
}
protected void useFlippedImage()
{
if (!flipped)
{
flipped = true;
flip();
}
}
protected void useDefaultImage()
{
if (flipped)
{
flipped = false;
flip();
}
}
private void flip() {
for (int i = 0; i < images.length; i++)
{
images[i].mirrorHorizontally();
}
}
protected void useSpecialImage(GreenfootImage image)
{
this.specialImage = image;
state = AnimationState.SPECIAL;
}
protected void animate()
{
state = AnimationState.ANIMATING;
}
protected void rest()
{
state = AnimationState.RESTING;
}
protected boolean isAnimating()
{
return state == AnimationState.ANIMATING;
}
}