-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSeedBank.java
137 lines (113 loc) · 5.26 KB
/
SeedBank.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class SeedBank here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SeedBank extends Actor
{
/**
* Act - do whatever the SeedBank wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public MyWorld MyWorld;
public SunCounter suncounter = new SunCounter();
public SeedPacket[] bank;
public SeedPacket selectedPacket = null;
public TransparentObject image = null;
public TransparentObject transparent = null;
public static final int x1 = 182;
public static final int x2 = 702;
public static final int xSpacing = Board.xSpacing;
public static final int y1 = 62;
public static final int y2 = 417;
public static final int ySpacing = Board.ySpacing;
public SeedBank(SeedPacket[] bank) {
this.bank = bank;
}
public void act() {
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse != null) {
if (image != null) {
if ((mouse.getX() < x1 || mouse.getX() > x2 || mouse.getY() < y1 || mouse.getY() > y2)
|| (MyWorld.board.Board[(int)((mouse.getY()-y1)/ySpacing)][(int)((mouse.getX()-x1)/xSpacing)] != null)) {
image.setTransparent(false);
image.setLocation(mouse.getX(), mouse.getY());
} else {
int x = (int)((mouse.getX()-x1)/xSpacing);
int y = (int)((mouse.getY()-y1)/ySpacing);
image.setTransparent(true);
image.setLocation(x*Board.xSpacing+Board.xOffset, y*Board.ySpacing+Board.yOffset);
}
} else {
}
if (Greenfoot.mouseClicked(null)) {
MyWorld.moveHitbox();
//Debug: System.out.println(mouse.getX()+" "+ mouse.getY());
if (image != null) {
if (mouse.getX() < x1 || mouse.getX() > x2 || mouse.getY() < y1 || mouse.getY() > y2) {
MyWorld.removeObject(image);
image = null;
boolean selected = false;
AudioPlayer.play(80,"tap.mp3", "tap2.mp3");
if (MyWorld.hitbox.getTouching().contains(selectedPacket)) {
selected = true;
}
if (!selected) {
selectedPacket.setSelected(false);
selectedPacket = null;
}
} else {
int x = (int)((mouse.getX()-x1)/xSpacing);
int y = (int)((mouse.getY()-y1)/ySpacing);
if (MyWorld.board.Board[y][x] == null) {
MyWorld.board.placePlant(x, y, selectedPacket.getPlant());
suncounter.removeSun(selectedPacket.sunCost);
getWorld().removeObject(image);
image = null;
selectedPacket.startRecharge();
selectedPacket.setRecharged(false);
selectedPacket.setSelected(false);
selectedPacket = null;
}
}
}
for (Object i : MyWorld.hitbox.getTouching()) {
if (i instanceof SeedPacket) {
SeedPacket clicked = (SeedPacket)i;
if (selectedPacket != clicked) {
if (clicked.recharged) {
if (selectedPacket != null) {
selectedPacket.setSelected(false);
selectedPacket = null;
}
selectedPacket = clicked;
clicked.setSelected(true);
AudioPlayer.play(80, "seedlift.mp3");
image = clicked.addImage();
} else {
AudioPlayer.play(80, "buzzer.mp3");
}
} else {
if (clicked.recharged) {
selectedPacket = null;
clicked.setSelected(false);
AudioPlayer.play(80, "seedlift.mp3");
}
}
}
}
}
}
}
@Override
public void addedToWorld(World world) {
MyWorld = (MyWorld)getWorld();
MyWorld.addObject(suncounter, 120, 50);
for (int i = 0; i < bank.length; i++) {
MyWorld.addObject(bank[i], 120, 120+i*50);
}
}
}