mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Fixed unknown names, Assetmanager now has initializeloader
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -13,7 +13,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
package org.toop;
|
package org.toop;
|
||||||
|
|
||||||
import org.toop.app.gui.LocalServerSelector;
|
import org.toop.app.gui.LocalServerSelector;
|
||||||
import org.toop.framework.assets.AssetManager;
|
import org.toop.framework.asset.AssetLoader;
|
||||||
|
import org.toop.framework.asset.AssetManager;
|
||||||
import org.toop.framework.audio.SoundManager;
|
import org.toop.framework.audio.SoundManager;
|
||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
@@ -15,9 +16,10 @@ import java.nio.file.NotDirectoryException;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
|
static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
|
||||||
var a = new AssetManager(new File("app/src/main/resources/assets"));
|
|
||||||
|
AssetManager.initializeLoader(new File("app/src/main/resources/assets"));
|
||||||
var b = new NetworkingClientManager();
|
var b = new NetworkingClientManager();
|
||||||
var c = new SoundManager(a);
|
var c = new SoundManager();
|
||||||
|
|
||||||
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("mainmenu.wav", true)).asyncPostEvent();
|
||||||
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
|
new EventFlow().addPostEvent(new AudioEvents.PlayAudio("sadtrombone.wav", true)).asyncPostEvent();
|
||||||
@@ -31,5 +33,6 @@ public class Main {
|
|||||||
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
|
javax.swing.SwingUtilities.invokeLater(LocalServerSelector::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void initSystems() throws NetworkingInitializationException, NotDirectoryException {}
|
private static void initSystems() throws NetworkingInitializationException, NotDirectoryException {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package org.toop.framework.assets;
|
package org.toop.framework.asset;
|
||||||
|
|
||||||
import org.toop.framework.SnowflakeGenerator;
|
import org.toop.framework.SnowflakeGenerator;
|
||||||
import org.toop.framework.assets.resources.BaseResource;
|
import org.toop.framework.asset.resources.BaseResource;
|
||||||
|
|
||||||
public class Asset <T extends BaseResource> {
|
public class Asset <T extends BaseResource> {
|
||||||
private final Long id;
|
private final Long id;
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
package org.toop.framework.assets;
|
package org.toop.framework.asset;
|
||||||
|
|
||||||
import org.toop.framework.assets.resources.AudioAsset;
|
import org.toop.framework.asset.resources.AudioAsset;
|
||||||
import org.toop.framework.assets.resources.BaseResource;
|
import org.toop.framework.asset.resources.BaseResource;
|
||||||
import org.toop.framework.assets.resources.ImageAsset;
|
import org.toop.framework.asset.resources.ImageAsset;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package org.toop.framework.assets;
|
package org.toop.framework.asset;
|
||||||
|
|
||||||
import org.toop.framework.assets.resources.*;
|
import org.toop.framework.asset.resources.*;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class AssetManager {
|
public class AssetManager {
|
||||||
|
private static AssetLoader loader = null;
|
||||||
private static final AssetManager INSTANCE = new AssetManager();
|
private static final AssetManager INSTANCE = new AssetManager();
|
||||||
private static final Map<String, Asset<? extends BaseResource>> assets = new HashMap<>();
|
private static final Map<String, Asset<? extends BaseResource>> assets = new HashMap<>();
|
||||||
|
|
||||||
@@ -14,6 +16,12 @@ public class AssetManager {
|
|||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void initializeLoader(File rootFolder) {
|
||||||
|
if (loader == null) {
|
||||||
|
loader = new AssetLoader(rootFolder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public <T extends BaseResource> ArrayList<Asset<T>> getAllOfType(Class<T> type) {
|
public <T extends BaseResource> ArrayList<Asset<T>> getAllOfType(Class<T> type) {
|
||||||
ArrayList<Asset<T>> list = new ArrayList<>();
|
ArrayList<Asset<T>> list = new ArrayList<>();
|
||||||
for (Asset<? extends BaseResource> asset : assets.values()) { // <-- use .values()
|
for (Asset<? extends BaseResource> asset : assets.values()) { // <-- use .values()
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.assets.resources;
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
import javax.sound.sampled.*;
|
import javax.sound.sampled.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public abstract class BaseResource {
|
||||||
|
|
||||||
|
final InputStream stream;
|
||||||
|
final File file;
|
||||||
|
|
||||||
|
BaseResource(final File file) {
|
||||||
|
this.file = file;
|
||||||
|
try {
|
||||||
|
this.stream = new BufferedInputStream(new FileInputStream(file));
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream getInputStream() {
|
||||||
|
return this.stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
public File getFile() {
|
||||||
|
return this.file;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.assets.resources;
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.assets.resources;
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
import javafx.scene.image.Image;
|
import javafx.scene.image.Image;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.toop.framework.assets.resources;
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
@@ -1,14 +1,7 @@
|
|||||||
package org.toop.framework.assets.resources;
|
package org.toop.framework.asset.resources;
|
||||||
|
|
||||||
import org.w3c.dom.Text;
|
|
||||||
|
|
||||||
import javax.sound.sampled.AudioSystem;
|
|
||||||
import javax.sound.sampled.Clip;
|
|
||||||
import javax.sound.sampled.LineUnavailableException;
|
|
||||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class TextAsset extends BaseResource implements LoadableResource {
|
public class TextAsset extends BaseResource implements LoadableResource {
|
||||||
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package org.toop.framework.assets.resources;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
|
|
||||||
public abstract class BaseResource {
|
|
||||||
|
|
||||||
final InputStream stream;
|
|
||||||
final File file;
|
|
||||||
public abstract class Resource {
|
|
||||||
final private byte[] rawData;
|
|
||||||
final private File file;
|
|
||||||
|
|
||||||
BaseResource(final File file) {
|
|
||||||
Resource(final File file) throws RuntimeException {
|
|
||||||
this.file = file;
|
|
||||||
try {
|
|
||||||
this.rawData = Files.readAllBytes(file.toPath());
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public InputStream getInputStream() {
|
|
||||||
return this.stream;
|
|
||||||
public Resource load() {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public InputStream getStream() {
|
|
||||||
return new BufferedInputStream(new ByteArrayInputStream(this.rawData));
|
|
||||||
}
|
|
||||||
|
|
||||||
public File getFile() {
|
|
||||||
return this.file;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
|
|
||||||
package org.toop.framework.assets.resources;
|
|
||||||
|
|
||||||
import javafx.scene.image.Image;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
public class ImageResource extends Resource {
|
|
||||||
|
|
||||||
private Image image = null;
|
|
||||||
|
|
||||||
public ImageResource(File imageFile) {
|
|
||||||
super(imageFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Image getImage() {
|
|
||||||
return this.image;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Resource load() {
|
|
||||||
this.image = new Image(this.getStream());
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package org.toop.framework.audio;
|
package org.toop.framework.audio;
|
||||||
|
|
||||||
import org.toop.framework.SnowflakeGenerator;
|
import org.toop.framework.SnowflakeGenerator;
|
||||||
import org.toop.framework.assets.Asset;
|
import org.toop.framework.asset.Asset;
|
||||||
import org.toop.framework.assets.AssetManager;
|
import org.toop.framework.asset.AssetManager;
|
||||||
import org.toop.framework.assets.resources.AudioResource;
|
import org.toop.framework.asset.resources.AudioAsset;
|
||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
|
|
||||||
@@ -12,15 +12,16 @@ import java.util.*;
|
|||||||
import javax.sound.sampled.*;
|
import javax.sound.sampled.*;
|
||||||
|
|
||||||
public class SoundManager {
|
public class SoundManager {
|
||||||
|
private final AssetManager asm = AssetManager.getInstance();
|
||||||
private final Map<Long, Clip> activeClips = new HashMap<>();
|
private final Map<Long, Clip> activeClips = new HashMap<>();
|
||||||
private final HashMap<String, AudioResource> audioResources = new HashMap<>();
|
private final HashMap<String, AudioAsset> audioResources = new HashMap<>();
|
||||||
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
|
private final SnowflakeGenerator idGenerator = new SnowflakeGenerator(); // TODO: Don't create a new generator
|
||||||
|
|
||||||
public SoundManager(AssetManager asm) {
|
public SoundManager() {
|
||||||
// Get all Audio Resources and add them to a list.
|
// Get all Audio Resources and add them to a list.
|
||||||
for (Asset<AudioResource> resource : asm.getAllResourceOfType(AudioResource.class).values()) {
|
for (Asset<AudioAsset> asset : asm.getAllOfType(AudioAsset.class)) {
|
||||||
try {
|
try {
|
||||||
this.addAudioResource(resource);
|
this.addAudioResource(asset);
|
||||||
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
|
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -38,52 +39,47 @@ public class SoundManager {
|
|||||||
this.stopSound(event.clipId());
|
this.stopSound(event.clipId());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addAudioResource(Asset<AudioResource> audioAsset)
|
private void addAudioResource(Asset<AudioAsset> audioAsset)
|
||||||
throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
throws IOException, UnsupportedAudioFileException, LineUnavailableException {
|
||||||
AudioResource audioResource = audioAsset.getResource();
|
|
||||||
|
|
||||||
this.audioResources.put(audioAsset.getName(), audioResource);
|
this.audioResources.put(audioAsset.getName(), audioAsset.getResource());
|
||||||
}
|
}
|
||||||
|
|
||||||
private long playSound(String audioFileName, boolean loop) {
|
private long playSound(String audioFileName, boolean loop) {
|
||||||
try {
|
AudioAsset asset = audioResources.get(audioFileName);
|
||||||
AudioResource resource = audioResources.get(audioFileName);
|
|
||||||
|
|
||||||
// Return -1 which indicates resource wasn't available
|
// Return -1 which indicates resource wasn't available
|
||||||
if (resource == null){
|
if (asset == null){
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
// Get a new clip from resource
|
|
||||||
Clip clip = resource.getNewClip();
|
|
||||||
|
|
||||||
// If supposed to loop make it loop, else just start it once
|
|
||||||
if (loop){
|
|
||||||
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
clip.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate id for clip
|
|
||||||
long clipId = idGenerator.nextId();
|
|
||||||
|
|
||||||
// store it so we can stop it later
|
|
||||||
activeClips.put(clipId, clip); // TODO: Do on snowflake for specific sound to stop
|
|
||||||
|
|
||||||
// remove when finished (only for non-looping sounds)
|
|
||||||
clip.addLineListener(event -> {
|
|
||||||
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
|
|
||||||
activeClips.remove(clipId);
|
|
||||||
clip.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Return id so it can be stopped
|
|
||||||
return clipId;
|
|
||||||
} catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get a new clip from resource
|
||||||
|
Clip clip = asset.getClip();
|
||||||
|
|
||||||
|
// If supposed to loop make it loop, else just start it once
|
||||||
|
if (loop) {
|
||||||
|
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
clip.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate id for clip
|
||||||
|
long clipId = idGenerator.nextId();
|
||||||
|
|
||||||
|
// store it so we can stop it later
|
||||||
|
activeClips.put(clipId, clip); // TODO: Do on snowflake for specific sound to stop
|
||||||
|
|
||||||
|
// remove when finished (only for non-looping sounds)
|
||||||
|
clip.addLineListener(event -> {
|
||||||
|
if (event.getType() == LineEvent.Type.STOP && !clip.isRunning()) {
|
||||||
|
activeClips.remove(clipId);
|
||||||
|
clip.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return id so it can be stopped
|
||||||
|
return clipId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopSound(long clipId) {
|
public void stopSound(long clipId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user