-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.java
100 lines (79 loc) · 1.88 KB
/
Entity.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
import javafx.scene.canvas.GraphicsContext;
public abstract class Entity {
protected boolean intersects; // pour indiquer si en collision ou pas, ce qui va impacter sa couleur
protected double x, y;
protected double r;
protected double vx;
public Entity(double x, double y) {
this.x = x;
this.y = y;
}
public Entity(double x, double y, double vx) {
this.x = x;
this.y = y;
this.vx = vx;
}
/**
* Met à jour la position et la vitesse de l'objet
*
* @param dt (Temps écoulé depuis le dernier update() en secondes)
*/
public abstract void update(double dt);
/**
* illustrer l'objet en question en fonction du statut debug (en tant que balle ou image)
* @param context
*/
public abstract void draw(GraphicsContext context, boolean debug);
/**
* met à jour le statut "intersects" (attribut) de chaque Entity si ils sont en intersection
* (permet de savoir quelle couleur mettre)
* @param other Entity
* @return boolean true si collision
*/
public boolean testCollision(Entity other) {
if (this.intersects(other)) { // si collision
intersects = true;
other.intersects = true;
return true;
} else {
this.intersects = false;
other.intersects = false;
return false;
}
}
/**
* Indique s'il y a intersection entre les deux balles
*
* @param other
* @return true s'il y a intersection
*/
public boolean intersects(Entity other) {
double dx = this.x - other.x;
double dy = this.y - other.y;
double d2 = dx * dx + dy * dy;
// d^2 < r^2
return d2 < (this.r + other.r) * (this.r + other.r);
}
// getters/setters :
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 getR() {
return r;
}
public double getVx() {
return vx;
}
public void setVx(double vx) {
this.vx = vx;
}
}