Skip to content

Commit

Permalink
AWing cannons
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaHelios committed Jul 7, 2024
1 parent d50fa18 commit 4f1cd95
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

import com.parzivail.pswg.Resources;
import com.parzivail.pswg.entity.ship.RZ1Awing;
import com.parzivail.util.generics.Make;
import org.joml.Matrix4f;

import java.util.HashSet;

public class RigRZ1 extends ModelRig<RZ1Awing>
{
public static final RigRZ1 INSTANCE = new RigRZ1();
public static final String CANNON_LEFT = "CannonLeft";
public static final String CANNON_RIGHT = "CannonRight";
public static final HashSet<String> CANNONS = Make.hashSet(CANNON_LEFT, CANNON_RIGHT);

protected RigRZ1()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.parzivail.pswg.entity.ship;

import com.parzivail.pswg.container.SwgSounds;
import com.parzivail.pswg.entity.rigs.RigRZ1;
import com.parzivail.pswg.entity.rigs.RigT65B;
import com.parzivail.pswg.features.blasters.BlasterUtil;
import com.parzivail.util.entity.collision.CapsuleVolume;
import com.parzivail.util.entity.collision.ICollisionVolume;
import com.parzivail.util.entity.collision.IComplexEntityHitbox;
import com.parzivail.util.entity.collision.SweptTriangleVolume;
import com.parzivail.util.math.ColorUtil;
import com.parzivail.util.math.MathUtil;
import com.parzivail.util.math.QuatUtil;
import com.parzivail.util.math.Transform;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.data.DataTracker;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.data.TrackedDataHandlerRegistry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.joml.Matrix4f;
Expand Down Expand Up @@ -87,12 +99,69 @@ public class RZ1Awing extends ShipEntity implements IComplexEntityHitbox
private static final SweptTriangleVolume VOL_BODY_BOTTOM_MID_FRONTB = new SweptTriangleVolume(new Vec3d(-1.125, -0.827873, -5.4674), new Vec3d(1.125, -0.827873, -5.4674), new Vec3d(1.25, -1.45287, -2.3424), 0.2);
private static final SweptTriangleVolume VOL_BODY_BOTTOM_MID_MIDA = new SweptTriangleVolume(new Vec3d(-1.25, -1.45287, -2.3424), new Vec3d(-1.875, -1.70287, 0.5326), new Vec3d(1.25, -1.45287, -2.3424), 0.2);
private static final SweptTriangleVolume VOL_BODY_BOTTOM_MID_MIDB = new SweptTriangleVolume(new Vec3d(1.875, -1.70287, 0.532598), new Vec3d(-1.875, -1.70287, 0.5326), new Vec3d(1.25, -1.45287, -2.3424), 0.2);
private static final TrackedData<Byte> CANNON_BITS = DataTracker.registerData(RZ1Awing.class, TrackedDataHandlerRegistry.BYTE);
private static final int CANNON_STATE_MASK = 0b00000001;
private static final String[] CANNON_ORDER = { RigRZ1.CANNON_LEFT, RigRZ1.CANNON_RIGHT };

public RZ1Awing(EntityType<?> type, World world)
{
super(type, world);
}

@Override
protected void initDataTracker()
{
super.initDataTracker();
getDataTracker().startTracking(CANNON_BITS, (byte)0);
}

@Override
public void acceptFireInput()
{
var passenger = getControllingPassenger();
if (!(passenger instanceof PlayerEntity player))
return;

var pos = this.getPos();
var rotation = getRotation();
var stack = new Transform();

var cannonState = getCannonState();
var p = RigRZ1.INSTANCE.getWorldPosition(stack, this, rotation, CANNON_ORDER[cannonState], 0).add(pos);

var convergenceDistance = 40;
var forward = QuatUtil.rotate(MathUtil.V3D_NEG_Z.multiply(convergenceDistance), rotation);
var boltRotation = QuatUtil.lookAt(p, pos.add(forward));

var pDir = QuatUtil.rotate(MathUtil.V3D_POS_Z.multiply(5f), boltRotation);

BlasterUtil.fireBolt(getWorld(), player, pDir.normalize(), 100, distance -> (double)50, true, blasterBoltEntity -> {
blasterBoltEntity.setVelocity(pDir);
blasterBoltEntity.setPos(p.x, p.y, p.z);
blasterBoltEntity.setColor(ColorUtil.packHsv(0.98f, 1, 1));
});

getWorld().playSound(null, player.getBlockPos(), SwgSounds.Ship.XWINGT65B_FIRE, SoundCategory.PLAYERS, 1, 1 + (float)getWorld().random.nextGaussian() / 10);

cannonState++;
setCannonState(cannonState);
}

public byte getCannonState()
{
return (byte)(getDataTracker().get(CANNON_BITS) & CANNON_STATE_MASK);
}

public void setCannonState(byte cannonState)
{
byte cannons = getDataTracker().get(CANNON_BITS);

cannons &= ~CANNON_STATE_MASK;
cannons |= cannonState & CANNON_STATE_MASK;

getDataTracker().set(CANNON_BITS, cannons);
}

@Override
public ICollisionVolume[] getCollision()
{
Expand Down
Binary file modified projects/pswg/src/main/resources/data/pswg/rigs/ship/awing_rz1.p3dr
Binary file not shown.

0 comments on commit 4f1cd95

Please sign in to comment.