diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-19 09:43:27 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2014-09-19 09:43:27 +0200 |
commit | abab7886b47873cec83f064625e11f5eac8d205e (patch) | |
tree | b490d5eea28fea1fba7f04293b41fa3df1326143 | |
parent | 127dd3f7518ada1598adc176585ee7151d4ba7be (diff) | |
download | RustAndDust-abab7886b47873cec83f064625e11f5eac8d205e.zip RustAndDust-abab7886b47873cec83f064625e11f5eac8d205e.tar.gz |
add engine/gfx to replate scene2d usage
10 files changed, 456 insertions, 0 deletions
diff --git a/core/src/ch/asynk/tankontank/engine/gfx/AbstractDrawable.java b/core/src/ch/asynk/tankontank/engine/gfx/AbstractDrawable.java new file mode 100644 index 0000000..5d4705c --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/AbstractDrawable.java @@ -0,0 +1,89 @@ +package ch.asynk.tankontank.engine.gfx; + +import com.badlogic.gdx.graphics.Color; + +public abstract class AbstractDrawable implements Drawable +{ + protected float w; + protected float h; + protected float x; + protected float y; + protected int r; + protected float s; + protected Color color; + + public AbstractDrawable() + { + this.w = 0f; + this.h = 0f; + this.x = 0f; + this.y = 0f; + this.r = 0; + this.s = 1f; + this.color = new Color(1, 1, 1, 1); + } + + @Override + public float getWidth() + { + return w; + } + + @Override + public float getHeight() + { + return h; + } + + @Override + public float getX() + { + return x; + } + + @Override + public float getY() + { + return y; + } + + @Override + public int getRotation() + { + return r; + } + + @Override + public void setRotation(int r) + { + this.r = r; + } + + @Override + public void setScale(float s) + { + this.s = s; + } + + @Override + public void setPosition(float x, float y) + { + this.x = x; + this.y = y; + } + + @Override + public void moveBy(float dx, float dy) + { + this.x += dx; + this.y += dy; + } + + @Override + public void setCoords(float x, float y, int r) + { + this.x = x; + this.y = y; + this.r = r; + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/Animation.java b/core/src/ch/asynk/tankontank/engine/gfx/Animation.java new file mode 100644 index 0000000..6d79046 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/Animation.java @@ -0,0 +1,10 @@ +package ch.asynk.tankontank.engine.gfx; + +public interface Animation +{ + public Node getNode(); + + public boolean act(float delta); + + public void free(); +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/Drawable.java b/core/src/ch/asynk/tankontank/engine/gfx/Drawable.java new file mode 100644 index 0000000..8b700df --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/Drawable.java @@ -0,0 +1,29 @@ +package ch.asynk.tankontank.engine.gfx; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.utils.Disposable; + +public interface Drawable extends Disposable +{ + public float getWidth(); + + public float getHeight(); + + public float getX(); + + public float getY(); + + public int getRotation(); + + public void setRotation(int r); + + public void setScale(float s); + + public void setPosition(float x, float y); + + public void moveBy(float dx, float dy); + + public void setCoords(float x, float y, int r); + + public void draw(Batch batch, float parentAlpha); +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/Node.java b/core/src/ch/asynk/tankontank/engine/gfx/Node.java new file mode 100644 index 0000000..4c94e50 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/Node.java @@ -0,0 +1,14 @@ +package ch.asynk.tankontank.engine.gfx; + +import com.badlogic.gdx.graphics.g2d.Batch; + +import ch.asynk.tankontank.engine.Layer; + +public interface Node extends Drawable +{ + public void act(float delta); + + public void clear(); + + public void setLayer(Layer layer); +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/TextureDrawable.java b/core/src/ch/asynk/tankontank/engine/gfx/TextureDrawable.java new file mode 100644 index 0000000..3803d15 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/TextureDrawable.java @@ -0,0 +1,39 @@ +package ch.asynk.tankontank.engine.gfx; + +import com.badlogic.gdx.Gdx; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; + +public class TextureDrawable extends AbstractDrawable +{ + private Texture texture; + + public TextureDrawable(Texture texture) + { + this.texture = texture; + this.w = texture.getWidth(); + this.h = texture.getHeight(); + } + + @Override + public void dispose() + { + texture.dispose(); + } + + @Override + public void draw(Batch batch, float parentAlpha) + { + batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); + + if (r != 0f) + batch.draw(texture, x, y, (w / 2f), (h / 2f), w, h, s, s, r, (int) x, (int) y, (int) w, (int) h, false, false); + else { + if (s == 1f) + batch.draw(texture, x, y); + else + batch.draw(texture, x, y, getWidth() * s, getHeight() * s); + } + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/TextureRegionDrawable.java b/core/src/ch/asynk/tankontank/engine/gfx/TextureRegionDrawable.java new file mode 100644 index 0000000..afb54d9 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/TextureRegionDrawable.java @@ -0,0 +1,29 @@ +package ch.asynk.tankontank.engine.gfx; + +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.graphics.g2d.TextureRegion; + +public class TextureRegionDrawable extends AbstractDrawable +{ + private TextureRegion region; + + public TextureRegionDrawable(TextureRegion region) + { + this.region = region; + this.w = region.getRegionWidth(); + this.h = region.getRegionHeight(); + } + + @Override + public void dispose() + { + } + + @Override + public void draw(Batch batch, float parentAlpha) + { + batch.setColor(color.r, color.g, color.b, color.a * parentAlpha); + + batch.draw(region, x, y, (w / 2f), (h / 2f), w, h, s, s, r, true); + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/AnimationSequence.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/AnimationSequence.java new file mode 100644 index 0000000..69a9c7a --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/AnimationSequence.java @@ -0,0 +1,68 @@ +package ch.asynk.tankontank.engine.gfx.animations; + +import java.util.Vector; + +import com.badlogic.gdx.utils.Pool; + +import ch.asynk.tankontank.engine.gfx.Node; +import ch.asynk.tankontank.engine.gfx.Animation; + +public class AnimationSequence implements Animation, Pool.Poolable +{ + private Vector<Animation> animations; + + private static final Pool<AnimationSequence> animationSequencePool = new Pool<AnimationSequence>() { + @Override + protected AnimationSequence newObject() { + return new AnimationSequence(); + } + }; + + public static AnimationSequence get(int capacity) + { + AnimationSequence seq = animationSequencePool.obtain(); + if (seq.animations == null) + seq.animations = new Vector<Animation>(capacity); + else + seq.animations.setSize(capacity); + + return seq; + } + + @Override + public void reset() + { + for (int i = 0, n = animations.size(); i < n; i++) + animations.get(i).free(); + animations.clear(); + } + + @Override + public void free() + { + animationSequencePool.free(this); + } + + @Override + public Node getNode() + { + return animations.get(0).getNode(); + } + + public void addAnimation(Animation animation) + { + animations.add(animation); + } + + public boolean act(float delta) + { + if(animations.isEmpty()) return true; + + Animation animation = animations.get(0); + if (animation.act(delta)) { + animations.removeElementAt(0); + } + + return (animations.isEmpty()); + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/MoveToAnimation.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/MoveToAnimation.java new file mode 100644 index 0000000..d95e33a --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/MoveToAnimation.java @@ -0,0 +1,74 @@ +package ch.asynk.tankontank.engine.gfx.animations; + +import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.utils.Pool; + +import ch.asynk.tankontank.engine.gfx.Node; + +public class MoveToAnimation extends TimedAnimation +{ + private Node node; + private float fromX; + private float fromY; + private float fromR; + private float toX; + private float toY; + private float toR; + + private static final Pool<MoveToAnimation> moveToAnimationPool = new Pool<MoveToAnimation>() { + @Override + protected MoveToAnimation newObject() { + return new MoveToAnimation(); + } + }; + + @Override + public Node getNode() + { + return node; + } + + @Override + public void free() + { + moveToAnimationPool.free(this); + } + + public static MoveToAnimation get(Node node, Vector3 v, float duration) + { + return get(node, v.x, v.y, v.z, duration); + } + + public static MoveToAnimation get(Node node, float x, float y, float r, float duration) + { + MoveToAnimation a = moveToAnimationPool.obtain(); + + a.node = node; + a.toX = x; + a.toY = y; + a.toR = r; + a.duration = duration; + + return a; + } + + protected void begin() + { + fromX = node.getX(); + fromY = node.getY(); + fromR = node.getRotation(); + } + + protected void end() + { + free(); + } + + protected void update(float percent) + { + if (percent == 1f) + node.setCoords(toX, toY, (int) toR); + else + node.setCoords(fromX + ((toX - fromX) * percent), fromY + ((toY - fromY) * percent), (int) (fromR + ((toR - fromR) * percent))); + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/RunnableAnimation.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/RunnableAnimation.java new file mode 100644 index 0000000..5a4f235 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/RunnableAnimation.java @@ -0,0 +1,57 @@ +package ch.asynk.tankontank.engine.gfx.animations; + +import com.badlogic.gdx.utils.Pool; + +import ch.asynk.tankontank.engine.gfx.Node; +import ch.asynk.tankontank.engine.gfx.Animation; + +public class RunnableAnimation implements Animation, Pool.Poolable +{ + private Runnable runnable; + private boolean ran; + + private static final Pool<RunnableAnimation> runnableAnimationPool = new Pool<RunnableAnimation>() { + @Override + protected RunnableAnimation newObject() { + return new RunnableAnimation(); + } + }; + + public static RunnableAnimation get(Runnable runnable) + { + RunnableAnimation a = runnableAnimationPool.obtain(); + a.runnable = runnable; + return a; + } + + @Override + public void reset() + { + ran = false; + } + + @Override + public void free() + { + runnableAnimationPool.free(this); + } + + @Override + public Node getNode() + { + return null; + } + + @Override + public boolean act(float delta) + { + if (ran) return true; + + runnable.run(); + runnable = null; + ran = true; + free(); + + return true; + } +} diff --git a/core/src/ch/asynk/tankontank/engine/gfx/animations/TimedAnimation.java b/core/src/ch/asynk/tankontank/engine/gfx/animations/TimedAnimation.java new file mode 100644 index 0000000..c7854d0 --- /dev/null +++ b/core/src/ch/asynk/tankontank/engine/gfx/animations/TimedAnimation.java @@ -0,0 +1,47 @@ +package ch.asynk.tankontank.engine.gfx.animations; + +import com.badlogic.gdx.utils.Pool.Poolable; + +import ch.asynk.tankontank.engine.gfx.Animation; + +abstract public class TimedAnimation implements Animation, Poolable +{ + private float time; + private boolean began; + private boolean completed; + protected float duration; + + @Override + public void reset() + { + time = 0f; + began = false; + completed = false; + } + + abstract protected void begin(); + abstract protected void end(); + abstract protected void update(float percent); + + public boolean act(float delta) + { + if (completed) return true; + + if (!began) { + begin(); + began = true; + } + + time += delta; + completed = (time >= duration); + + if (!completed) { + update(time / duration); + return false; + } + + update(1); + end(); + return true; + } +} |