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.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Array;
import forge.Forge;
import forge.adventure.util.Config;
import forge.util.MyRandom;
@@ -18,170 +19,154 @@ import forge.util.MyRandom;
*/
public class MapActor extends Actor {
private boolean removeIfEffectsAreFinished;
public void removeAfterEffects() {
removeIfEffectsAreFinished=true;
removeIfEffectsAreFinished = true;
}
static class CurrentEffect
{
public CurrentEffect(String path,ParticleEffect effect,Vector2 offset,boolean overlay)
{
static class CurrentEffect {
public CurrentEffect(String path, ParticleEffect effect, Vector2 offset, boolean overlay) {
this.path = path;
this.effect=effect;
this.offset=offset;
this.overlay=overlay;
this.effect = effect;
this.offset = offset;
this.overlay = overlay;
}
private final String path;
public ParticleEffect effect;
public Vector2 offset;
public boolean overlay=true;
public boolean overlay = true;
}
Texture debugTexture;
protected float collisionHeight=0.4f;
protected float collisionHeight = 0.4f;
final int objectId;
Array<CurrentEffect> effects=new Array<>();
Array<CurrentEffect> effects = new Array<>();
public void removeEffect(String effectFly) {
for(int i=0;i<effects.size;i++)
{
CurrentEffect currentEffect =effects.get(i);
if(currentEffect.path.equals(effectFly))
{
for(ParticleEmitter emitter:currentEffect.effect.getEmitters()) {
for (int i = 0; i < effects.size; i++) {
CurrentEffect currentEffect = effects.get(i);
if (currentEffect.path.equals(effectFly)) {
for (ParticleEmitter emitter : currentEffect.effect.getEmitters()) {
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();
}
public void playEffect(String path,float duration,boolean overlay)
{
playEffect(path,duration,overlay,Vector2.Zero);
public void playEffect(String path, float duration, boolean overlay) {
playEffect(path, duration, overlay, Vector2.Zero);
}
public void playEffect(String path,float duration)
{
playEffect(path,duration,true,Vector2.Zero);
public void playEffect(String path, float duration) {
playEffect(path, duration, true, Vector2.Zero);
}
public void playEffect(String path)
{
playEffect(path,0,true,Vector2.Zero);
public void playEffect(String path) {
playEffect(path, 0, true, Vector2.Zero);
}
public MapActor(int objectId)
{
this.objectId=objectId;
public MapActor(int objectId) {
this.objectId = objectId;
}
public int getObjectId()
{
public int getObjectId() {
return objectId;
}
private Texture getDebugTexture() {
if (debugTexture == null) {
Pixmap pixmap = new Pixmap((int) getWidth(), (int) getHeight(), Pixmap.Format.RGBA8888);
//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);
pixmap.dispose();
}
return debugTexture;
}
final Rectangle boundingRect=new Rectangle();
boolean isCollidingWithPlayer=false;
protected void onPlayerCollide()
{
final Rectangle boundingRect = new Rectangle();
boolean isCollidingWithPlayer = false;
protected void onPlayerCollide() {
}
boolean boundDebug=false;
public void setBoundDebug(boolean debug)
{
boundDebug=debug;
boolean boundDebug = false;
public void setBoundDebug(boolean debug) {
boundDebug = debug;
}
@Override
public void draw(Batch batch, float alpha) {
if(boundDebug)
{
batch.draw(getDebugTexture(),getX(),getY());
if (boundDebug) {
batch.draw(getDebugTexture(), getX(), getY());
}
for(CurrentEffect effect:effects)
{
if(effect.overlay)
for (CurrentEffect effect : effects) {
if (effect.overlay)
effect.effect.draw(batch);
}
}
protected void beforeDraw(Batch batch, float parentAlpha) {
for(CurrentEffect effect:effects)
{
if(!effect.overlay)
for (CurrentEffect effect : effects) {
if (!effect.overlay)
effect.effect.draw(batch);
}
}
float getCenterX() {
private Vector2 getCenter() {
float scale = 1f;
if (this instanceof EnemySprite) {
scale = ((EnemySprite) this).getData().scale;
}
return getX()+(getWidth()*scale)/2;
}
float getCenterY() {
float scale = 1f;
if (this instanceof EnemySprite) {
scale = ((EnemySprite) this).getData().scale;
}
return getY()+(getHeight()*scale)/2;
return new Vector2(getX() + (getWidth() * scale) / 2, getY() + (getHeight() * scale) / 2);
}
@Override
public void act(float delta) {
super.act(delta);
for(int i=0;i<effects.size;i++)
{
CurrentEffect effect=effects.get(i);
for (int i = 0; i < effects.size; i++) {
CurrentEffect effect = effects.get(i);
effect.effect.update(delta);
effect.effect.setPosition(getCenterX()+effect.offset.x,getCenterY()+effect.offset.y);
if(effect.effect.isComplete())
{
effect.effect.setPosition(getCenter().x + effect.offset.x, getCenter().y + effect.offset.y);
if (effect.effect.isComplete()) {
effects.removeIndex(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);
}
@Override
protected void positionChanged() {
super.positionChanged();
@@ -201,28 +186,26 @@ public class MapActor extends Actor {
public Rectangle boundingRect() {
return boundingRect;
}
public boolean collideWithPlayer(PlayerSprite other) {
boolean newIsColliding= collideWith(other);
if(newIsColliding)
{
if(!isCollidingWithPlayer)
boolean newIsColliding = collideWith(other);
if (newIsColliding) {
if (!isCollidingWithPlayer)
onPlayerCollide();
isCollidingWithPlayer=true;
}
else
{
isCollidingWithPlayer=false;
isCollidingWithPlayer = true;
} else {
isCollidingWithPlayer = false;
}
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;
}
@@ -232,7 +215,6 @@ public class MapActor extends Actor {
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();
}
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.AssetManager;
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.resolvers.AbsoluteFileHandleResolver;
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.TextureData;
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.TextureRegion;
import com.badlogic.gdx.utils.Disposable;
@@ -270,6 +272,19 @@ public class Assets implements Disposable {
}
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() {
if (defaultImage == null) {
@@ -506,9 +521,7 @@ public class Assets implements Disposable {
currentMemory = calculateTextureSize(assetManager, fileName1, type1);
};
}
super.load(fileName, type, parameter);
}