Skip to content

Commit 8558a74

Browse files
committed
Added possibility to define conf and galaxies on compiled code
1 parent ff6cc0e commit 8558a74

File tree

6 files changed

+140
-10
lines changed

6 files changed

+140
-10
lines changed

build.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<project name="kompfys" basedir="." default="main">
2+
3+
<!-- set global properties for this build -->
4+
<property name="main-class" value="ee.liiser.siim.galaxies.Main"/>
5+
<property name="src.dir" value="src"/>
6+
<property name="build.dir" value="build"/>
7+
<property name="classes.dir" value="${build.dir}/classes"/>
8+
<property name="jar.dir" value="${build.dir}/jar"/>
9+
<property name="lib.dir" value="lib"/>
10+
11+
<!-- adds every jar in the lib directory to the classpath-->
12+
<path id="classpath">
13+
<fileset dir="${lib.dir}" includes="**/*.jar"/>
14+
</path>
15+
16+
<target name="clean">
17+
<delete dir="${build.dir}"/>
18+
</target>
19+
20+
<target name="compile">
21+
<mkdir dir="${classes.dir}"/>
22+
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
23+
</target>
24+
25+
<target name="jar" depends="compile">
26+
<mkdir dir="${jar.dir}"/>
27+
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
28+
<manifest>
29+
<attribute name="Main-Class" value="${main-class}"/>
30+
</manifest>
31+
</jar>
32+
</target>
33+
34+
<target name="run" depends="jar">
35+
<java fork="true" classname="${main-class}">
36+
<classpath>
37+
<path refid="classpath"/>
38+
<path location="${jar.dir}/${ant.project.name}.jar"/>
39+
</classpath>
40+
</java>
41+
</target>
42+
43+
<target name="clean-build" depends="clean,jar"/>
44+
45+
<target name="main" depends="clean,run"/>
46+
</project>

conf.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#Method(Velocity/Basic),dt,fps
2+
Velocity,0.001,60

galaxies.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#position;velocity;normal;width;starcount
2+
0,0,0;0,0,0;0,0,1;1;500
3+
0,5,-30;0,0,0.5;0,1,0;1;500

src/ee/liiser/siim/galaxies/Main.java

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package ee.liiser.siim.galaxies;
22

3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
39
import javax.vecmath.Vector3d;
410

511
import ee.liiser.siim.galaxies.calculations.Calculator;
@@ -26,7 +32,7 @@ public class Main {
2632
/**
2733
* An array to hold all galaxy cores in the system
2834
*/
29-
//FIXME should not be public
35+
// FIXME should not be public
3036
public static Core[] cores;
3137

3238
/**
@@ -38,26 +44,83 @@ public class Main {
3844
* Calculation method to use. Value should be one of
3945
* {@link Calculator.Method}
4046
*/
41-
private static final Method METHOD = Method.VELOCITY_VERLET;
47+
private static Method METHOD = Method.VELOCITY_VERLET;
4248

4349
public static void main(String[] args) {
44-
50+
Galaxy[] galaxies = null;
51+
52+
if(args.length >= 2){
53+
try {
54+
BufferedReader r = new BufferedReader(new FileReader(args[1]));
55+
String line;
56+
do{
57+
line = r.readLine();
58+
}while(line != null && line.startsWith("#"));
59+
r.close();
60+
String[] params = line.split(",");
61+
if(params[0].toLowerCase().equals("velocity")){
62+
METHOD = Method.VELOCITY_VERLET;
63+
}else if(params[0].toLowerCase().equals("basic")){
64+
METHOD = Method.BASIC_VERLET;
65+
}else{
66+
throw new IllegalArgumentException(params[0] + " is not a method in " + args[1]);
67+
}
68+
Calculator.dt = Double.parseDouble(params[1]);
69+
GraphicsThread.fps = Double.parseDouble(params[2]);
70+
71+
} catch (FileNotFoundException e) {
72+
e.printStackTrace();
73+
} catch (IOException e) {
74+
e.printStackTrace();
75+
}
76+
}
77+
4578
ObjectFactory factory = new ObjectFactory(METHOD);
46-
47-
Galaxy galaxy1 = factory.makeGalaxy(new Vector3d(), new Vector3d(), new Vector3d(0,0,1), 1, STARCOUNT);
48-
Galaxy galaxy2 = factory.makeGalaxy(new Vector3d(0,5,-30), new Vector3d(0,0,0.5), new Vector3d(0,1,0), 1, STARCOUNT);
4979

50-
Galaxy[] galaxies = new Galaxy[]{galaxy1, galaxy2};
5180

81+
if (args.length >= 1) {
82+
83+
try {
84+
BufferedReader r = new BufferedReader(new FileReader(args[0]));
85+
ArrayList<Galaxy> list = new ArrayList<Galaxy>();
86+
while(true){
87+
String line = r.readLine();
88+
if(line == null) break;
89+
if(line.isEmpty()) break;
90+
if(line.startsWith("#")) continue;
91+
92+
list.add(factory.makeGalaxy(line));
93+
94+
}
95+
r.close();
96+
galaxies = new Galaxy[list.size()];
97+
galaxies = list.toArray(galaxies);
98+
} catch (FileNotFoundException e) {
99+
e.printStackTrace();
100+
} catch (IOException e) {
101+
e.printStackTrace();
102+
}
103+
104+
} else {
105+
// Default galaxy configuration
106+
Galaxy galaxy1 = factory.makeGalaxy(new Vector3d(), new Vector3d(),
107+
new Vector3d(0, 0, 1), 1, STARCOUNT);
108+
Galaxy galaxy2 = factory.makeGalaxy(new Vector3d(0, 5, -30),
109+
new Vector3d(0, 0, 0.5), new Vector3d(0, 1, 0), 1,
110+
STARCOUNT);
111+
112+
galaxies = new Galaxy[] { galaxy1, galaxy2 };
113+
}
52114
run(galaxies);
53-
115+
54116
}
55117

56118
/**
57119
* Main entry point of the application. Call this with an array of galaxies
58120
* to simulate their movement
59121
*
60-
* @param galaxies is the array of galaxies to be simulated
122+
* @param galaxies
123+
* is the array of galaxies to be simulated
61124
*/
62125
public static void run(Galaxy[] galaxies) {
63126

src/ee/liiser/siim/galaxies/data/ObjectFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ public class ObjectFactory {
2424
public ObjectFactory(Method method) {
2525
this.method = method;
2626
}
27+
28+
/**
29+
* Generates a galaxy from a formatted string
30+
* @param string
31+
* @return galaxy
32+
*/
33+
public Galaxy makeGalaxy(String line) {
34+
String[] parts = line.split(";");
35+
Vector3d[] vecs = new Vector3d[3];
36+
for(int i = 0; i < 3; i++){
37+
String[] nrs = parts[i].split(",");
38+
vecs[i] = new Vector3d(Double.parseDouble(nrs[0]), Double.parseDouble(nrs[1]), Double.parseDouble(nrs[2]));
39+
}
40+
41+
return makeGalaxy(vecs[0], vecs[1], vecs[2], Double.parseDouble(parts[3]), Integer.parseInt(parts[4]));
42+
}
2743

2844
/**
2945
* Generates a galaxy

src/ee/liiser/siim/galaxies/drawing/GraphicsThread.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
public class GraphicsThread extends Thread {
1111

12-
private static final double fps = 60.0f;
12+
public static double fps = 60.0f;
1313

1414
private Drawable[] points;
1515

0 commit comments

Comments
 (0)