Added songs, new libraries and more code
This commit is contained in:
@@ -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
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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) {}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>();
|
||||
|
||||
Reference in New Issue
Block a user