Skip to content

Commit

Permalink
Merge pull request #460 from tonihele/feature/misc-improvements
Browse files Browse the repository at this point in the history
Feature/misc improvements
  • Loading branch information
tonihele authored Jul 9, 2024
2 parents b6eea33 + f50c52c commit 4751e51
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ dependencies {
implementation fileTree(dir: 'lib', include: ['*.jar'])
implementation "org.jmonkeyengine:jme3-core:$jmonkeyengine_version"
implementation "org.jmonkeyengine:jme3-desktop:$jmonkeyengine_version"
implementation "org.jmonkeyengine:jme3-awt-dialogs:$jmonkeyengine_version"
implementation "org.jmonkeyengine:jme3-plugins:$jmonkeyengine_version"
implementation "org.jmonkeyengine:jme3-effects:$jmonkeyengine_version"
implementation "org.jmonkeyengine:jme3-networking:$jmonkeyengine_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import toniarts.openkeeper.tools.convert.map.KwdFile;
import toniarts.openkeeper.tools.convert.map.Thing;
import toniarts.openkeeper.tools.convert.map.Variable;
import toniarts.openkeeper.utils.Utils;
import toniarts.openkeeper.utils.WorldUtils;
import toniarts.openkeeper.view.map.MapViewController;

Expand Down Expand Up @@ -244,7 +245,12 @@ public EntityId addRoomGold(short ownerId, int x, int y, int money, int maxMoney

@Override
public EntityId addLooseGold(short ownerId, int x, int y, int money, int maxMoney) {
return loadObject(OBJECT_GOLD_ID, ownerId, x, y, 0, money, null, null, null, maxMoney);
Vector3f pos = WorldUtils.pointToVector3f(x, y);

// Add a slight offset to make things look nicer, stuff not clumping together
pos.x = pos.x + Utils.getRandom().nextFloat(MapViewController.TILE_WIDTH) - (MapViewController.TILE_WIDTH / 2f);
pos.z = pos.z + Utils.getRandom().nextFloat(MapViewController.TILE_WIDTH) - (MapViewController.TILE_WIDTH / 2f);
return loadObject(OBJECT_GOLD_ID, ownerId, pos, 0, money, null, null, null, maxMoney);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,20 @@ public void resetReEvaluationTimer() {
@Override
public void addGold(int amount) {
Gold gold = entityData.getComponent(entityId, Gold.class);
entityData.setComponent(entityId, new Gold(gold.gold + amount, gold.maxGold));

// Drop excess gold, we can only carry so much
int maxGoldCanAdd = gold.maxGold - gold.gold;
int goldToAdd = Math.min(amount, maxGoldCanAdd);
int looseGold = amount - goldToAdd;

if (goldToAdd > 0) {
entityData.setComponent(entityId, new Gold(gold.gold + goldToAdd, gold.maxGold));
}

if (looseGold > 0) {
Point coordinates = getCreatureCoordinates();
objectsController.addLooseGold(getOwnerId(), coordinates.x, coordinates.y, looseGold, (int) gameSettings.get(Variable.MiscVariable.MiscType.MAX_GOLD_PILE_OUTSIDE_TREASURY).getValue());
}
}

@Override
Expand Down Expand Up @@ -1122,29 +1135,38 @@ public void imprison(short playerId) {
@Override
public boolean isStateTimeExceeded() {
double timeSpent = gameTimer.getGameTime() - entityData.getComponent(entityId, CreatureAi.class).stateStartTime;
double stateTargetTime;

switch (stateMachine.getCurrentState()) {
case STUNNED: {
case STUNNED -> {
// Hmm, this might actually be the level variable, the stun seems to be the time fallen when dropped
return timeSpent >= entityData.getComponent(entityId, CreatureComponent.class).stunDuration;
stateTargetTime = entityData.getComponent(entityId, CreatureComponent.class).stunDuration;
}
case FALLEN: {
return timeSpent >= entityData.getComponent(entityId, CreatureComponent.class).stunDuration;
case FALLEN -> {
stateTargetTime = entityData.getComponent(entityId, CreatureComponent.class).stunDuration;
}
case GETTING_UP: {
return timeSpent >= getAnimationTime(creature, Creature.AnimationType.GET_UP);
case GETTING_UP -> {
stateTargetTime = getAnimationTime(creature, Creature.AnimationType.GET_UP);
}
case ENTERING_DUNGEON: {
return timeSpent >= getAnimationTime(creature, Creature.AnimationType.ENTRANCE);
case ENTERING_DUNGEON -> {
stateTargetTime = getAnimationTime(creature, Creature.AnimationType.ENTRANCE);
}
case MELEE_ATTACK: {
return timeSpent >= getAnimationTime(creature, Creature.AnimationType.MELEE_ATTACK);
case MELEE_ATTACK -> {
stateTargetTime = getAnimationTime(creature, Creature.AnimationType.MELEE_ATTACK);
}
case EATING: {
return timeSpent >= getAnimationTime(creature, Creature.AnimationType.EATING);
case EATING -> {
stateTargetTime = getAnimationTime(creature, Creature.AnimationType.EATING);
}
case FLEE -> {
// I couldn't find a variable for this, so lets use this for now
stateTargetTime = gameSettings.get(Variable.MiscVariable.MiscType.IMP_IDLE_DELAY_BEFORE_REEVALUATION_SECONDS).getValue();
}
default -> {
stateTargetTime = Double.MAX_VALUE;
}
}
return false;

return stateTargetTime < timeSpent;
}

private static double getAnimationTime(Creature creature, Creature.AnimationType animation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public void enter(ICreatureController entity) {

@Override
public void update(ICreatureController entity) {
if (!entity.shouldFleeOrAttack()) {
if (entity.isStateTimeExceeded() && !entity.shouldFleeOrAttack()) {
entity.getStateMachine().changeState(IDLE);
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/toniarts/openkeeper/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Optional;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.random.RandomGenerator;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import toniarts.openkeeper.Main;
Expand Down Expand Up @@ -173,6 +174,15 @@ public static <T> Optional<T> getRandomItem(Collection<T> collection) {
return collection.stream().skip(RANDOM.nextInt(collection.size())).findFirst();
}

/**
* Returns the random generator used by the game
*
* @return random generator
*/
public static RandomGenerator getRandom() {
return RANDOM;
}

/**
* Get the game main text resource bundle
*
Expand Down

0 comments on commit 4751e51

Please sign in to comment.