Projekt Alien Defence von Schick&Kannix eingekauft
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package controller;
|
||||
|
||||
import model.Level;
|
||||
import model.persistence.IPersistance;
|
||||
import toDo.User;
|
||||
|
||||
public class AlienDefenceController {
|
||||
|
||||
//Teilcontroller
|
||||
private GameController gameController;
|
||||
private LevelController levelController;
|
||||
private TargetController targetController;
|
||||
private AttemptController attemptController;
|
||||
//TODO UserController implementieren
|
||||
|
||||
//Persistenz
|
||||
private IPersistance alienDefenceModel;
|
||||
|
||||
public AlienDefenceController(IPersistance alienDefenceModel) {
|
||||
super();
|
||||
this.alienDefenceModel = alienDefenceModel;
|
||||
this.attemptController = new AttemptController(alienDefenceModel);
|
||||
this.levelController = new LevelController(alienDefenceModel);
|
||||
this.targetController = new TargetController(alienDefenceModel);
|
||||
}
|
||||
|
||||
public IPersistance getAlienDefenceModel() {
|
||||
return alienDefenceModel;
|
||||
}
|
||||
|
||||
public AttemptController getAttemptController() {
|
||||
return attemptController;
|
||||
}
|
||||
|
||||
public LevelController getLevelController() {
|
||||
return levelController;
|
||||
}
|
||||
|
||||
public TargetController getTargetController() {
|
||||
return targetController;
|
||||
}
|
||||
|
||||
public GameController startGame(Level selectedLevel, User user) {
|
||||
this.gameController = new GameController(selectedLevel, user, this);
|
||||
return this.gameController;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package controller;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import model.Level;
|
||||
import model.persistence.IAttemptPersistance;
|
||||
import model.persistence.IPersistance;
|
||||
|
||||
public class AttemptController {
|
||||
|
||||
private IAttemptPersistance attemptPersistance;
|
||||
|
||||
/**
|
||||
* erstellt ein neues Objekt eines AttemptController welches Attemptobjekte in
|
||||
* der übergebenen Datenhaltung persisiert
|
||||
*
|
||||
* @param alienDefenceModel.getAttemptDB()
|
||||
* Persistenzklasse der Attemptobjekte
|
||||
*/
|
||||
public AttemptController(IPersistance alienDefenceModel) {
|
||||
this.attemptPersistance = alienDefenceModel.getAttemptPersistance();
|
||||
}
|
||||
|
||||
public Vector<Vector<String>> getAllAttemptsPerLevel(Level level, int game_id) {
|
||||
return attemptPersistance.getAllAttemptsPerLevel(level, game_id);
|
||||
}
|
||||
|
||||
public int getPlayerPosition() {
|
||||
return attemptPersistance.getPlayerPosition();
|
||||
}
|
||||
|
||||
public void deleteHighscore(int level_id) {
|
||||
attemptPersistance.deleteHighscore(level_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* calculates points from attempt for highscore TODO create formula here
|
||||
*
|
||||
* @param level Levelobjekt
|
||||
* @param hitcounter Controllerobjekt das die Treffer und Reaktionszeiten misst
|
||||
* @return points
|
||||
*/
|
||||
public int calculatePoints(Level level, HitCounter hitcounter) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package controller;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import model.Level;
|
||||
import model.Point;
|
||||
import model.Target;
|
||||
import toDo.User;
|
||||
import view.menue.Highscore;
|
||||
|
||||
public class GameController {
|
||||
|
||||
private AlienDefenceController alienDefenceController;
|
||||
private Level currentLevel;
|
||||
private User currentUser;
|
||||
private boolean hasWon;
|
||||
private HitCounter hitCounter;
|
||||
private boolean setHighscore = true;
|
||||
private long timer = 0L, starttimer = 0L, endtimer = 0L;
|
||||
private List<Target> targets;
|
||||
|
||||
public GameController(Level selectedLevel, User user, AlienDefenceController alienDefenceController) {
|
||||
|
||||
this.currentLevel = selectedLevel;
|
||||
this.currentUser = user;
|
||||
this.hitCounter = new HitCounter(currentLevel.getTargets().size());
|
||||
this.targets = currentLevel.getTargets();
|
||||
this.hasWon = false;
|
||||
this.alienDefenceController = alienDefenceController;
|
||||
}
|
||||
|
||||
public void doLogik(long delta) {
|
||||
timer += delta;
|
||||
|
||||
// time for next logic step
|
||||
|
||||
// section to run once a second
|
||||
if (timer >= 1000) {
|
||||
timer -= 100;
|
||||
|
||||
if (this.isAllTargetDestroyed()) {
|
||||
this.hasWon = true;
|
||||
}
|
||||
|
||||
if ((this.timeleft() <= 0 || this.isHasWon()) && this.setHighscore) {
|
||||
|
||||
setHighscore = false; //damit keine doppelten Eintrüge erzeugt werden
|
||||
|
||||
// TODO Highscoreeintrag erzeugen Highscore >> F_user_id, F_level_id, shots,
|
||||
// hits, reaction_time
|
||||
System.out.println("Highscoreeintrag für " + currentLevel.getName() + ": " + currentUser.getFirst_name()
|
||||
+ " hat " + this.getShotsFired() + " mal auf " + this.getTargets().size()
|
||||
+ " Ziele gefeuert und " + this.getHits() + " mal getroffen.");
|
||||
System.out.println("Highscorepunkte: " + alienDefenceController.getAttemptController().calculatePoints(this.currentLevel, this.hitCounter));
|
||||
int insert_id = 0;
|
||||
// AttemptDB attemptDB =
|
||||
// this.alienDefenceController.getAlienDefenceModel().getAttemptPersistance();
|
||||
// int insert_id = attemptDB.createHighscoreEntry(currentUser.getP_user_id(),
|
||||
// currentLevel.getLevel_id(),
|
||||
// this.getShotsFired(), this.getHits(), relReactionDiffernce);
|
||||
|
||||
new Highscore(this.alienDefenceController.getAttemptController(), currentLevel,
|
||||
insert_id);
|
||||
|
||||
}
|
||||
}
|
||||
// steps that use only delta
|
||||
}
|
||||
|
||||
// fire
|
||||
public void fireShot(int x, int y) {
|
||||
boolean isHit = false;
|
||||
|
||||
// iterates through all targets and checks if a target is hit
|
||||
for (Target t : targets) {
|
||||
if (t.getHitbox().contains(new Point(x, y)) && !t.isHit()) {
|
||||
|
||||
long differnce = time() - t.getTime();
|
||||
this.hitCounter.addReactionTime(differnce);
|
||||
|
||||
if (differnce > 0 && (t.getTime() + t.getDuration()) > time()) {
|
||||
isHit = true;
|
||||
t.setHit(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hit
|
||||
if (isHit) {
|
||||
this.hitCounter.hit();
|
||||
} else
|
||||
this.hitCounter.miss();
|
||||
|
||||
}
|
||||
|
||||
public double getAccuracy() {
|
||||
return hitCounter.getAccuracy();
|
||||
}
|
||||
|
||||
public Level getCurrLevel() {
|
||||
return currentLevel;
|
||||
}
|
||||
|
||||
public int getHits() {
|
||||
return this.hitCounter.getHit();
|
||||
}
|
||||
|
||||
public int getShotsFired() {
|
||||
return this.hitCounter.getShots();
|
||||
}
|
||||
|
||||
public List<Target> getTargets() {
|
||||
return (this.targets != null) ? this.targets : (new LinkedList<Target>());
|
||||
}
|
||||
|
||||
private boolean isAllTargetDestroyed() {
|
||||
for (Target t : this.targets) {
|
||||
if (!t.isHit())
|
||||
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isHasWon() {
|
||||
return this.hasWon;
|
||||
}
|
||||
|
||||
public void setCurrLevel(Level currLevel) {
|
||||
this.currentLevel = currLevel;
|
||||
}
|
||||
|
||||
public void setHasWon(boolean hasWon) {
|
||||
this.hasWon = hasWon;
|
||||
}
|
||||
|
||||
public void startLevel() {
|
||||
this.starttimer = System.currentTimeMillis();
|
||||
this.endtimer = this.starttimer + currentLevel.getDuration();
|
||||
}
|
||||
|
||||
public long time() {
|
||||
return System.currentTimeMillis() - this.starttimer;
|
||||
}
|
||||
|
||||
public long timeleft() {
|
||||
return (starttimer == 0) ? this.currentLevel.getDuration()
|
||||
: (this.endtimer - System.currentTimeMillis()) / 1000;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package controller;
|
||||
|
||||
public class HitCounter {
|
||||
|
||||
private int shots, hit, targets;
|
||||
private long sumReactionDiffernce;
|
||||
|
||||
/**
|
||||
* erstellt einen neuen Hitcounter
|
||||
*
|
||||
* @param target
|
||||
* Anzahl der zu zühlenden Ziele, übergeben Sie die maximale Anzahl
|
||||
* der Ziele des Levels
|
||||
*/
|
||||
public HitCounter(int target) {
|
||||
this.reset(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt die Anzahl der abgegebenen Schüsse zurück
|
||||
* @return Anzahl der abgegebenen Schüsse
|
||||
*/
|
||||
public int getShots() {
|
||||
return shots;
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt die Anzahl der Treffer zurück
|
||||
* @return Anzahl der Treffer
|
||||
*/
|
||||
public int getHit() {
|
||||
return hit;
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt die Anzahl der noch nicht getroffenen Targets zurück
|
||||
* @return Anzahl der noch nicht getroffenen Targets
|
||||
*/
|
||||
public int getTargets() {
|
||||
return targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* setzt den Wert der Target auf dem im Parameter übergebenen Wert
|
||||
* @param targets neue Targetanzahl nicht getroffener Ziele
|
||||
*/
|
||||
public void setTargets(int targets) {
|
||||
this.targets = targets;
|
||||
}
|
||||
|
||||
public long getReactionTime() {
|
||||
if(this.getHit() == 0)
|
||||
return 0;
|
||||
else
|
||||
return this.sumReactionDiffernce / this.getHit();
|
||||
}
|
||||
|
||||
public long getSumReactionDiffernce() {
|
||||
return sumReactionDiffernce;
|
||||
}
|
||||
|
||||
public void addReactionTime(long time) {
|
||||
this.sumReactionDiffernce += time;
|
||||
}
|
||||
|
||||
/**
|
||||
* registriert einen Treffer
|
||||
*/
|
||||
public void hit() {
|
||||
this.hit++;
|
||||
this.shots++;
|
||||
}
|
||||
|
||||
/**
|
||||
* registriert einen Fehlschuss
|
||||
*/
|
||||
public void miss() {
|
||||
this.shots++;
|
||||
}
|
||||
|
||||
/**
|
||||
* berechnet die Treffergenauigkeit
|
||||
* @return Wert zwischen 0-100% der Treffergenauigkeit auf 2 Nachkommastellen gerundet
|
||||
*/
|
||||
public double getAccuracy() {
|
||||
return Math.round((100.0 / this.shots * this.hit) * 100.0) / 100.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* setzt den HitCounter zurück
|
||||
* @param target
|
||||
*/
|
||||
public void reset(int target) {
|
||||
this.targets = target;
|
||||
this.shots = 0;
|
||||
this.hit = 0;
|
||||
this.sumReactionDiffernce = 0L;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import model.Level;
|
||||
import model.persistence.ILevelPersistance;
|
||||
import model.persistence.IPersistance;
|
||||
|
||||
public class LevelController {
|
||||
|
||||
private ILevelPersistance levelPersistance;
|
||||
public final String DEFAULT_LEVELNAME = "unnamed";
|
||||
public final String DEFAULT_BACKGROUND_PICTURE_URL = "background_1.jpg";
|
||||
public final long DEFAULT_DURATION = 90000L;
|
||||
|
||||
/**
|
||||
* erstellt ein neues Objekt eines LevelControllers, welche Levelobjekte der
|
||||
* übergebenen Datenhaltung persistiert
|
||||
*
|
||||
* @param alienDefenceModel
|
||||
* Persistenzklasse des gesamten Model
|
||||
*/
|
||||
public LevelController(IPersistance alienDefenceModel) {
|
||||
this.levelPersistance = alienDefenceModel.getLevelPersistance();
|
||||
}
|
||||
|
||||
/**
|
||||
* legt ein neues Level mit den Standardwerten an
|
||||
*
|
||||
* @return Levelobjekt
|
||||
*/
|
||||
public Level createLevel() {
|
||||
int level_id = levelPersistance.createLevel(DEFAULT_LEVELNAME, DEFAULT_BACKGROUND_PICTURE_URL, DEFAULT_DURATION);
|
||||
return new Level(level_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt das konkrete Level zurück
|
||||
*
|
||||
* @param level_id
|
||||
* @return Levelobjekt oder null bei nicht vorhandener Level_id
|
||||
*/
|
||||
public Level readLevel(int level_id) {
|
||||
List<Level> levels = levelPersistance.readAllLevel();
|
||||
for (int i = 0; i < levels.size(); i++) {
|
||||
if (level_id == levels.get(i).getLevel_id()) {
|
||||
return levels.get(i);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* liest alle Level aus der DB
|
||||
*
|
||||
* @return Levelliste
|
||||
*/
|
||||
public List<Level> readAllLevels() {
|
||||
return this.levelPersistance.readAllLevel();
|
||||
}
|
||||
|
||||
/**
|
||||
* validiert die ünderungen und speichert das Level ab
|
||||
*
|
||||
* @param lvl
|
||||
*/
|
||||
public void updateLevel(Level lvl) {
|
||||
// TODO write Code to validate Level-Data
|
||||
levelPersistance.updateLevel(lvl);
|
||||
}
|
||||
|
||||
/**
|
||||
* lüscht ein Level aus der Datenhaltung
|
||||
*
|
||||
* @param level_id
|
||||
*/
|
||||
public void deleteLevel(int level_id) {
|
||||
this.levelPersistance.deleteLevel(level_id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import model.Level;
|
||||
import model.Target;
|
||||
import model.persistence.IPersistance;
|
||||
import model.persistence.ITargetPersistance;
|
||||
|
||||
public class TargetController {
|
||||
|
||||
private ITargetPersistance targetPersistance;
|
||||
|
||||
/**
|
||||
* erstellt ein neues Objekt eines TargetControllers welches Targetobjekte in
|
||||
* der übergebenen Datenhaltung persisiert
|
||||
*
|
||||
* @param alienDefenceModel
|
||||
* Persistenzklasse der Targetobjekte
|
||||
*/
|
||||
public TargetController(IPersistance alienDefenceModel) {
|
||||
this.targetPersistance = alienDefenceModel.getTargetPersistance();
|
||||
}
|
||||
|
||||
/**
|
||||
* fügt dem gewühlten Level ein neues Target hinzu
|
||||
*
|
||||
* @param target
|
||||
* neues Ziel
|
||||
* @param lvl
|
||||
* ausgewühltes Level
|
||||
* @return -1 wenn die Operation nicht geklappt hat, sonst die Target_id des
|
||||
* neuen Ziels
|
||||
*/
|
||||
public Target createTarget(Level lvl) {
|
||||
Target target = new Target(600, 400, 150, 50, 1000, 1000, "ufo_3.png");
|
||||
int image_id = 1;
|
||||
int target_id = targetPersistance.createTarget(target, lvl.getLevel_id(), image_id);
|
||||
target.setTarget_id(target_id);
|
||||
lvl.getTargets().add(target);
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* gibt alle Targets zu einem Level aus der Datenhaltung aus
|
||||
*
|
||||
* @param level_id
|
||||
* eindeutige Nummer des Levels
|
||||
* @return Liste mit Zielen des Levels
|
||||
*/
|
||||
public List<Target> readTargets(int level_id) {
|
||||
return this.targetPersistance.readAllTargetsPerLevel(level_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* tauscht ein Target im Levelobjekt aus und persistiert die ünderungen
|
||||
*
|
||||
* @param lvl
|
||||
* zu welchem Level das Target gehürt
|
||||
* @param target
|
||||
* das zu ündernde Target
|
||||
* @return true wenn das angegebene Target in der Liste gefunden wurde, false
|
||||
* wenn nicht
|
||||
*/
|
||||
public boolean updateTarget(Level lvl, Target target) {
|
||||
boolean success = false;
|
||||
// Target im Level austauschen
|
||||
ListIterator<Target> iterator = lvl.getTargets().listIterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (iterator.next().getTarget_id() == target.getTarget_id()) {
|
||||
iterator.set(target);
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
// Target in der Persistenz ündern
|
||||
if (success) {
|
||||
this.targetPersistance.updateTarget(target);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* lüscht ein Target aus dem Levelobjekt und persistiert die ünderungen
|
||||
*
|
||||
* @param lvl
|
||||
* zu welchem Level das Target gehürt
|
||||
* @param target
|
||||
* das zu lüschende Target
|
||||
* @return true wenn das angegebene Target in der Liste gefunden wurde, false
|
||||
* wenn nicht
|
||||
*/
|
||||
public boolean deleteTarget(Level lvl, int target_id) {
|
||||
boolean success = false;
|
||||
|
||||
ListIterator<Target> iterator = lvl.getTargets().listIterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (iterator.next().getTarget_id() == target_id) {
|
||||
iterator.remove();
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Target in Persistenz lüschen
|
||||
if (success) {
|
||||
this.targetPersistance.deleteTarget(target_id);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package model;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Vector;
|
||||
|
||||
import toDo.User;
|
||||
|
||||
public class Attempt {
|
||||
|
||||
// Attribute
|
||||
private User user;
|
||||
private Level level;
|
||||
private int placement;
|
||||
private double hitsShots;
|
||||
private double hitsTargets;
|
||||
private double reactionDuration;
|
||||
private double highscorePoints;
|
||||
private LocalDateTime datetime;
|
||||
|
||||
// Konstruktor
|
||||
public Attempt(int place, Level level, double hitsShots, double hitsTargets, double reactionDuration,
|
||||
double highscoreFormula, User user, LocalDateTime datetime) {
|
||||
super();
|
||||
this.placement = place;
|
||||
this.level = level;
|
||||
this.reactionDuration = reactionDuration;
|
||||
this.highscorePoints = highscoreFormula;
|
||||
this.user = user;
|
||||
this.hitsShots = hitsShots;
|
||||
this.hitsTargets = hitsTargets;
|
||||
this.datetime = datetime;
|
||||
}
|
||||
|
||||
public Vector<String> getRowVector() {
|
||||
Vector<String> v = new Vector<String>(8);
|
||||
v.addElement(String.valueOf(this.placement));
|
||||
v.addElement(this.user.getFirst_name() + " " + this.user.getSur_name());
|
||||
v.addElement(this.datetime.getDayOfMonth() +"."+this.datetime.getMonthValue() + "." + this.datetime.getYear());;
|
||||
v.addElement(this.datetime.getHour() + ":" + this.datetime.getMinute());
|
||||
v.addElement(String.valueOf((int) this.hitsTargets));
|
||||
v.addElement(String.valueOf((int) this.hitsShots));
|
||||
v.addElement(String.valueOf((int) this.reactionDuration));
|
||||
v.addElement(String.valueOf((int) this.highscorePoints));
|
||||
return v;
|
||||
}
|
||||
|
||||
public Level getLevel() {
|
||||
return this.level;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package model;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Hitbox {
|
||||
private Point point;
|
||||
private int width;
|
||||
private int height;
|
||||
public static final int MAXWIDTH = 1200;
|
||||
public static final int MAXHEIGHT = 1000;
|
||||
private static Random rand = new Random();
|
||||
|
||||
public Hitbox() {
|
||||
this.point = new Point();
|
||||
this.width = 0;
|
||||
this.height = 0;
|
||||
}
|
||||
|
||||
public Hitbox(int x, int y, int breite, int hoehe) {
|
||||
super();
|
||||
this.point = new Point(x, y);
|
||||
this.setWidth(breite);
|
||||
this.setHeight(hoehe);
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return this.point.getX();
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.point.setX(x);
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return this.point.getY();
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.point.setY(y);
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
public void setWidth(int width) {
|
||||
this.width = Math.abs(width);
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = Math.abs(height);
|
||||
}
|
||||
|
||||
public boolean contains(Point p) {
|
||||
return contains(p.getX(), p.getY());
|
||||
}
|
||||
|
||||
public boolean contains(int x, int y) {
|
||||
return this.point.getX() <= x && x <= this.point.getX() + this.width && this.point.getY() <= y
|
||||
&& y <= this.point.getY() + this.height;
|
||||
}
|
||||
|
||||
public boolean contains(Hitbox hitbox) {
|
||||
Point upperleft = new Point(hitbox.getX(), hitbox.getY());
|
||||
Point lowerright = new Point(hitbox.getX() + hitbox.getWidth(), hitbox.getY() + hitbox.getHeight());
|
||||
return contains(upperleft) && contains(lowerright);
|
||||
}
|
||||
|
||||
public static Hitbox generateRandomHitbox() {
|
||||
int x = rand.nextInt(MAXWIDTH + 1);
|
||||
int y = rand.nextInt(MAXHEIGHT + 1);
|
||||
int width = rand.nextInt(MAXWIDTH - x + 1);
|
||||
int height = rand.nextInt(MAXHEIGHT - y + 1);
|
||||
return new Hitbox(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Hitbox [x=" + this.point.getX() + ", y=" + this.point.getY() + ", width=" + width + ", height=" + height
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Level {
|
||||
|
||||
/** unique value */
|
||||
private int level_id;
|
||||
/** name of that level */
|
||||
private String name;
|
||||
/** List of Targets */
|
||||
private List<Target> targets;
|
||||
/** duration a level lasts im ms */
|
||||
private long duration;
|
||||
/** default duration for a level is 90s */
|
||||
public final long DEFAULT_DURATION = 90000L;
|
||||
/** path to background image */
|
||||
private String backgroundImage;
|
||||
|
||||
public Level() {
|
||||
this.targets = new ArrayList<Target>(100);
|
||||
this.name = "unnamed";
|
||||
this.duration = DEFAULT_DURATION;
|
||||
this.backgroundImage = "background_1.jpg";
|
||||
}
|
||||
|
||||
public Level(int level_id) {
|
||||
this();
|
||||
this.level_id = level_id;
|
||||
}
|
||||
|
||||
public int getLevel_id() {
|
||||
return this.level_id;
|
||||
}
|
||||
|
||||
public void setLevel_id(int level_id) {
|
||||
this.level_id = level_id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Target> getTargets() {
|
||||
return this.targets;
|
||||
}
|
||||
|
||||
public void setTargets(List<Target> targets) {
|
||||
this.targets = targets;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return this.duration;
|
||||
}
|
||||
|
||||
public void setDuration(long duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public String getBackgroundImage() {
|
||||
return backgroundImage;
|
||||
};
|
||||
|
||||
public void setBackgroundImage(String backgroundImage) {
|
||||
this.backgroundImage = backgroundImage;
|
||||
}
|
||||
|
||||
public String[][] getTargetsAsTableModel() {
|
||||
String[][] result = new String[this.targets.size()][];
|
||||
int i = 0;
|
||||
for (Target t : this.targets) {
|
||||
result[i++] = t.getData();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String[] getData() {
|
||||
return new String[] { "" + this.level_id, this.name, this.backgroundImage, "" + this.targets.size(),
|
||||
"" + this.duration };
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Level [level_id=" + level_id + ", name=" + name + ", duration=" + duration + ", backgroundImage="
|
||||
+ backgroundImage + ", targets=" + targets + "]";
|
||||
}
|
||||
|
||||
/** use this Level only for test porposes */
|
||||
public static Level getDefaultLevel() {
|
||||
Level level = new Level();
|
||||
// Objekte des Levels hier eintragen
|
||||
level.targets.add(new Target(100, 100, 75, 150, 0, 500000, "lemon.png"));
|
||||
level.targets.add(new Target(200, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(300, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(400, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(500, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(600, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(700, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(800, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(900, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1000, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1100, 100, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(100, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(200, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(300, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(400, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(500, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(600, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(700, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(800, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(900, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1000, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1100, 300, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(100, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(200, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(300, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(400, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(500, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(600, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(700, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(800, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(900, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1000, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1100, 500, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(100, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(200, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(300, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(400, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(500, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(600, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(700, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(800, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(900, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1000, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.targets.add(new Target(1100, 700, 75, 150, 0, 500000, "ballon.png"));
|
||||
level.duration = 25000L;
|
||||
level.name = "DefaultTestLevel 001";
|
||||
level.level_id = 1;
|
||||
return level;
|
||||
}
|
||||
|
||||
public static String[] getLevelDescriptions() {
|
||||
return new String[] { "Nr.", "Name", "Hintergrund", "Targets", "Dauer" };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package model;
|
||||
|
||||
public class Point {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Point() {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
}
|
||||
|
||||
public Point(int x, int y) {
|
||||
super();
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public boolean equals(Point p) {
|
||||
return this.x == p.getX() && this.y == p.getY();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Punkt [x=" + x + ", y=" + y + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package model;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Target {
|
||||
|
||||
private int target_id;
|
||||
/** the area of a target */
|
||||
private Hitbox hitbox;
|
||||
/** time a target appears after game begins */
|
||||
private long time;
|
||||
/** time a target is able to hit */
|
||||
private long duration;
|
||||
private boolean hit;
|
||||
private String imageAddress;
|
||||
|
||||
/** image of target */
|
||||
|
||||
public Target() {
|
||||
super();
|
||||
this.target_id = 0;
|
||||
this.hitbox = new Hitbox(100, 100, 150, 50);
|
||||
this.time = 1000;
|
||||
this.duration = Long.MAX_VALUE;
|
||||
this.setHit(false);
|
||||
this.setImageAddress("ufo_1.png");
|
||||
}
|
||||
|
||||
public Target(int x, int y, int width, int height, long time, long duration, String image) {
|
||||
super();
|
||||
this.target_id = 0;
|
||||
this.hitbox = new Hitbox(x, y, width, height);
|
||||
this.time = time;
|
||||
this.duration = duration;
|
||||
this.setHit(false);
|
||||
this.setImageAddress(image);
|
||||
}
|
||||
|
||||
public Target(Hitbox hitbox, long time, long duration) {
|
||||
super();
|
||||
this.target_id = 0;
|
||||
this.hitbox = hitbox;
|
||||
this.time = time;
|
||||
this.duration = duration;
|
||||
this.setHit(false);
|
||||
}
|
||||
|
||||
public int getTarget_id() {
|
||||
return target_id;
|
||||
}
|
||||
|
||||
public void setTarget_id(int target_id) {
|
||||
this.target_id = target_id;
|
||||
}
|
||||
|
||||
public Hitbox getHitbox() {
|
||||
return hitbox;
|
||||
}
|
||||
|
||||
public void setHitbox(Hitbox hitbox) {
|
||||
this.hitbox = hitbox;
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public long getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public void setDuration(long duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
public boolean isHit() {
|
||||
return hit;
|
||||
}
|
||||
|
||||
public void setHit(boolean hit) {
|
||||
this.hit = hit;
|
||||
}
|
||||
|
||||
public String getImageAddress() {
|
||||
return imageAddress;
|
||||
}
|
||||
|
||||
public void setImageAddress(String imageAdress) {
|
||||
this.imageAddress = imageAdress;
|
||||
}
|
||||
|
||||
public static Target getRandomTarget(int screenResolution_X, int screenResolution_Y) {
|
||||
Random rand = new Random();
|
||||
return new Target(rand.nextInt(screenResolution_X - 50), rand.nextInt(screenResolution_Y - 50), 350, 129, 0,
|
||||
90000, "ufo_4.png");
|
||||
}
|
||||
|
||||
public String[] getData() {
|
||||
return new String[] { this.target_id + "", this.hitbox.getX() + "", this.hitbox.getY() + "",
|
||||
this.hitbox.getWidth() + "", this.hitbox.getHeight() + "", this.getImageAddress(), this.getTime() + "",
|
||||
this.getDuration() + "" };
|
||||
}
|
||||
|
||||
public static String[] getTableDescriptions() {
|
||||
return new String[] { "Nr.", "X", "Y", "Breite", "Hühe", "Bild", "Startzeit", "Dauer" };
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Target [target_id=" + target_id + ", hitbox=" + hitbox + ", time=" + time + ", duration=" + duration
|
||||
+ ", hit=" + hit + ", imageAddress=" + imageAddress + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package model.persistence;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import model.Level;
|
||||
|
||||
public interface IAttemptPersistance {
|
||||
|
||||
int createHighscoreEntry(int F_user_id, int F_level_id, int shots, int hits, long reaction_time);
|
||||
int getPlayerPosition();
|
||||
Vector<Vector<String>> getAllAttemptsPerLevel(Level level, int game_id);
|
||||
//no update needed
|
||||
void deleteHighscore(int level_id);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package model.persistence;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import model.Level;
|
||||
|
||||
public interface ILevelPersistance {
|
||||
|
||||
/**
|
||||
* legt in der Datenbank ein neues Level an
|
||||
*
|
||||
* @return level_id für das neue Level
|
||||
*/
|
||||
int createLevel(String levelname, String backgroundUrl, long duration);
|
||||
|
||||
/**
|
||||
* gibt alle Level aus der Datenbank als Liste zurück
|
||||
*
|
||||
* @return Liste aller Level
|
||||
*/
|
||||
List<Level> readAllLevel();
|
||||
|
||||
/**
|
||||
* aktualisiert die Daten eines Levels
|
||||
*
|
||||
* @param lvl
|
||||
* ein Levelobjekt
|
||||
*/
|
||||
void updateLevel(Level lvl);
|
||||
|
||||
/**
|
||||
* lüscht ein Level aus der Datenbank
|
||||
*
|
||||
* @param P_level_id
|
||||
*/
|
||||
void deleteLevel(int level_id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package model.persistence;
|
||||
|
||||
public interface IPersistance {
|
||||
|
||||
IAttemptPersistance getAttemptPersistance();
|
||||
ILevelPersistance getLevelPersistance();
|
||||
ITargetPersistance getTargetPersistance();
|
||||
IUserPersistance getUserPersistance();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package model.persistence;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import model.Target;
|
||||
|
||||
public interface ITargetPersistance {
|
||||
|
||||
int createTarget(Target target, int level_id, int image_id);
|
||||
|
||||
List<Target> readAllTargetsPerLevel(int level_id);
|
||||
|
||||
void updateTarget(Target target);
|
||||
|
||||
void deleteTarget(int target_id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package model.persistence;
|
||||
|
||||
import toDo.User;
|
||||
|
||||
public interface IUserPersistance {
|
||||
|
||||
User readUser(String username);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package model.persistenceDB;
|
||||
|
||||
public class AccessDB {
|
||||
|
||||
// Attribute
|
||||
private String dbName;
|
||||
private String user;
|
||||
private String password;
|
||||
private String url;
|
||||
|
||||
// Konstruktor
|
||||
public AccessDB() {
|
||||
this.url = "jdbc:mysql://localhost:3306/";
|
||||
this.dbName = "alien_defence";
|
||||
this.user = "root";
|
||||
this.password = "";
|
||||
}
|
||||
|
||||
// Getter und Setter
|
||||
public String getDbName() {
|
||||
return dbName;
|
||||
}
|
||||
|
||||
public void setDbName(String dbName) {
|
||||
this.dbName = dbName;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getFullURL() {
|
||||
return this.url + this.dbName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package model.persistenceDB;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.Vector;
|
||||
|
||||
import model.Attempt;
|
||||
import model.Level;
|
||||
import model.persistence.IAttemptPersistance;
|
||||
import toDo.User;
|
||||
|
||||
public class AttemptDB implements IAttemptPersistance {
|
||||
|
||||
// Attribute
|
||||
private AccessDB dbAccess;
|
||||
private int playerPosition = -100000;
|
||||
|
||||
// Konstuktor
|
||||
public AttemptDB(AccessDB dbAccess) {
|
||||
this.dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
// Eintrag des Spiels in die Highscore wird vorgenommen - Aufgerufen wird von
|
||||
// der Klasse GameController
|
||||
public int createHighscoreEntry(int F_user_id, int F_level_id, int shots, int hits, long reaction_time) {
|
||||
String sql = "INSERT INTO attempts (F_user_id, F_level_id, shots, hits, reaction_time, date, time)"
|
||||
+ " values (?, ?, ?, ?, ?, ?, ?)";
|
||||
int last_inserted_id = 0;
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword());
|
||||
PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);) {
|
||||
|
||||
// Aktuelle Datum und Uhrzeit holen und in Strings speichern
|
||||
LocalDate datum = LocalDate.now(); // Erstellt Datum-Objekt heute
|
||||
LocalTime time = LocalTime.now();
|
||||
String now = time.getHour() + ":" + time.getMinute() + ":" + time.getSecond();
|
||||
|
||||
statement.setInt(1, F_user_id);
|
||||
statement.setInt(2, F_level_id);
|
||||
statement.setInt(3, shots);
|
||||
statement.setInt(4, hits);
|
||||
statement.setLong(5, reaction_time);
|
||||
statement.setString(6, datum.toString());
|
||||
statement.setString(7, now);
|
||||
|
||||
statement.execute();
|
||||
|
||||
ResultSet rs = statement.getGeneratedKeys(); // Letzte eingtragene ID
|
||||
if (rs.next()) {
|
||||
last_inserted_id = rs.getInt(1);
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
return last_inserted_id;
|
||||
}
|
||||
|
||||
public Vector<Vector<String>> getAllAttemptsPerLevel(Level level, int game_id) {
|
||||
Vector<Vector<String>> vecTests = null;
|
||||
String url = dbAccess.getUrl() + dbAccess.getDbName();
|
||||
|
||||
String highscoreFormula = "(hits / targets * 1000)*0.4 + (hits / shots * 1000)*0.2 + (1000 - (reaction_time / sum_duration * 1000))*0.4";
|
||||
|
||||
String query = "SELECT P_attempt_id, targets, shots, hits, reaction_time, first_name, sur_name, date, time, (hits / targets * 1000) AS hitsTargets, (hits / shots * 1000) AS hitsShots, (1000 - (reaction_time / sum_duration * 1000)) AS reactionDuration,"
|
||||
+ highscoreFormula + "AS highscoreFormula FROM "
|
||||
+ "(SELECT F_level_id, COUNT(P_target_id) AS targets, SUM(duration ) AS sum_duration FROM `targets` WHERE F_level_id = "
|
||||
+ level.getLevel_id() + ") AS count_targets INNER JOIN "
|
||||
+ "attempts ON attempts.F_level_id = count_targets.F_level_id INNER JOIN "
|
||||
+ "users ON P_user_id = F_user_id ORDER BY highscoreFormula DESC, reactionDuration DESC";
|
||||
|
||||
try {
|
||||
Connection con = DriverManager.getConnection(url, dbAccess.getUser(), dbAccess.getPassword());
|
||||
Statement stmt = con.createStatement();
|
||||
ResultSet results = stmt.executeQuery(query);
|
||||
vecTests = getVecTest(level, game_id, results);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
return vecTests;
|
||||
}
|
||||
|
||||
public int getPlayerPosition() {
|
||||
return this.playerPosition;
|
||||
}
|
||||
|
||||
// 2 Diese Methode wird vom Konstruktor aufgerufen.
|
||||
// Daten aus der Datenbank werden in einem zweidimensionalen Vector gespeichert.
|
||||
private Vector<Vector<String>> getVecTest(Level level, int gameId, ResultSet results) {
|
||||
|
||||
Vector<Vector<String>> vecTests = new Vector<Vector<String>>();
|
||||
try {
|
||||
int nummerierung = 0;
|
||||
while (results.next()) {
|
||||
|
||||
nummerierung++;
|
||||
|
||||
//Markierung des Spielers für Highscoreansicht
|
||||
//TODO Verstoü gegen 3 Schichtenarchitektur beheben
|
||||
if (results.getInt("P_attempt_id") == gameId)
|
||||
this.playerPosition = nummerierung - 1;
|
||||
|
||||
LocalDate date = results.getDate("date").toLocalDate();
|
||||
User user = new User();
|
||||
user.setFirst_name(results.getString("first_name"));
|
||||
user.setSur_name(results.getString("sur_name"));
|
||||
LocalDateTime dateTime = date.atTime(results.getTime("time").toLocalTime());
|
||||
Attempt tmp = new Attempt(nummerierung, level, results.getDouble("hitsShots"),
|
||||
results.getDouble("hitsTargets"), results.getInt("reactionDuration"),
|
||||
results.getInt("highscoreFormula"), user, dateTime);
|
||||
|
||||
Vector<String> v = tmp.getRowVector();
|
||||
vecTests.add(v);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
return vecTests;
|
||||
}
|
||||
|
||||
// lüscht alle Eintrüge
|
||||
public void deleteHighscore(int level_id) {
|
||||
|
||||
String url = dbAccess.getFullURL();
|
||||
try {
|
||||
|
||||
Connection con = DriverManager.getConnection(url, dbAccess.getUser(), dbAccess.getPassword());
|
||||
|
||||
String query = "DELETE FROM attempts WHERE F_level_id = ?";
|
||||
|
||||
PreparedStatement preparedStmt = con.prepareStatement(query);
|
||||
preparedStmt.setInt(1, level_id);
|
||||
|
||||
preparedStmt.execute();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package model.persistenceDB;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import model.Level;
|
||||
import model.persistence.ILevelPersistance;
|
||||
import model.persistence.ITargetPersistance;
|
||||
|
||||
public class LevelDB implements ILevelPersistance {
|
||||
|
||||
private AccessDB dbAccess;
|
||||
|
||||
public LevelDB(AccessDB dbAccess) {
|
||||
this.dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
public int createLevel(String levelname, String backgroundUrl, long duration) {
|
||||
String sql = "INSERT INTO levels (name, background, duration) VALUES (?, ?, ?);";
|
||||
|
||||
int lastKey = -1;
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword());
|
||||
PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);) {
|
||||
|
||||
statement.setString(1, levelname);
|
||||
statement.setString(2, backgroundUrl);
|
||||
statement.setLong(3, duration);
|
||||
|
||||
statement.execute();
|
||||
|
||||
ResultSet generatedKeys = statement.getGeneratedKeys();
|
||||
if (generatedKeys.next()) {
|
||||
lastKey = generatedKeys.getInt(1);
|
||||
}
|
||||
generatedKeys.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return lastKey;
|
||||
}
|
||||
|
||||
public List<Level> readAllLevel() {
|
||||
String sql = "SELECT * FROM levels ORDER BY P_level_id;";
|
||||
List<Level> allLevels = new Vector<Level>();
|
||||
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword());
|
||||
Statement statement = con.createStatement();
|
||||
ResultSet rs = statement.executeQuery(sql)) {
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
Level level = new Level();
|
||||
level.setLevel_id(rs.getInt("P_level_id")); // ID von Level
|
||||
level.setName(rs.getString("name")); // Name von Level
|
||||
level.setDuration(rs.getInt("duration")); // Dauer von Level
|
||||
level.setBackgroundImage(rs.getString("background")); // Hintergrundsbild
|
||||
|
||||
// Targets auslesen und dem Level hinzufügen
|
||||
ITargetPersistance targetDB = new TargetDB(this.dbAccess);
|
||||
|
||||
level.setTargets(targetDB.readAllTargetsPerLevel(level.getLevel_id()));
|
||||
|
||||
allLevels.add(level);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.getMessage();
|
||||
e.printStackTrace();
|
||||
}
|
||||
return allLevels;
|
||||
}
|
||||
|
||||
public void updateLevel(Level lvl) {
|
||||
String sql = "UPDATE levels SET name = ?, background = ?, duration = ? WHERE p_level_id = ?;";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword()); PreparedStatement statement = con.prepareStatement(sql)) {
|
||||
|
||||
statement.setString(1, lvl.getName());
|
||||
statement.setString(2, lvl.getBackgroundImage());
|
||||
statement.setLong(3, lvl.getDuration());
|
||||
statement.setInt(4, lvl.getLevel_id());
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteLevel(int level_id) {
|
||||
String sql = "DELETE FROM levels WHERE P_level_id = " + level_id + ";";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword()); Statement statement = con.createStatement()) {
|
||||
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package model.persistenceDB;
|
||||
|
||||
import model.persistence.IAttemptPersistance;
|
||||
import model.persistence.ILevelPersistance;
|
||||
import model.persistence.IPersistance;
|
||||
import model.persistence.ITargetPersistance;
|
||||
import model.persistence.IUserPersistance;
|
||||
import toDo.UserDB;
|
||||
|
||||
public class PersistanceDB implements IPersistance{
|
||||
|
||||
private LevelDB levelDB;
|
||||
private UserDB userDB;
|
||||
private AttemptDB attemptDB;
|
||||
private TargetDB targetDB;
|
||||
|
||||
public PersistanceDB() {
|
||||
AccessDB dbAccess = new AccessDB();
|
||||
this.levelDB = new LevelDB(dbAccess);
|
||||
this.userDB = new UserDB(dbAccess);
|
||||
this.attemptDB = new AttemptDB(dbAccess);
|
||||
this.targetDB = new TargetDB(dbAccess);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAttemptPersistance getAttemptPersistance() {
|
||||
return this.attemptDB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ILevelPersistance getLevelPersistance() {
|
||||
return this.levelDB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITargetPersistance getTargetPersistance() {
|
||||
return this.targetDB;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IUserPersistance getUserPersistance() {
|
||||
return this.userDB;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package model.persistenceDB;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
import model.Hitbox;
|
||||
import model.Target;
|
||||
import model.persistence.ITargetPersistance;
|
||||
|
||||
public class TargetDB implements ITargetPersistance {
|
||||
private AccessDB dbAccess;
|
||||
|
||||
public TargetDB(AccessDB dbAccess) {
|
||||
super();
|
||||
if (dbAccess != null)
|
||||
this.dbAccess = dbAccess;
|
||||
else
|
||||
this.dbAccess = new AccessDB();
|
||||
}
|
||||
|
||||
public int createTarget(Target target, int level_id, int image_id) {
|
||||
String sql = "INSERT INTO targets (F_level_id, image_address, x_position, y_position, width, height, time, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
|
||||
int lastKey = -1;
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword());
|
||||
PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);) {
|
||||
|
||||
statement.setInt(1, level_id);
|
||||
statement.setString(2, target.getImageAddress());
|
||||
statement.setInt(3, target.getHitbox().getX());
|
||||
statement.setInt(4, target.getHitbox().getY());
|
||||
statement.setInt(5, target.getHitbox().getWidth());
|
||||
statement.setInt(6, target.getHitbox().getHeight());
|
||||
statement.setLong(7, target.getTime());
|
||||
statement.setLong(8, target.getDuration());
|
||||
statement.execute();
|
||||
|
||||
ResultSet generatedKeys = statement.getGeneratedKeys();
|
||||
if (generatedKeys.next()) {
|
||||
lastKey = generatedKeys.getInt(1);
|
||||
}
|
||||
generatedKeys.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return lastKey;
|
||||
}
|
||||
|
||||
public List<Target> readAllTargetsPerLevel(int level_id) {
|
||||
String sql = "SELECT * FROM targets WHERE F_level_id = ? ORDER BY P_target_id;";
|
||||
List<Target> alltargets = new Vector<Target>();
|
||||
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword()); PreparedStatement statement = con.prepareStatement(sql)) {
|
||||
|
||||
statement.setInt(1, level_id);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
|
||||
Target target = new Target();
|
||||
target.setTarget_id(rs.getInt("P_target_id"));
|
||||
target.setHitbox(new Hitbox(rs.getInt("x_position"), rs.getInt("y_position"), rs.getInt("width"),
|
||||
rs.getInt("height")));
|
||||
target.setTime(rs.getLong("time"));
|
||||
target.setDuration(rs.getLong("duration"));
|
||||
target.setImageAddress(rs.getString("image_address"));
|
||||
|
||||
alltargets.add(target);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.getMessage();
|
||||
e.printStackTrace();
|
||||
}
|
||||
return alltargets;
|
||||
}
|
||||
|
||||
public void updateTarget(Target target) {
|
||||
String sql = "UPDATE targets SET x_position = ?, y_position = ?, width = ?, height = ?, time = ?, duration = ?, image_address = ? WHERE P_target_id = ?;";
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword()); PreparedStatement statement = con.prepareStatement(sql)) {
|
||||
|
||||
statement.setInt(1, target.getHitbox().getX());
|
||||
statement.setInt(2, target.getHitbox().getY());
|
||||
statement.setInt(3, target.getHitbox().getWidth());
|
||||
statement.setInt(4, target.getHitbox().getHeight());
|
||||
statement.setLong(5, target.getTime());
|
||||
statement.setLong(6, target.getDuration());
|
||||
statement.setString(7, target.getImageAddress());
|
||||
statement.setInt(8, target.getTarget_id());
|
||||
statement.executeUpdate();
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteTarget(int target_id) {
|
||||
String sql = "DELETE FROM targets WHERE P_target_id = " + target_id + ";";
|
||||
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword()); Statement statement = con.createStatement()) {
|
||||
statement.executeUpdate(sql);
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package model.persistenceDummy;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
import model.Level;
|
||||
import model.persistence.IAttemptPersistance;
|
||||
|
||||
/**
|
||||
* Klasse mit Dummywerten zum Testen der View und des Controllers
|
||||
* @author Tim Tenbusch
|
||||
*
|
||||
*/
|
||||
public class AttemptDummy implements IAttemptPersistance{
|
||||
|
||||
public int createHighscoreEntry(int F_user_id, int F_level_id, int shots, int hits, long reaction_time) {
|
||||
// fleiüig speichern
|
||||
return 1;
|
||||
}
|
||||
|
||||
public Vector<Vector<String>> getAllAttemptsPerLevel(Level level, int game_id) {
|
||||
Vector<Vector<String>> highscore = new Vector<Vector<String>>();
|
||||
Vector<String> eintrag1 = new Vector<String>();
|
||||
Vector<String> eintrag2 = new Vector<String>();
|
||||
eintrag1.addElement("1");
|
||||
eintrag2.addElement("2");
|
||||
eintrag1.addElement("Dummy Persistenz");
|
||||
eintrag2.addElement("Vorname Nachname");
|
||||
eintrag1.addElement("17.02.2021");
|
||||
eintrag2.addElement("18.02.2021");
|
||||
eintrag1.addElement("15:21");
|
||||
eintrag2.addElement("17:01");
|
||||
eintrag1.addElement("7");
|
||||
eintrag2.addElement("9");
|
||||
eintrag1.addElement("421");
|
||||
eintrag2.addElement("13");
|
||||
eintrag1.addElement("250");
|
||||
eintrag2.addElement("270");
|
||||
eintrag1.addElement("356");
|
||||
eintrag2.addElement("481");
|
||||
highscore.add(eintrag1);
|
||||
highscore.add(eintrag2);
|
||||
return highscore;
|
||||
}
|
||||
|
||||
public int getPlayerPosition() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public void deleteHighscore(int level_id) {
|
||||
//Omnomnom Anfrage gefressen
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package model.persistenceDummy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import model.Level;
|
||||
import model.Target;
|
||||
import model.persistence.ILevelPersistance;
|
||||
|
||||
public class LevelDummy implements ILevelPersistance {
|
||||
|
||||
public int createLevel(String levelname, String backgroundUrl, long duration) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public List<Level> readAllLevel() {
|
||||
List<Level> levels = new ArrayList<Level>();
|
||||
Level level1 = new Level();
|
||||
level1.setLevel_id(1);
|
||||
level1.setName("Level 1");
|
||||
level1.setBackgroundImage("background_1.jpg");
|
||||
level1.setDuration(10000);
|
||||
List<Target> targets = new ArrayList<Target>();
|
||||
Target t1 = new Target(100, 100, 150, 50, 1000, 2000, "ufo_1.png");
|
||||
targets.add(t1);
|
||||
Target t2 = new Target(5, 100, 150, 50, 2000, 2000, "ufo_2.png");
|
||||
targets.add(t2);
|
||||
Target t3 = new Target(800, 800, 150, 50, 3000, 2000, "ufo_3.png");
|
||||
targets.add(t3);
|
||||
Target t4 = new Target(600, 400, 150, 50, 4000, 2000, "ufo_4.png");
|
||||
targets.add(t4);
|
||||
Target t5 = new Target(220, 400, 150, 50, 5000, 2000, "ufo_5.png");
|
||||
targets.add(t5);
|
||||
|
||||
level1.setTargets(targets);
|
||||
levels.add(level1);
|
||||
Level level2 = new Level();
|
||||
level2.setLevel_id(2);
|
||||
level2.setName("Level 2");
|
||||
level2.setBackgroundImage("background_2.jpg");
|
||||
level2.setDuration(5000);
|
||||
level2.setTargets(targets);
|
||||
levels.add(level2);
|
||||
Level level3 = new Level();
|
||||
level3.setLevel_id(3);
|
||||
level3.setName("Level 3");
|
||||
level3.setBackgroundImage("background_10.jpg");
|
||||
level3.setDuration(10000);
|
||||
level3.setTargets(targets);
|
||||
levels.add(level3);
|
||||
return levels;
|
||||
}
|
||||
|
||||
public void updateLevel(Level lvl) {
|
||||
|
||||
}
|
||||
|
||||
public void deleteLevel(int level_id) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package model.persistenceDummy;
|
||||
|
||||
import model.persistence.IAttemptPersistance;
|
||||
import model.persistence.ILevelPersistance;
|
||||
import model.persistence.IPersistance;
|
||||
import model.persistence.ITargetPersistance;
|
||||
import model.persistence.IUserPersistance;
|
||||
|
||||
public class PersistanceDummy implements IPersistance{
|
||||
|
||||
private LevelDummy levelDummy;
|
||||
private UserDummy userDummy;
|
||||
private AttemptDummy attemptDummy;
|
||||
private TargetDummy targetDummy;
|
||||
|
||||
public PersistanceDummy() {
|
||||
|
||||
this.levelDummy = new LevelDummy();
|
||||
this.userDummy = new UserDummy();
|
||||
this.attemptDummy = new AttemptDummy();
|
||||
this.targetDummy = new TargetDummy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAttemptPersistance getAttemptPersistance() {
|
||||
return this.attemptDummy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ILevelPersistance getLevelPersistance() {
|
||||
return this.levelDummy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITargetPersistance getTargetPersistance() {
|
||||
return this.targetDummy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IUserPersistance getUserPersistance() {
|
||||
return this.userDummy;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package model.persistenceDummy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import model.Target;
|
||||
import model.persistence.ITargetPersistance;
|
||||
|
||||
public class TargetDummy implements ITargetPersistance {
|
||||
|
||||
public int createTarget(Target target, int level_id, int image_id) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public List<Target> readAllTargetsPerLevel(int level_id) {
|
||||
List<Target> targets = new ArrayList<Target>();
|
||||
Target t1 = new Target(100, 100, 150, 50, 1000, 2000, "ufo_1.png");
|
||||
targets.add(t1);
|
||||
Target t2 = new Target(5, 100, 150, 50, 2000, 2000, "ufo_2.png");
|
||||
targets.add(t2);
|
||||
Target t3 = new Target(800, 800, 150, 50, 3000, 2000, "ufo_3.png");
|
||||
targets.add(t3);
|
||||
Target t4 = new Target(600, 400, 150, 50, 4000, 2000, "ufo_4.png");
|
||||
targets.add(t4);
|
||||
Target t5 = new Target(220, 400, 150, 50, 5000, 2000, "ufo_5.png");
|
||||
targets.add(t5);
|
||||
return targets;
|
||||
}
|
||||
|
||||
public void updateTarget(Target target) {
|
||||
|
||||
}
|
||||
|
||||
public void deleteTarget(int target_id) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package model.persistenceDummy;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import model.persistence.IUserPersistance;
|
||||
import toDo.User;
|
||||
|
||||
/**
|
||||
* Dummyklasse zum Testen
|
||||
* @author Tim Tenbusch
|
||||
*
|
||||
*/
|
||||
public class UserDummy implements IUserPersistance {
|
||||
|
||||
public User readUser(String username) {
|
||||
return new User(1, "Dummy", "Persistenz", LocalDate.now(), "Dummystr.", "12C", "11111", "Nowhere", username, "pass", 12000, "gefangen", 1.58);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package toDo;
|
||||
|
||||
//TODO create a usermanagement
|
||||
public class CreateUserWindow {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package toDo;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class User {
|
||||
|
||||
private int p_user_id;
|
||||
private String first_name;
|
||||
private String sur_name;
|
||||
private LocalDate birthday;
|
||||
private String street;
|
||||
private String house_number;
|
||||
private String postal_code;
|
||||
private String city;
|
||||
private String loginname;
|
||||
private String password;
|
||||
private int salary_expectations;
|
||||
private String marital_status;
|
||||
private double final_grade;
|
||||
|
||||
public User(int p_user_id, String first_name, String sur_name, LocalDate birthday, String street,
|
||||
String house_number, String postal_code, String city, String loginname, String password,
|
||||
int salary_expectations, String marital_status, double final_grade) {
|
||||
super();
|
||||
this.p_user_id = p_user_id;
|
||||
this.first_name = first_name;
|
||||
this.sur_name = sur_name;
|
||||
this.birthday = birthday;
|
||||
this.street = street;
|
||||
this.house_number = house_number;
|
||||
this.postal_code = postal_code;
|
||||
this.city = city;
|
||||
this.loginname = loginname;
|
||||
this.password = password;
|
||||
this.salary_expectations = salary_expectations;
|
||||
this.marital_status = marital_status;
|
||||
this.final_grade = final_grade;
|
||||
}
|
||||
|
||||
public User(int p_user_id, String login, String password) {
|
||||
super();
|
||||
this.p_user_id = p_user_id;
|
||||
this.loginname = login;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public int getP_user_id() {
|
||||
return p_user_id;
|
||||
}
|
||||
|
||||
public void setP_user_id(int p_user_id) {
|
||||
this.p_user_id = p_user_id;
|
||||
}
|
||||
|
||||
public String getFirst_name() {
|
||||
return first_name;
|
||||
}
|
||||
|
||||
public void setFirst_name(String first_name) {
|
||||
this.first_name = first_name;
|
||||
}
|
||||
|
||||
public String getSur_name() {
|
||||
return sur_name;
|
||||
}
|
||||
|
||||
public void setSur_name(String sur_name) {
|
||||
this.sur_name = sur_name;
|
||||
}
|
||||
|
||||
public LocalDate getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
|
||||
public void setBirthday(LocalDate birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
|
||||
public String getHouse_number() {
|
||||
return house_number;
|
||||
}
|
||||
|
||||
public void setHouse_number(String house_number) {
|
||||
this.house_number = house_number;
|
||||
}
|
||||
|
||||
public String getPostal_code() {
|
||||
return postal_code;
|
||||
}
|
||||
|
||||
public void setPostal_code(String postal_code) {
|
||||
this.postal_code = postal_code;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getLoginname() {
|
||||
return loginname;
|
||||
}
|
||||
|
||||
public void setLoginname(String loginname) {
|
||||
this.loginname = loginname;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public double getFinal_grade() {
|
||||
return final_grade;
|
||||
}
|
||||
|
||||
public void setFinal_grade(double final_grade) {
|
||||
this.final_grade = final_grade;
|
||||
}
|
||||
|
||||
public String getMarital_status() {
|
||||
return marital_status;
|
||||
}
|
||||
|
||||
public void setMarital_status(String marital_status) {
|
||||
this.marital_status = marital_status;
|
||||
}
|
||||
|
||||
public int getSalary_expectations() {
|
||||
return salary_expectations;
|
||||
}
|
||||
|
||||
public void setSalary_expectations(int salary_expectations) {
|
||||
this.salary_expectations = salary_expectations;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package toDo;
|
||||
|
||||
import model.persistence.IUserPersistance;
|
||||
|
||||
/**
|
||||
* controller for users
|
||||
* @author Clara Zufall
|
||||
* TODO implement this class
|
||||
*/
|
||||
public class UserController {
|
||||
|
||||
private IUserPersistance userPersistance;
|
||||
|
||||
public UserController(IUserPersistance userPersistance) {
|
||||
this.userPersistance = userPersistance;
|
||||
}
|
||||
|
||||
public void createUser(User user) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* liest einen User aus der Persistenzschicht und gibt das Userobjekt zurück
|
||||
* @param username eindeutige Loginname
|
||||
* @param passwort das richtige Passwort
|
||||
* @return Userobjekt, null wenn der User nicht existiert
|
||||
*/
|
||||
public User readUser(String username, String passwort) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void changeUser(User user) {
|
||||
|
||||
}
|
||||
|
||||
public void deleteUser(User user) {
|
||||
|
||||
}
|
||||
|
||||
public boolean checkPassword(String username, String passwort) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package toDo;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import model.persistence.IUserPersistance;
|
||||
import model.persistenceDB.AccessDB;
|
||||
|
||||
/**
|
||||
* databaseconnection for userobjects, Story usermanagement
|
||||
* @author Clara Zufall
|
||||
* TODO finish this class
|
||||
*/
|
||||
public class UserDB implements IUserPersistance{
|
||||
|
||||
private AccessDB dbAccess;
|
||||
|
||||
public UserDB(AccessDB dbAccess) {
|
||||
this.dbAccess = dbAccess;
|
||||
}
|
||||
|
||||
/**
|
||||
* read userdata by unique username
|
||||
*
|
||||
* @param username
|
||||
* @return userobject, null if user didn't exists
|
||||
*/
|
||||
public User readUser(String username) {
|
||||
String sql = "SELECT * FROM users WHERE login_name = ? ;";
|
||||
User user = null;
|
||||
try (Connection con = DriverManager.getConnection(this.dbAccess.getFullURL(), this.dbAccess.getUser(),
|
||||
this.dbAccess.getPassword()); PreparedStatement statement = con.prepareStatement(sql)) {
|
||||
|
||||
statement.setString(1, username);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
while (rs.next()) {
|
||||
user = new User(rs.getInt("P_user_id"), rs.getString("first_name"), rs.getString("sur_name"),
|
||||
rs.getDate("birthday").toLocalDate(), rs.getString("street"), rs.getString("house_number"),
|
||||
rs.getString("postal_code"), rs.getString("city"), rs.getString("login_name"),
|
||||
rs.getString("password"), rs.getInt("salary_expectations"), rs.getString("marital_status"),
|
||||
rs.getBigDecimal("final_grade").doubleValue());
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
System.err.println(ex.getMessage());
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package view;
|
||||
|
||||
import controller.AlienDefenceController;
|
||||
import model.persistence.IPersistance;
|
||||
import model.persistenceDummy.PersistanceDummy;
|
||||
import view.menue.MainMenu;
|
||||
|
||||
public class StartAlienDefence {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
IPersistance alienDefenceModel = new PersistanceDummy();//TODO new PersistanceDB();
|
||||
AlienDefenceController alienDefenceController = new AlienDefenceController(alienDefenceModel);
|
||||
MainMenu mainMenu = new MainMenu(alienDefenceController);
|
||||
|
||||
mainMenu.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package view.game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Point;
|
||||
import java.awt.Toolkit;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import controller.GameController;
|
||||
|
||||
/**
|
||||
* GUI der Klasse _______
|
||||
*
|
||||
* @author Tenbusch
|
||||
*
|
||||
*/
|
||||
public class GameGUI extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private GameJPanel spielfeld;
|
||||
private GameController gc;
|
||||
public final long STARTTIME;
|
||||
private final int WIDTH = 1280;
|
||||
private final int HEIGHT = 960;
|
||||
|
||||
// Konstruktor
|
||||
public GameGUI(GameController gc) {
|
||||
super("Spiel v0.0 - FPS: ");
|
||||
|
||||
setCursor(Toolkit.getDefaultToolkit().createCustomCursor(new ImageIcon("pictures/crosshair.png").getImage(),
|
||||
new Point(10, 10), "Cursor"));
|
||||
|
||||
// Fenstergestaltung
|
||||
setSize(WIDTH, HEIGHT);
|
||||
setLocation(10, 10);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
// Controller registrieren
|
||||
this.gc = gc;
|
||||
|
||||
setBackground(new Color(200, 200, 200));
|
||||
|
||||
gc.setHasWon(false);
|
||||
|
||||
spielfeld = new GameJPanel(gc);
|
||||
getContentPane().add(spielfeld);
|
||||
setVisible(true);
|
||||
|
||||
// Startzeit setzen
|
||||
STARTTIME = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public void start() {
|
||||
|
||||
long lastStep = System.currentTimeMillis() - 1;
|
||||
|
||||
// Spielfeld vorbereiten
|
||||
gc.startLevel();
|
||||
|
||||
while (true) {
|
||||
|
||||
// FPS berechnen
|
||||
long delta = System.currentTimeMillis() - lastStep;
|
||||
lastStep = System.currentTimeMillis();
|
||||
setTitle("Spiel v0.0 - FPS: " + (1000 / delta));
|
||||
|
||||
// Spiellogik
|
||||
this.gc.doLogik(delta);
|
||||
|
||||
repaint();
|
||||
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
|
||||
if (gc.isHasWon() || gc.timeleft() <= -1) {
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
dispose();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package view.game;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.SwingConstants;
|
||||
|
||||
import controller.GameController;
|
||||
import model.Target;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class GameJPanel extends JPanel {
|
||||
|
||||
// Attribute die zum Zeichnen aus dem Controller geholt werden müssen (konkrete
|
||||
// Objekte erst in init() erzeugen)
|
||||
private GameController gc;
|
||||
private MouseClickListener mouseClickListener;
|
||||
private List<TargetPainter> rechteckePainter;
|
||||
private BufferedImage img, win, loose;
|
||||
private JLabel lblTimeleft;
|
||||
|
||||
public GameJPanel(GameController gc) {
|
||||
super(); // do the JPanel-stuff
|
||||
this.gc = gc;
|
||||
this.mouseClickListener = new MouseClickListener(gc);
|
||||
this.addMouseListener(this.mouseClickListener);
|
||||
this.lblTimeleft = new JLabel(gc.timeleft() + "s");
|
||||
this.lblTimeleft.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
this.lblTimeleft.setBounds(1000, 10, 200, 40);
|
||||
this.lblTimeleft.setFont(new Font("Comic Sans Serif", Font.BOLD, 30));
|
||||
init();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Spielfläche initialisieren
|
||||
*/
|
||||
public void init() {
|
||||
try {
|
||||
this.img = ImageIO.read(new File("./pictures/" + this.gc.getCurrLevel().getBackgroundImage()));
|
||||
this.win = ImageIO.read(new File("./pictures/YouWin.png"));
|
||||
this.loose = ImageIO.read(new File("./pictures/YouLoose.png"));
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
LinkedList<TargetPainter> rechteckePainter = new LinkedList<TargetPainter>();
|
||||
for (Target t : gc.getTargets()) {
|
||||
rechteckePainter.add(new TargetPainter(t));
|
||||
}
|
||||
this.rechteckePainter = rechteckePainter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
|
||||
// Abfrage Gewinn oder
|
||||
if (gc.timeleft() <= 0 || gc.isHasWon()) {
|
||||
|
||||
g.setColor(Color.GRAY);
|
||||
if (gc.isHasWon()) {
|
||||
g.drawImage(this.win, 300, 120, null);
|
||||
} else {
|
||||
g.drawImage(this.loose, 300, 120, null);
|
||||
}
|
||||
g.setFont(new Font("Arial", Font.PLAIN, 16));
|
||||
// g.drawString(gc.getShotsFired() + " Schüsse mit " + gc.getAccuracy() + " %
|
||||
// Treffergenauigkeit", 300, 850);
|
||||
|
||||
} else {
|
||||
|
||||
// Spielfeldhintergrund zeichnen
|
||||
if (img != null) {
|
||||
g.drawImage(this.img, 0, 0, null);
|
||||
} else {
|
||||
g.setColor(Color.GREEN);
|
||||
g.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
}
|
||||
|
||||
// System.out.println(gc.time());
|
||||
// Targets zeichnen
|
||||
for (TargetPainter rp : this.rechteckePainter) {
|
||||
rp.paint(g, gc.time());
|
||||
}
|
||||
|
||||
// Sekundenanzeige
|
||||
if (gc.timeleft() <= 5)
|
||||
g.setColor(Color.RED);
|
||||
else
|
||||
g.setColor(Color.WHITE);
|
||||
g.setFont(new Font("Comic Sans Serif", Font.PLAIN, 30));
|
||||
g.drawString(gc.timeleft() + "", 1210, 40);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package view.game;
|
||||
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
import controller.GameController;
|
||||
|
||||
public class MouseClickListener extends MouseAdapter {
|
||||
|
||||
private GameController gc;
|
||||
|
||||
public MouseClickListener(GameController gc) {
|
||||
this.gc = gc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
gc.fireShot(e.getX(), e.getY());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package view.game;
|
||||
|
||||
import java.awt.Graphics;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import model.Target;
|
||||
|
||||
public class TargetPainter {
|
||||
|
||||
private Target target;
|
||||
private BufferedImage image;
|
||||
private BufferedImage imageDestroyed;
|
||||
|
||||
public TargetPainter(Target target) {
|
||||
this.target = target;
|
||||
try {
|
||||
this.image = ImageIO.read(new File("./pictures/" + target.getImageAddress()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
this.imageDestroyed = ImageIO.read(new File("./pictures/explosion.png"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void paint(Graphics g, long time) {
|
||||
|
||||
long targetStartTime = target.getTime();
|
||||
long targetEndTime = target.getTime() + target.getDuration();
|
||||
|
||||
// Prüft, ob das Target erscheint.
|
||||
if ((time < targetStartTime || time > targetEndTime))
|
||||
return;
|
||||
|
||||
if (this.target.isHit() && time < targetEndTime) {
|
||||
|
||||
g.drawImage(this.imageDestroyed, this.target.getHitbox().getX(), this.target.getHitbox().getY(),
|
||||
this.target.getHitbox().getWidth(), this.target.getHitbox().getHeight(), null);
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
g.drawImage(this.image, this.target.getHitbox().getX(), this.target.getHitbox().getY(),
|
||||
this.target.getHitbox().getWidth(), this.target.getHitbox().getHeight(), null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package view.menue;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
|
||||
import controller.AttemptController;
|
||||
import model.Level;
|
||||
|
||||
public class Highscore extends JFrame {
|
||||
|
||||
// Attribute
|
||||
private AttemptController attemptController;
|
||||
private Level level;
|
||||
|
||||
public Highscore(AttemptController attemptController, Level level) {
|
||||
this(attemptController, level, 0);
|
||||
}
|
||||
|
||||
// Konstruktor
|
||||
public Highscore(AttemptController attemptController, Level level, int game_id) {
|
||||
this.attemptController = attemptController;
|
||||
this.level = level;
|
||||
|
||||
// Zweidimensioaler Vector, mit Inhalt der Tabelle wird geholt.
|
||||
Vector<Vector<String>> vecRow = attemptController.getAllAttemptsPerLevel(level, game_id);
|
||||
|
||||
int mark = attemptController.getPlayerPosition();
|
||||
|
||||
setLayout(new BorderLayout(5, 10));
|
||||
|
||||
// Spaltenüberschriften
|
||||
Vector<String> title = new Vector<>();
|
||||
title.add("Rang");
|
||||
title.add("Spieler");
|
||||
title.add("Datum");
|
||||
title.add("Uhrzeit");
|
||||
title.add("Trefferwert");
|
||||
title.add("Genauigkeitswert");
|
||||
title.add("Reaktionswert");
|
||||
title.add("Highscore-Wert");
|
||||
|
||||
// Tablle basierend auf zweidimensionalem Vector
|
||||
JTable table = new JTable(vecRow, title);
|
||||
if (mark >= 0)
|
||||
table.setRowSelectionInterval(mark, mark);
|
||||
setMinimumSize(new Dimension(650, 500));
|
||||
getContentPane().add(new JScrollPane(table), BorderLayout.NORTH);
|
||||
setTitle("Highscore-Liste"); // Titel
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
pack();
|
||||
setVisible(true);
|
||||
|
||||
JButton btnZielndern = new JButton("Highscoreliste löschen");
|
||||
btnZielndern.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnAendern_Clicked(arg0);
|
||||
}
|
||||
});
|
||||
|
||||
JPanel pnlSouth = new JPanel();
|
||||
pnlSouth.add(btnZielndern);
|
||||
|
||||
// fügt Panel mit Button hinzu
|
||||
add(pnlSouth, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
public void btnAendern_Clicked(ActionEvent evt) {
|
||||
this.attemptController.deleteHighscore(level.getLevel_id());
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package view.menue;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Font;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
|
||||
import controller.LevelController;
|
||||
import model.Level;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class LevelChooser extends JPanel {
|
||||
|
||||
private LevelController lvlControl;
|
||||
private LeveldesignWindow leveldesignWindow;
|
||||
private JTable tblLevels;
|
||||
private DefaultTableModel jTableData;
|
||||
|
||||
/**
|
||||
* Create the panel.
|
||||
*
|
||||
* @param leveldesignWindow
|
||||
*/
|
||||
public LevelChooser(LevelController lvlControl, LeveldesignWindow leveldesignWindow) {
|
||||
this.lvlControl = lvlControl;
|
||||
this.leveldesignWindow = leveldesignWindow;
|
||||
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
JPanel pnlButtons = new JPanel();
|
||||
add(pnlButtons, BorderLayout.SOUTH);
|
||||
|
||||
JButton btnNewLevel = new JButton("Neues Level");
|
||||
btnNewLevel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnNewLevel_Clicked();
|
||||
}
|
||||
});
|
||||
pnlButtons.add(btnNewLevel);
|
||||
|
||||
JButton btnUpdateLevel = new JButton("ausgew\u00E4hltes Level bearbeiten");
|
||||
btnUpdateLevel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
btnUpdateLevel_Clicked();
|
||||
}
|
||||
});
|
||||
pnlButtons.add(btnUpdateLevel);
|
||||
|
||||
JButton btnDeleteLevel = new JButton("Level l\u00F6schen");
|
||||
btnDeleteLevel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
btnDeleteLevel_Clicked();
|
||||
}
|
||||
});
|
||||
pnlButtons.add(btnDeleteLevel);
|
||||
|
||||
JLabel lblLevelauswahl = new JLabel("Levelauswahl");
|
||||
lblLevelauswahl.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
lblLevelauswahl.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
add(lblLevelauswahl, BorderLayout.NORTH);
|
||||
|
||||
JScrollPane spnLevels = new JScrollPane();
|
||||
add(spnLevels, BorderLayout.CENTER);
|
||||
|
||||
tblLevels = new JTable();
|
||||
tblLevels.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
spnLevels.setViewportView(tblLevels);
|
||||
|
||||
this.updateTableData();
|
||||
}
|
||||
|
||||
private String[][] getLevelsAsTableModel() {
|
||||
List<Level> levels = this.lvlControl.readAllLevels();
|
||||
String[][] result = new String[levels.size()][];
|
||||
int i = 0;
|
||||
for (Level l : levels) {
|
||||
result[i++] = l.getData();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void updateTableData() {
|
||||
this.jTableData = new DefaultTableModel(this.getLevelsAsTableModel(), Level.getLevelDescriptions());
|
||||
this.tblLevels.setModel(jTableData);
|
||||
}
|
||||
|
||||
public void btnNewLevel_Clicked() {
|
||||
this.leveldesignWindow.startLevelEditor();
|
||||
}
|
||||
|
||||
public void btnUpdateLevel_Clicked() {
|
||||
int level_id = Integer
|
||||
.parseInt((String) this.tblLevels.getModel().getValueAt(this.tblLevels.getSelectedRow(), 0));
|
||||
this.leveldesignWindow.startLevelEditor(level_id);
|
||||
}
|
||||
|
||||
public void btnDeleteLevel_Clicked() {
|
||||
int level_id = Integer
|
||||
.parseInt((String) this.tblLevels.getModel().getValueAt(this.tblLevels.getSelectedRow(), 0));
|
||||
this.lvlControl.deleteLevel(level_id);
|
||||
this.updateTableData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
package view.menue;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Font;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.SystemColor;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.SwingConstants;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
|
||||
import controller.LevelController;
|
||||
import controller.TargetController;
|
||||
import model.Level;
|
||||
import model.Target;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class LevelEditor extends JPanel {
|
||||
|
||||
private LevelController lvlControl;
|
||||
private TargetController targetControl;
|
||||
private LeveldesignWindow leveldesignWindow;
|
||||
private Level lvl;
|
||||
private JTable tblTargets;
|
||||
private DefaultTableModel jTableData;
|
||||
private JTextField tfdLevelname;
|
||||
private JTextField tfdLevel_id;
|
||||
private JTextField tfdLevelDuration;
|
||||
private JTextField tfdTarget_id;
|
||||
private JTextField tfdX;
|
||||
private JTextField tfdY;
|
||||
private JTextField tfdWidth;
|
||||
private JTextField tfdHeight;
|
||||
private JTextField tfdStarttime;
|
||||
private JTextField tfdTargetDuration;
|
||||
private JTextField tfdLevelBackground;
|
||||
private JTextField tfdTargetImage;
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
public LevelEditor(LeveldesignWindow leveldesignWindow, LevelController lvlControl, TargetController targetControl,
|
||||
Level lvl) {
|
||||
this.leveldesignWindow = leveldesignWindow;
|
||||
this.lvlControl = lvlControl;
|
||||
this.targetControl = targetControl;
|
||||
this.lvl = lvl;
|
||||
|
||||
setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
JLabel lblLeveleditor = new JLabel("Leveleditor");
|
||||
lblLeveleditor.setHorizontalAlignment(SwingConstants.CENTER);
|
||||
lblLeveleditor.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
add(lblLeveleditor, BorderLayout.NORTH);
|
||||
|
||||
JPanel pnlMain = new JPanel();
|
||||
add(pnlMain, BorderLayout.CENTER);
|
||||
pnlMain.setLayout(new GridLayout(0, 2, 20, 0));
|
||||
|
||||
tblTargets = new JTable();
|
||||
tblTargets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
tblTargets.getSelectionModel().addListSelectionListener(new RowListener());
|
||||
jTableData = new DefaultTableModel(this.lvl.getTargetsAsTableModel(), Target.getTableDescriptions());
|
||||
tblTargets.setModel(jTableData);
|
||||
|
||||
JScrollPane spnTargets = new JScrollPane(tblTargets);
|
||||
pnlMain.add(spnTargets);
|
||||
|
||||
JPanel pnlInputs = new JPanel();
|
||||
pnlMain.add(pnlInputs);
|
||||
pnlInputs.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
JPanel pnlInputMask = new JPanel();
|
||||
pnlInputMask.setBackground(SystemColor.activeCaption);
|
||||
pnlInputMask.setBorder(new EmptyBorder(10, 5, 10, 5));
|
||||
pnlInputs.add(pnlInputMask, BorderLayout.NORTH);
|
||||
pnlInputMask.setLayout(new GridLayout(0, 2, 0, 10));
|
||||
|
||||
|
||||
JLabel lblLevelUeberschrift = new JLabel("Level");
|
||||
lblLevelUeberschrift.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
pnlInputMask.add(lblLevelUeberschrift);
|
||||
|
||||
JLabel lblLevelUeberschrift2 = new JLabel("");
|
||||
pnlInputMask.add(lblLevelUeberschrift2);
|
||||
|
||||
JLabel lblLevelid = new JLabel("Level ID");
|
||||
pnlInputMask.add(lblLevelid);
|
||||
|
||||
tfdLevel_id = new JTextField(this.lvl.getLevel_id() + "");
|
||||
tfdLevel_id.setEditable(false);
|
||||
tfdLevel_id.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
pnlInputMask.add(tfdLevel_id);
|
||||
tfdLevel_id.setColumns(10);
|
||||
|
||||
JLabel lblLevelname = new JLabel("Name des Level");
|
||||
pnlInputMask.add(lblLevelname);
|
||||
|
||||
tfdLevelname = new JTextField(this.lvl.getName());
|
||||
tfdLevelname.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
pnlInputMask.add(tfdLevelname);
|
||||
tfdLevelname.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblLevelDuration = new JLabel("Spieldauer (Millisekunden)");
|
||||
pnlInputMask.add(lblLevelDuration);
|
||||
|
||||
tfdLevelDuration = new JTextField(this.lvl.getDuration() + "");
|
||||
tfdLevelDuration.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
pnlInputMask.add(tfdLevelDuration);
|
||||
tfdLevelDuration.setColumns(10);
|
||||
|
||||
JButton btnBack = new JButton("zurück zur Levelauswahl");
|
||||
btnBack.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnBack_Clicked(arg0);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
JLabel lblbackground = new JLabel("Hintergrundsbild");
|
||||
pnlInputMask.add(lblbackground);
|
||||
|
||||
tfdLevelBackground = new JTextField();
|
||||
tfdLevelBackground.setHorizontalAlignment(SwingConstants.RIGHT);
|
||||
pnlInputMask.add(tfdLevelBackground);
|
||||
tfdLevelBackground.setColumns(10);
|
||||
pnlInputMask.add(btnBack);
|
||||
|
||||
|
||||
JButton btnSaveLevelChanges = new JButton("\u00C4nderungen am Level speichern");
|
||||
btnSaveLevelChanges.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnSaveLevelChanges_Clicked(arg0);
|
||||
}
|
||||
});
|
||||
pnlInputMask.add(btnSaveLevelChanges);
|
||||
|
||||
|
||||
JPanel pnlInputMaskTargets = new JPanel();
|
||||
pnlInputMaskTargets.setBorder(new EmptyBorder(10, 5, 10, 5));
|
||||
pnlInputMaskTargets.setBackground(SystemColor.inactiveCaption);
|
||||
pnlInputs.add(pnlInputMaskTargets, BorderLayout.CENTER);
|
||||
pnlInputMaskTargets.setLayout(new GridLayout(0, 2, 0, 10));
|
||||
|
||||
|
||||
JLabel lblUeberschrift = new JLabel("Targets");
|
||||
lblUeberschrift.setFont(new Font("Arial", Font.BOLD, 18));
|
||||
pnlInputMaskTargets.add(lblUeberschrift);
|
||||
|
||||
JLabel lbllblUeberschrift2 = new JLabel("");
|
||||
pnlInputMaskTargets.add(lbllblUeberschrift2);
|
||||
|
||||
|
||||
JLabel lblTargetid = new JLabel("Target ID");
|
||||
pnlInputMaskTargets.add(lblTargetid);
|
||||
|
||||
tfdTarget_id = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdTarget_id);
|
||||
tfdTarget_id.setColumns(10);
|
||||
|
||||
tfdTarget_id.setEditable(false);
|
||||
|
||||
|
||||
JLabel lblZiel = new JLabel("Ziel:");
|
||||
pnlInputMaskTargets.add(lblZiel);
|
||||
|
||||
JLabel lblZiel2 = new JLabel("");
|
||||
pnlInputMaskTargets.add(lblZiel2);
|
||||
|
||||
|
||||
JLabel lblTargetpicture = new JLabel("Bilddatei des Ziels");
|
||||
lblTargetpicture.setHorizontalAlignment(SwingConstants.LEFT);
|
||||
pnlInputMaskTargets.add(lblTargetpicture);
|
||||
|
||||
tfdTargetImage = new JTextField();
|
||||
tfdTargetImage.setHorizontalAlignment(SwingConstants.LEFT);
|
||||
pnlInputMaskTargets.add(tfdTargetImage);
|
||||
tfdTargetImage.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblBreite = new JLabel("Breite des Ziels");
|
||||
pnlInputMaskTargets.add(lblBreite);
|
||||
|
||||
tfdWidth = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdWidth);
|
||||
tfdWidth.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblHhe = new JLabel("Höhe des Ziels");
|
||||
pnlInputMaskTargets.add(lblHhe);
|
||||
|
||||
tfdHeight = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdHeight);
|
||||
tfdHeight.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblX = new JLabel("X Position des Ziels (max. 1280 - Breite) ");
|
||||
pnlInputMaskTargets.add(lblX);
|
||||
|
||||
tfdX = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdX);
|
||||
tfdX.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblY = new JLabel("Y Position des Ziels (max. 960 - Höhe)");
|
||||
pnlInputMaskTargets.add(lblY);
|
||||
|
||||
tfdY = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdY);
|
||||
tfdY.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblZeiten = new JLabel("Zeiten: ");
|
||||
pnlInputMaskTargets.add(lblZeiten);
|
||||
|
||||
JLabel lblZeiten2 = new JLabel(" ");
|
||||
pnlInputMaskTargets.add(lblZeiten2);
|
||||
|
||||
|
||||
JLabel lblStartzeit = new JLabel("Startzeit in nach Spielstart (Millisekunden)");
|
||||
pnlInputMaskTargets.add(lblStartzeit);
|
||||
|
||||
tfdStarttime = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdStarttime);
|
||||
tfdStarttime.setColumns(10);
|
||||
|
||||
|
||||
JLabel lblAnzeigedauer = new JLabel("Anzeigedauer (Millisekunden)");
|
||||
pnlInputMaskTargets.add(lblAnzeigedauer);
|
||||
|
||||
tfdTargetDuration = new JTextField();
|
||||
pnlInputMaskTargets.add(tfdTargetDuration);
|
||||
tfdTargetDuration.setColumns(10);
|
||||
|
||||
|
||||
JPanel panel_2 = new JPanel();
|
||||
pnlInputs.add(panel_2, BorderLayout.SOUTH);
|
||||
|
||||
JButton btnNeuesZiel = new JButton("Neues Ziel");
|
||||
btnNeuesZiel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnErstellen_Clicked(arg0);
|
||||
}
|
||||
});
|
||||
panel_2.add(btnNeuesZiel);
|
||||
|
||||
JButton btnZielndern = new JButton("Ziel \u00E4ndern");
|
||||
btnZielndern.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnAendern_Clicked(arg0);
|
||||
}
|
||||
});
|
||||
panel_2.add(btnZielndern);
|
||||
|
||||
JButton btnDeleteTarget = new JButton("Ziel l\u00F6schen");
|
||||
btnDeleteTarget.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
btnLoeschen_Clicked(arg0);
|
||||
}
|
||||
});
|
||||
panel_2.add(btnDeleteTarget);
|
||||
|
||||
}
|
||||
|
||||
public Level getLvl() {
|
||||
return lvl;
|
||||
}
|
||||
|
||||
public void setLvl(Level lvl) {
|
||||
this.lvl = lvl;
|
||||
updateTableData();
|
||||
this.tfdLevel_id.setText("" + lvl.getLevel_id());
|
||||
this.tfdLevelname.setText(lvl.getName());
|
||||
this.tfdLevelDuration.setText("" + lvl.getDuration());
|
||||
this.tfdLevelBackground.setText(lvl.getBackgroundImage());
|
||||
}
|
||||
|
||||
public void updateTableData() {
|
||||
jTableData = new DefaultTableModel(this.lvl.getTargetsAsTableModel(), Target.getTableDescriptions());
|
||||
tblTargets.setModel(jTableData);
|
||||
}
|
||||
|
||||
public void targetAnzeigen_JTable(int row) {
|
||||
this.tfdTarget_id.setText((String) tblTargets.getValueAt(row, 0));
|
||||
this.tfdX.setText((String) tblTargets.getValueAt(row, 1));
|
||||
this.tfdY.setText((String) tblTargets.getValueAt(row, 2));
|
||||
this.tfdWidth.setText((String) tblTargets.getValueAt(row, 3));
|
||||
this.tfdHeight.setText((String) tblTargets.getValueAt(row, 4));
|
||||
this.tfdTargetImage.setText((String) tblTargets.getValueAt(row, 5));
|
||||
this.tfdStarttime.setText((String) tblTargets.getValueAt(row, 6));
|
||||
this.tfdTargetDuration.setText((String) tblTargets.getValueAt(row, 7));
|
||||
}
|
||||
|
||||
public void targetAnzeigen_JTable(Target target) {
|
||||
this.tfdTarget_id.setText(target.getTarget_id() + "");
|
||||
this.tfdX.setText(target.getHitbox().getX() + "");
|
||||
this.tfdY.setText(target.getHitbox().getY() + "");
|
||||
this.tfdWidth.setText(target.getHitbox().getWidth() + "");
|
||||
this.tfdHeight.setText(target.getHitbox().getHeight() + "");
|
||||
this.tfdTargetImage.setText(target.getImageAddress());
|
||||
this.tfdStarttime.setText(target.getTime() + "");
|
||||
this.tfdTargetDuration.setText(target.getDuration() + "");
|
||||
}
|
||||
|
||||
public Target targetAuslesen() {
|
||||
Target currentTarget = null;
|
||||
for (Target t : this.lvl.getTargets()) {
|
||||
if (t.getTarget_id() == Integer.parseInt(tfdTarget_id.getText())) {
|
||||
currentTarget = t;
|
||||
}
|
||||
}
|
||||
if (currentTarget != null) {
|
||||
|
||||
currentTarget.getHitbox().setX(Integer.parseInt(tfdX.getText()));
|
||||
currentTarget.getHitbox().setY(Integer.parseInt(tfdY.getText()));
|
||||
currentTarget.getHitbox().setWidth(Integer.parseInt(tfdWidth.getText()));
|
||||
currentTarget.getHitbox().setHeight(Integer.parseInt(tfdHeight.getText()));
|
||||
|
||||
currentTarget.setDuration(Long.parseLong(tfdTargetDuration.getText()));
|
||||
currentTarget.setTime(Long.parseLong(tfdStarttime.getText()));
|
||||
currentTarget.setImageAddress(tfdTargetImage.getText());
|
||||
}
|
||||
return currentTarget;
|
||||
}
|
||||
|
||||
public void btnErstellen_Clicked(ActionEvent evt) {
|
||||
Target target = this.targetControl.createTarget(this.lvl);
|
||||
if (target != null)
|
||||
targetAnzeigen_JTable(target);
|
||||
updateTableData();
|
||||
}
|
||||
|
||||
public void btnLoeschen_Clicked(ActionEvent evt) {
|
||||
int target_id = Integer.parseInt(this.tfdTarget_id.getText());
|
||||
this.targetControl.deleteTarget(this.lvl, target_id);
|
||||
updateTableData();
|
||||
// TODO clear Textfield-Data
|
||||
}
|
||||
|
||||
public void btnAendern_Clicked(ActionEvent evt) {
|
||||
this.targetControl.updateTarget(this.lvl, this.targetAuslesen());
|
||||
updateTableData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Zurück zur Levelauswahl
|
||||
*
|
||||
* @param evt
|
||||
*/
|
||||
public void btnBack_Clicked(ActionEvent evt) {
|
||||
this.leveldesignWindow.showLevelChooser();
|
||||
}
|
||||
|
||||
public void btnSaveLevelChanges_Clicked(ActionEvent evt) {
|
||||
this.lvl.setName(this.tfdLevelname.getText());
|
||||
this.lvl.setBackgroundImage(this.tfdLevelBackground.getText());
|
||||
this.lvl.setDuration(Integer.parseInt(this.tfdLevelDuration.getText()));
|
||||
|
||||
this.lvlControl.updateLevel(this.lvl);
|
||||
}
|
||||
|
||||
private class RowListener implements ListSelectionListener {
|
||||
public void valueChanged(ListSelectionEvent event) {
|
||||
if (event.getValueIsAdjusting()) {
|
||||
return;
|
||||
}
|
||||
if (tblTargets.getSelectedRow() < 0)
|
||||
tblTargets.clearSelection();
|
||||
else
|
||||
targetAnzeigen_JTable(tblTargets.getSelectedRow());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package view.menue;
|
||||
|
||||
import java.awt.CardLayout;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import controller.LevelController;
|
||||
import controller.TargetController;
|
||||
import model.Level;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class LeveldesignWindow extends JFrame {
|
||||
|
||||
private LevelController lvlControl;
|
||||
private JPanel contentPane;
|
||||
private LevelChooser cardChooseLevel;
|
||||
private LevelEditor cardLevelEditor;
|
||||
|
||||
private CardLayout cards;
|
||||
|
||||
/**
|
||||
* Create the frame.
|
||||
*/
|
||||
public LeveldesignWindow(LevelController lvlControl, TargetController targetControl) {
|
||||
this.lvlControl = lvlControl;
|
||||
|
||||
setTitle("Leveldesigner");
|
||||
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
setBounds(100, 100, 1200, 800);
|
||||
contentPane = new JPanel();
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
this.cards = new CardLayout();
|
||||
contentPane.setLayout(cards);
|
||||
|
||||
this.cardChooseLevel = new LevelChooser(lvlControl, this);
|
||||
contentPane.add(cardChooseLevel, "levelChooser");
|
||||
|
||||
this.cardLevelEditor = new LevelEditor(this, lvlControl, targetControl, Level.getDefaultLevel());
|
||||
contentPane.add(cardLevelEditor, "levelEditor");
|
||||
|
||||
this.showLevelChooser();
|
||||
this.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* display leveleditor with a new level
|
||||
*/
|
||||
public void startLevelEditor() {
|
||||
this.cardLevelEditor.setLvl(this.lvlControl.createLevel());
|
||||
this.cards.show(contentPane, "levelEditor");
|
||||
}
|
||||
|
||||
/**
|
||||
* disply leveleditor with a new level
|
||||
*
|
||||
* @param level_id
|
||||
*/
|
||||
public void startLevelEditor(int level_id) {
|
||||
this.cardLevelEditor.setLvl(this.lvlControl.readLevel(level_id));
|
||||
this.cards.show(contentPane, "levelEditor");
|
||||
}
|
||||
|
||||
/**
|
||||
* display a jTable with all Levels
|
||||
*/
|
||||
public void showLevelChooser() {
|
||||
this.cards.show(contentPane, "levelChooser");
|
||||
this.cardChooseLevel.updateTableData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
package view.menue;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Image;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JPasswordField;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
import controller.AlienDefenceController;
|
||||
import controller.GameController;
|
||||
import model.Level;
|
||||
import model.persistenceDB.PersistanceDB;
|
||||
import toDo.User;
|
||||
import view.game.GameGUI;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class MainMenu extends JFrame {
|
||||
|
||||
private AlienDefenceController alienDefenceController;
|
||||
private JTextField loginTextField;
|
||||
private JPasswordField passwordTextField;
|
||||
private int selectedLevel = 0;
|
||||
|
||||
private String[] getLevelNames(List<Level> arrLevel) {
|
||||
String[] arrLevelNames = new String[arrLevel.size()];
|
||||
|
||||
for (int i = 0; i < arrLevel.size(); i++) {
|
||||
arrLevelNames[i] = arrLevel.get(i).getName(); // Array aus Arraylist erstellt
|
||||
}
|
||||
|
||||
return arrLevelNames;
|
||||
}
|
||||
|
||||
// Konstruktor
|
||||
public MainMenu(AlienDefenceController alienDefenceController) {
|
||||
|
||||
this.alienDefenceController = alienDefenceController;
|
||||
|
||||
// Frame Formatierungen
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setBounds(100, 100, 450, 300);
|
||||
JPanel contentPane = new JPanel();
|
||||
contentPane.setBackground(new Color(0, 0, 0));
|
||||
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
|
||||
setContentPane(contentPane);
|
||||
contentPane.setLayout(new GridBagLayout()); // GridBagLayout
|
||||
GridBagConstraints c = new GridBagConstraints();
|
||||
c.fill = GridBagConstraints.HORIZONTAL;
|
||||
|
||||
// JLable mit üBerschrift
|
||||
JLabel lblheadline = new JLabel(" ALIEN DEFENCE");
|
||||
lblheadline.setForeground(new Color(124, 252, 0));
|
||||
lblheadline.setFont(new Font("Yu Gothic UI", Font.BOLD, 20));
|
||||
c.gridx = 0;
|
||||
c.gridy = 0;
|
||||
contentPane.add(lblheadline, c);
|
||||
|
||||
// JPanel mit Logo
|
||||
JPanel p = new JPanel() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private ImageIcon imageIcon = new ImageIcon("./pictures/logo.png");
|
||||
private Image image = imageIcon.getImage();
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
if (image != null) {
|
||||
g.drawImage(image, 5, 8, 145, 145, this);
|
||||
}
|
||||
}
|
||||
};
|
||||
c.ipady = 150; // make this component tall
|
||||
c.ipadx = 120;
|
||||
c.weightx = 0.0;
|
||||
c.gridwidth = 15;
|
||||
c.gridx = 0;
|
||||
c.gridy = 1;
|
||||
contentPane.add(p, c);
|
||||
|
||||
// Text Login
|
||||
JLabel loginText = new JLabel("Login: "); // Einfacher Text
|
||||
loginText.setForeground(Color.orange);
|
||||
c.ipady = 0;
|
||||
c.ipadx = 0;
|
||||
c.gridwidth = 0;
|
||||
c.gridx = 0;
|
||||
c.gridy = 2;
|
||||
contentPane.add(loginText, c);
|
||||
|
||||
// Textfeld
|
||||
loginTextField = new JTextField(15);
|
||||
c.gridy = 3;
|
||||
contentPane.add(loginTextField, c);
|
||||
|
||||
// Text Passwort
|
||||
JLabel passwordText = new JLabel("Passwort: "); // Einfacher Text
|
||||
passwordText.setForeground(Color.orange);
|
||||
c.gridy = 4;
|
||||
contentPane.add(passwordText, c);
|
||||
|
||||
// Textfeld
|
||||
passwordTextField = new JPasswordField(15);
|
||||
c.gridy = 5;
|
||||
contentPane.add(passwordTextField, c);
|
||||
|
||||
// Text Level
|
||||
JLabel levelText = new JLabel("Level: "); // Einfacher Text
|
||||
levelText.setForeground(Color.orange);
|
||||
c.gridy = 6;
|
||||
contentPane.add(levelText, c);
|
||||
|
||||
// Levelliste für die ComboBox abrufen
|
||||
List<Level> arrLevel = this.alienDefenceController.getLevelController().readAllLevels();
|
||||
String[] arrLevelNames = getLevelNames(arrLevel);
|
||||
|
||||
// Level Auswahlbox - ActionListener
|
||||
JComboBox<String> combo = new JComboBox<String>(arrLevelNames);
|
||||
c.gridy = 7;
|
||||
contentPane.add(combo, c);
|
||||
ActionListener actLisCombo = new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
selectedLevel = combo.getSelectedIndex();
|
||||
}
|
||||
};
|
||||
combo.addActionListener(actLisCombo); // Listener
|
||||
|
||||
// Button Spielen - ActionListener
|
||||
JButton btnNewButton = new JButton("Spielen");
|
||||
btnNewButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
// User aus Datenbank holen
|
||||
User user = alienDefenceController.getAlienDefenceModel().getUserPersistance().readUser(loginTextField.getText());
|
||||
|
||||
// Spielstarten, wenn Nutzer existiert und Passwort übereinstimmt
|
||||
if (user != null && user.getPassword().equals(new String(passwordTextField.getPassword()))) {
|
||||
|
||||
Thread t = new Thread("GameThread") {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
GameController gameController = alienDefenceController.startGame(arrLevel.get(selectedLevel), user);
|
||||
new GameGUI(gameController).start();
|
||||
}
|
||||
};
|
||||
t.start();
|
||||
} else {
|
||||
// Fehlermeldung - Zugangsdaten fehlerhaft
|
||||
JOptionPane.showMessageDialog(null, "Zugangsdaten nicht korrekt", "Fehler",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
c.gridy = 8;
|
||||
contentPane.add(btnNewButton, c);
|
||||
|
||||
// Button Testen - ActionListener
|
||||
JButton btnTestButton = new JButton("Testen");
|
||||
btnTestButton.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
// Erstellt Modell von aktuellen Nutzer
|
||||
User user = new User(1, "test", "pass");
|
||||
|
||||
Thread t = new Thread("GameThread") {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
List<Level> arrLevel = alienDefenceController.getLevelController().readAllLevels();
|
||||
|
||||
GameController gameController = alienDefenceController.startGame(arrLevel.get(selectedLevel), user);
|
||||
new GameGUI(gameController).start();
|
||||
}
|
||||
};
|
||||
t.start();
|
||||
}
|
||||
});
|
||||
c.gridy = 9;
|
||||
contentPane.add(btnTestButton, c);
|
||||
|
||||
// Button Highscore
|
||||
JButton btnNewButton_2 = new JButton("Highscore");
|
||||
|
||||
btnNewButton_2.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
new Highscore(alienDefenceController.getAttemptController(), arrLevel.get(selectedLevel));
|
||||
}
|
||||
});
|
||||
// selectedLevel
|
||||
|
||||
c.gridy = 10;
|
||||
contentPane.add(btnNewButton_2, c);
|
||||
|
||||
// Button Leveleditor
|
||||
JButton btnNewButton_1 = new JButton("Leveleditor");
|
||||
btnNewButton_1.setBackground(Color.ORANGE);
|
||||
btnNewButton_1.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
new LeveldesignWindow(alienDefenceController.getLevelController(), alienDefenceController.getTargetController());
|
||||
}
|
||||
});
|
||||
c.gridy = 11;
|
||||
contentPane.add(btnNewButton_1, c);
|
||||
|
||||
// Button Beenden
|
||||
JButton btnNewButton_3 = new JButton("Beenden");
|
||||
btnNewButton_3.setBackground(Color.GRAY);
|
||||
btnNewButton_3.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
c.gridy = 12;
|
||||
c.anchor = GridBagConstraints.PAGE_END;
|
||||
contentPane.add(btnNewButton_3, c);
|
||||
this.pack();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user