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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.camilstaps.route66;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
* Class for an individual car
*
* @author Pieter Koopman, Camil Staps
*/
public class Car {
public static final int
CARWIDTH = 20,
CARLENGTH = 40,
MINCARSPACE = 5,
MINCARSPEED = 2,
MAXCARSPEED = 4;
public static final Color[] carColours = {Color.red, Color.blue, Color.black, Color.gray, Color.green,
Color.pink, Color.orange, Color.magenta, Color.lightGray};
private final int speed, number; // speed and car-number
private final Direction direction; // driving direction, used by paint
private int location; // current location on the road
private final Color colour; // colour of this car
private final Random random; // here to ensure that every car gets a new
private final Driver driver; // this car's driver
private final Model model; // the model the car is in
/**
* The constructor:
* - choose a random speed
* - set right location, direction and colour
* - create new Driver object
*
* @param number of the car
* @param model the model the car is in
*/
public Car(int number, Model model) {
this.number = number;
random = new Random();
speed = MINCARSPEED + random.nextInt(MAXCARSPEED - MINCARSPEED + 1);
direction = Direction.intToDirection(number);
location = RoadView.WINDOWSIZE - 2 - (number/Model.DIRECTIONS) * (CARLENGTH + MINCARSPACE);
colour = carColours[number % carColours.length];
this.model = model;
driver = new Driver(this, model);
}
/**
* Get the driver
* @return
*/
public Driver getDriver() {
return driver;
}
/**
* Get the direction
* @return
*/
public Direction getDirection() {
return direction;
}
/**
* Get the number of this car
* @return
*/
public int getNumber() {
return number;
}
/**
* Get the current location of this car
* @return
*/
public int getLocation() {
return location;
}
/**
* Get the location the car would move to if it would move
* @return
*/
public int getNewLocation() {
return (location + speed) % RoadView.WINDOWSIZE;
}
/**
* Move the car one step
*/
public void step() {
location = getNewLocation();
//System.err.println("Car ");
model.update();
}
/**
* Is the car in front of the crossing? That is, is the car now not on the
* crossing but would it be there after the next step?
* @return
*/
public boolean isInFrontOfCrossing() {
return location < RoadView.getStartCrossing() && getNewLocation() >= RoadView.getStartCrossing();
}
/**
* Is the car on the crossing?
* @return
*/
public boolean isOnCrossing() {
return location > RoadView.getStartCrossing() && location - CARLENGTH < RoadView.getEndCrossing();
}
/**
* Paint this car
* @param g
*/
public void paint(Graphics g) {
int x, y, w, h;
switch (direction) {
case North:
x = RoadView.WINDOWSIZE / 2 + 1;
y = RoadView.WINDOWSIZE - location;
w = Car.CARWIDTH;
h = Car.CARLENGTH;
break;
case East:
x = location - Car.CARLENGTH;
y = RoadView.WINDOWSIZE / 2 + 1;
w = Car.CARLENGTH;
h = Car.CARWIDTH;
break;
case South:
x = RoadView.WINDOWSIZE / 2 - Car.CARWIDTH - 1;
y = location - Car.CARLENGTH;
w = Car.CARWIDTH;
h = Car.CARLENGTH;
break;
case West:
x = RoadView.WINDOWSIZE - location - 1;
y = RoadView.WINDOWSIZE / 2 - Car.CARWIDTH - 1;
w = Car.CARLENGTH;
h = Car.CARWIDTH;
break;
default:
x = y = w = h = 10;
}
g.setColor(colour);
g.fillRect(x, y, w, h);
g.setColor(Color.WHITE);
g.drawString(String.valueOf(number), x + w / 6, y + h / 2 + 4);
}
}
|