Skip to content

Commit

Permalink
Update 20241211 - Cannon Turret Update
Browse files Browse the repository at this point in the history
Update Cannon Turret to now show particle (WIP).

[CHANGELOG]
🟢 Added a required constructor parameter for the Turret Entity for handling Turret Remover interaction for all its subclasses.
🟡 Updated EoL formatting from LR to CRLF.
🟡 Renamed client's 'ModParticles' class to 'ModClientParticles'.
🟡 Moved the Turret Removed item interaction handler from the subclass (Cannon Turret) to the base class (Turret Entity).
🟡 Updated particle keyframe handler for the "Firing Sequence" controller.
🟡 Updated the LOGS constant for Turret Entity class to include all the stripped variants.
🔴 Removed unused imports.
🔴 Removed the "interactMob" method in the Cannon Turret in favor of requiring it to the Turret Entity base class's constructor's parameter.
  • Loading branch information
Virus5600 committed Dec 11, 2024
1 parent 4f5671e commit ef521b1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
charset = utf-8
end_of_line = lf
end_of_line = crlf
indent_size = 4
indent_style = tab
insert_final_newline = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.virus5600.defensive_measures;

import com.virus5600.defensive_measures.particle.ModParticles;
import com.virus5600.defensive_measures.particle.ModClientParticles;
import com.virus5600.defensive_measures.renderer.ModEntityRenderer;
import net.fabricmc.api.ClientModInitializer;

Expand All @@ -21,7 +21,7 @@ public void onInitializeClient() {

// Renderers
ModEntityRenderer.registerEntityRenderers();
ModParticles.registerParticles();
ModClientParticles.registerParticles();

// Networking

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@
import net.minecraft.registry.Registry;

@Environment(EnvType.CLIENT)
public class ModParticles {
public static final SimpleParticleType CANNON_FUSE = register("cannon_fuse", true);
public static final SimpleParticleType CANNON_FLASH = register("cannon_flash", false);

private static SimpleParticleType register(String identifier, boolean shouldAlwaysSpawn) {
return Registry.register(
Registries.PARTICLE_TYPE,
identifier,
FabricParticleTypes.simple(shouldAlwaysSpawn)
);
}

public class ModClientParticles {
public static void registerParticles() {
DefensiveMeasures.LOGGER.info("REGISTERING PARTICLES FOR {}...", DefensiveMeasures.MOD_NAME);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
import java.util.Map;

import com.virus5600.defensive_measures.item.ModItems;
import com.virus5600.defensive_measures.particle.ModParticles;
import com.virus5600.defensive_measures.sound.ModSoundEvents;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.ai.RangedAttackMob;
import net.minecraft.entity.ai.goal.ActiveTargetGoal;
import net.minecraft.entity.ai.goal.LookAroundGoal;
import net.minecraft.entity.ai.goal.LookAtEntityGoal;
import net.minecraft.entity.ai.goal.ProjectileAttackGoal;
import net.minecraft.entity.ai.goal.*;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
Expand All @@ -24,16 +22,12 @@
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.mob.Monster;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ArrowEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

Expand Down Expand Up @@ -69,19 +63,16 @@ public class CannonTurretEntity extends TurretEntity implements GeoEntity, Range
private static Map<Item, List<Object[]>> effectSource;

private final AnimatableInstanceCache geoCache = GeckoLibUtil.createInstanceCache(this);
/**
* Defines the current target of this Cannon.
*/
@Nullable
private LivingEntity currentTarget = null;
private double attCooldown = totalAttCooldown;
private boolean animPlayed = false;

protected ProjectileAttackGoal attackGoal;

//////////////////
// CONSTRUCTORS //
//////////////////
public CannonTurretEntity(EntityType<? extends MobEntity> entityType, World world) {
super(entityType, world, TurretMaterial.METAL, ArrowEntity.class);
super(entityType, world, TurretMaterial.METAL, ArrowEntity.class, ModItems.CANNON_TURRET);
this.setShootSound(ModSoundEvents.TURRET_CANNON_SHOOT);
this.setHealSound(ModSoundEvents.TURRET_REPAIR_METAL);
this.addHealables(healables);
Expand All @@ -93,8 +84,9 @@ public CannonTurretEntity(EntityType<? extends MobEntity> entityType, World worl
//////////////////
@Override
protected void initGoals() {
this.attackGoal = new ProjectileAttackGoal(this, 0, totalAttCooldown, 16.625F);
// Goals
this.goalSelector.add(1, new ProjectileAttackGoal(this, 0, totalAttCooldown, 16.625F));
this.goalSelector.add(1, attackGoal);
this.goalSelector.add(2, new LookAtEntityGoal(this, MobEntity.class, 8.0F, 0.02F, true));
this.goalSelector.add(8, new LookAroundGoal(this));

Expand Down Expand Up @@ -150,10 +142,10 @@ public void shootAt(LivingEntity target, float pullProgress) {
this.getWorld().spawnEntity(projectile);
this.triggerAnim("Firing Sequence", "Shoot");
} catch (IllegalArgumentException | SecurityException e) {
e.printStackTrace();
e.printStackTrace(System.out);

DefensiveMeasures.LOGGER.error("");
DefensiveMeasures.LOGGER.error(" " + DefensiveMeasures.MOD_ID.toUpperCase() + " ERROR OCCURRED ");
DefensiveMeasures.LOGGER.error(" {} ERROR OCCURRED ", DefensiveMeasures.MOD_ID.toUpperCase());
DefensiveMeasures.LOGGER.error("===== ERROR MSG START =====");
DefensiveMeasures.LOGGER.error("LOCALIZED ERROR MESSAGE:");
DefensiveMeasures.LOGGER.error(e.getLocalizedMessage());
Expand All @@ -173,19 +165,23 @@ public void tick() {
this.setBodyYaw(0);

if (this.isShooting() && this.hasTarget()) {
if (!this.getShootingFXDone()) {
this.triggerAnim("Firing Sequence", "firing_sequence");
if (this.attackGoal != null) {
if (this.attackGoal.shouldContinue()) {
if (--this.attCooldown <= 0) {
this.setShootingFXDone(false);
this.setFuseLit(false);
this.attCooldown = totalAttCooldown;
}
else {
this.setShootingFXDone(true);
this.setFuseLit(true);
this.triggerAnim("Firing Sequence", "Charge");
}
}
}
}
}

// TODO: Move code to TurretEntity then add a new protected variable where the item counterpart of the turret will be defined.
@Override
public ActionResult interactMob(PlayerEntity player, Hand hand) {
return Itemable.tryItem(player, hand, this, ModItems.TURRET_REMOVER, ModItems.CANNON_TURRET)
.orElse(super.interactMob(player, hand));
}

//////////////////////////////////////
// QUESTION METHODS (True or False) //
//////////////////////////////////////
Expand Down Expand Up @@ -245,32 +241,47 @@ private <E extends CannonTurretEntity>PlayState idleController(final AnimationSt
);
}

// TODO: Fix particle key-framing
// TODO: Fix particle spawning
private <E extends CannonTurretEntity>PlayState firingSequenceController(final AnimationState<E> event) {
Vec3d fusePos = this.getRelativePos(0, 0, 0),
Vec3d fusePos = this.getRelativePos(0, 1, 0),
barrelPos = this.getRelativePos(0, 0, 0);

String shootAnim = "animation.cannon_turret.shoot",
chargeAnim = "animation.cannon_turret.fuse";

// Shooting sequence
event.getController()
.setParticleKeyframeHandler((state) -> {
String locator = state.getKeyframeData().getLocator(),
effectName = state.getKeyframeData().getEffect(),
currentState = "fuse";

System.out.println("Locator: " + locator + " | Effect: " + effectName);
if (this.hasTarget() && this.isShooting()) {
if (!this.getShootingFXDone()) currentState = "firingSequence";
if (this.isFuseLit()) currentState = "fuse";
else currentState = "shoot";
}

System.out.println("Locator: " + locator + " | Effect: " + effectName + " | State: " + currentState);

if (currentState.equals("fuse")) {
System.out.println("Fuse Position: " + fusePos.toString());
this.getWorld().addParticle(
ModParticles.CANNON_FUSE,
fusePos.getX(), fusePos.getY(), fusePos.getZ(),
0,0,0
);
}
else {
System.out.println("Barrel Position: " + barrelPos.toString());
this.getWorld().addParticle(
ModParticles.CANNON_FLASH,
barrelPos.getX(), barrelPos.getY(), barrelPos.getZ(),
0,0,0
);
}

state.getController()
.setAnimation(
RawAnimation
.begin()
.thenPlay(currentState.equals("fuse") ? chargeAnim : shootAnim)
.thenPlay("animation.cannon_turret." + currentState)
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ public class TurretEntity extends MobEntity implements Itemable, RangedAttackMob
* The material of this turret.
*/
protected TurretMaterial material;
/**
* The item counterpart of this item. Refer to {@link ModItems} list
* to see the list of items that can be converted to this entity.
* The items should be a subclass of {@link TurretItem}.
*/
protected Item itemable;
/**
* A randomizer for this turret's instance.
*/
Expand Down Expand Up @@ -202,7 +208,19 @@ public class TurretEntity extends MobEntity implements Itemable, RangedAttackMob
Items.MANGROVE_LOG,
Items.OAK_LOG,
Items.SPRUCE_LOG,
Items.WARPED_STEM
Items.WARPED_HYPHAE,
Items.WARPED_STEM,
Items.STRIPPED_ACACIA_LOG,
Items.STRIPPED_BIRCH_LOG,
Items.STRIPPED_CHERRY_LOG,
Items.STRIPPED_CRIMSON_STEM,
Items.STRIPPED_DARK_OAK_LOG,
Items.STRIPPED_JUNGLE_LOG,
Items.STRIPPED_MANGROVE_LOG,
Items.STRIPPED_OAK_LOG,
Items.STRIPPED_SPRUCE_LOG,
Items.STRIPPED_WARPED_HYPHAE,
Items.STRIPPED_WARPED_STEM
});

//////////////////
Expand All @@ -211,32 +229,39 @@ public class TurretEntity extends MobEntity implements Itemable, RangedAttackMob

/**
* Constructs a new {@code TurretEntity} with the given {@code EntityType}, {@code World},
* {@code TurretMaterial}, and {@code Class} of the projectile.
* {@code TurretMaterial}, {@code Class} of the projectile, and {@code Item}.
*
* @param entityType The type of this entity.
* @param world The world this entity is in.
* @param material The material of this turret.
* @param projectile The class of the projectile this turret will shoot.
* @param itemable The itemable counterpart of this entity.
*
* @see #itemable
*/
public TurretEntity(EntityType<? extends MobEntity> entityType, World world, TurretMaterial material, Class<?> projectile) {
this(entityType, world, material);
public TurretEntity(EntityType<? extends MobEntity> entityType, World world, TurretMaterial material, Class<?> projectile, Item itemable) {
this(entityType, world, material, itemable);
this.projectile = projectile;
}

/**
* Constructs a new {@code TurretEntity} with the given {@code EntityType}, {@code World},
* and {@code TurretMaterial}. If the {@code projectile} is not yet defined, the default
* {@code TurretMaterial}, and {@code Item}. If the {@code projectile} is not yet defined, the default
* projectile will be used, which is an {@code ArrowEntity}.
*
* @param entityType The type of this entity.
* @param world The world this entity is in.
* @param material The material of this turret.
* @param itemable The itemable counterpart of this entity.
*
* @see #itemable
*/
public TurretEntity(EntityType<? extends MobEntity> entityType, World world, TurretMaterial material) {
public TurretEntity(EntityType<? extends MobEntity> entityType, World world, TurretMaterial material, Item itemable) {
super(entityType, world);
DefensiveMeasures.LOGGER.debug("Creating a new TurretEntity called {}", entityType.getName());

this.material = material;
this.itemable = itemable;
this.random = Random.create();
this.lookControl = new TurretEntity.TurretLookControl(this);

Expand All @@ -251,8 +276,6 @@ public TurretEntity(EntityType<? extends MobEntity> entityType, World world, Tur

@Override
protected void initDataTracker(DataTracker.Builder builder) {
// super.initDataTracker(builder);

// Entity related tracking
builder.add(LEVEL, this.level)
.add(FROM_ITEM, (byte) 1)
Expand All @@ -270,7 +293,6 @@ protected void initDataTracker(DataTracker.Builder builder) {
.add(TARGET_POS_X, 0f)
.add(TARGET_POS_Y, 0f)
.add(TARGET_POS_Z, 0f);
// .add(ATTACHED_FACE, Direction.DOWN);
super.initDataTracker(builder);
}

Expand Down Expand Up @@ -306,9 +328,9 @@ public EntityData initialize(ServerWorldAccess world, LocalDifficulty difficulty
*/
protected void tryAttachOrFall() {
Direction dir = this.findAttachableSide(this.getBlockPos());
if (dir == null)
if (dir != null)
this.setAttachedFace(dir);
else if (dir != Direction.DOWN)
else
this.tryFall();
}

Expand Down Expand Up @@ -396,6 +418,11 @@ else if (this.isHealableItem(item.getItem())) {

if (isSuccess)
return ActionResult.SUCCESS;

if (item.getItem() == ModItems.TURRET_REMOVER) {
return Itemable.tryItem(player, hand, this, ModItems.TURRET_REMOVER, this.itemable)
.orElse(super.interactMob(player, hand));
}
return super.interactMob(player, hand);
}

Expand Down Expand Up @@ -621,7 +648,8 @@ protected Direction getAttachedFace() {
* @return Vec3d the relative position of this point, assuming that the origin is at <b>[0, 0, 0]</b>
*/
public Vec3d getRelativePos(double xOffset, double yOffset, double zOffset) {
return this.getRotationVecClient().add(this.getPos());
return this.getRotationVecClient().add(this.getPos())
.add(xOffset, yOffset, zOffset);
}

@Override
Expand Down Expand Up @@ -979,6 +1007,8 @@ public void shootAt(LivingEntity target, float pullProgress) {
this.setShooting(target != null);

try {
String targetName = target != null ? target.getName().getString() : "(nothing)";
System.out.println("Shooting at " + targetName + " with a pull progress of " + pullProgress);
this.setHasTarget(target != null);

if (target != null) {
Expand Down Expand Up @@ -1006,7 +1036,7 @@ public void shootAt(LivingEntity target, float pullProgress) {
| SecurityException
| NoSuchMethodException e)
{
e.printStackTrace();
e.printStackTrace(System.out);

DefensiveMeasures.LOGGER.error("");
DefensiveMeasures.LOGGER.error("\t {} ERROR OCCURRED\t ", DefensiveMeasures.MOD_ID.toUpperCase());
Expand Down

0 comments on commit ef521b1

Please sign in to comment.