-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tanks v0.3.4 - Added Tank Registry class, and made harder tanks appea…
…r rarer
- Loading branch information
Showing
11 changed files
with
126 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,78 @@ | ||
package tanks; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
|
||
public class Registry | ||
{ | ||
public ArrayList<TankRegistry> tankRegistries = new ArrayList<TankRegistry>(); | ||
protected double maxTankWeight = 0; | ||
|
||
static class TankRegistry | ||
{ | ||
Class<? extends Tank> tank; | ||
String name; | ||
int weight; | ||
public final Class<? extends Tank> tank; | ||
public final String name; | ||
public final double weight; | ||
|
||
protected double startWeight; | ||
protected double endWeight; | ||
|
||
public TankRegistry(Class<? extends Tank> tank, String name, int weight) | ||
public TankRegistry(Registry r, Class<? extends Tank> tank, String name, double weight) | ||
{ | ||
this.tank = tank; | ||
this.name = name; | ||
this.weight = weight; | ||
|
||
this.startWeight = r.maxTankWeight; | ||
r.maxTankWeight += weight; | ||
this.endWeight = r.maxTankWeight; | ||
|
||
r.tankRegistries.add(this); | ||
} | ||
|
||
public Tank getTank(double x, double y, double a) | ||
{ | ||
try | ||
{ | ||
return tank.getConstructor(double.class, double.class, double.class).newInstance(x, y, a); | ||
} | ||
catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) | ||
{ | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
public TankRegistry getRandomTank() | ||
{ | ||
double random = Math.random() * maxTankWeight; | ||
|
||
for (int i = 0; i < tankRegistries.size(); i++) | ||
{ | ||
TankRegistry r = tankRegistries.get(i); | ||
|
||
if (random >= r.startWeight && random < r.endWeight) | ||
{ | ||
return r; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public TankRegistry getRegistry(String name) | ||
{ | ||
for (int i = 0; i < tankRegistries.size(); i++) | ||
{ | ||
TankRegistry r = tankRegistries.get(i); | ||
|
||
if (r.name.equals(name)) | ||
{ | ||
return r; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters