Added songs, new libraries and more code

This commit is contained in:
2015-05-29 12:47:25 +02:00
parent 4f4b10fd89
commit 7b21aa1e09
67 changed files with 388 additions and 75 deletions
+80
View File
@@ -0,0 +1,80 @@
package audio;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class AudioPlayer {
private Clip clip;
public AudioPlayer() {
clip = null;
}
public void setClip(Song s) {
try {
clip = null;
AudioInputStream ais = s.getAudioStream();
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
} catch (Exception e) {
e.printStackTrace();
}
}
public void play() {
if (clip == null)
return;
clip.start();
}
public void pause()
{
if (clip == null)
return;
if (clip.isRunning())
{
clip.stop();
}
}
public void play(int framePosition) {
if (clip == null)
return;
stop();
clip.setFramePosition(framePosition);
clip.start();
}
public void stop() {
if (clip == null)
return;
if (clip.isRunning())
{
clip.stop();
clip.setFramePosition(0);
}
}
public void close() {
stop();
if(clip != null)
{
clip.close();
}
}
public long getProgress()
{
if(clip != null)
return clip.getMicrosecondPosition();
else
return 0L;
}
}
+66 -1
View File
@@ -1,10 +1,16 @@
package audio;
import image.Images;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.media.Media;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Song {
@@ -18,6 +24,10 @@ public class Song {
private File background;
private File banner;
private File file;
private BufferedImage backgroundImage;
private BufferedImage bannerImage;
private AudioInputStream audioStream;
private List<SongInstance> songs;
@@ -32,6 +42,10 @@ public class Song {
background = null;
banner = null;
file = null;
backgroundImage = null;
bannerImage = null;
audioStream = null;
songs = new ArrayList<SongInstance>();
}
@@ -83,6 +97,27 @@ public class Song {
public File getAudio() {
return audio;
}
public AudioInputStream getAudioStream()
{
if(audioStream == null)
{
try {
this.audioStream = AudioSystem.getAudioInputStream(audio);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
audioStream.reset();
} catch (IOException e) {
e.printStackTrace();
}
return audioStream;
}
public void setAudio(File audio) {
this.audio = audio;
@@ -94,11 +129,30 @@ public class Song {
public void setBackground(File background) {
this.background = background;
}
public BufferedImage getBackgroundImage()
{
if(backgroundImage == null)
{
this.backgroundImage = Images.readImage(background);
}
return backgroundImage;
}
public File getBanner() {
return banner;
}
public BufferedImage getBannerImage()
{
if(bannerImage == null)
{
this.bannerImage = Images.readImage(banner);
}
return bannerImage;
}
public void setBanner(File banner) {
this.banner = banner;
@@ -115,4 +169,15 @@ public class Song {
public List<SongInstance> getSongs() {
return songs;
}
public void close()
{
if(audioStream != null)
{
try {
audioStream.close();
} catch (IOException e) {
}
}
}
}
+6 -6
View File
@@ -40,12 +40,12 @@ public class DirScanner {
{
for(File file2 : file.listFiles(csf))
{
try {
Song s = JSONReader.readSong(file2);
songs.add(s);
} catch (IOException e) {
e.printStackTrace();
}
try {
Song s = JSONReader.readSong(file2);
songs.add(s);
} catch (IOException e1) {
}
}
}
+6 -7
View File
@@ -10,7 +10,6 @@ import java.io.InputStream;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import model.GameModel;
@@ -54,17 +53,17 @@ public class JSONReader {
JsonObject file = obj.getJsonObject("file");
File audio = new File(f.getParent() + File.separator + file.getString("audio"));
if(!audio.exists() || !audio.getName().endsWith(".mp3"))
if(!audio.exists() || !audio.getName().toLowerCase().endsWith(".mp3"))
throw new FileNotFoundException("Audio file does not exist: " + audio.getPath());
s.setAudio(audio);
File background = new File(f.getParent() + File.separator + file.getString("background"));
if(!background.exists() || !(background.getName().endsWith(".jpg") || background.getName().endsWith(".png")))
if(!background.exists() || !(background.getName().toLowerCase().endsWith(".jpg") || background.getName().toLowerCase().endsWith(".png")))
throw new FileNotFoundException("Background image does not exist: " + background.getPath());
s.setBackground(background);
File banner = new File(f.getParent() + File.separator + file.getString("banner"));
if(!banner.exists() || !(banner.getName().endsWith(".jpg") || banner.getName().endsWith(".png")))
if(!banner.exists() || !(banner.getName().toLowerCase().endsWith(".jpg") || banner.getName().toLowerCase().endsWith(".png")))
throw new FileNotFoundException("Banner image does not exist: " + banner.getPath());
s.setBanner(banner);
@@ -90,10 +89,10 @@ public class JSONReader {
si.addObjectInstance( readObjectInstance(object.getJsonObject(i)) );
}
JsonArray button = obj.getJsonArray("button");
for(int i = 0; i < button.size(); i++)
JsonArray buttons = obj.getJsonArray("buttons");
for(int i = 0; i < buttons.size(); i++)
{
si.addButtonInstance( readButtonInstance(button.getJsonObject(i)) );
si.addButtonInstance( readButtonInstance(buttons.getJsonObject(i)) );
}
return si;
+19 -3
View File
@@ -3,8 +3,10 @@ package control;
import java.util.ArrayList;
import java.util.List;
import model.SongHandler;
import model.gameState.GameState;
import model.gameState.MenuState;
import model.gameState.PickSongState;
import model.gameState.PlayState;
public class GameStateManager {
@@ -13,17 +15,31 @@ public class GameStateManager {
public GameState currentState;
private int index = 0;
public GameStateManager(){
public enum State {
MENU_STATE,
PLAY_STATE,
PICKSONG_STATE
}
public GameStateManager(SongHandler sh){
gamestates = new ArrayList<GameState>();
gamestates.add(new MenuState(this));
gamestates.add(new PlayState(this));
gamestates.add(new MenuState(this, sh));
gamestates.add(new PlayState(this, sh));
gamestates.add(new PickSongState(this, sh));
currentState = gamestates.get(0);
}
public void setState(State st)
{
currentState = gamestates.get(st.ordinal());
currentState.init();
}
public void next() {
index++;
index %= gamestates.size();
currentState = gamestates.get(index);
currentState.init();
}
public void update(){
+32 -22
View File
@@ -1,6 +1,7 @@
package image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -8,31 +9,40 @@ import javax.imageio.ImageIO;
import main.Main;
public class Images{
public class Images {
public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
public Images(){
}
public Images() {
static{
try{
images.add(ImageIO.read(Main.class.getResource("/image/player.png")));
images.add(ImageIO.read(Main.class.getResource("/image/player2.png")));
}catch(IOException e){
e.printStackTrace();
}
}
}
public static BufferedImage getImage(ImageType img)
{
return images.get(img.ordinal());
}
static {
try {
images.add(ImageIO.read(Main.class.getResource("/image/player.png")));
images.add(ImageIO.read(Main.class.getResource("/image/player2.png")));
public enum ImageType
{
player,player2
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static BufferedImage getImage(ImageType img) {
return images.get(img.ordinal());
}
public enum ImageType {
player, player2
}
public static BufferedImage readImage(File f) {
BufferedImage bf = null;
try {
bf = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
return bf;
}
}
+17 -11
View File
@@ -13,6 +13,7 @@ import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import model.GameModel;
import model.SongHandler;
import view.GameView;
import control.GameControl;
import control.GameStateManager;
@@ -24,19 +25,14 @@ public class Window extends JFrame {
private static final long serialVersionUID = -9222956702898533696L;
public static boolean ON_RASP;
public final static int WIDTH = 1280;
public final static int HEIGHT = 1024;
public Window(boolean ON_RASP)
{
//Create window
super("Arcade");
setSize(1280, 1024);
//Set window close listener
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(WIDTH, HEIGHT);
//Create Events
LedHandler led = null;
@@ -62,12 +58,22 @@ public class Window extends JFrame {
JoystickHandler jsh = new JoystickHandler();
//Create Instances
GameStateManager gsm = new GameStateManager();
SongHandler sh = new SongHandler();
GameStateManager gsm = new GameStateManager(sh);
GameView view = new GameView(led,gsm);
GameModel model = new GameModel(gsm);
GameModel model = new GameModel(sh, gsm);
GameControl control = new GameControl(model, view,gsm);
setContentPane(view);
//Set window close listener
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
sh.close();
System.exit(0);
}
});
//Create EventListeners
if(!Window.ON_RASP){
addKeyListener(bth);
+4 -5
View File
@@ -15,14 +15,13 @@ public class GameModel implements ActionListener{
private Timer update;
public static Color[] colors = {Color.MAGENTA,Color.RED,Color.GREEN,Color.YELLOW,Color.CYAN,Color.WHITE};
private GameStateManager gsm;
private SongHandler sh;
SongHandler sh;
public GameModel(GameStateManager gsm)
public GameModel(SongHandler sh, GameStateManager gsm)
{
this.gsm = gsm;
sh = new SongHandler();
this.sh = sh;
update = new Timer(1000/30, this);
update.start();
@@ -34,6 +33,6 @@ public class GameModel implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
gsm.update();
gsm.update();
}
}
+69 -8
View File
@@ -1,11 +1,12 @@
package model;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javazoom.jl.player.Player;
import main.Window;
import audio.AudioPlayer;
import audio.Song;
import audio.io.DirScanner;
@@ -17,13 +18,17 @@ public class SongHandler {
private int currentIndex;
private File dir;
private AudioPlayer p;
public SongHandler()
{
songs = new ArrayList<Song>();
currentSong = null;
currentIndex = 0;
currentIndex = -1;
p = new AudioPlayer();
if(Window.ON_RASP)
dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs");
@@ -31,27 +36,83 @@ public class SongHandler {
dir = new File("Songs");
songs = DirScanner.scanDirectories(dir);
currentSong = songs.get(currentIndex);
}
public void next()
{
currentIndex++;
currentIndex%=songs.size();
currentSong = songs.get(currentIndex);
updatePlayer();
}
public void previous()
{
currentIndex--;
currentIndex%=songs.size();
currentSong = songs.get(currentIndex);
updatePlayer();
}
public void set(int i)
{
if(currentIndex != i)
{
currentIndex = i;
updatePlayer();
}
}
private void updatePlayer()
{
if(currentIndex < 0)
currentIndex = songs.size() + currentIndex;
currentIndex%=songs.size();
currentSong = songs.get(currentIndex);
p.stop();
p.setClip(currentSong);
}
public long getProgress()
{
return p.getProgress();
}
public List<Song> getSongs()
{
return songs;
}
public Song getCurrentSong()
{
return currentSong;
}
public void close()
{
p.close();
for(Song s : songs)
{
s.close();
}
}
public void play()
{
p.play();
}
public void play(boolean b)
{
if(b)
{
p.play((int)currentSong.getSampleStart()*10000);
}
}
public void pause()
{
p.pause();
}
public void stop()
{
p.stop();
}
}
+4 -2
View File
@@ -2,6 +2,7 @@ package model.gameState;
import java.awt.Graphics2D;
import model.SongHandler;
import control.GameStateManager;
import control.button.ButtonEvent;
import control.joystick.JoystickEvent;
@@ -9,10 +10,11 @@ import control.joystick.JoystickEvent;
public abstract class GameState {
protected GameStateManager gsm;
protected SongHandler sh;
public GameState(GameStateManager gsm) {
super();
public GameState(GameStateManager gsm, SongHandler sh) {
this.gsm = gsm;
this.sh = sh;
}
public abstract void init();
+10 -8
View File
@@ -2,14 +2,16 @@ package model.gameState;
import java.awt.Graphics2D;
import model.SongHandler;
import control.GameStateManager;
import control.GameStateManager.State;
import control.button.ButtonEvent;
import control.joystick.JoystickEvent;
public class MenuState extends GameState {
public MenuState(GameStateManager gsm) {
super(gsm);
public MenuState(GameStateManager gsm, SongHandler sh) {
super(gsm, sh);
}
@Override
public void init() {
@@ -26,6 +28,7 @@ public class MenuState extends GameState {
@Override
public void draw(Graphics2D g2) {
g2.drawString("Press 0 to start the game", 1280/2, 1024/2);
g2.drawString("Press 1 to pick a song", 1280/2, 1024/2 + 40);
}
@@ -34,20 +37,19 @@ public class MenuState extends GameState {
switch(e.getButton().getButtonID()){
case 0:
gsm.next();
gsm.setState(State.PLAY_STATE);
break;
case 1:
gsm.setState(State.PICKSONG_STATE);
break;
}
}
@Override
public void buttonReleased(ButtonEvent e) {
}
@Override
public void onJoystickMoved(JoystickEvent e) {
// TODO Auto-generated method stub
}
public void onJoystickMoved(JoystickEvent e) {}
}
+59
View File
@@ -0,0 +1,59 @@
package model.gameState;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.image.BufferedImage;
import model.SongHandler;
import control.GameStateManager;
import control.button.ButtonEvent;
import control.joystick.JoystickEvent;
public class PickSongState extends GameState {
public PickSongState(GameStateManager gsm, SongHandler sh) {
super(gsm, sh);
}
@Override
public void init() {
sh.set(0);
sh.play(true);
}
@Override
public void update() {
// TODO Auto-generated method stub
}
@Override
public void draw(Graphics2D g2d) {
g2d.drawImage(sh.getCurrentSong().getBackgroundImage(), null, 0, 0);
g2d.drawImage(sh.getCurrentSong().getBannerImage(), null, 400, 0);
}
@Override
public void buttonPressed(ButtonEvent e) {}
@Override
public void buttonReleased(ButtonEvent e) {}
@Override
public void onJoystickMoved(JoystickEvent e) {
switch (e.getJoystick().getPos()) {
case LEFT:
sh.previous();
sh.play(true);
break;
case RIGHT:
sh.next();
sh.play(true);
break;
default:
break;
}
}
}
+3 -2
View File
@@ -11,6 +11,7 @@ import java.util.Iterator;
import java.util.List;
import model.GameModel;
import model.SongHandler;
import model.drawObjects.Bullet;
import model.drawObjects.Enemy;
import model.drawObjects.Player;
@@ -34,8 +35,8 @@ public class PlayState extends GameState{
public static int currentScore = 0;
public static int lifePoints = 100;
public PlayState(GameStateManager gsm) {
super(gsm);
public PlayState(GameStateManager gsm, SongHandler sh) {
super(gsm, sh);
area = new PlayArea((int) borderRect.getX(),1024,1024,100);
infoPanel = new InfoPanel(0, 0);
enemys = new ArrayList<Enemy>();
BIN
View File
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 221 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 296 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 132 KiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 310 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 253 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 247 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 228 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 201 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 327 KiB

File diff suppressed because one or more lines are too long
Binary file not shown.

After

Width:  |  Height:  |  Size: 177 KiB

Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.