Skip to content

Commit

Permalink
size component
Browse files Browse the repository at this point in the history
  • Loading branch information
natanmaia95 committed Sep 18, 2024
1 parent 6e6e76e commit d46440c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nateplays.my_neoforge_mod.entity.ModEntities;
import com.nateplays.my_neoforge_mod.entity.ai.MosswineAttackGoal;
import com.nateplays.my_neoforge_mod.entity.interfaces.ILevelableEntity;
import com.nateplays.my_neoforge_mod.entity.interfaces.IVaryingSizeEntity;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
Expand All @@ -27,7 +28,7 @@
import net.neoforged.neoforge.common.Tags;
import org.jetbrains.annotations.Nullable;

public class MosswineEntity extends Animal implements ILevelableEntity {
public class MosswineEntity extends Animal implements ILevelableEntity, IVaryingSizeEntity {

private static final EntityDataAccessor<Boolean> ATTACKING =
SynchedEntityData.defineId(MosswineEntity.class, EntityDataSerializers.BOOLEAN);
Expand Down Expand Up @@ -86,6 +87,7 @@ public SpawnGroupData finalizeSpawn(ServerLevelAccessor level, DifficultyInstanc
if (!level.isClientSide()) {
this.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(Items.IRON_SWORD));
setIsGoldHead(random.nextFloat() < 0.1f);
makeSizeToSpawn();
}
return super.finalizeSpawn(level, difficulty, spawnType, spawnGroupData);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.nateplays.my_neoforge_mod.entity.interfaces;

import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import org.jetbrains.annotations.ApiStatus;

public interface IVaryingSizeEntity {

//Scale will only affect the _base_ scale attribute. Modifiers (from potions, etc) will be added on top.
public enum CrownType {
NONE, SMALL_SILVER, LARGE_SILVER, SMALL_GOLD, LARGE_GOLD
}

//Override these!!
default double getMinScale() {return 0.9;}
default double getMaxScale() {return 1.1;}


@ApiStatus.NonExtendable
default void setScale(double value) {
AttributeInstance scaleAttr = ((LivingEntity) this).getAttribute(Attributes.SCALE);
if (scaleAttr == null) return;
scaleAttr.setBaseValue(value);
}

default void makeSizeToSpawn() {
this.setScale(rollScale());
}




default double rollScale() {
RandomSource randomSource = RandomSource.create();
float exactEdgeProbability = 0.1f;
double min = getMinScale(); double max = getMaxScale();

float randomValue = randomSource.nextFloat();

if (randomValue < exactEdgeProbability/2.0) {
return min;
} else if (randomValue < exactEdgeProbability) {
return max;
} else {
return min + (max-min)*randomSource.nextFloat();
}
}



// Use these for user interfaces
default double getDefaultDisplaySize() {
return ((LivingEntity)this).getType().getDimensions().width();
}
default double getDisplaySize() {
return getDefaultDisplaySize()* ((LivingEntity)this).getScale();
}
default double getMinDisplaySize() {
return getDefaultDisplaySize()*getMinScale();
}
default double getMaxDisplaySize() {
return getDefaultDisplaySize()*getMaxScale();
}

default CrownType getCrownType() {
double scale = ((LivingEntity)this).getScale();
double min = getMinScale(); double max = getMaxScale();

if (scale == min) return CrownType.SMALL_SILVER;
if (scale == max) return CrownType.LARGE_SILVER;

if (scale < min) return CrownType.SMALL_GOLD;
else if (scale > max) return CrownType.LARGE_GOLD;
else return CrownType.NONE;
}
}

0 comments on commit d46440c

Please sign in to comment.