-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pea.java
101 lines (89 loc) · 3.08 KB
/
Pea.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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Demonstration of how to implement Pea as a subclass of SmoothMover.
*
* This is incomplete by design -- it intended only to help give you ideas
* and possible starting points for your own program!
*
* @author bcanada
* @version 2014.01.17
*/
public class Pea extends SmoothMover
{
private boolean exploding;
private int explodeCycleCount;
public Pea()
{
// a Vector object constructor has two input parameters:
// direction (i.e., angle) and length (magnitude).
super( new Vector( 0, 3.0 ) );
exploding = false;
} // end Pea constructor
/**
* Act - do whatever the Pea wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// When a pea is shot, it moves in the current direction based on
// its current movement vector.
// this is used to override the default wrap-around behavior of SmoothMover objects
/*
* NOTE: the SmoothMover's exactX field must have protected access if we want to
* be able to use it as if it were a field of the Pea class. However, we typically define
* all fields as private, so in order to avoid "breaking encapsulation," we access
* the value of exactX by means of the public getExactX method defined in SmoothMover.
*
* (Please see the SmoothMover source code, and don't be afraid to experiment!)
*/
if ( getExactX() >= getWorld().getWidth() - 5 )
{
getWorld().removeObject( this );
} // end if
// Still, because Pea is a subclass of SmoothMover, this is how the above if-statement
// could look if the SmoothMover's exactX field had protected access (instead of private):
/*
* if( exactX >= getWorld().getWidth() - 5 )
* {
* getWorld().removeObject( this );
* }
*
*/
if ( exploding )
{
setImage( "peaSplat.png" );
explodeCycleCount--;
if ( explodeCycleCount == 0 )
{
getWorld().removeObject( this );
} // end inner if
}
else
{
move();
} // end outer if/else
} // end method act
/**
* Set method for updating the exploding status as true or false
*
* @param explodeStatus
*/
public void setExplode( boolean explodeStatus )
{
exploding = explodeStatus;
if ( exploding )
{
explodeCycleCount = 10;
}
}
/**
* Get method for retrieving the exploding status for the given Pea
* (TODO: Check to see if anything is actually calling this method)
*
* @return the Pea object's exploding status
*/
public boolean getExplode()
{
return exploding;
} // end method getExplode()
} // end class Pea