diff --git a/data.mv.db b/data.mv.db index fc1d0ab..e8e9d83 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/com/bartlomiejpluta/demo/entity/MapObject.java b/src/main/java/com/bartlomiejpluta/demo/entity/MapObject.java index c82ce32..3f51bd3 100644 --- a/src/main/java/com/bartlomiejpluta/demo/entity/MapObject.java +++ b/src/main/java/com/bartlomiejpluta/demo/entity/MapObject.java @@ -1,141 +1,40 @@ package com.bartlomiejpluta.demo.entity; +import com.bartlomiejpluta.base.api.context.Context; import com.bartlomiejpluta.base.api.context.ContextHolder; -import com.bartlomiejpluta.base.api.move.Direction; -import com.bartlomiejpluta.base.util.path.CharacterPath; -import com.bartlomiejpluta.base.util.path.PathExecutor; +import com.bartlomiejpluta.demo.runner.DemoRunner; import lombok.Getter; import lombok.NonNull; import java.util.concurrent.CompletableFuture; -import java.util.function.Supplier; -import static java.util.Objects.requireNonNull; import static java.util.concurrent.CompletableFuture.completedFuture; -public abstract class MapObject extends NamedCharacter { - private static final float INTERVAL = 0.05f; - private static final float COOLDOWN = 0.5f; - protected final PathExecutor pathExecutor = new PathExecutor<>(this); - private final String interactSound; - - protected boolean interacting = false; - - private Supplier> beforeAll; - private Supplier> before; - private Supplier> after; - private Supplier> afterAll; - - private CompletableFuture future; +public abstract class MapObject extends com.bartlomiejpluta.base.util.world.MapObject { + protected final Context context; + protected final DemoRunner runner; @Getter private final String name; - + private final String interactSound; public MapObject(@NonNull String id) { - this(DB.dao.map_object.find(id)); + this(DB.dao.object.find(id)); } - public MapObject(@NonNull DB.model.MapObjectModel template) { - super(ContextHolder.INSTANCE.getContext().createCharacter(A.charsets.get(template.getCharset()).uid)); - short frame = requireNonNull(template.getFrame()); + public MapObject(@NonNull DB.model.ObjectModel template) { + super(ContextHolder.INSTANCE.getContext().createCharacter(A.charsets.get(template.getCharset()).uid), template.getFrame()); + this.context = ContextHolder.INSTANCE.getContext(); + this.runner = DemoRunner.instance(); this.name = template.getName(); this.interactSound = A.sounds.get(template.getInteractSound()).uid; - - setBlocking(true); - disableAnimation(); - - setAnimationFrame(frame); - pathExecutor.setRepeat(1); - - pathExecutor.setPath(new CharacterPath() - .run(this::startInteraction) - .turn(Direction.LEFT, frame) - .wait(INTERVAL) - .turn(Direction.RIGHT, frame) - .wait(INTERVAL) - .turn(Direction.UP, frame) - .wait(INTERVAL) - .run(this::runInteraction) - .suspend(() -> future) - .wait(INTERVAL) - .turn(Direction.RIGHT, frame) - .wait(INTERVAL) - .turn(Direction.LEFT, frame) - .wait(INTERVAL) - .turn(Direction.DOWN, frame) - .run(this::finishInteraction) - .suspend(() -> future) - .wait(COOLDOWN) - .run(this::completeInteraction) - ); - } - - public MapObject beforeAll(@NonNull Supplier> action) { - this.beforeAll = action; - return this; - } - - public MapObject before(@NonNull Supplier> action) { - this.before = action; - return this; - } - - public MapObject after(@NonNull Supplier> action) { - this.after = action; - return this; - } - - public MapObject afterAll(@NonNull Supplier> action) { - this.afterAll = action; - return this; - } - - public void triggerInteraction() { - if (interacting) { - return; - } - - pathExecutor.reset(); - - if (beforeAll != null) { - beforeAll.get().thenRun(() -> interacting = true); - } else { - interacting = true; - } - } - - private void runInteraction() { - this.future = (before != null ? before.get() : completedFuture(null)) - .thenCompose(v -> interact()) - .thenCompose(v -> (after != null ? after.get() : completedFuture(null))); - } - - private void startInteraction() { - if (interactSound != null) { - context.playSound(interactSound); - } - } - - private void finishInteraction() { - this.future = afterAll != null ? afterAll.get() : completedFuture(null); - } - - private void completeInteraction() { - interacting = false; } @Override - public void update(float dt) { - if (interacting) { - pathExecutor.execute(getLayer(), dt); + protected @NonNull CompletableFuture onInteractionBegin() { + if (interactSound != null) { + context.playSound(interactSound); } - } - protected final void reset() { - setFaceDirection(Direction.DOWN); - pathExecutor.reset(); - interacting = false; + return completedFuture(null); } - - protected abstract CompletableFuture interact(); } \ No newline at end of file