Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add level-required action condition #75

Merged
merged 11 commits into from
Jun 4, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{ "tag": "mul-1", "position": { "row": 7, "col": 9 }, "assetPath": "assets/bush.png" },
{ "tag": "mul-1", "position": { "row": 8, "col": 9 }, "assetPath": "assets/bush.png" },
{ "tag": "mul-1", "position": { "row": 9, "col": 9 }, "assetPath": "assets/bush.png" },
{ "tag": "object-1", "position": { "row": 6, "col": 5 }, "assetPath": "assets/bunny.png"},
{ "tag": "knight-game-end", "position": { "row": 6, "col": 5 }},
{ "tag": "object-2", "position": { "row": 5, "col": 0 }, "assetPath": "assets/zombie.png" },
{ "tag": "object-3", "position": { "row": 1, "col": 1 }, "assetPath": "assets/zombie.png" }
],
Expand All @@ -53,4 +53,4 @@
},
"width": 10,
"height": 10
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"tag": "knight-game-end",
"assetPath": "assets/knight.png",
"onLeftClick": {
"tag": "actiontag",
"type": "game-end",
"description": "You won, congratulations!",
"condition": {
"type": "level-required",
"level": 2
}
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ Condition consists of following properties:
* `opponent-tag` - Tag of the opponent that must be defeated to unlock the action. Note that as for now, the engine
does not check whether specified object exists or has battle action set. For now you must guarantee
the correctness. Aliases: `tag`, `opponentTag`.
* `level-required` - Requires following props:
* `level` - Required player level. Player's level must be >= given value for the condition to be satisfied.

## Example of full configuration structure

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/io/rpg/config/model/ConditionConfigBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@ public class ConditionConfigBundle implements ConfigWithValidation {
@Nullable
private ConditionType type;

/**
* {@link io.rpg.model.actions.condition.ItemRequiredCondition} <br>
* {@link io.rpg.model.actions.condition.DefeatOpponentCondition}
*/
@Nullable
@SerializedName(value = "item-tag", alternate = {"itemTag", "opponent-tag", "tag", "opponentTag"})
private String objectTag;

@Nullable
@SerializedName(value = "level", alternate = {"required-level", "requiredLevel"})
private Integer requiredLevel;

@Nullable
public ConditionType getType() {
return type;
Expand All @@ -25,6 +33,11 @@ public String getObjectTag() {
return objectTag;
}

@Nullable
public Integer getRequiredLevel() {
return requiredLevel;
}

Result<Void, Exception> validateItemRequired() {
ErrorMessageBuilder builder = new ErrorMessageBuilder();

Expand All @@ -40,6 +53,18 @@ Result<Void, Exception> validateDefeatOpponent() {
return validateItemRequired();
}

Result<Void, Exception> validateLevelRequired() {
ErrorMessageBuilder builder = new ErrorMessageBuilder();

if (requiredLevel == null) {
builder.append("No level provided");
} else if (requiredLevel <= 0) {
builder.append("Level must be > 0");
}

return builder.isEmpty() ? Result.ok() : Result.err(new Exception(builder.toString()));
}

@Override
public Result<Void, Exception> validate() {
ErrorMessageBuilder builder = new ErrorMessageBuilder();
Expand All @@ -55,6 +80,7 @@ public Result<Void, Exception> validate() {
switch (type) {
case ITEM_REQUIRED -> { return validateItemRequired(); }
case DEFEAT_OPPONENT -> { return validateDefeatOpponent(); }
case LEVEL_REQUIRED -> { return validateLevelRequired(); }
default -> throw new IllegalArgumentException("Not implemented condition type!");
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/rpg/model/actions/ConditionType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public enum ConditionType {

@SerializedName("defeat-opponent")
DEFEAT_OPPONENT,

@SerializedName("level-required")
LEVEL_REQUIRED,
}
17 changes: 14 additions & 3 deletions src/main/java/io/rpg/model/actions/condition/ConditionEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

import java.lang.ref.WeakReference;

public class ConditionEngine {
public final class ConditionEngine {
private final WeakReference<Controller> weakRefController;

public ConditionEngine(final Controller controller) {
this.weakRefController = new WeakReference<>(controller);
}

public boolean evaluateItemRequiredCondition(ItemRequiredCondition condition) {
public boolean evaluate(ItemRequiredCondition condition) {
// TODO: Implement this when the inventory gets implemented.
// Scheme:
// return weakRefControler
Expand All @@ -24,12 +24,23 @@ public boolean evaluateItemRequiredCondition(ItemRequiredCondition condition) {
return true;
}

public boolean evaluateDefeatOpponentCondition(DefeatOpponentCondition condition) {
public boolean evaluate(DefeatOpponentCondition condition) {
return weakRefController
.get()
.getPlayerController()
.getPlayer()
.getDefeatedOpponents()
.contains(condition.getOpponentTag());
}

public boolean evaluate(LevelRequiredCondition condition) {
// TODO: Implement this when the level system is implemented.
// For now I just return true.
// return weakRefController
// .get()
// .getPlayerController()
// .getPlayer()
// .getLevel() >= condition.getLevel();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.rpg.config.model.ConditionConfigBundle;
import org.jetbrains.annotations.Nullable;

public class ConditionFactory {
public final class ConditionFactory {
@Nullable
public static Condition fromConfig(ConditionConfigBundle config) {
if (config == null) {
Expand All @@ -15,6 +15,7 @@ public static Condition fromConfig(ConditionConfigBundle config) {
switch (config.getType()) {
case ITEM_REQUIRED -> { return itemRequiredFromConfig(config); }
case DEFEAT_OPPONENT -> { return defeatOpponentFromConfig(config); }
case LEVEL_REQUIRED -> { return levelRequiredFromConfig(config); }
default -> throw new IllegalArgumentException("Not implemented condition type: " + config.getType().toString());
}
}
Expand All @@ -28,4 +29,9 @@ private static DefeatOpponentCondition defeatOpponentFromConfig(ConditionConfigB
assert config.getObjectTag() != null;
return new DefeatOpponentCondition(config.getObjectTag());
}

private static LevelRequiredCondition levelRequiredFromConfig(ConditionConfigBundle config) {
assert config.getRequiredLevel() != null;
return new LevelRequiredCondition(config.getRequiredLevel());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.rpg.model.actions.ConditionType;
import org.jetbrains.annotations.NotNull;

public class DefeatOpponentCondition extends Condition {
public final class DefeatOpponentCondition extends Condition {

@NotNull
private final String opponentTag;
Expand All @@ -15,7 +15,7 @@ public DefeatOpponentCondition(@NotNull final String tag) {

@Override
public boolean acceptEngine(ConditionEngine engine) {
return engine.evaluateDefeatOpponentCondition(this);
return engine.evaluate(this);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io.rpg.model.actions.ConditionType;
import org.jetbrains.annotations.NotNull;

public class ItemRequiredCondition extends Condition {
public final class ItemRequiredCondition extends Condition {

@NotNull
private final String requiredItemTag;
Expand All @@ -15,7 +15,7 @@ public ItemRequiredCondition(@NotNull final String tag) {

@Override
public boolean acceptEngine(ConditionEngine engine) {
return engine.evaluateItemRequiredCondition(this);
return engine.evaluate(this);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.rpg.model.actions.condition;

import io.rpg.model.actions.ConditionType;

public final class LevelRequiredCondition extends Condition {

private final int level;

public LevelRequiredCondition(int requiredLevel) {
super(ConditionType.LEVEL_REQUIRED);
this.level = requiredLevel;
}

public int getLevel() {
return level;
}

@Override
public boolean acceptEngine(ConditionEngine engine) {
return engine.evaluate(this);
}
}