I forgor to push
This commit is contained in:
@@ -5,12 +5,14 @@
|
||||
package MP2_chapter4_CalebFontenot;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javafx.animation.KeyFrame;
|
||||
import javafx.animation.Timeline;
|
||||
import javafx.beans.property.DoubleProperty;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
@@ -27,7 +29,9 @@ import javafx.util.Duration;
|
||||
*/
|
||||
public class BouncingBall extends Pane {
|
||||
|
||||
private List<MediaPlayer> mediaPlayers = new ArrayList<>();
|
||||
GridPane textPane = new GridPane();
|
||||
private String currentlyPlaying;
|
||||
private final int COLLISION_COOLDOWN = 20;
|
||||
private long timeSinceLastCollisionEvent = 0;
|
||||
final double RACKET_LENGTH = 100;
|
||||
@@ -41,27 +45,33 @@ public class BouncingBall extends Pane {
|
||||
private Label ballCords = new Label();
|
||||
private Label playerScoreLabel = new Label("Player Score: 0");
|
||||
private Label computerScoreLabel = new Label("Computer Score: 0");
|
||||
private Label nowPlaying = new Label();
|
||||
private Button restartBgm = new Button("Restart BGM");
|
||||
|
||||
private Timeline animation;
|
||||
|
||||
private int computerScore, playerScore = 0;
|
||||
|
||||
public BouncingBall()
|
||||
{
|
||||
public BouncingBall() {
|
||||
restartBgm.setOnMouseClicked(e -> {
|
||||
playSound("bgm");
|
||||
});
|
||||
circle.setFill(Color.RED); // Set ball color
|
||||
racket.setFill(Color.BLUE);
|
||||
textPane.add(racketTime, 0, 0);
|
||||
textPane.add(ballCords, 0, 1);
|
||||
textPane.add(playerScoreLabel, 0, 2);
|
||||
textPane.add(computerScoreLabel, 0, 3);
|
||||
textPane.add(nowPlaying, 0, 4);
|
||||
textPane.add(restartBgm, 0, 5);
|
||||
getChildren().addAll(circle, racket, textPane); // Place a ball into this pane
|
||||
racket.relocate(0, 580);
|
||||
playSound("bgm");
|
||||
// Create an animation for moving the ball
|
||||
animation = new Timeline(new KeyFrame(Duration.millis(1), new EventHandler<ActionEvent>() {
|
||||
@Override
|
||||
public void handle(ActionEvent t)
|
||||
{
|
||||
|
||||
public void handle(ActionEvent t) {
|
||||
|
||||
timeSinceLastCollisionEvent++;
|
||||
racketTime.setText("Frames since last collision: " + timeSinceLastCollisionEvent);
|
||||
moveBall();
|
||||
@@ -87,8 +97,7 @@ public class BouncingBall extends Pane {
|
||||
|
||||
}
|
||||
|
||||
private boolean processRacketCollision()
|
||||
{
|
||||
private boolean processRacketCollision() {
|
||||
boolean racketCollision = racket.getBoundsInParent().intersects(circle.getBoundsInParent());
|
||||
|
||||
if (racketCollision && (timeSinceLastCollisionEvent > COLLISION_COOLDOWN)) { // This is second condition is a cooldown. It prevents odd behavior happening with the ball and the racket if the racket hits the ball at certain angles.
|
||||
@@ -102,8 +111,7 @@ public class BouncingBall extends Pane {
|
||||
}
|
||||
}
|
||||
|
||||
private void moveBall()
|
||||
{
|
||||
private void moveBall() {
|
||||
|
||||
// Check boundaries
|
||||
if (x < radius || x > getWidth() - radius) {
|
||||
@@ -121,40 +129,33 @@ public class BouncingBall extends Pane {
|
||||
ballCoordsToLabel();
|
||||
}
|
||||
|
||||
public void play()
|
||||
{
|
||||
public void play() {
|
||||
animation.play();
|
||||
}
|
||||
|
||||
public void pause()
|
||||
{
|
||||
public void pause() {
|
||||
animation.pause();
|
||||
}
|
||||
|
||||
public void increaseSpeed()
|
||||
{
|
||||
public void increaseSpeed() {
|
||||
animation.setRate(animation.getRate() * 1.5);
|
||||
System.out.println(animation.getRate());
|
||||
}
|
||||
|
||||
public void decreaseSpeed()
|
||||
{
|
||||
public void decreaseSpeed() {
|
||||
animation.setRate(animation.getRate() * 1.5 > 0 ? animation.getRate() / 1.5 : 0);
|
||||
System.out.println(animation.getRate());
|
||||
}
|
||||
|
||||
public DoubleProperty rateProperty()
|
||||
{
|
||||
public DoubleProperty rateProperty() {
|
||||
return animation.rateProperty();
|
||||
}
|
||||
|
||||
public void moveRacket(double x)
|
||||
{
|
||||
public void moveRacket(double x) {
|
||||
racket.relocate(x, 580);
|
||||
}
|
||||
|
||||
public void showInfoLabel()
|
||||
{
|
||||
public void showInfoLabel() {
|
||||
double paneHeight = getHeight();
|
||||
double paneWidth = getWidth();
|
||||
getChildren().add(infoLabel);
|
||||
@@ -162,59 +163,93 @@ public class BouncingBall extends Pane {
|
||||
infoLabel.relocate(paneWidth / 4.0, paneHeight / 2.0);
|
||||
}
|
||||
|
||||
public void hideInfoLabel()
|
||||
{
|
||||
public void hideInfoLabel() {
|
||||
getChildren().remove(infoLabel);
|
||||
}
|
||||
|
||||
private void incrementPlayerScore()
|
||||
{
|
||||
private void incrementPlayerScore() {
|
||||
playerScoreLabel.setText("Player score: " + ++playerScore);
|
||||
}
|
||||
|
||||
private void incrementComputerScore()
|
||||
{
|
||||
private void incrementComputerScore() {
|
||||
computerScoreLabel.setText("Computer score: " + ++computerScore);
|
||||
|
||||
}
|
||||
|
||||
private void ballCoordsToLabel()
|
||||
{
|
||||
private void ballCoordsToLabel() {
|
||||
ballCords.setText("Ball coords: " + x + ", " + y);
|
||||
}
|
||||
|
||||
private void playSound(String sampleType)
|
||||
{
|
||||
String sample = "";
|
||||
private void playSound(String sampleType) {
|
||||
boolean isSfx = true;
|
||||
String sample = "sound/sfx/Sample_00";
|
||||
int randInt = (int) (Math.random() * 2);
|
||||
switch (sampleType) {
|
||||
case "collision":
|
||||
randInt = (int) (Math.random() * 2);
|
||||
isSfx = true;
|
||||
switch (randInt) {
|
||||
case 0:
|
||||
sample = "05";
|
||||
sample += "05";
|
||||
break;
|
||||
case 1:
|
||||
sample = "07";
|
||||
sample += "07";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "racket":
|
||||
randInt = (int) (Math.random() * 2);
|
||||
isSfx = true;
|
||||
switch (randInt) {
|
||||
case 0:
|
||||
sample = "10";
|
||||
sample += "10";
|
||||
break;
|
||||
case 1:
|
||||
sample = "12";
|
||||
sample += "12";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case "bgm":
|
||||
sample = "sound/bgm/";
|
||||
randInt = (int) (Math.random() * 3);
|
||||
isSfx = false;
|
||||
switch (randInt) {
|
||||
case 0:
|
||||
currentlyPlaying = "Caleb Fontenot - Blips";
|
||||
nowPlaying.setText("Now playing: " + currentlyPlaying);
|
||||
break;
|
||||
case 1:
|
||||
currentlyPlaying = "Caleb Fontenot - DEAL WITH IT";
|
||||
nowPlaying.setText("Now playing: " + currentlyPlaying);
|
||||
break;
|
||||
case 2:
|
||||
currentlyPlaying = "Caleb Fontenot - Tomodachi Moment";
|
||||
nowPlaying.setText("Now playing: " + currentlyPlaying);
|
||||
break;
|
||||
}
|
||||
sample += currentlyPlaying;
|
||||
break;
|
||||
}
|
||||
|
||||
Media sound = new Media(new File("sound/Sample_00" + sample + ".wav").toURI().toString());
|
||||
Media sound = new Media(new File(sample + ".wav").toURI().toString());
|
||||
MediaPlayer mediaPlayer = new MediaPlayer(sound);
|
||||
|
||||
mediaPlayer.setStartTime(Duration.ZERO);
|
||||
|
||||
mediaPlayer.setAutoPlay(true);
|
||||
// If we're playing BGM, lower the volume a little
|
||||
if (!isSfx) {
|
||||
mediaPlayer.setVolume(0.6);
|
||||
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
|
||||
}
|
||||
mediaPlayer.play();
|
||||
//mediaPlayer.setOnEndOfMedia();
|
||||
mediaPlayers.add(mediaPlayer); // Prevent the JVM's GC from deleting our media player if there's already one running
|
||||
// Free the media player when it has finished playing a sound.
|
||||
mediaPlayer.setOnEndOfMedia(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
//System.out.println("I'm done playing sound! Please take me to the ether, Java GC!");
|
||||
mediaPlayer.stop();
|
||||
mediaPlayer.dispose();
|
||||
mediaPlayers.remove(mediaPlayer);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package MP2_chapter4_CalebFontenot;
|
||||
|
||||
import javafx.scene.media.MediaPlayer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class MediaType {
|
||||
private MediaPlayer mediaPlayer;
|
||||
private boolean isBgm;
|
||||
|
||||
public MediaType(MediaPlayer mediaPlayer, boolean isBgm) {
|
||||
this.mediaPlayer = mediaPlayer;
|
||||
this.isBgm = isBgm;
|
||||
}
|
||||
|
||||
public boolean isIsBgm() {
|
||||
return isBgm;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user