-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFishingLine.java
More file actions
122 lines (108 loc) · 3.21 KB
/
FishingLine.java
File metadata and controls
122 lines (108 loc) · 3.21 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* FishingLine is a line attached to a Fisher - it:
* * grows to bottom of screen
* * returns back to sender
*
* @author Stephen Blythe
* @version 8/2018
*/
public class FishingLine extends Actor
{
private int size; // current line length (pixels)
private int growthRate;// how fast (in what direction) line is growing
/**
* constructs a fishing line of default length 1 and moves to
* bottom of screen at rate of two pixels per act call.
*/
public FishingLine()
{
size=1; // as small as possible to start with
growthRate=2; // grow towards bottom of screen
redraw(); // generte the line's image
}
// redraws the imge for this fishing line, whic is a narrow
// vertical bar of the current size.
private void redraw()
{
// build an image that is just big enough
GreenfootImage img=new GreenfootImage(1,size);
// fill the image with gray color
img.setColor(Color.GRAY);
img.fill();
// use the image we just built as this object's image.
setImage(img);
}
/**
* returns the existence of this Line in the world
*
* @return true if this line is in a world, false otherwise
*/
public boolean inWorld()
{
return getWorld()!=null;
}
/**
* returns true if this line has been reeled out yet.
*
* @return true if the line has been reeeled out, false otherwise
*/
public boolean isOut()
{
return inWorld();
}
/**
* the x locator for the "hook" end of this line
*
* @return the x loxation of the end of the line.
*/
public int endX()
{
return getX();
}
/**
* the y locator for the "hook" end of this line
*
* @return the y loxation of the end of the line.
*/
public int endY()
{
// y location is middle of image, need to add 1/2 of image to y
return getY()+size/2;
}
/**
* start sending the line out, even if it is already returning.
*/
public void cast()
{
growthRate=2;
}
/**
* start reeling the line back in.
*/
public void startReelback()
{
growthRate= -Math.abs(growthRate);
}
/**
* grow the line toward the bottom or shrink it toward the top,
* based on it's current growthRate
*/
public void act()
{
size+=growthRate; // move the line by this much - might be down or up
// if the line hs no size left, renove it.
if (size<=0)
{
getWorld().removeObject(this); // remove this line from the world
return; // no need to do anything else!
}
// move the line so it apppears to grow or shrink properly
setLocation(getX(), getY()+growthRate/Math.abs(growthRate));
redraw();
// if we're at bottom of world, start moving line back up.
if (getY()+size/2 >= getWorld().getHeight()-1)
//growthRate=-growthRate;
startReelback();
}
}