-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffect.java
More file actions
160 lines (143 loc) · 4.3 KB
/
Copy pathEffect.java
File metadata and controls
160 lines (143 loc) · 4.3 KB
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Effect is an abstract class that provides methods for managing visual and auditory
* effects, such as fading in and fading out, removing ones' self after
* a certain number of acts, and controlling sound of the effect.
*
* @author Yixin Cai
* @version 2022-10-24
*/
public abstract class Effect extends Actor implements iSoundMaker
{
protected GifImage gifImage;
protected GreenfootImage image;
protected GreenfootSound sound;
protected boolean fadeIn;
protected boolean fadeOut;
protected int fadeDuration;
protected int duration;
protected int tik;
/**
* Constructor of Effect
* @param duration The number of acts it exsits
*/
public Effect(int duration) {
this(duration, false);
}
/**
* Constructor of Effect
* @param duration The number of acts it exsits
* @param fade True if it has fade effect, False otherwise
*/
public Effect(int duration, boolean fade) {
this(duration, fade, fade);
}
/**
* Constructor of Effect
* @param duration The number of acts it exsits
* @param fadeOut True if it has fade out effect, False otherwise
* @param fadeIn True if it has fade in effect, False otherwise
*/
public Effect(int duration, boolean fadeOut, boolean fadeIn) {
this(duration, fadeOut, fadeIn, 0);
}
/**
* Constructor of Effect
* @param duration The number of acts it exsits
* @param fadeOut True if it has fade out effect, False otherwise
* @param fadeIn True if it has fade in effect, False otherwise
* @param delay The number of acts need to delay
*/
public Effect(int duration, boolean fadeOut, boolean fadeIn, int delay) {
this.duration = duration - delay;
this.tik = 0 - delay;
this.fadeOut = fadeOut;
this.fadeIn = fadeIn;
fadeDuration = 90;
}
/**
* This method is called by the Greenfoot system when this actor has been inserted into the world.
* @param w The World
*/
public void addedToWorld(World w) {
VehicleWorld vw = (VehicleWorld)w;
if (sound != null) {
setVolume(vw.getVolume());
}
}
/**
* Act - do whatever the Effect wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() {
if (gifImage != null) {
setImage(gifImage.getCurrentImage());
}
tik++;
if (tik < 0) {
// do nothing if in delay
return;
}
else if (tik >= duration) {
// remove self after effect duration
removeSelf();
}
else if (fadeIn && tik < fadeDuration) {
// fade in
fade(tik, fadeDuration);
}
else if (fadeOut && duration - tik < fadeDuration) {
// fade out
fade(duration - tik, fadeDuration);
}
}
/**
* Method to remove object and stop sound
*/
protected void removeSelf() {
stopSound();
endEffect();
getWorld().removeObject(this);
}
/**
* An abstract method to end effect
*/
protected abstract void endEffect();
/**
* Update image transparency to achieve fade effect
* Modified from Mr.Cohen's codes.
* @param numerator The numerator to calculate transparency
* @param dinominator The dinominator to calculate transparency
*/
protected void fade(int numerator, int dinominator) {
double percent = numerator / (double)dinominator;
// Transparency 0 -- 255
int newTransparency = (int)(percent * 255);
image.setTransparency(newTransparency);
}
/**
* Play sound in loop
*/
public void playSound() {
if (sound != null) {
sound.playLoop();
}
}
/**
* Stop sound
*/
public void stopSound() {
if (sound != null) {
sound.stop();
}
}
/**
* Set sound volume
* @param volume The value of volume
*/
public void setVolume(int volume) {
if (sound != null) {
sound.setVolume(volume);
}
}
}