move ParticleEffect load to AssetManager

This commit is contained in:
Anthony Calosa
2023-03-12 07:19:57 +08:00
parent 63fedcdd81
commit 2a82a51fab
2 changed files with 98 additions and 103 deletions

View File

@@ -9,6 +9,7 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import forge.Forge;
import forge.adventure.util.Config; import forge.adventure.util.Config;
import forge.util.MyRandom; import forge.util.MyRandom;
@@ -18,170 +19,154 @@ import forge.util.MyRandom;
*/ */
public class MapActor extends Actor { public class MapActor extends Actor {
private boolean removeIfEffectsAreFinished; private boolean removeIfEffectsAreFinished;
public void removeAfterEffects() { public void removeAfterEffects() {
removeIfEffectsAreFinished=true; removeIfEffectsAreFinished = true;
} }
static class CurrentEffect static class CurrentEffect {
{ public CurrentEffect(String path, ParticleEffect effect, Vector2 offset, boolean overlay) {
public CurrentEffect(String path,ParticleEffect effect,Vector2 offset,boolean overlay)
{
this.path = path; this.path = path;
this.effect=effect; this.effect = effect;
this.offset=offset; this.offset = offset;
this.overlay=overlay; this.overlay = overlay;
} }
private final String path; private final String path;
public ParticleEffect effect; public ParticleEffect effect;
public Vector2 offset; public Vector2 offset;
public boolean overlay=true; public boolean overlay = true;
} }
Texture debugTexture; Texture debugTexture;
protected float collisionHeight=0.4f; protected float collisionHeight = 0.4f;
final int objectId; final int objectId;
Array<CurrentEffect> effects=new Array<>(); Array<CurrentEffect> effects = new Array<>();
public void removeEffect(String effectFly) { public void removeEffect(String effectFly) {
for(int i=0;i<effects.size;i++) for (int i = 0; i < effects.size; i++) {
{ CurrentEffect currentEffect = effects.get(i);
CurrentEffect currentEffect =effects.get(i); if (currentEffect.path.equals(effectFly)) {
if(currentEffect.path.equals(effectFly)) for (ParticleEmitter emitter : currentEffect.effect.getEmitters()) {
{
for(ParticleEmitter emitter:currentEffect.effect.getEmitters()) {
emitter.setContinuous(false); emitter.setContinuous(false);
} }
} }
} }
} }
public void playEffect(String path,float duration,boolean overlay,Vector2 offset)
{
ParticleEffect effect = new ParticleEffect();
effect.load(Config.instance().getFile(path),Config.instance().getFile(path).parent());
effect.setPosition(getCenterX(), getCenterY());
effects.add(new CurrentEffect(path, effect, offset, overlay));
if(duration!=0)//ParticleEffect.setDuration uses an integer for some reason
{
for(ParticleEmitter emitter:effect.getEmitters()){
emitter.setContinuous(false);
emitter.duration = duration;
emitter.durationTimer = 0.0F;
}
public void playEffect(String path, float duration, boolean overlay, Vector2 offset) {
ParticleEffect effect = Forge.getAssets().getEffect(Config.instance().getFile(path));
if (effect != null) {
effect.setPosition(getCenter().x, getCenter().y);
effects.add(new CurrentEffect(path, effect, offset, overlay));
//ParticleEffect.setDuration uses an integer for some reason
if (duration != 0) {
for (ParticleEmitter emitter : effect.getEmitters()) {
emitter.setContinuous(false);
emitter.duration = duration;
emitter.durationTimer = 0.0F;
}
}
} }
effect.start(); effect.start();
} }
public void playEffect(String path,float duration,boolean overlay)
{ public void playEffect(String path, float duration, boolean overlay) {
playEffect(path,duration,overlay,Vector2.Zero); playEffect(path, duration, overlay, Vector2.Zero);
} }
public void playEffect(String path,float duration)
{ public void playEffect(String path, float duration) {
playEffect(path,duration,true,Vector2.Zero); playEffect(path, duration, true, Vector2.Zero);
} }
public void playEffect(String path)
{ public void playEffect(String path) {
playEffect(path,0,true,Vector2.Zero); playEffect(path, 0, true, Vector2.Zero);
} }
public MapActor(int objectId)
{ public MapActor(int objectId) {
this.objectId=objectId; this.objectId = objectId;
} }
public int getObjectId()
{ public int getObjectId() {
return objectId; return objectId;
} }
private Texture getDebugTexture() { private Texture getDebugTexture() {
if (debugTexture == null) { if (debugTexture == null) {
Pixmap pixmap = new Pixmap((int) getWidth(), (int) getHeight(), Pixmap.Format.RGBA8888); Pixmap pixmap = new Pixmap((int) getWidth(), (int) getHeight(), Pixmap.Format.RGBA8888);
//pixmap.setColor(1.0f,0,0,0.5f); //pixmap.setColor(1.0f,0,0,0.5f);
pixmap.setColor(MyRandom.getRandom().nextFloat(),MyRandom.getRandom().nextFloat(),MyRandom.getRandom().nextFloat(),0.5f); pixmap.setColor(MyRandom.getRandom().nextFloat(), MyRandom.getRandom().nextFloat(), MyRandom.getRandom().nextFloat(), 0.5f);
pixmap.fillRectangle((int)(boundingRect.x - getX()), (int)(getHeight()- boundingRect.getHeight()) + (int)(boundingRect.y - getY()), (int)boundingRect.getWidth(), (int)boundingRect.getHeight()); pixmap.fillRectangle((int) (boundingRect.x - getX()), (int) (getHeight() - boundingRect.getHeight()) + (int) (boundingRect.y - getY()), (int) boundingRect.getWidth(), (int) boundingRect.getHeight());
debugTexture = new Texture(pixmap); debugTexture = new Texture(pixmap);
pixmap.dispose(); pixmap.dispose();
} }
return debugTexture; return debugTexture;
} }
final Rectangle boundingRect=new Rectangle();
boolean isCollidingWithPlayer=false; final Rectangle boundingRect = new Rectangle();
protected void onPlayerCollide()
{ boolean isCollidingWithPlayer = false;
protected void onPlayerCollide() {
} }
boolean boundDebug=false;
public void setBoundDebug(boolean debug) boolean boundDebug = false;
{
boundDebug=debug; public void setBoundDebug(boolean debug) {
boundDebug = debug;
} }
@Override @Override
public void draw(Batch batch, float alpha) { public void draw(Batch batch, float alpha) {
if (boundDebug) {
if(boundDebug) batch.draw(getDebugTexture(), getX(), getY());
{
batch.draw(getDebugTexture(),getX(),getY());
} }
for (CurrentEffect effect : effects) {
if (effect.overlay)
for(CurrentEffect effect:effects)
{
if(effect.overlay)
effect.effect.draw(batch); effect.effect.draw(batch);
} }
} }
protected void beforeDraw(Batch batch, float parentAlpha) { protected void beforeDraw(Batch batch, float parentAlpha) {
for (CurrentEffect effect : effects) {
for(CurrentEffect effect:effects) if (!effect.overlay)
{
if(!effect.overlay)
effect.effect.draw(batch); effect.effect.draw(batch);
} }
} }
float getCenterX() {
private Vector2 getCenter() {
float scale = 1f; float scale = 1f;
if (this instanceof EnemySprite) { if (this instanceof EnemySprite) {
scale = ((EnemySprite) this).getData().scale; scale = ((EnemySprite) this).getData().scale;
} }
return getX()+(getWidth()*scale)/2;
} return new Vector2(getX() + (getWidth() * scale) / 2, getY() + (getHeight() * scale) / 2);
float getCenterY() {
float scale = 1f;
if (this instanceof EnemySprite) {
scale = ((EnemySprite) this).getData().scale;
}
return getY()+(getHeight()*scale)/2;
} }
@Override @Override
public void act(float delta) { public void act(float delta) {
super.act(delta); super.act(delta);
for(int i=0;i<effects.size;i++) for (int i = 0; i < effects.size; i++) {
{ CurrentEffect effect = effects.get(i);
CurrentEffect effect=effects.get(i);
effect.effect.update(delta); effect.effect.update(delta);
effect.effect.setPosition(getCenterX()+effect.offset.x,getCenterY()+effect.offset.y); effect.effect.setPosition(getCenter().x + effect.offset.x, getCenter().y + effect.offset.y);
if(effect.effect.isComplete()) if (effect.effect.isComplete()) {
{
effects.removeIndex(i); effects.removeIndex(i);
i--; i--;
effect.effect.dispose(); //assetmanager should handle dispose automatically
//effect.effect.dispose();
} }
} }
if(effects.size==0&&removeIfEffectsAreFinished&&getParent()!=null) if (effects.size == 0 && removeIfEffectsAreFinished && getParent() != null)
getParent().removeActor(this); getParent().removeActor(this);
} }
@Override @Override
protected void positionChanged() { protected void positionChanged() {
super.positionChanged(); super.positionChanged();
@@ -201,28 +186,26 @@ public class MapActor extends Actor {
public Rectangle boundingRect() { public Rectangle boundingRect() {
return boundingRect; return boundingRect;
} }
public boolean collideWithPlayer(PlayerSprite other) { public boolean collideWithPlayer(PlayerSprite other) {
boolean newIsColliding= collideWith(other); boolean newIsColliding = collideWith(other);
if(newIsColliding) if (newIsColliding) {
{ if (!isCollidingWithPlayer)
if(!isCollidingWithPlayer)
onPlayerCollide(); onPlayerCollide();
isCollidingWithPlayer=true; isCollidingWithPlayer = true;
} } else {
else isCollidingWithPlayer = false;
{
isCollidingWithPlayer=false;
} }
return isCollidingWithPlayer; return isCollidingWithPlayer;
} }
public boolean collideWith(Rectangle other) {
return boundingRect().overlaps(other);
public boolean collideWith(Rectangle other) {
return boundingRect().overlaps(other);
} }
public int getId(){ public int getId() {
return objectId; return objectId;
} }
@@ -232,7 +215,6 @@ public class MapActor extends Actor {
public boolean collideWith(Actor other) { public boolean collideWith(Actor other) {
return boundingRect.x < other.getX() + other.getWidth() && boundingRect.x + boundingRect.width > other.getX() && boundingRect.y < other.getY() + other.getHeight() && boundingRect.y + boundingRect.height > other.getY(); return boundingRect.x < other.getX() + other.getWidth() && boundingRect.x + boundingRect.width > other.getX() && boundingRect.y < other.getY() + other.getHeight() && boundingRect.y + boundingRect.height > other.getY();
} }
public float getCollisionHeight() { public float getCollisionHeight() {

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetLoaderParameters; import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver; import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.ParticleEffectLoader;
import com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter; import com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter;
import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver; import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
@@ -12,6 +13,7 @@ import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.TextureData; import com.badlogic.gdx.graphics.TextureData;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.utils.Disposable;
@@ -270,6 +272,19 @@ public class Assets implements Disposable {
} }
return t; return t;
} }
public ParticleEffect getEffect(FileHandle file) {
if (file == null || !file.exists() || !FileType.Absolute.equals(file.type())) {
System.err.println("Failed to load: " + file +"!.");
return null;
}
ParticleEffect effect = manager().get(file.path(), ParticleEffect.class, false);
if (effect == null) {
manager().load(file.path(), ParticleEffect.class, new ParticleEffectLoader.ParticleEffectParameter());
manager().finishLoadingAsset(file.path());
effect = manager().get(file.path(), ParticleEffect.class);
}
return effect;
}
public Texture getDefaultImage() { public Texture getDefaultImage() {
if (defaultImage == null) { if (defaultImage == null) {
@@ -506,9 +521,7 @@ public class Assets implements Disposable {
currentMemory = calculateTextureSize(assetManager, fileName1, type1); currentMemory = calculateTextureSize(assetManager, fileName1, type1);
}; };
} }
super.load(fileName, type, parameter); super.load(fileName, type, parameter);
} }