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:
@@ -1,7 +1,7 @@
|
||||
package org.toop.framework.assets;
|
||||
package org.toop.framework.asset;
|
||||
|
||||
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> {
|
||||
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.assets.resources.BaseResource;
|
||||
import org.toop.framework.assets.resources.ImageAsset;
|
||||
import org.toop.framework.asset.resources.AudioAsset;
|
||||
import org.toop.framework.asset.resources.BaseResource;
|
||||
import org.toop.framework.asset.resources.ImageAsset;
|
||||
|
||||
import java.io.File;
|
||||
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.*;
|
||||
|
||||
public class AssetManager {
|
||||
private static AssetLoader loader = null;
|
||||
private static final AssetManager INSTANCE = new AssetManager();
|
||||
private static final Map<String, Asset<? extends BaseResource>> assets = new HashMap<>();
|
||||
|
||||
@@ -14,6 +16,12 @@ public class AssetManager {
|
||||
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) {
|
||||
ArrayList<Asset<T>> list = new ArrayList<>();
|
||||
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 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.FileNotFoundException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.toop.framework.assets.resources;
|
||||
package org.toop.framework.asset.resources;
|
||||
|
||||
import javafx.scene.image.Image;
|
||||
import java.io.File;
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.toop.framework.assets.resources;
|
||||
package org.toop.framework.asset.resources;
|
||||
|
||||
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.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
||||
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;
|
||||
|
||||
import org.toop.framework.SnowflakeGenerator;
|
||||
import org.toop.framework.assets.Asset;
|
||||
import org.toop.framework.assets.AssetManager;
|
||||
import org.toop.framework.assets.resources.AudioResource;
|
||||
import org.toop.framework.asset.Asset;
|
||||
import org.toop.framework.asset.AssetManager;
|
||||
import org.toop.framework.asset.resources.AudioAsset;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
|
||||
@@ -12,15 +12,16 @@ import java.util.*;
|
||||
import javax.sound.sampled.*;
|
||||
|
||||
public class SoundManager {
|
||||
private final AssetManager asm = AssetManager.getInstance();
|
||||
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
|
||||
|
||||
public SoundManager(AssetManager asm) {
|
||||
public SoundManager() {
|
||||
// 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 {
|
||||
this.addAudioResource(resource);
|
||||
this.addAudioResource(asset);
|
||||
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -38,52 +39,47 @@ public class SoundManager {
|
||||
this.stopSound(event.clipId());
|
||||
}
|
||||
|
||||
private void addAudioResource(Asset<AudioResource> audioAsset)
|
||||
private void addAudioResource(Asset<AudioAsset> audioAsset)
|
||||
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) {
|
||||
try {
|
||||
AudioResource resource = audioResources.get(audioFileName);
|
||||
AudioAsset asset = audioResources.get(audioFileName);
|
||||
|
||||
// Return -1 which indicates resource wasn't available
|
||||
if (resource == null){
|
||||
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);
|
||||
// Return -1 which indicates resource wasn't available
|
||||
if (asset == null){
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
|
||||
Reference in New Issue
Block a user