Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a formatting rules section and some unit tests. #167

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/maven-build-exe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
cache: maven
- name: Build with Maven
run: mvn -B -Pexe package --file pom.xml
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: Project16x16.exe
path: target/*.exe
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ It is recommended to create a commit every time you complete a task:
That's it for the Source tree setup! Now, you are ready to [open up the code editor](wiki). Don't forget to come back to this file when you are ready to know about [the Process of Making Changes](#the-process-of-making-changes).

## The Process of Making Changes
### Before starting coding... formatting rules!
Having a codebase formatted consistently makes things easier. Code will be easier to understand for new contributors and the risk of mistakes will be reduced.
Make sure to configure your IDE so that it uses **Eclipse_formatter.xml** file rules. You can also tell your IDE to automatically format the code before each save action. It is importanto to make sure that the code is formatted before any commit.

### Issues
You are now ready to contribute! Github represents tasks as issues. You can find all of the issues [here](https://github.com/Stephcraft/Project-16x16/issues) or in the **Issues** tab of the repository. If you found a bug, would like to add a feature or simply do not find something interesting to contribute to, then you can [create your own issue](https://github.com/Stephcraft/Project-16x16/issues/new).

Expand Down
18 changes: 6 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,12 @@
<artifactId>AppleJavaExtensions</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.14.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/project_16x16/Audio.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import ddf.minim.Minim;

/**
* Provides static methods for playing game audio: background music and sound
* Provides methods for playing game audio: background music and sound
* effects.
*
* @author micycle1
Expand All @@ -20,7 +20,7 @@ public final class Audio {
private static float gainBGM = 0;
private static float gainSFX = 0;

private static Minim minim;
private Minim minim;

/**
* Background music, which are referenced as enums.
Expand Down Expand Up @@ -58,16 +58,20 @@ public String getPath() {
}
}

private static final HashMap<SFX, AudioSample> SFX_MAP = new HashMap<>(); // could load from json
private static final HashMap<BGM, AudioPlayer> BGM_MAP = new HashMap<>(); // could load from json
private HashMap<SFX, AudioSample> SFX_MAP = new HashMap<>(); // could load from json
private HashMap<BGM, AudioPlayer> BGM_MAP = new HashMap<>(); // could load from json

protected Minim createMinim(SideScroller sideScroller) {
return new Minim(sideScroller);
}

/**
* Call during setup to instantiate a connection to Minim (the audio backend).
*
* @param s
*/
public static void assignApplet(SideScroller sideScroller) {
minim = new Minim(sideScroller);
public void assignApplet(SideScroller sideScroller) {
minim = createMinim(sideScroller);
for (SFX sfx : SFX.values()) {
AudioSample sample = minim.loadSample(sfx.getPath());
if (sample != null) {
Expand Down Expand Up @@ -98,7 +102,7 @@ public static void assignApplet(SideScroller sideScroller) {
* @see #play(SFX, float)
* @see ddf.minim.AudioSample#trigger()
*/
public static void play(SFX sound) {
public void play(SFX sound) {
if (SFX_MAP.containsKey(sound)) {
SFX_MAP.get(sound).trigger();
}
Expand All @@ -116,7 +120,7 @@ public static void play(SFX sound) {
* @see #play(SFX)
* @see ddf.minim.AudioSample#trigger()
*/
public static void play(SFX sound, float gain) {
public void play(SFX sound, float gain) {
if (SFX_MAP.containsKey(sound)) {
SFX_MAP.get(sound).setGain(gain);
SFX_MAP.get(sound).trigger();
Expand All @@ -134,7 +138,7 @@ public static void play(SFX sound, float gain) {
* @param sound BGM track
* @see #play(BGM, float)
*/
public static void play(BGM sound) {
public void play(BGM sound) {
if (BGM_MAP.get(sound).isPlaying()) {
return;
}
Expand Down Expand Up @@ -162,7 +166,7 @@ public static void play(BGM sound) {
* @param gain gain, in decibels (where negative is quieter). Default = 0.
* @see #play(BGM)
*/
public static void play(BGM sound, float gain) {
public void play(BGM sound, float gain) {
if (BGM_MAP.containsKey(sound)) {
BGM_MAP.get(sound).setGain(gain);
play(sound);
Expand All @@ -177,7 +181,7 @@ public static void play(BGM sound, float gain) {
*
* @param gain gain, in decibels (where negative is quieter). Default = 0.
*/
public static void setGainBGM(float gain) {
public void setGainBGM(float gain) {
gainBGM = gain;
BGM_MAP.values().forEach(sound -> sound.setGain(gainBGM));
}
Expand All @@ -187,23 +191,23 @@ public static void setGainBGM(float gain) {
*
* @param gain gain, in decibels (where negative is quieter). Default = 0.
*/
public static void setGainSFX(float gain) {
public void setGainSFX(float gain) {
gainSFX = gain;
SFX_MAP.values().forEach(sound -> sound.setGain(gainSFX));
}

/**
* Global mute.
*/
public static void mute() {
public void mute() {
SFX_MAP.values().forEach(sound -> sound.mute());
BGM_MAP.values().forEach(sound -> sound.mute());
}

/**
* Global unmute.
*/
public static void unMute() {
public void unMute() {
SFX_MAP.values().forEach(sound -> sound.unmute());
BGM_MAP.values().forEach(sound -> sound.unmute());
}
Expand All @@ -214,7 +218,7 @@ public static void unMute() {
*
* @see Minim#stop()
*/
public static void exit() {
public void exit() {
minim.stop();
}

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/project_16x16/SideScroller.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,27 @@
import javafx.scene.transform.Scale;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PSurface;
import processing.core.PVector;

import processing.event.MouseEvent;
import processing.javafx.PSurfaceFX;

import project_16x16.Options.Option;
import project_16x16.components.AnimationComponent;
import project_16x16.entities.Player;
import project_16x16.factory.AudioFactory;
import project_16x16.multiplayer.Multiplayer;
import project_16x16.scene.*;
import project_16x16.scene.AudioSettings;
import project_16x16.scene.GameplayScene;
import project_16x16.scene.GameplayScene.GameModes;
import project_16x16.scene.MainMenu;
import project_16x16.scene.MultiplayerClientMenu;
import project_16x16.scene.MultiplayerHostMenu;
import project_16x16.scene.MultiplayerMenu;
import project_16x16.scene.PScene;
import project_16x16.scene.PauseMenu;
import project_16x16.scene.Settings;
import project_16x16.ui.Notifications;

/**
Expand Down Expand Up @@ -157,7 +163,7 @@ protected PSurface initSurface() {
*/
private void closeWindowEvent(WindowEvent event) {
try {
Audio.exit();
AudioFactory.getInstance().exit();
game.exit();
} finally {
stage.close();
Expand Down Expand Up @@ -188,7 +194,7 @@ public void setup() {
load();
AnimationComponent.assignApplet(this);
Notifications.assignApplet(this);
Audio.assignApplet(this);
AudioFactory.getInstance().assignApplet(this);

// Create scene
sceneHistory = new ArrayDeque<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.apache.commons.collections.map.MultiValueMap;

import processing.core.PImage;
import project_16x16.Audio;
import project_16x16.Audio.SFX;
import project_16x16.SideScroller;
import project_16x16.factory.AudioFactory;

/**
* The Animation Class
Expand Down Expand Up @@ -84,7 +84,7 @@ public PImage animate() {
}
Collection<SFX> coll = (Collection<SFX>) sounds.get((int) currentFrame); // TODO high overhead?
if (coll != null) {
coll.forEach(sound -> Audio.play(sound));
coll.forEach(sound -> AudioFactory.getInstance().play(sound));
}
return frame;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/project_16x16/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import processing.core.PImage;
import processing.core.PVector;
import processing.data.JSONObject;
import project_16x16.Audio;
import project_16x16.Audio.SFX;
import project_16x16.Constants;
import project_16x16.Options;
Expand All @@ -15,6 +14,7 @@
import project_16x16.Tileset;
import project_16x16.Utility;
import project_16x16.components.AnimationComponent;
import project_16x16.factory.AudioFactory;
import project_16x16.objects.CollidableObject;
import project_16x16.objects.EditableObject;
import project_16x16.projectiles.Swing;
Expand Down Expand Up @@ -204,7 +204,7 @@ private void handleKeyboardInput() {
state.flying = true;
state.jumping = true;
velocity.y -= speedJump;
Audio.play(SFX.JUMP);
AudioFactory.getInstance().play(SFX.JUMP);
if (state.dashing) {
state.dashing = false;
velocity.y *= 1.2f;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/project_16x16/factory/AudioFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package project_16x16.factory;

import project_16x16.Audio;

public class AudioFactory {

private static Audio audio;

public static Audio getInstance() {
if (audio == null) {
audio = new Audio();
}

return audio;
}

public static Audio createInstance() {
return new Audio();
}

}
11 changes: 5 additions & 6 deletions src/main/java/project_16x16/scene/AudioSettings.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package project_16x16.scene;

import processing.core.PApplet;
import processing.core.PConstants;
import processing.event.KeyEvent;
import processing.event.MouseEvent;
import project_16x16.Audio;
import project_16x16.Constants;
import project_16x16.Options;
import project_16x16.Options.Option;
import project_16x16.SideScroller;
import project_16x16.factory.AudioFactory;
import project_16x16.ui.Button;
import project_16x16.ui.Notifications;
import project_16x16.ui.Slider;
Expand Down Expand Up @@ -79,8 +78,8 @@ void mouseDragged(MouseEvent e) {
volumeSFX.update();
float volBGM = 20 * (float) Math.log(volumeBGM.getValue());
float volSFX = 20 * (float) Math.log(volumeSFX.getValue());
Audio.setGainBGM(volBGM);
Audio.setGainSFX(volSFX);
AudioFactory.getInstance().setGainBGM(volBGM);
AudioFactory.getInstance().setGainSFX(volSFX);
}

@Override
Expand All @@ -90,8 +89,8 @@ void mouseReleased(MouseEvent e) {

if (quit.hover()) {
// revert sound changes if menu is quit
Audio.setGainBGM(originalVolumeBGM);
Audio.setGainSFX(originalVolumeSFX);
AudioFactory.getInstance().setGainBGM(originalVolumeBGM);
AudioFactory.getInstance().setGainSFX(originalVolumeSFX);
game.returnScene();
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/project_16x16/scene/GameplayScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import processing.data.JSONArray;
import processing.data.JSONObject;
import processing.event.MouseEvent;
import project_16x16.Audio;
import project_16x16.Audio.BGM;
import project_16x16.Options;
import project_16x16.SideScroller;
Expand All @@ -22,6 +21,7 @@
import project_16x16.components.Tile;
import project_16x16.components.Tile.TileType;
import project_16x16.entities.Player;
import project_16x16.factory.AudioFactory;
import project_16x16.multiplayer.Multiplayer;
import project_16x16.objects.BackgroundObject;
import project_16x16.objects.CollidableObject;
Expand Down Expand Up @@ -202,7 +202,7 @@ private void setup() {
public void switchTo() {
super.switchTo();
((PauseMenu) GameScenes.PAUSE_MENU.getScene()).switched = false;
Audio.play(BGM.TEST1);
AudioFactory.getInstance().play(BGM.TEST1);
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/project_16x16/scene/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
import processing.core.PGraphics;
import processing.event.KeyEvent;
import processing.event.MouseEvent;

import project_16x16.SideScroller;
import project_16x16.Utility;
import project_16x16.Audio;
import project_16x16.Audio.BGM;
import project_16x16.Constants;
import project_16x16.SideScroller;
import project_16x16.SideScroller.GameScenes;
import project_16x16.scene.PScene;
import project_16x16.Utility;
import project_16x16.factory.AudioFactory;
import project_16x16.ui.Button;

/**
Expand Down Expand Up @@ -72,7 +70,7 @@ public MainMenu(SideScroller a) {
@Override
public void switchTo() {
super.switchTo();
Audio.play(BGM.TEST3);
AudioFactory.getInstance().play(BGM.TEST3);
}

@Override
Expand Down
Loading