Repository navigation
Expand file tree
/
Copy pathT1Ball.java
More file actions
59 lines (49 loc) · 1.42 KB
/
Copy pathT1Ball.java
File metadata and controls
59 lines (49 loc) · 1.42 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
/**
* T1Ball.java
* Table 1
* Daniel Alexander Miranda
*
* Responsibilities:
* - Handle ball position, velocity, and movement logic
* - Detect wall and paddle collisions
* - Publish ball state updates via MQTT
* - Subscribe to ball state for sync (if this instance is not the host)
*/
public class T1Ball {
private int x, y;
private int dx = 9, dy = 9;
private final int WIDTH = 10, HEIGHT = 10;
private final int FIELD_WIDTH = 800, FIELD_HEIGHT = 600;
private T1Publisher publisher;
public T1Ball(T1Publisher publisher) {
this.publisher = publisher;
this.x = FIELD_WIDTH / 2;
this.y = FIELD_HEIGHT / 2;
}
public void move() {
x += dx;
y += dy;
if (y <= 0 || y >= FIELD_HEIGHT - HEIGHT) {
dy = -dy;
}
if (x <= 0 || x >= FIELD_WIDTH - WIDTH) {
dx = -dx;
}
publishState();
}
public void publishState() {
String message = x + "," + y;
publisher.publish("pong/ball", message);
}
public void updateFromMessage(String message) {
String[] parts = message.split(",");
if (parts.length == 2) {
x = Integer.parseInt(parts[0]);
y = Integer.parseInt(parts[1]);
}
}
public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return WIDTH; }
public int getHeight() { return HEIGHT; }
}