-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoadObject.java
More file actions
72 lines (62 loc) · 2.64 KB
/
RoadObject.java
File metadata and controls
72 lines (62 loc) · 2.64 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
package com.javarush.games.racer.road;
import com.javarush.games.racer.GameObject;
import com.javarush.games.racer.ShapeMatrix;
import java.util.List;
public class RoadObject extends GameObject {
public RoadObjectType type;
public int speed;
public RoadObject(RoadObjectType type, int x, int y) {
super(x, y);
this.type = type;
this.matrix = getMatrix(type);
this.width = matrix[0].length;
this.height = matrix.length;
}
/**
* Метод, отвечающий за передвижение препятствия. У препятствия может быть своя скорость и дополнительная,
* которая зависит от скорости движения игрока.
*/
public void move(int boost, List<RoadObject> items) {
this.y += boost;
}
/**
* Проверяет текущий объект и объект, который пришел в качестве параметра, на пересечение их изображений
* с учетом дистанции distance.
* Например, если в качестве distance передать число 12, а 2 объекта расположены друг от друга на расстоянии меньшем,
* чем 12 ячеек игрового поля, метод вернет true. В ином случае вернет false.
*/
public boolean isCollisionWithDistance(RoadObject roadObject, int distance) {
if ((x - distance > roadObject.x + roadObject.width) || (x + width + distance < roadObject.x)) {
return false;
}
if ((y - distance > roadObject.y + roadObject.height) || (y + height + distance < roadObject.y)) {
return false;
}
return true;
}
/**
* Возвращает матрицу изображения объекта в зависимости от его типа.
*/
private static int[][] getMatrix(RoadObjectType type) {
switch (type) {
case CAR:
return ShapeMatrix.PASSENGER_CAR;
case BUS:
return ShapeMatrix.BUS;
case SPORT_CAR:
return ShapeMatrix.SPORT_CAR;
case THORN:
return ShapeMatrix.THORN;
case DRUNK_CAR:
return ShapeMatrix.DRUNK_CAR;
default:
return ShapeMatrix.TRUCK;
}
}
/**
* Возвращает высоту объекта.
*/
public static int getHeight(RoadObjectType type) {
return getMatrix(type).length;
}
}