Skip to content

Commit

Permalink
chore: associate action emitter with action
Browse files Browse the repository at this point in the history
This required some refactoring and allowed me to improve object creation
code & simplify battle action creation
  • Loading branch information
kkafar committed Jun 4, 2022
1 parent 32d5e4b commit f9f48d9
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 54 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/rpg/model/actions/Action.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.rpg.model.actions;

import io.rpg.model.object.GameObject;
import org.jetbrains.annotations.Nullable;

/**
* A marker interface for action classes.
*/
Expand All @@ -9,7 +12,22 @@ public interface Action {
public void acceptActionEngine(ActionEngine engine) {
/* noop */
}

@Override
public void setEmitter(GameObject emitter) {
/* noop */
}

@Override
public @Nullable GameObject getEmitter() {
return null;
}
};

void acceptActionEngine(final ActionEngine engine);

void setEmitter(GameObject emitter);

@Nullable
GameObject getEmitter();
}
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/model/actions/ActionEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void onAction(BattleAction action) {
});
}

private void actionGuard(ConditionalAction action, Runnable actionLogic) {
private void actionGuard(BaseAction action, Runnable actionLogic) {
if (action.getCondition() != null && !action.getCondition().acceptEngine(controller().getConditionEngine())) {
logger.info("Action not executed due to condition being not satisfied");
return;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/rpg/model/actions/BaseAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.rpg.model.actions;

import io.rpg.model.actions.condition.Condition;
import io.rpg.model.object.GameObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class BaseAction implements Action {
@Nullable
private final Condition condition;

@Nullable
private GameObject emitter;

public BaseAction(@Nullable Condition condition) {
this.condition = condition;
}

@Nullable
public Condition getCondition() {
return condition;
}

public void setEmitter(GameObject emitter) {
this.emitter = emitter;
}

@Nullable
public GameObject getEmitter() {
return emitter;
}
}
9 changes: 2 additions & 7 deletions src/main/java/io/rpg/model/actions/BattleAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
import io.rpg.model.actions.condition.Condition;
import io.rpg.model.object.GameObject;

public final class BattleAction extends ConditionalAction {
private GameObject opponent;
public final class BattleAction extends BaseAction {
private final int reward;

public BattleAction(int reward, Condition condition) {
super(condition);
this.reward = reward;
}

public void setOpponent(GameObject object) {
this.opponent = object;
}

public GameObject getOpponent() {
return opponent;
return getEmitter();
}

public int getReward() {
Expand Down
18 changes: 0 additions & 18 deletions src/main/java/io/rpg/model/actions/ConditionalAction.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/io/rpg/model/actions/DialogueAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Class for storing local data needed to perform a dialogue action.
*/
public class DialogueAction extends ConditionalAction {
public class DialogueAction extends BaseAction {
public final String text;
public final Image image;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/model/actions/GameEndAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.rpg.model.actions.condition.Condition;

public class GameEndAction extends ConditionalAction {
public class GameEndAction extends BaseAction {
public final String description;

public GameEndAction(String description, Condition condition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Class for storing local data needed to preform a location change action.
*/

public class LocationChangeAction extends ConditionalAction {
public class LocationChangeAction extends BaseAction {
public final String destinationLocationTag;
public final Point2D playerPosition;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/rpg/model/actions/QuizAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Class for storing local data needed to preform a quiz action.
*/

public class QuizAction extends ConditionalAction {
public class QuizAction extends BaseAction {

public final Question question;
private int pointsToEarn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Class for storing local data needed to perform a show description action.
*/
public class ShowDescriptionAction extends ConditionalAction {
public class ShowDescriptionAction extends BaseAction {
public final String description;
public final Image image;

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/rpg/model/object/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ public void setPosition(Position playerPosition) {

public void setOnRightClickAction(@NotNull Action onRightClickAction) {
this.onRightClickAction = onRightClickAction;
this.onRightClickAction.setEmitter(this);
}

public void setOnLeftClickAction(@NotNull Action onLeftClickAction) {
this.onLeftClickAction = onLeftClickAction;
this.onLeftClickAction.setEmitter(this);
}

public void onRightClick() {
Expand All @@ -144,5 +146,6 @@ public void onApproach() {

public void setOnApproach(Action onApproach) {
this.onApproach = onApproach;
this.onApproach.setEmitter(this);
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/rpg/util/ActionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public class ActionFactory {
* @return
*/
public static Action fromConfig(ActionConfigBundle config) {
if (config == null) {
return Action.VOID;
}

assert config.getActionType() != null : "Null action type! Make sure to call validation " +
"method after the config object is inflated from JSON!";

Expand Down
35 changes: 12 additions & 23 deletions src/main/java/io/rpg/util/GameObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class GameObjectFactory {
*/
public static GameObject fromConfig(GameObjectConfig config) {

Action onLeftClickAction = config.getOnLeftClick() != null ? ActionFactory.fromConfig(config.getOnLeftClick()) : Action.VOID;
Action onRightClickAction = config.getOnRightClick() != null ? ActionFactory.fromConfig(config.getOnRightClick()) : Action.VOID;
Action onApproach = config.getOnApproach() != null ? ActionFactory.fromConfig(config.getOnApproach()) : Action.VOID;
Action onLeftClickAction = ActionFactory.fromConfig(config.getOnLeftClick());
Action onRightClickAction = ActionFactory.fromConfig(config.getOnRightClick());
Action onApproach = ActionFactory.fromConfig(config.getOnApproach());

if (onRightClickAction.equals(Action.VOID)) {
onRightClickAction = new ShowDescriptionAction(
Expand All @@ -39,34 +39,17 @@ public static GameObject fromConfig(GameObjectConfig config) {
}

// Not implemented in model for now, however they should be
Action onClickAction = config.getOnClick() != null ? ActionFactory.fromConfig(config.getOnClick()) : Action.VOID;

Action onClickAction = ActionFactory.fromConfig(config.getOnClick());


if (config instanceof PlayerConfig) {
Player player = new Player(config.getTag(), config.getPosition(), config.getAssetPath());
player.setOnLeftClickAction(onLeftClickAction);
player.setOnRightClickAction(onRightClickAction);
player.setOnApproach(onApproach);
setActionsOnObject(player, onLeftClickAction, onRightClickAction, onApproach);
// player.setStrength(config.getStrength());
return player;
} else {
GameObject gameObject = new GameObject(config.getTag(), config.getPosition());

gameObject.setOnLeftClickAction(onLeftClickAction);
gameObject.setOnRightClickAction(onRightClickAction);
gameObject.setOnApproach(onApproach);

if (onLeftClickAction instanceof BattleAction) {
((BattleAction) onLeftClickAction).setOpponent(gameObject);
}
if (onRightClickAction instanceof BattleAction) {
((BattleAction) onRightClickAction).setOpponent(gameObject);
}
if (onApproach instanceof BattleAction) {
((BattleAction) onApproach).setOpponent(gameObject);
}

setActionsOnObject(gameObject, onLeftClickAction, onRightClickAction, onApproach);
// TODO: Create ActionFactory & inflate the actions
return gameObject;
}
Expand All @@ -88,4 +71,10 @@ public static LinkedList<GameObject> fromConfigs(Iterable<GameObjectConfig> conf
}
return gameObjects;
}

private static void setActionsOnObject(GameObject object, Action onLeftClick, Action onRightClick, Action onApproach) {
object.setOnLeftClickAction(onLeftClick);
object.setOnRightClickAction(onRightClick);
object.setOnApproach(onApproach);
}
}

0 comments on commit f9f48d9

Please sign in to comment.