Added support for assigning float to stat

Created Stat#encodeFloat(float,int) to encode floats into fixed point int
Added maxhp stat to monster attributes
Changed debugging to output float stat
This commit is contained in:
Collin Smith
2020-08-30 15:51:44 -07:00
parent b4a77e02a2
commit 8fb6bb5fa5
3 changed files with 14 additions and 2 deletions

View File

@ -123,7 +123,7 @@ public class Actioneer extends PassiveSystem {
log.debug("{} attack {}", entityId, targetId);
Attributes attrs = mAttributesWrapper.get(targetId).attrs;
log.debug("{} {}", targetId, attrs.get(Stat.hitpoints));
log.debug("{} {}", targetId, attrs.get(Stat.hitpoints).toFloat());
break;
case 27: // teleport
mPosition.get(entityId).position.set(targetVec);

View File

@ -157,7 +157,11 @@ public class ServerEntityFactory extends EntityFactory {
Attributes attrs = new Attributes();
PropertyList base = attrs.base();
base.clear();
base.put(Stat.hitpoints, MathUtils.random(monstats.minHP[0], monstats.maxHP[0]));
final float hitpoints = MathUtils.random(monstats.minHP[0], monstats.maxHP[0]);
// FIXME: this can be more elegant -- add support for putting float or auto converting?
final int encodedHitpoints = Stat.encodeFloat(hitpoints, 8); // see above note, this is crude
base.put(Stat.hitpoints, encodedHitpoints);
base.put(Stat.maxhp, encodedHitpoints);
attrs.reset(); // propagate base changes
mAttributesWrapper.create(id).attrs = attrs;

View File

@ -820,6 +820,14 @@ public class Stat implements Comparable<Stat> {
return ((val >>> shift) + ((val & mask) / (float) pow));
}
public void setFloat(float value) {
this.val = encodeFloat(value, entry.ValShift);
}
public static int encodeFloat(float value, int precision) {
return (int) (value * (1 << precision));
}
public long toLong() {
return UnsignedInts.toLong(val);
}