Separated animations in GameAnimations and added static

This commit is contained in:
2026-04-05 21:20:33 -03:00
parent fee4237e91
commit 44a432d93e
7 changed files with 127 additions and 36 deletions

View File

@@ -7,6 +7,7 @@ import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import io.github.eldek0.animation.GameAnimations;
import io.github.eldek0.asset.GameAssetManager;
import io.github.eldek0.screen.GameScene;
@@ -16,6 +17,7 @@ public class App extends Game {
public static final int SCREEN_WIDTH = 1024;
public static final int SCREEN_HEIGHT = 768;
public static final boolean DEBUG = false;
public static boolean LOADED = false;
private GameScene gameScene;
public static OrthographicCamera camera;
@@ -38,6 +40,9 @@ public class App extends Game {
assets.loadMenuAssets();
assets.finishLoading();
System.out.println("Assets Loaded");
GameAnimations.load();
System.out.println("Animations Loaded");
App.LOADED = true;
this.setScreen(new GameScene(this));
}
@@ -45,7 +50,6 @@ public class App extends Game {
public void render() {
super.render();
Gdx.graphics.setTitle(Gdx.graphics.getFramesPerSecond() + " FPS");
}
@Override

View File

@@ -0,0 +1,48 @@
package io.github.eldek0.animation;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import io.github.eldek0.App;
public class FrameAnimation {
private final Animation<Texture> animation;
private float stateTime = 0f;
public FrameAnimation(float frameDuration, Texture[] frames) {
this.animation = new Animation<>(frameDuration, frames);
}
public void update(float delta) {
stateTime += delta;
}
public void reset() {
stateTime = 0f;
}
public void setPlayMode(Animation.PlayMode mode) {
animation.setPlayMode(mode);
}
public boolean isFinished() {
return animation.isAnimationFinished(stateTime);
}
public void draw(SpriteBatch batch, float alpha) {
Color color = batch.getColor();
float oldAlpha = color.a;
Texture frame = animation.getKeyFrame(stateTime);
batch.setColor(color.r, color.g, color.b, alpha);
batch.draw(frame, 0, 0, App.SCREEN_WIDTH, App.SCREEN_HEIGHT);
batch.setColor(color.r, color.g, color.b, oldAlpha);
}
public void draw(SpriteBatch batch) {
this.draw(batch, 1f);
}
}

View File

@@ -0,0 +1,24 @@
package io.github.eldek0.animation;
import com.badlogic.gdx.graphics.g2d.Animation;
import io.github.eldek0.App;
public class GameAnimations {
public static float FRAME_DURATION = 0.03f;
public static FrameAnimation monitorAnimation;
public static FrameAnimation maskInAnimation;
public static FrameAnimation bipAnimation;
public static FrameAnimation staticAnimation;
public static void load() {
monitorAnimation = new FrameAnimation(FRAME_DURATION, App.assets.monitor.sprites);
maskInAnimation = new FrameAnimation(FRAME_DURATION, App.assets.mask.sprites);
bipAnimation = new FrameAnimation(FRAME_DURATION, App.assets.cameras.staticB);
staticAnimation = new FrameAnimation(FRAME_DURATION, App.assets.cameras.staticA);
staticAnimation.setPlayMode(Animation.PlayMode.LOOP);
}
}

View File

@@ -4,12 +4,14 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Json;
import io.github.eldek0.App;
import io.github.eldek0.animation.GameAnimations;
import io.github.eldek0.config.RangeLong;
import io.github.eldek0.config.SpriteConfig;
import io.github.eldek0.config.animatronic.AnimState;
@@ -120,6 +122,13 @@ public class Camera {
}
onTouchDown();
if (!GameAnimations.bipAnimation.isFinished()) {
GameAnimations.bipAnimation.update(delta);
}
GameAnimations.staticAnimation.update(delta);
}
public void renderBackground(SpriteBatch batch) {
@@ -141,6 +150,8 @@ public class Camera {
if (rect != null && rect.contains(position)) {
inCameraRoom = config.id;
lightOn = false;
GameAnimations.bipAnimation.reset();
GameAnimations.bipAnimation.setPlayMode(Animation.PlayMode.NORMAL);
return;
}
}
@@ -263,6 +274,8 @@ public class Camera {
public void renderUI(SpriteBatch batch) {
if (!gameScene.hud.isInsideCamera()) return;
GameAnimations.staticAnimation.draw(batch, 0.5f);
for (SpriteConfig config : layout.getSortedSprites()) {
if (!config.active()) continue;
@@ -288,6 +301,10 @@ public class Camera {
}
drawCameraButtons(batch);
if (!GameAnimations.bipAnimation.isFinished()) {
GameAnimations.bipAnimation.draw(batch);
}
}
public void renderHitboxes(ShapeRenderer shapeRenderer) {

View File

@@ -8,36 +8,32 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import io.github.eldek0.App;
import io.github.eldek0.animation.FrameAnimation;
public class Button {
public static float FRAME_DURATION = 0.03f;
private boolean inside = false;
private boolean beingPressed = false;
private boolean quitting = false;
private boolean entering = false;
private boolean enable = true;
private final Texture buttonTexture;
private final Rectangle bounds;
private final float x, y;
private final Animation<Texture> animation;
private float stateTime;
private final FrameAnimation transition;
private boolean enable = true;
public Button(Texture sprite, float x, float y, Rectangle bounds, Texture[] transitionFrames) {
public Button(Texture sprite, float x, float y, Rectangle bounds, FrameAnimation transition) {
this.buttonTexture = sprite;
this.x = x;
this.y = y;
this.bounds = bounds;
animation = new Animation<>(FRAME_DURATION, transitionFrames);
this.transition = transition;
}
public void update(float delta) {
if (!enable){return;}
if (!enable) return;
Vector2 position = App.convertPosToWorldPos(new Vector2(Gdx.input.getX(), Gdx.input.getY()));
if (bounds.contains(position)) {
@@ -49,57 +45,56 @@ public class Button {
beingPressed = false;
}
if (isInTransition()){
stateTime += delta;
if (isInTransition()) {
transition.update(delta);
}
if (animation.isAnimationFinished(stateTime)) {
if (transition.isFinished()) {
entering = false;
quitting = false;
}
}
public void render(SpriteBatch batch) {
if (!enable){return;}
if (!enable) return;
batch.draw(buttonTexture, x, y, buttonTexture.getWidth(), buttonTexture.getHeight());
}
public void renderHitbox(ShapeRenderer shapeRenderer) {
if (!enable){return;}
if (!enable) return;
shapeRenderer.rect(bounds.x, bounds.y, bounds.width, bounds.height);
}
public void renderAnimation(SpriteBatch batch) {
if (isInTransition()){
Texture currentFrame = animation.getKeyFrame(stateTime);
batch.draw(currentFrame, 0, 0, App.SCREEN_WIDTH, App.SCREEN_HEIGHT);
if (isInTransition()) {
transition.draw(batch);
}
}
private void handleButton() {
if (isInTransition()) return;
if (!inside) {
entering = true;
quitting = false;
inside = true;
animation.setPlayMode(Animation.PlayMode.NORMAL);
transition.setPlayMode(Animation.PlayMode.NORMAL);
} else {
quitting = true;
entering = false;
inside = false;
animation.setPlayMode(Animation.PlayMode.REVERSED);
transition.setPlayMode(Animation.PlayMode.REVERSED);
}
stateTime = 0f;
transition.reset();
}
public boolean isEnable() {return enable;}
public void enable() {this.enable = true;}
public void disable() {this.enable = false;}
public boolean isInTransition(){return entering || quitting;}
public boolean isInside() { return inside; }
public boolean isBeingPressed() { return beingPressed; }
public boolean isQuitting() { return quitting; }
public boolean isEntering() { return entering; }
public boolean isEnable() { return enable; }
public void enable() { this.enable = true; }
public void disable() { this.enable = false; }
public boolean isInTransition() { return entering || quitting; }
public boolean isInside() { return inside; }
public boolean isBeingPressed() { return beingPressed; }
public boolean isQuitting() { return quitting; }
public boolean isEntering() { return entering; }
}

View File

@@ -3,12 +3,14 @@ package io.github.eldek0.ui;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import io.github.eldek0.App;
import io.github.eldek0.asset.GameAssetManager;
import io.github.eldek0.animation.GameAnimations;
public class CameraButton extends Button {
public CameraButton(float x, float y) {
super(getButtonTexture(), x, y, new Rectangle(x + 10, 0, 480, getButtonTexture().getHeight() + y),
App.assets.monitor.sprites);
super(getButtonTexture(), x, y,
new Rectangle(x + 10, 0, 480, getButtonTexture().getHeight() + y),
GameAnimations.monitorAnimation
);
}
private static Texture getButtonTexture() {

View File

@@ -3,12 +3,13 @@ package io.github.eldek0.ui;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import io.github.eldek0.App;
import io.github.eldek0.animation.GameAnimations;
import io.github.eldek0.asset.GameAssetManager;
public class MaskButton extends Button {
public MaskButton(float x, float y) {
super(getButtonTexture(), x, y, new Rectangle(x + 10, 0, 480, getButtonTexture().getHeight() + y),
App.assets.mask.sprites);
GameAnimations.maskInAnimation);
}
private static Texture getButtonTexture() {