Skip to content

Commit

Permalink
Added rascal gameplay loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Faboslav committed Sep 30, 2023
1 parent 7ee5191 commit 38cabe0
Show file tree
Hide file tree
Showing 22 changed files with 402 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,66 @@
import com.faboslav.friendsandfoes.entity.ai.brain.RascalBrain;
import com.faboslav.friendsandfoes.entity.animation.AnimatedEntity;
import com.faboslav.friendsandfoes.init.FriendsAndFoesSoundEvents;
import com.faboslav.friendsandfoes.mixin.ChunkRegionAccessor;
import com.faboslav.friendsandfoes.mixin.ServerWorldAccessor;
import com.faboslav.friendsandfoes.util.RandomGenerator;
import com.mojang.serialization.Dynamic;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.ai.brain.Brain;
import net.minecraft.entity.ai.brain.MemoryModuleType;
import net.minecraft.entity.ai.pathing.PathNodeType;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.IllusionerEntity;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.passive.IronGolemEntity;
import net.minecraft.entity.passive.PassiveEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.registry.tag.StructureTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.SoundEvent;
import net.minecraft.tag.StructureTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.ServerWorldAccess;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.gen.StructureAccessor;
import net.minecraft.world.gen.structure.Structure;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

public final class RascalEntity extends PassiveEntity implements AnimatedEntity
{
private AnimationContextTracker animationContextTracker;

private static final TrackedData<Integer> CAUGHT_COUNT;

public RascalEntity(EntityType<? extends PassiveEntity> entityType, World world) {
super(entityType, world);
this.setPathfindingPenalty(PathNodeType.RAIL, 0.0F);
this.setPathfindingPenalty(PathNodeType.UNPASSABLE_RAIL, 0.0F);
this.setPathfindingPenalty(PathNodeType.DAMAGE_OTHER, -1.0F);
}

public static boolean canSpawn(
EntityType<? extends MobEntity> rascalEntityType,
ServerWorldAccess serverWorldAccess,
SpawnReason spawnReason,
BlockPos blockPos,
Random random
EntityType<? extends MobEntity> rascalEntityType,
ServerWorldAccess serverWorldAccess,
SpawnReason spawnReason,
BlockPos blockPos,
Random random
) {
if(spawnReason == SpawnReason.NATURAL) {
if (spawnReason == SpawnReason.NATURAL) {
StructureAccessor structureAccessor = serverWorldAccess.toServerWorld().getStructureAccessor();

if (
structureAccessor.getStructureContaining(
blockPos,
StructureTags.MINESHAFT
).hasChildren()
&& blockPos.getY() < 63
&& serverWorldAccess.isSkyVisible(blockPos) == false
structureAccessor.getStructureContaining(
blockPos,
StructureTags.MINESHAFT
).hasChildren()
&& blockPos.getY() < 63
&& serverWorldAccess.isSkyVisible(blockPos) == false
) {
return true;
}
Expand All @@ -88,6 +88,12 @@ public AnimationContextTracker getAnimationContextTracker() {
return this.animationContextTracker;
}

@Override
protected void initDataTracker() {
super.initDataTracker();
this.dataTracker.startTracking(CAUGHT_COUNT, 0);
}

@Nullable
@Override
public PassiveEntity createChild(ServerWorld world, PassiveEntity entity) {
Expand Down Expand Up @@ -120,7 +126,7 @@ protected void mobTick() {
public static DefaultAttributeContainer.Builder createAttributes() {
return MobEntity.createMobAttributes()
.add(EntityAttributes.GENERIC_MAX_HEALTH, 10.0D)
.add(EntityAttributes.GENERIC_MOVEMENT_SPEED, 0.8D)
.add(EntityAttributes.GENERIC_MOVEMENT_SPEED, 0.55D)
.add(EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE, 1.0D);
}

Expand Down Expand Up @@ -153,28 +159,28 @@ public boolean damage(
return false;
}

public SoundEvent getApproveSound() {
return FriendsAndFoesSoundEvents.ENTITY_RASCAL_APPROVE.get();
public SoundEvent getNodSound() {
return FriendsAndFoesSoundEvents.ENTITY_RASCAL_NOD.get();
}

public void playApproveSound() {
this.playSound(this.getApproveSound(), 1.0F, RandomGenerator.generateFloat(1.1F, 1.3F));
public void playNodSound() {
this.playSound(this.getNodSound(), 1.0F, RandomGenerator.generateFloat(1.15F, 1.3F));
}

public SoundEvent getRewardSound() {
return FriendsAndFoesSoundEvents.ENTITY_RASCAL_REWARD.get();
}

public void playRewardSound() {
this.playSound(this.getRewardSound(), 1.0F, RandomGenerator.generateFloat(1.1F, 1.3F));
this.playSound(this.getRewardSound(), 1.0F, RandomGenerator.generateFloat(1.15F, 1.3F));
}

public SoundEvent getBadRewardSound() {
return FriendsAndFoesSoundEvents.ENTITY_RASCAL_REWARD_BAD.get();
}

public void playBadRewardSound() {
this.playSound(this.getBadRewardSound(), 1.0F, RandomGenerator.generateFloat(1.1F, 1.3F));
this.playSound(this.getBadRewardSound(), 1.0F, RandomGenerator.generateFloat(1.15F, 1.3F));
}

@Override
Expand All @@ -184,8 +190,12 @@ protected SoundEvent getAmbientSound() {

@Override
public void playAmbientSound() {
if (this.isHidden()) {
return;
}

SoundEvent soundEvent = this.getAmbientSound();
this.playSound(soundEvent, 1.0F, RandomGenerator.generateFloat(1.1F, 1.3F));
this.playSound(soundEvent, 1.0F, RandomGenerator.generateFloat(1.15F, 1.3F));
}

@Override
Expand All @@ -196,16 +206,48 @@ protected SoundEvent getHurtSound(DamageSource source) {
@Override
protected void playHurtSound(DamageSource source) {
this.ambientSoundChance = -this.getMinAmbientSoundDelay();
this.playSound(this.getHurtSound(source), 1.0F, RandomGenerator.generateFloat(1.1F, 1.3F));
this.playSound(this.getHurtSound(source), 1.0F, RandomGenerator.generateFloat(1.15F, 1.3F));
}

@Override
protected void playStepSound(
BlockPos pos,
BlockState state
) {
if (
this.isHidden()
|| state.isLiquid()
) {
return;
}

super.playStepSound(pos, state);
}

public boolean isHidden() {
return this.getBrain().getOptionalRegisteredMemory(MemoryModuleType.AVOID_TARGET).orElse(null) instanceof PlayerEntity;
}

public int getCaughtCount() {
return this.dataTracker.get(CAUGHT_COUNT);
}

public void addToCaughtCount() {
this.dataTracker.set(CAUGHT_COUNT, this.getCaughtCount() + 1);
}

public boolean shouldGiveReward() {
return this.getCaughtCount() >= 3;
}

public void spawnCloudParticles() {
this.spawnParticles(ParticleTypes.CLOUD, 16);
this.spawnParticles(ParticleTypes.CLOUD, 16, 0.1D);
}

public void spawnParticles(
ParticleEffect particleEffect,
int amount
int amount,
double speed
) {
World world = this.getWorld();

Expand All @@ -223,9 +265,13 @@ public void spawnParticles(
this.getRandom().nextGaussian() * 0.02D,
this.getRandom().nextGaussian() * 0.02D,
this.getRandom().nextGaussian() * 0.02D,
1.0D
speed
);
}
}

static {
CAUGHT_COUNT = DataTracker.registerData(RascalEntity.class, TrackedDataHandlerRegistry.INTEGER);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private SoundEvent getImpactSound() {

private void playImpactSound() {
SoundEvent soundEvent = this.getImpactSound();
this.playSound(soundEvent, 1.0F, 0.95F + RandomGenerator.generateFloat(0.95F, 1.05F));
this.playSound(soundEvent, 1.0F, 0.95F + RandomGenerator.generateFloat(0.95F, 1.05F));
}
}

Loading

0 comments on commit 38cabe0

Please sign in to comment.