-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoon.java
More file actions
75 lines (69 loc) · 2.19 KB
/
Copy pathMoon.java
File metadata and controls
75 lines (69 loc) · 2.19 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;
/**
* Moon is an effect. A moon gif will show on the World. It moves from
* left to right and rise in the first half and fall in the second half.
* While the moon is moving, the background image will be changed to
* dark mode. Right after moon appears, all of the vihecles, the vehicle
* spawners and lanes will change direction.
*
* @author Yixin Cai
* @version 2022-10-24
*/
public class Moon extends Effect
{
// instance variable
private int speed;
/**
* Constructor of Moon
* @param duration The number of acts that it exists
*/
public Moon(int duration) {
super(duration);
speed = 2;
gifImage = new GifImage("halloweenMoon.gif");
setImage(gifImage.getCurrentImage());
}
/**
* 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)getWorld();
vw.startMoon();
}
/**
* Act - do whatever the Moon wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
super.act();
moveMoon();
}
/**
* Move the moon. Moon gif will should move from left to right.
* Rise during the first half and fall during the second half.
*/
private void moveMoon() {
// x position increment based on tik
int x = 75 + (int) ( (650 * tik / (double) duration) );
if (x < 400) {
// y position increment during the first half
int y = 75 + (int) ( (150 / (double) duration) * (150 - tik) );
setLocation(x, y);
}
else {
// y position decrement during the second half
int y = 75 - (int) ( (150 / (double) duration) * (150 - tik) );
setLocation(x, y);
}
}
/**
* Method to end the effect
*/
protected void endEffect() {
VehicleWorld vw = (VehicleWorld)getWorld();
vw.stopMoon();
}
}