-
Notifications
You must be signed in to change notification settings - Fork 6
/
FallingSun.java
115 lines (99 loc) · 3.54 KB
/
FallingSun.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Sun here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class FallingSun extends FallingObject
{
/**
* Act - do whatever the Sun wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public MyWorld MyWorld;
public GreenfootImage[] sun;
public boolean clicked = false;
public boolean beenClicked = false;
public long lastFrame2 = System.nanoTime();
public long deltaTime2 = System.nanoTime();
public FallingSun() {
super(0.6, 0, 0, 0, (long)Random.Int(2000, 10000));
sun = importSprites("sun", 2);
}
public void update() {
currentFrame = System.nanoTime();
deltaTime = (currentFrame - lastFrame) / 1000000;
deltaTime2 = (currentFrame - lastFrame2) / 1000000;
animate(sun, 200, true);
if (!beenClicked) {
if (checkClick()) {
beenClicked = true;
AudioPlayer.play(90, "points.mp3");
MyWorld.seedbank.suncounter.addSun(25);
}
}
if (!beenClicked) {
if (deltaTime < fallTime) {
double x = getExactX()+hSpeed;
double y = getExactY()+vSpeed;
setLocation(x,y);
turn(rotate);
vSpeed = vSpeed + acceleration;
lastFrame2 = System.nanoTime();
} else {
if (deltaTime2 > 10000) {
if (getImage().getTransparency() > 0) {
if (getImage().getTransparency()-20 <= 0) {
getImage().setTransparency(0);
} else {
getImage().setTransparency(getImage().getTransparency()-20);
}
} else {
getWorld().removeObject(this);
return;
}
}
}
} else {
if (!(Math.abs(getX()-SunCounter.x) < 20 && Math.abs(getY()-SunCounter.y) < 20)) {
turnTowards(SunCounter.x,SunCounter.y);
move(20);
}
}
checkDeath();
}
public boolean checkClick() {
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null && Greenfoot.mouseClicked(null)) {
MyWorld.moveHitbox();
if (intersects(MyWorld.hitbox)) {
clicked = true;
} else {
clicked = false;
}
return clicked;
}
clicked = false;
return clicked;
}
public void checkDeath() {
if (Math.abs(getX()-SunCounter.x) < 20 && Math.abs(getY()-SunCounter.y) < 20) {
move(0.5);
if (getImage().getTransparency() > 0) {
if (getImage().getTransparency()-20 <= 0) {
getImage().setTransparency(0);
} else {
getImage().setTransparency(getImage().getTransparency()-20);
}
} else {
getWorld().removeObject(this);
return;
}
}
}
@Override
public void addedToWorld(World world) {
MyWorld = (MyWorld)getWorld();
}
}