-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathRoadSegment.java
153 lines (133 loc) · 4.4 KB
/
RoadSegment.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
145
146
147
148
149
150
151
152
153
/**
* Models a simple slice of road, a kerb and offroad grass for the game Racer.
*
* @author Joe Finney ([email protected])
*/
public class RoadSegment
{
public static String ROUGH_COLOUR = "#00FF00";
public static String KERB_COLOUR = "#808080";
public static double KERB_WIDTH = 10;
private Rectangle[] parts = new Rectangle[4];
private double xPosition;
private double yPosition;
private double width;
private double height;
private GameArena arena;
private double xSpeed;
private double ySpeed;
/**
* Creates a new RoadSegment, at the given location and size.
*
* @param x The x position on the screen of the road segment.
* @param y The y position on the screen of the road segment.
* @param width The width of this road segment (in pixels)
* @param height The height of this road segment (in pixels)
* @param a The GameArena upon which to draw this road segment.
*/
public RoadSegment(double x, double y, double width, double height, GameArena a)
{
this.width = width;
this.height = height;
arena = a;
double roughWidth1 = x - width/2;
double roughWidth2 = arena.getArenaWidth() - (x + width/2);
double roughX1 = -width/2 - roughWidth1/2;
double roughX2 = width/2 + roughWidth2/2;
parts[0] = new Rectangle(roughX1, height/2, roughWidth1, height, ROUGH_COLOUR);
parts[1] = new Rectangle(roughX2, height/2, roughWidth2, height, ROUGH_COLOUR);
parts[2] = new Rectangle(-width/2-KERB_WIDTH/2, height/2, KERB_WIDTH, height, KERB_COLOUR);
parts[3] = new Rectangle(width/2+KERB_WIDTH/2, height/2, KERB_WIDTH, height, KERB_COLOUR);
this.setXPosition(x);
this.setYPosition(y);
for (int i=0; i < parts.length; i++)
arena.addRectangle(parts[i]);
}
/**
* Changes the position of this RoadSegment to the given location
*
* @param x The new x positition of this RoadSegment on the screen.
*/
public void setXPosition(double x)
{
double dx = x - xPosition;
for (int i=0; i < parts.length; i++)
parts[i].setXPosition(parts[i].getXPosition() + dx);
xPosition = x;
}
/**
* Changes the position of this RoadSegment to the given location
*
* @param y The new y positition of this RoadSegment on the screen.
*/
public void setYPosition(double y)
{
double dy = y - yPosition;
for (int i=0; i < parts.length; i++)
parts[i].setYPosition(parts[i].getYPosition() + dy);
yPosition = y;
}
/**
* Determines the position of this RoadSegment on the screen
*
* @return The x position of the centre of this RoadSegment on the screen.
*/
public double getXPosition()
{
return xPosition;
}
/**
* Determines the position of this RoadSegment on the screen
*
* @return The y position of the centre of this RoadSegment on the screen.
*/
public double getYPosition()
{
return yPosition;
}
/**
* Provides a list (array) containing all the GameArena Rectangle objects that
* make up this road segment.
*
* @return The list of Rectangles that makes up this RoadSegment
*/
public Rectangle[] getParts()
{
return parts;
}
/**
* Sets the speed of this RoadSegment in the X axis - i.e. the number of pixels it moves in the X axis every time move() is called.
*
* @param s The new speed of this RoadSegment in the x axis
*/
public void setXSpeed(double s)
{
xSpeed = s;
}
/**
* Sets the speed of this RoadSegment in the Y axis - i.e. the number of pixels it moves in the Y axis every time move() is called.
*
* @param s The new speed of this RoadSegment in the y axis
*/
public void setYSpeed(double s)
{
ySpeed = s;
}
/**
* Updates the position of this RoadSegment by a small amount, depending upon its speed.
* see setXSpeed(0 and setYSpeed() methods.
*/
public void move()
{
this.setXPosition(xPosition + xSpeed);
this.setYPosition(yPosition + ySpeed);
}
/**
* Delete all the Rectangles that make up this RoadSegment from the screen
*/
public void remove()
{
for (int i=0; i < parts.length; i++)
arena.removeRectangle(parts[i]);
}
}