-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameObject.java
146 lines (99 loc) · 3.24 KB
/
GameObject.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
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
package asteroidsApp;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import java.io.*;
public class GameObject {
private Node view;
public Point2D velocity = new Point2D(0, 0);
private double rotationSpeed = 5;
private boolean alive = true;
public GameObject(Node view) {
this.view = view;
}
public void update() {
view.setTranslateX(view.getTranslateX() + velocity.getX());
view.setTranslateY(view.getTranslateY() + velocity.getY());
}
public void setVelocity(Point2D velocity) {
this.velocity = velocity;
}
public Point2D getVelocity() {
return velocity;
}
public Node getView() {
return view;
}
public boolean isAlive() {
return alive;
}
public boolean isDead() {
return !alive;
}
public void setAlive(boolean alive) {
this.alive = alive;
}
public double getRotate() {
return view.getRotate();
}
public void rotateRight() {
view.setRotate(view.getRotate() + rotationSpeed);
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())), Math.sin(Math.toRadians(getRotate()))));
}
public void rotateLeft() {
view.setRotate(view.getRotate() - rotationSpeed);
setVelocity(new Point2D(Math.cos(Math.toRadians(getRotate())), Math.sin(Math.toRadians(getRotate()))));
}
public boolean isColliding(GameObject other) {
return getView().getBoundsInParent().intersects(other.getView().getBoundsInParent());
}
public static void writeHighScore(int score){
BufferedWriter bw = null;
FileWriter fw = null;
final String FILENAME = "storage/highscore.txt";
System.out.println("Score: " + score);
String sScore = Integer.toString(score);
try{
fw = new FileWriter(FILENAME);
bw = new BufferedWriter(fw);
bw.write(sScore);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static String readHighScore(){
BufferedReader br = null;
FileReader fr = null;
final String FILENAME = "storage/highscore.txt";
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
sCurrentLine = br.readLine();
if(sCurrentLine != null){
return sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return "0";
}
}