Skip to content

Commit

Permalink
Moved some life stage related code to enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
ata4 committed Jul 24, 2015
1 parent 8a774a0 commit 0fbf8b7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -850,12 +850,16 @@ public void setScalePublic(float scale) {
double posXTmp = posX;
double posYTmp = posY;
double posZTmp = posZ;
boolean onGroundTmp = onGround;

setScale(scale);

// workaround for a vanilla bug; the position is apparently not set correcty
// after changing the entity size, causing asynchronous server/client positioning
setPosition(posXTmp, posYTmp, posZTmp);

// otherwise, setScale stops the dragon from landing while it is growing
onGround = onGroundTmp;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package info.ata4.minecraft.dragon.server.entity.helper;

import info.ata4.minecraft.dragon.util.math.MathX;
import net.minecraft.util.MathHelper;

/**
Expand All @@ -18,14 +19,19 @@
*/
public enum DragonLifeStage {

EGG(0, 24000),
HATCHLING(24000, 24000),
JUVENILE(48000, 24000),
ADULT(72000, -1);
EGG(0, 24000, 0.25f),
HATCHLING(24000, 24000, 0.33f),
JUVENILE(48000, 24000, 0.66f),
ADULT(72000, -1, 1);

DragonLifeStage(int startTicks, int durationTicks) {
public final int startTicks;
public final int durationTicks; // -1 means infinite
public final float scale;

DragonLifeStage(int startTicks, int durationTicks, float scale) {
this.startTicks = startTicks;
this.durationTicks = durationTicks;
this.scale = scale;
}

public static DragonLifeStage getLifeStageFromTickCount(int ticksSinceCreation) {
Expand All @@ -40,6 +46,33 @@ public static DragonLifeStage getLifeStageFromTickCount(int ticksSinceCreation)
}
return ADULT;
}

public static float getScaleFromTickCount(int ticksSinceCreation) {
DragonLifeStage lifeStage = getLifeStageFromTickCount(ticksSinceCreation);
int timeInThisStage = ticksSinceCreation - lifeStage.startTicks;
float fractionOfStage = timeInThisStage / (float) lifeStage.durationTicks;

switch (lifeStage) {
// constant size for egg and adult stage
case EGG:
return EGG.scale;

case ADULT:
return ADULT.scale;

// interpolated size for hatchling and juvenile stages
case HATCHLING:
return MathX.lerp(HATCHLING.scale, JUVENILE.scale, fractionOfStage);

case JUVENILE:
return MathX.lerp(JUVENILE.scale, ADULT.scale, fractionOfStage);

// this should never happen unless more life stages have been added
// without updating this method
default:
throw new RuntimeException("Unimplemented life stage: " + lifeStage);
}
}

public static int clipTickCountToValid(int ticksSinceCreation) {
return MathHelper.clamp_int(
Expand All @@ -48,7 +81,4 @@ public static int clipTickCountToValid(int ticksSinceCreation) {
ADULT.durationTicks
);
}

public final int startTicks;
public final int durationTicks;
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ public DragonLifeStage getLifeStage() {
int age = getTicksSinceCreation();
return DragonLifeStage.getLifeStageFromTickCount(age);
}

/**
* Returns the size multiplier for the current age.
*
* @return size
*/
// public float getScale() {
// int age = getTicksSinceCreation();
// return DragonLifeStage.getScaleFromTickCount(age);
// }

public int getTicksSinceCreation() {
if (!dragon.worldObj.isRemote) {
Expand All @@ -122,7 +132,7 @@ public void readFromNBT(NBTTagCompound nbt) {
ticksSinceCreationServer = ticksRead;
dataWatcher.updateObject(dataIndexTicksSinceCreation, ticksSinceCreationServer);
}

/**
* Returns the size multiplier for the current age.
*
Expand Down Expand Up @@ -323,9 +333,7 @@ private void updateEgg() {
}

private void updateScale() {
boolean savedOnGround = dragon.onGround; // otherwise, setScale stops the dragon from landing while it is growing
dragon.setScalePublic(getScale());
dragon.onGround = savedOnGround;
}

@Override
Expand Down

0 comments on commit 0fbf8b7

Please sign in to comment.