- code cleanup
- Verbeteringen in stoppen van audio -- Simpelere afhandeling -- Thread stopt wanneer (clip != null) ipv .stop()
This commit is contained in:
@@ -35,17 +35,15 @@ public class AudioPlayer{
|
||||
} catch (JavaLayerException | FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if(clip != null){
|
||||
Thread thread = new Thread(() -> {
|
||||
while (clip != null){
|
||||
try {
|
||||
clip.play();
|
||||
} catch (JavaLayerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}});
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@@ -54,14 +52,13 @@ public class AudioPlayer{
|
||||
{
|
||||
clip.close();
|
||||
clip = null;
|
||||
|
||||
try {
|
||||
if(fis != null)
|
||||
fis.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(thread != null && thread.isAlive())
|
||||
thread.stop();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ public class GameStateManager {
|
||||
public GameState currentState;
|
||||
private SongHandler sh;
|
||||
|
||||
private int index = 0;
|
||||
public int fps;
|
||||
private int index = 0;
|
||||
public int fps;
|
||||
private float timeOfNoAction = 0,
|
||||
maxTimeToHaveNoAction = 45000;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class GameStateManager {
|
||||
|
||||
public GameStateManager(SongHandler sh, SQLConnector sql){
|
||||
this.sh = sh;
|
||||
gamestates = new ArrayList<GameState>();
|
||||
gamestates = new ArrayList<>();
|
||||
gamestates.add(new TitleState(this, sh, sql));
|
||||
gamestates.add(new MenuState(this, sh, sql));
|
||||
gamestates.add(new HelpState(this, sh, sql));
|
||||
@@ -51,18 +51,10 @@ public class GameStateManager {
|
||||
|
||||
public void setState(State st)
|
||||
{
|
||||
if (st.ordinal() > 0)
|
||||
if (currentState != null)
|
||||
currentState.stopAudio();
|
||||
|
||||
currentState = gamestates.get(st.ordinal());
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
public void next() {
|
||||
index++;
|
||||
index %= gamestates.size();
|
||||
currentState = gamestates.get(index);
|
||||
init();
|
||||
}
|
||||
|
||||
@@ -89,13 +81,11 @@ public class GameStateManager {
|
||||
public void buttonPressed(ButtonEvent e) {
|
||||
timeOfNoAction = 0;
|
||||
currentState.buttonPressed(e);
|
||||
|
||||
}
|
||||
|
||||
public void buttonReleased(ButtonEvent e) {
|
||||
timeOfNoAction = 0;
|
||||
currentState.buttonReleased(e);
|
||||
|
||||
}
|
||||
|
||||
public void onJoystickMoved(JoystickEvent e) {
|
||||
|
||||
@@ -16,10 +16,10 @@ public class NetworkHandler implements Runnable{
|
||||
private DatagramSocket udp;
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
private int port;
|
||||
|
||||
private boolean running;
|
||||
private Thread t;
|
||||
private Thread t;
|
||||
|
||||
private byte[] send,
|
||||
receive;
|
||||
|
||||
+10
-13
@@ -7,14 +7,14 @@ import control.NetworkHandler;
|
||||
|
||||
public class Button {
|
||||
|
||||
private Color color;
|
||||
private int ledID,
|
||||
buttonID;
|
||||
private NetworkHandler ntw;
|
||||
public int pressed;
|
||||
private Color color;
|
||||
private int ledID,
|
||||
buttonID;
|
||||
public int pressed;
|
||||
|
||||
private NetworkHandler ntw;
|
||||
|
||||
public Button(int buttonID, int ledID, NetworkHandler ntw)
|
||||
{
|
||||
public Button(int buttonID, int ledID, NetworkHandler ntw) {
|
||||
color = new Color(255,255,255);
|
||||
this.ledID = ledID;
|
||||
this.buttonID = buttonID;
|
||||
@@ -23,17 +23,14 @@ public class Button {
|
||||
setLed();
|
||||
}
|
||||
|
||||
private void setLed()
|
||||
{
|
||||
if(Window.ON_RASP)
|
||||
{
|
||||
private void setLed() {
|
||||
if(Window.ON_RASP) {
|
||||
ntw.setLed(ledID, color.getGreen(), color.getRed(), color.getBlue());
|
||||
ntw.show();
|
||||
}
|
||||
}
|
||||
|
||||
public void setColor(Color newColor)
|
||||
{
|
||||
public void setColor(Color newColor) {
|
||||
color = newColor;
|
||||
setLed();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package control.button;
|
||||
|
||||
public interface ButtonListener {
|
||||
public void buttonPressed(ButtonEvent e);
|
||||
public void buttonReleased(ButtonEvent e);
|
||||
void buttonPressed(ButtonEvent e);
|
||||
void buttonReleased(ButtonEvent e);
|
||||
}
|
||||
|
||||
+13
-33
@@ -16,10 +16,8 @@ import audio.SongInstance;
|
||||
|
||||
public class SQLConnector
|
||||
{
|
||||
|
||||
private Connection myConn = null;
|
||||
|
||||
public SQLConnector()
|
||||
public SQLConnector()
|
||||
{
|
||||
this("178.62.254.153", "3306","colorstrike","csadmin","aardbei123");
|
||||
}
|
||||
@@ -52,7 +50,7 @@ public class SQLConnector
|
||||
//System.out.println(query);
|
||||
Statement st = executeResultQuery(query);
|
||||
ResultSet result = null;
|
||||
List<Highscore> hsc = new ArrayList<Highscore>();
|
||||
List<Highscore> hsc = new ArrayList<>();
|
||||
|
||||
try {
|
||||
result = st.getResultSet();
|
||||
@@ -73,7 +71,7 @@ public class SQLConnector
|
||||
{
|
||||
Statement st = executeResultQuery("SELECT * FROM highscore WHERE date=CURDATE() AND songinstance = (SELECT id FROM songinstance WHERE difficulty='" + si.getDifficulty() + "' AND song=(SELECT id FROM song WHERE folder='" + s.getFolder() + "')) ORDER BY score DESC");
|
||||
ResultSet result = null;
|
||||
List<Highscore> hsc = new ArrayList<Highscore>();
|
||||
List<Highscore> hsc = new ArrayList<>();
|
||||
try {
|
||||
result = st.getResultSet();
|
||||
} catch (SQLException e1) {
|
||||
@@ -92,8 +90,7 @@ public class SQLConnector
|
||||
}
|
||||
|
||||
public int addHighscore(Song s, SongInstance si, String name, int currentScore) {
|
||||
if(name.length() <= 5 && currentScore <= Integer.MAX_VALUE)
|
||||
{
|
||||
if(name.length() <= 5 && currentScore <= Integer.MAX_VALUE) {
|
||||
String query = "INSERT INTO highscore (username, score, date, songinstance) VALUES ('" + name.trim() + "', " + currentScore + ", CURDATE(), (SELECT id FROM songinstance WHERE difficulty='" + si.getDifficulty() + "' AND song=(SELECT id FROM song WHERE folder='" + s.getFolder() + "')))";
|
||||
//System.out.println(query);
|
||||
return executeInsertQuery(query);
|
||||
@@ -106,7 +103,6 @@ public class SQLConnector
|
||||
String query = "INSERT INTO playdata (enemies_missed, enemies_hit, buttons_pressed, joystick_moved, start_time, play_time, songinstance) VALUES (" + enemies_missed + ", " + enemies_hit + ", " + buttons_pressed + ", " + joystick_moved + ", NOW(), " + time + ", (SELECT id FROM songinstance WHERE difficulty='" + si.getDifficulty() + "' AND song=(SELECT id FROM song WHERE folder='" + s.getFolder() + "')))";
|
||||
//System.out.println(query);
|
||||
executeInsertQuery(query);
|
||||
|
||||
}
|
||||
|
||||
public void update(List<Song> songs)
|
||||
@@ -131,22 +127,15 @@ public class SQLConnector
|
||||
System.out.println("Empty return set");
|
||||
}
|
||||
|
||||
if(id != -1)
|
||||
{
|
||||
for(SongInstance si : s.getSongs())
|
||||
{
|
||||
if(id != -1) {
|
||||
for(SongInstance si : s.getSongs()) {
|
||||
Statement st2 = executeResultQuery("SELECT id FROM songinstance WHERE song=" + id + " AND difficulty='" + si.getDifficulty() + "'");
|
||||
|
||||
ResultSet rs2 = null;
|
||||
|
||||
try {
|
||||
rs2 = st2.getResultSet();
|
||||
if(rs2.first())
|
||||
{
|
||||
//System.err.println("SongInstance Already Exists ");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(!rs2.first()) {
|
||||
executeInsertQuery("INSERT INTO songinstance (song, enemies, difficulty) VALUES (" + id + ", " + si.getObjects().size() + ", '" + si.getDifficulty() + "');");
|
||||
//System.out.println("Songinstance Added");
|
||||
}
|
||||
@@ -168,10 +157,6 @@ public class SQLConnector
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//System.err.println("Insert Failed");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -182,11 +167,11 @@ public class SQLConnector
|
||||
Statement s = myConn.createStatement();
|
||||
s.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
|
||||
ResultSet rs = s.getGeneratedKeys();
|
||||
|
||||
int id = 0;
|
||||
if(rs.next())
|
||||
{
|
||||
id = rs.getInt(1);
|
||||
}
|
||||
|
||||
s.close();
|
||||
return id;
|
||||
}
|
||||
@@ -207,11 +192,9 @@ public class SQLConnector
|
||||
private Statement executeResultQuery(String query)
|
||||
{
|
||||
Statement s = null;
|
||||
|
||||
try{
|
||||
try {
|
||||
s = myConn.createStatement();
|
||||
s.executeQuery(query);
|
||||
|
||||
}
|
||||
catch( SQLException ex)
|
||||
{
|
||||
@@ -219,8 +202,7 @@ public class SQLConnector
|
||||
System.out.println("SQLState: " + ex.getSQLState());
|
||||
System.out.println("VendorError: " + ex.getErrorCode());
|
||||
}
|
||||
catch( Exception ex)
|
||||
{
|
||||
catch( Exception ex) {
|
||||
System.out.println("getMeasurement: " + ex.getMessage());
|
||||
}
|
||||
|
||||
@@ -232,10 +214,8 @@ public class SQLConnector
|
||||
public void finalize() throws Throwable
|
||||
{
|
||||
// Close database connection
|
||||
if( myConn != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
if( myConn != null ) {
|
||||
try {
|
||||
myConn.close();
|
||||
System.out.println("Database connection terminated");
|
||||
}
|
||||
|
||||
+6
-6
@@ -17,10 +17,9 @@ import main.Main;
|
||||
|
||||
public class Images {
|
||||
|
||||
public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
|
||||
public static ArrayList<BufferedImage> images = new ArrayList<>();
|
||||
|
||||
public Images() {
|
||||
|
||||
}
|
||||
|
||||
static {
|
||||
@@ -33,7 +32,7 @@ public class Images {
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/kast.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/gameover.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/help.png"))));
|
||||
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/gameover.png"))));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -44,7 +43,9 @@ public class Images {
|
||||
}
|
||||
|
||||
public enum ImageType {
|
||||
pulse,cursor,pressstart,colorstrike,background,aanwijzers,kast,gameover,help
|
||||
pulse, cursor, pressstart,
|
||||
colorstrike, background, aanwijzers,
|
||||
kast, gameover, help
|
||||
}
|
||||
|
||||
public static BufferedImage readImage(File f) {
|
||||
@@ -99,8 +100,7 @@ public class Images {
|
||||
public static VolatileImage initVolatileImage(int width, int height, int opc){
|
||||
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
|
||||
VolatileImage image = null;
|
||||
image = gc.createCompatibleVolatileImage(width, height, opc);
|
||||
VolatileImage image = gc.createCompatibleVolatileImage(width, height, opc);
|
||||
int valid = image.validate(gc);
|
||||
if (valid == VolatileImage.IMAGE_INCOMPATIBLE)
|
||||
image = initVolatileImage(width, height, opc);
|
||||
|
||||
@@ -8,12 +8,14 @@ import model.objects.Path;
|
||||
|
||||
public class Enemy extends DrawObject {
|
||||
|
||||
public static final double distanceToOctagon = 1000,secondsToEnd = 5.0;
|
||||
private int length;
|
||||
private double lengthOf1Side,distanceFromStart;//lengthOf1Side wordt alleen gebruikt als de lijn een schuine lijn is.
|
||||
public Line2D enemy;
|
||||
private Color c;
|
||||
private Path path;
|
||||
public static final double distanceToOctagon = 1000,
|
||||
secondsToEnd = 5.0;
|
||||
private int length;
|
||||
private double lengthOf1Side,
|
||||
distanceFromStart; //lengthOf1Side wordt alleen gebruikt als de lijn een schuine lijn is.
|
||||
public Line2D enemy;
|
||||
private Color c;
|
||||
private Path path;
|
||||
|
||||
public Enemy(int pathID,int lengthOfEnemy,Color c,Path path){
|
||||
super();
|
||||
@@ -89,9 +91,7 @@ public class Enemy extends DrawObject {
|
||||
double angleX,angleY;
|
||||
angleX = Math.cos(Math.toRadians(45))*distanceFromStart;
|
||||
angleY = Math.sin(Math.toRadians(45))*distanceFromStart;
|
||||
|
||||
|
||||
|
||||
|
||||
switch(index){
|
||||
case 0:
|
||||
y1 = path.getY1() + distanceFromStart;
|
||||
|
||||
@@ -105,6 +105,9 @@ public class GameOverState extends GameState {
|
||||
case 2:
|
||||
gsm.setState(State.MENU_STATE);
|
||||
break;
|
||||
case 3:
|
||||
gsm.setState(State.HELP_STATE);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,13 +20,12 @@ import data.io.SQLConnector;
|
||||
|
||||
public class HelpState extends GameState {
|
||||
|
||||
BufferedImage help = Images.getImage(ImageType.help);
|
||||
VolatileImage background;
|
||||
Font textFontSmall = new Font("OCR A Extended", Font.BOLD, 15);
|
||||
private BufferedImage help = Images.getImage(ImageType.help);
|
||||
private VolatileImage background;
|
||||
private Font textFontSmall = new Font("OCR A Extended", Font.BOLD, 15);
|
||||
|
||||
|
||||
int index = 0;
|
||||
int frame;
|
||||
int index = 0,
|
||||
frame;
|
||||
|
||||
|
||||
public HelpState(GameStateManager gsm, SongHandler sh, SQLConnector sql){
|
||||
@@ -41,23 +40,18 @@ public class HelpState extends GameState {
|
||||
|
||||
@Override
|
||||
public void update(float factor) {
|
||||
|
||||
frame++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Graphics2D g2) {
|
||||
g2.drawImage(background, 0, 0, 1280, 1024, null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buttonPressed(ButtonEvent e) {
|
||||
|
||||
if(e.getButton().getButtonID() == 2) {
|
||||
if(e.getButton().getButtonID() == 2)
|
||||
gsm.setState(State.MENU_STATE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -53,8 +53,6 @@ public class MenuState extends GameState {
|
||||
BufferedImage aanwijzers = Images.getImage(ImageType.aanwijzers);
|
||||
|
||||
private int index = 0,
|
||||
getDifSelect = 0,
|
||||
getOldDifSelect = -1,
|
||||
yPosDiffButton = 900,
|
||||
kleurButton1 = 0,
|
||||
kleurButton2 = 1,
|
||||
@@ -64,8 +62,8 @@ public class MenuState extends GameState {
|
||||
|
||||
public MenuState(GameStateManager gsm, SongHandler sh, SQLConnector sql) {
|
||||
super(gsm, sh, sql);
|
||||
buttons = new ArrayList<MenuButton>();
|
||||
buttons2 = new ArrayList<DifficultyButton>();
|
||||
buttons = new ArrayList<>();
|
||||
buttons2 = new ArrayList<>();
|
||||
this.songs = sh.getSongs();
|
||||
startanimation = true;
|
||||
subscreen = false;
|
||||
@@ -90,23 +88,19 @@ public class MenuState extends GameState {
|
||||
}
|
||||
@Override
|
||||
public void init() {
|
||||
if(subscreen)
|
||||
{
|
||||
if(subscreen) {
|
||||
ButtonHandler.getButton(1).setColor(GameModel.colors[0]);
|
||||
ButtonHandler.getButton(2).setColor(GameModel.colors[2]);
|
||||
generateSubScreenForeground();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
ButtonHandler.getButton(1).setColor(GameModel.colors[0]);
|
||||
|
||||
ButtonHandler.getButton(5).setColor(GameModel.colors[1]);
|
||||
ButtonHandler.getButton(6).setColor(GameModel.colors[4]);
|
||||
JoystickHandler.REPEAT = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -116,11 +110,11 @@ public class MenuState extends GameState {
|
||||
if(animationcounter == 5){
|
||||
startanimation = false;
|
||||
}
|
||||
}else if(subscreen){
|
||||
}/*else if(subscreen){
|
||||
//nextScreen();
|
||||
}else{
|
||||
//previousScreen();
|
||||
}
|
||||
}*/
|
||||
if(selected != oldselected){
|
||||
for(int i = 0; i < buttons.size(); i++){
|
||||
buttons.get(i).setSong(selectedToSong(selected+(i-2)));
|
||||
@@ -214,25 +208,20 @@ public class MenuState extends GameState {
|
||||
if(subscreen){
|
||||
if(e.getJoystick().getPos() == Joystick.Position.DOWN){
|
||||
difSelect--;
|
||||
if(difSelect < 0){
|
||||
if(difSelect < 0)
|
||||
difSelect += buttons2.size();
|
||||
}
|
||||
}else if(e.getJoystick().getPos() == Joystick.Position.UP){
|
||||
difSelect++;
|
||||
if(difSelect > buttons2.size()-1){
|
||||
else if(e.getJoystick().getPos() == Joystick.Position.UP)
|
||||
difSelect++;
|
||||
if(difSelect > buttons2.size()-1)
|
||||
difSelect = 0;
|
||||
}
|
||||
|
||||
}
|
||||
else if(e.getJoystick().getPos() == Joystick.Position.CENTER)
|
||||
{
|
||||
else if(e.getJoystick().getPos() == Joystick.Position.CENTER)
|
||||
sh.set(selectedToSong(selected).getSongs().get(difSelect));
|
||||
}
|
||||
//generateSubScreenForeground();
|
||||
}else{ //Screen for selecting song
|
||||
if(e.getJoystick().getPos() == Joystick.Position.DOWN){
|
||||
selected++;
|
||||
|
||||
|
||||
kleurButton1++;
|
||||
kleurButton1 %= 6;
|
||||
|
||||
@@ -247,8 +236,6 @@ public class MenuState extends GameState {
|
||||
|
||||
kleurButton5++;
|
||||
kleurButton5 %= 6;
|
||||
|
||||
|
||||
|
||||
}else if(e.getJoystick().getPos() == Joystick.Position.UP){
|
||||
selected--;
|
||||
@@ -269,7 +256,7 @@ public class MenuState extends GameState {
|
||||
kleurButton5 = 5;
|
||||
}
|
||||
|
||||
else if(e.getJoystick().getPos() == Position.CENTER)
|
||||
else if(e.getJoystick().getPos() == Joystick.Position.CENTER)
|
||||
{
|
||||
buttons2.clear();
|
||||
int instanceNr = 0;
|
||||
@@ -386,17 +373,16 @@ public class MenuState extends GameState {
|
||||
if(highscores.isEmpty())
|
||||
{
|
||||
highscores = sql.getHighscores(selectedToSong(selected), sh.getCurrentSong().getSongs().get(difSelect));
|
||||
|
||||
if(highscores.isEmpty())
|
||||
{
|
||||
highscoresFound = false;
|
||||
}
|
||||
else
|
||||
g2.drawString("All Time Highscores", 30, 300);
|
||||
}
|
||||
else
|
||||
g2.drawString("Daily Highscore", 30, 300);
|
||||
g2.setFont(textFont2);
|
||||
|
||||
|
||||
g2.setFont(textFont2);
|
||||
if(highscoresFound)
|
||||
{
|
||||
HIGHSCORES_TO_DISPLAY = HIGHSCORES_TO_DISPLAY > highscores.size() ? highscores.size() : HIGHSCORES_TO_DISPLAY;
|
||||
@@ -409,9 +395,7 @@ public class MenuState extends GameState {
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
g2.drawString("No Highscores found", 30, 400);
|
||||
}
|
||||
|
||||
/*
|
||||
List<Highscore> highscores = sql.getHighscores(selectedToSong(selected), sh.getCurrentSong().getSongs().get(difSelect));
|
||||
@@ -419,9 +403,8 @@ public class MenuState extends GameState {
|
||||
int highest = 0;
|
||||
int oldHighest = 0;
|
||||
*/
|
||||
for(DifficultyButton b : buttons2){
|
||||
for(DifficultyButton b : buttons2)
|
||||
b.draw(g2);
|
||||
}
|
||||
|
||||
g2.setFont(textFontSmall);
|
||||
|
||||
@@ -465,7 +448,6 @@ public class MenuState extends GameState {
|
||||
animationcounter++;
|
||||
if(buttons.get(button).getX() >= -200)
|
||||
buttonInAnimation(button+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,7 +457,6 @@ public class MenuState extends GameState {
|
||||
return songs.get(songs.size()+s);
|
||||
else
|
||||
return songs.get(s);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user