Make Entity supports KeyEventHandler
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.bartlomiejpluta.base.api.input;
|
package com.bartlomiejpluta.base.api.input;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
public interface KeyEventHandler {
|
public interface KeyEventHandler {
|
||||||
void handleKeyEvent(KeyEvent event);
|
void handleKeyEvent(KeyEvent event);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ import com.bartlomiejpluta.base.api.entity.Entity;
|
|||||||
import com.bartlomiejpluta.base.api.gui.GUI;
|
import com.bartlomiejpluta.base.api.gui.GUI;
|
||||||
import com.bartlomiejpluta.base.api.image.Image;
|
import com.bartlomiejpluta.base.api.image.Image;
|
||||||
import com.bartlomiejpluta.base.api.input.Input;
|
import com.bartlomiejpluta.base.api.input.Input;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyEventHandler;
|
||||||
import com.bartlomiejpluta.base.api.map.handler.MapHandler;
|
import com.bartlomiejpluta.base.api.map.handler.MapHandler;
|
||||||
import com.bartlomiejpluta.base.api.runner.GameRunner;
|
import com.bartlomiejpluta.base.api.runner.GameRunner;
|
||||||
import com.bartlomiejpluta.base.api.screen.Screen;
|
import com.bartlomiejpluta.base.api.screen.Screen;
|
||||||
@@ -39,7 +41,7 @@ import java.util.List;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Builder
|
@Builder
|
||||||
public class DefaultContext implements Context {
|
public class DefaultContext implements Context, KeyEventHandler {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final GameEngine engine;
|
private final GameEngine engine;
|
||||||
@@ -103,6 +105,8 @@ public class DefaultContext implements Context {
|
|||||||
this.input = input;
|
this.input = input;
|
||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
|
|
||||||
|
input.addKeyEventHandler(this);
|
||||||
|
|
||||||
gameRunner.init(this);
|
gameRunner.init(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,6 +236,19 @@ public class DefaultContext implements Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleKeyEvent(KeyEvent event) {
|
||||||
|
if (map == null || event.isConsumed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var layer : map.getLayers()) {
|
||||||
|
if (layer instanceof KeyEventHandler) {
|
||||||
|
((KeyEventHandler) layer).handleKeyEvent(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float dt) {
|
public void update(float dt) {
|
||||||
gameRunner.update(dt);
|
gameRunner.update(dt);
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import com.bartlomiejpluta.base.api.camera.Camera;
|
|||||||
import com.bartlomiejpluta.base.api.entity.Entity;
|
import com.bartlomiejpluta.base.api.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.entity.EntityStepInListener;
|
import com.bartlomiejpluta.base.api.entity.EntityStepInListener;
|
||||||
import com.bartlomiejpluta.base.api.entity.EntityStepOutListener;
|
import com.bartlomiejpluta.base.api.entity.EntityStepOutListener;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyEvent;
|
||||||
|
import com.bartlomiejpluta.base.api.input.KeyEventHandler;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
import com.bartlomiejpluta.base.api.map.layer.object.ObjectLayer;
|
||||||
import com.bartlomiejpluta.base.api.map.layer.object.PassageAbility;
|
import com.bartlomiejpluta.base.api.map.layer.object.PassageAbility;
|
||||||
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
import com.bartlomiejpluta.base.api.map.model.GameMap;
|
||||||
@@ -24,13 +26,14 @@ import java.util.Queue;
|
|||||||
import static java.lang.Float.compare;
|
import static java.lang.Float.compare;
|
||||||
import static java.lang.Integer.compare;
|
import static java.lang.Integer.compare;
|
||||||
|
|
||||||
public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
public class DefaultObjectLayer extends BaseLayer implements ObjectLayer, KeyEventHandler {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final ArrayList<Entity> entities = new ArrayList<>();
|
private final ArrayList<Entity> entities = new ArrayList<>();
|
||||||
|
|
||||||
private final List<EntityStepInListener> stepInListeners = new ArrayList<>();
|
private final List<EntityStepInListener> stepInListeners = new ArrayList<>();
|
||||||
private final List<EntityStepOutListener> stepOutListeners = new ArrayList<>();
|
private final List<EntityStepOutListener> stepOutListeners = new ArrayList<>();
|
||||||
|
private final List<KeyEventHandler> keyEventHandlers = new ArrayList<>();
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final PassageAbility[][] passageMap;
|
private final PassageAbility[][] passageMap;
|
||||||
@@ -63,6 +66,10 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
stepOutListeners.add((EntityStepOutListener) entity);
|
stepOutListeners.add((EntityStepOutListener) entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (entity instanceof KeyEventHandler) {
|
||||||
|
keyEventHandlers.add((KeyEventHandler) entity);
|
||||||
|
}
|
||||||
|
|
||||||
entity.setStepSize(stepSize.x(), stepSize.y());
|
entity.setStepSize(stepSize.x(), stepSize.y());
|
||||||
entities.add(entity);
|
entities.add(entity);
|
||||||
|
|
||||||
@@ -81,6 +88,10 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
stepOutListeners.remove(entity);
|
stepOutListeners.remove(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (entity instanceof KeyEventHandler) {
|
||||||
|
keyEventHandlers.remove(entity);
|
||||||
|
}
|
||||||
|
|
||||||
entity.onRemove(this);
|
entity.onRemove(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,6 +187,17 @@ public class DefaultObjectLayer extends BaseLayer implements ObjectLayer {
|
|||||||
return z == 0 ? compare(a.getPosition().y(), b.getPosition().y()) : z;
|
return z == 0 ? compare(a.getPosition().y(), b.getPosition().y()) : z;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleKeyEvent(KeyEvent event) {
|
||||||
|
for (var handler : keyEventHandlers) {
|
||||||
|
if (event.isConsumed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
handler.handleKeyEvent(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void notifyEntityStepIn(Movement movement, Entity entity) {
|
public void notifyEntityStepIn(Movement movement, Entity entity) {
|
||||||
for (var listener : stepInListeners) {
|
for (var listener : stepInListeners) {
|
||||||
|
|||||||
Reference in New Issue
Block a user