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 |
package bump_n_jump;
import java.awt.*;
import java.awt.geom.Rectangle2D;
/*
* Dog - represents the enemy character that kills the ball on impact.
*/
public class Dog extends Sprite {
private static final int NUMFRAMES = 4; //number of frames in the image strip.
private int direction;
private static final int LEFT = 0;
private static final int RIGHT = 1; //for if the dog is walking left or right.
private static final double SPRITE_WIDTH = 200.0; //set to be the width and height of the image used.
private static final double SPRITE_HEIGHT = 35;
private static final double DEFAULT_SPEED = 1.5; //default speed for the dog to move.
private static final double DEFAULT_ROOM = 200;
private double roomToRun; // Total of how far a dog will move before changing direction. Doesn't change after set.
private double distanceToTravel; // How far a dog is able to move before changing direction. Updates.
private double xComponent; //how fast the dog should move in the x direction.
private Rectangle2D rect; //the containing rectangle.
/*
* Constructor that takes an initial x and y.
*/
public Dog(double x, double y, double speed, double lengthToMove) {
super(x,y);
xComponent = speed;
frames = loadStripImageArray("../images/dogStrip.png", NUMFRAMES);
rect = new Rectangle2D.Double();
rect.setRect(x, y, SPRITE_WIDTH/NUMFRAMES - 3, SPRITE_HEIGHT);
direction = RIGHT;
roomToRun = lengthToMove;
distanceToTravel = roomToRun;
}
public Dog(double x, double y, double roomToMove) {
super(x,y);
xComponent = DEFAULT_SPEED;
frames = loadStripImageArray("../images/dogStrip.png", NUMFRAMES);
rect = new Rectangle2D.Double();
rect.setRect(x, y, SPRITE_WIDTH/NUMFRAMES, SPRITE_HEIGHT);
direction = RIGHT;
roomToRun = roomToMove;
distanceToTravel = roomToRun;
}
public Dog(double x, double y) {
super(x,y);
xComponent = DEFAULT_SPEED;
frames = loadStripImageArray("../images/dogStrip.png", NUMFRAMES);
rect = new Rectangle2D.Double();
rect.setRect(x, y, SPRITE_WIDTH/NUMFRAMES, SPRITE_HEIGHT);
direction = RIGHT;
roomToRun = DEFAULT_ROOM;
distanceToTravel = roomToRun;
}
/*
* drawSprite - called whenever we draw the screen.
*/
public void drawSprite(Graphics g) {
Graphics2D g2D = (Graphics2D)g;
g2D.drawImage(frames.get(currentFrame), (int)x, (int)y, null);
}
/*
* updateFrame - advances the frame of the sprite we are working with.
*/
public void updateFrame() {
if (direction == RIGHT) {
//change between the going left frames (2 and 3)
if (currentFrame == 2) currentFrame++;
else if (currentFrame == 3) currentFrame--;
else currentFrame = 2;
}
else if (direction == LEFT) {
//change between the going right frames (0 and 1)
if (currentFrame == 0) currentFrame++;
else if (currentFrame == 1) currentFrame--;
else currentFrame = 0;
}
}
/**
* checks if a collision has happened with a tile and will change dog's direction.
* @param t
*/
public void collisionWithWall(Tile t) {
if (rect.intersects(t.getRectangle())) {
changeDirection();
}
}
/**
* changeDirection - to be called whenever the dog needs to change directions.
*/
private void changeDirection() {
if (direction == LEFT)
direction = RIGHT;
else
direction = LEFT;
}
/**
* moves the dog along on its path.
*/
public void updateCoordinates() {
// Ensures that dogs don't wander further than we allow them to.
if ((distanceToTravel - xComponent) < 0) {
changeDirection();
distanceToTravel = roomToRun;
}
if (direction == LEFT) {
x -= xComponent;
}
else {
x += xComponent;
}
distanceToTravel -= xComponent;
rect.setRect(x, y, SPRITE_WIDTH/NUMFRAMES, SPRITE_HEIGHT);
}
public Rectangle2D getRectangle() {
return rect;
}
} |