Repository navigation
Expand file tree
/
Copy pathBaseObject.java
More file actions
73 lines (61 loc) · 1.69 KB
/
Copy pathBaseObject.java
File metadata and controls
73 lines (61 loc) · 1.69 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
73
package com.javarush.task.task24.task2413;
/**
* Базовый класс для всех объектов игры.
*/
public abstract class BaseObject {
//координаты
protected double x;
protected double y;
//радиус объекта
protected double radius;
protected BaseObject(double x, double y, double radius) {
this.x = x;
this.y = y;
this.radius = radius;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
/**
* Метод рисует свой объект на "канвасе".
*/
abstract void draw(Canvas canvas);
/**
* Двигаем себя на один ход.
*/
abstract void move();
/**
* Проверяем - не выходит ли (x,y) за границы.
*/
void checkBorders(double minx, double maxx, double miny, double maxy) {
if (x < minx) x = minx;
if (x > maxx) x = maxx;
if (y < miny) y = miny;
if (y > maxy) y = maxy;
}
/**
* Проверяем - пересекаются ли переданный(o) и наш(this) объекты.
*/
boolean isIntersec(BaseObject o) {
double dx = x - o.x;
double dy = y - o.y;
double destination = Math.sqrt(dx * dx + dy * dy);
double destination2 = Math.max(radius, o.radius);
return destination <= destination2;
}
}