generated from sigonasr2/JavaProjectTemplate
Include sound engine
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
9fe872b7c2
commit
883649be4d
BIN
sounds/battle.wav
Normal file
BIN
sounds/battle.wav
Normal file
Binary file not shown.
BIN
sounds/laserShoot.wav
Normal file
BIN
sounds/laserShoot.wav
Normal file
Binary file not shown.
@ -11,6 +11,7 @@ import sig.engine.Point;
|
||||
import sig.engine.Sprite;
|
||||
import sig.engine.Transform;
|
||||
import sig.engine.PolygonStructure;
|
||||
import sig.engine.Sound;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.List;
|
||||
@ -32,10 +33,14 @@ public class JavaProjectTemplate {
|
||||
Player pl = new Player();
|
||||
Sprite bookSpr = new Sprite("book.png");
|
||||
|
||||
Sound backgroundMusic = new Sound("battle.wav",true);
|
||||
Sound laserShootSound = new Sound("laserShoot.wav");
|
||||
|
||||
public void initializeGame(){
|
||||
//Initialize your game here.
|
||||
game.SetBorderColor(Color.BRIGHT_BLACK);
|
||||
game.Cursor_SetCursor(new Sprite("cursor.png"),0,0);
|
||||
backgroundMusic.play();
|
||||
}
|
||||
|
||||
public void updateGame(double fElapsedTime) {
|
||||
@ -57,6 +62,9 @@ public class JavaProjectTemplate {
|
||||
if (Key.isHeld(KeyEvent.VK_S)) {
|
||||
pl.y+=200*fElapsedTime;
|
||||
}
|
||||
if (Key.isPressed(KeyEvent.VK_SPACE)) {
|
||||
laserShootSound.play();
|
||||
}
|
||||
|
||||
if (Mouse.isPressed(2)) { //If middle click is pressed, reset the player position.
|
||||
pl.x=pl.y=200;
|
||||
|
@ -65,6 +65,8 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
public static Panel p;
|
||||
public static JFrame f;
|
||||
|
||||
List<Sound> currentlyPlayingSounds = new ArrayList<Sound>();
|
||||
|
||||
int ACTUAL_WINDOW_WIDTH=JavaProjectTemplate.WINDOW_WIDTH,ACTUAL_WINDOW_HEIGHT=JavaProjectTemplate.WINDOW_HEIGHT;
|
||||
Point<Integer> vViewSize = new Point<Integer>(1,1);
|
||||
Point<Integer> vViewPos = new Point<Integer>(1,1);
|
||||
|
57
src/sig/engine/Sound.java
Normal file
57
src/sig/engine/Sound.java
Normal file
@ -0,0 +1,57 @@
|
||||
package sig.engine;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
public class Sound {
|
||||
public final static File SOUNDS_FOLDER = new File("..","sounds");
|
||||
AudioInputStream data;
|
||||
Clip c;
|
||||
boolean loop;
|
||||
public Sound(java.lang.String filename){
|
||||
this(filename,false);
|
||||
}
|
||||
public Sound(java.lang.String filename,boolean loop) {
|
||||
this.loop=loop;
|
||||
try {
|
||||
data=AudioSystem.getAudioInputStream(new File(SOUNDS_FOLDER.getAbsolutePath(),filename));
|
||||
c = AudioSystem.getClip();
|
||||
c.open(data);
|
||||
System.out.println("Loaded sound for "+filename+".");
|
||||
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("ERROR: Failed to load sound in "+filename+".");
|
||||
}
|
||||
}
|
||||
public void play(){
|
||||
c.setFramePosition(0);
|
||||
c.loop((loop)?Clip.LOOP_CONTINUOUSLY:0);
|
||||
}
|
||||
public boolean isPlaying(){
|
||||
return c.isRunning();
|
||||
}
|
||||
public void unload(){
|
||||
try {
|
||||
c.close();
|
||||
data.close();
|
||||
c=null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
public boolean canLoop(){
|
||||
return loop;
|
||||
}
|
||||
public void setCanLoop(boolean canLoop) {
|
||||
loop=canLoop;
|
||||
if (c!=null) {
|
||||
c.loop((loop)?Clip.LOOP_CONTINUOUSLY:0);
|
||||
}
|
||||
}
|
||||
}
|
@ -25,10 +25,11 @@ public class Sprite{
|
||||
this.width=img.getWidth();
|
||||
this.height=img.getHeight();
|
||||
this.img=img;
|
||||
System.out.println("Loaded sprite for "+filename+".");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("ERROR: Failed to load sprite in "+filename+".");
|
||||
}
|
||||
System.out.println("Loaded sprite for "+filename+".");
|
||||
}
|
||||
|
||||
public BufferedImage getImg() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user