-
Notifications
You must be signed in to change notification settings - Fork 6
/
SeedPacket.java
144 lines (114 loc) · 3.98 KB
/
SeedPacket.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
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SeedPacket here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SeedPacket extends Actor
{
/**
* Act - do whatever the SeedPacket wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public long deltaTime;
public long deltaTime2;
public long lastFrame = System.nanoTime();
public long lastFrame2 = System.nanoTime();
public long rechargeTime;
public long currentFrame = System.nanoTime();
public int sunCost;
public boolean recharged = false;
public GreenfootImage recharge;
public String name;
public boolean selected = false;
public MyWorld MyWorld;
public boolean doneRechargeTime = false;
public GreenfootImage image1;
public GreenfootImage image2;
public SeedPacket(long rechargeTime, boolean recharged, int sunCost, String name) {
this.rechargeTime = rechargeTime;
this.recharged = recharged;
this.sunCost = sunCost;
this.name = name;
this.image1 = new GreenfootImage(name+"1.png");
this.image2 = new GreenfootImage(name+"2.png");
}
public void addedToWorld(World world) {
setRecharged(recharged);
if (recharged) {
doneRechargeTime = true;
setImage(image1);
} else {
setImage(image2);
}
MyWorld = ((MyWorld)getWorld());
recharge = new GreenfootImage(getImage().getWidth(), getImage().getHeight());
}
public void act() {
currentFrame = System.nanoTime();
deltaTime = (currentFrame - lastFrame) / 1000000;
deltaTime2 = (currentFrame - lastFrame2) / 1000000;
if (deltaTime < rechargeTime && !doneRechargeTime) {
if (!recharged && deltaTime2 > 500L) {
setImage(name+"2.png");
recharge.setColor(Color.BLACK);
recharge.clear();
int height = getImage().getHeight() - (int)Math.round(getImage().getHeight()*((double)deltaTime/rechargeTime));
recharge.setTransparency(110);
recharge.fillRect(0, 0, getImage().getWidth(), height);
getImage().drawImage(recharge, 0, 0);
lastFrame2 = System.nanoTime();
}
} else if (!recharged && !doneRechargeTime){
doneRechargeTime = true;
setImage(image2);
}
if (MyWorld.seedbank.suncounter.sun >= sunCost) {
if (!recharged) {
if (deltaTime > rechargeTime) {
setRecharged(true);
} else {
setRecharged(false);
}
}
} else {
setRecharged(false);
}
// Add your action code here.
}
public void startRecharge() {
lastFrame = currentFrame;
doneRechargeTime = false;
}
public void setRecharged(boolean charge) {
if (recharged != charge) {
recharged = charge;
if (recharged) {
setImage(image1);
} else {
setImage(image2);
}
}
}
public void setSelected(boolean bool) {
selected = bool;
if (selected) {
setImage(image2);
} else {
setImage(image1);
}
}
public boolean getCharge() {
return recharged;
}
public boolean getSelected() {
return selected;
}
public TransparentObject addImage() {
return null;
}
public Plant getPlant() {
return null;
}
}