Simplify movement triggering code | create helper basic control utility method
This commit is contained in:
@@ -2,16 +2,16 @@ package com.bartlomiejpluta.base.api.character;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.api.animation.Animated;
|
import com.bartlomiejpluta.base.api.animation.Animated;
|
||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.event.Event;
|
|
||||||
import com.bartlomiejpluta.base.api.event.EventType;
|
|
||||||
import com.bartlomiejpluta.base.api.move.Direction;
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
import com.bartlomiejpluta.base.api.move.Movable;
|
import com.bartlomiejpluta.base.api.move.Movable;
|
||||||
|
import com.bartlomiejpluta.base.api.move.Movement;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
public interface Character extends Movable, Animated, Entity {
|
public interface Character extends Movable, Animated, Entity {
|
||||||
|
|
||||||
|
Movement move(Direction direction);
|
||||||
|
|
||||||
Direction getFaceDirection();
|
Direction getFaceDirection();
|
||||||
|
|
||||||
void setFaceDirection(Direction direction);
|
void setFaceDirection(Direction direction);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class RandomMovementAI<N extends NPC> implements AI {
|
|||||||
if (!npc.isMoving()) {
|
if (!npc.isMoving()) {
|
||||||
if (accumulator > threshold) {
|
if (accumulator > threshold) {
|
||||||
Direction direction = Direction.values()[random.nextInt(4)];
|
Direction direction = Direction.values()[random.nextInt(4)];
|
||||||
layer.pushMovement(npc.prepareMovement(direction));
|
npc.move(direction);
|
||||||
accumulator = 0.0f;
|
accumulator = 0.0f;
|
||||||
threshold = random.nextFloat() * intervalSeconds;
|
threshold = random.nextFloat() * intervalSeconds;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,11 @@ public abstract class CharacterDelegate implements Character {
|
|||||||
return character.getMovement();
|
return character.getMovement();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Movement move(Direction direction) {
|
||||||
|
return character.move(direction);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Direction getFaceDirection() {
|
public Direction getFaceDirection() {
|
||||||
return character.getFaceDirection();
|
return character.getFaceDirection();
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.bartlomiejpluta.base.util.input;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.character.Character;
|
||||||
|
import com.bartlomiejpluta.base.api.input.Input;
|
||||||
|
import com.bartlomiejpluta.base.api.input.Key;
|
||||||
|
import com.bartlomiejpluta.base.api.move.Direction;
|
||||||
|
|
||||||
|
public class InputUtil {
|
||||||
|
public static void handleBasicControl(Character character, Input input) {
|
||||||
|
if (character.isMoving() || character.getLayer() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.isKeyPressed(Key.KEY_LEFT_CONTROL)) {
|
||||||
|
if (input.isKeyPressed(Key.KEY_DOWN)) {
|
||||||
|
character.setFaceDirection(Direction.DOWN);
|
||||||
|
} else if (input.isKeyPressed(Key.KEY_UP)) {
|
||||||
|
character.setFaceDirection(Direction.UP);
|
||||||
|
} else if (input.isKeyPressed(Key.KEY_LEFT)) {
|
||||||
|
character.setFaceDirection(Direction.LEFT);
|
||||||
|
} else if (input.isKeyPressed(Key.KEY_RIGHT)) {
|
||||||
|
character.setFaceDirection(Direction.RIGHT);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (input.isKeyPressed(Key.KEY_DOWN)) {
|
||||||
|
character.move(Direction.DOWN);
|
||||||
|
} else if (input.isKeyPressed(Key.KEY_UP)) {
|
||||||
|
character.move(Direction.UP);
|
||||||
|
} else if (input.isKeyPressed(Key.KEY_LEFT)) {
|
||||||
|
character.move(Direction.LEFT);
|
||||||
|
} else if (input.isKeyPressed(Key.KEY_RIGHT)) {
|
||||||
|
character.move(Direction.RIGHT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -144,6 +144,13 @@ public class DefaultCharacter extends MovableSprite implements Character {
|
|||||||
return animation.getFuture();
|
return animation.getFuture();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Movement move(Direction direction) {
|
||||||
|
var movement = prepareMovement(direction);
|
||||||
|
layer.pushMovement(movement);
|
||||||
|
return movement;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean move(Movement movement) {
|
public boolean move(Movement movement) {
|
||||||
if (super.move(movement)) {
|
if (super.move(movement)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user