Skip to content

Commit

Permalink
Animated Tracks and Semi Functional Barrels
Browse files Browse the repository at this point in the history
Ports from 1.7.10 FM+
  • Loading branch information
war-monger committed Oct 3, 2019
1 parent 839435d commit 254fa35
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 12 deletions.
99 changes: 99 additions & 0 deletions src/main/java/com/flansmod/client/model/AnimTankTrack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.flansmod.client.model;

import java.util.ArrayList;

import net.minecraft.item.ItemStack;

import com.flansmod.common.vector.Vector3f;


public class AnimTankTrack {

public ArrayList<Vector3f> points = new ArrayList<Vector3f>();
public float trackLinkLength = 0;

public AnimTankTrack (ArrayList<Vector3f> trackPoints, float linkLength)
{
points = trackPoints;
trackLinkLength = linkLength;
}

public void setLinkLength(float length)
{
trackLinkLength = length;
}

public float distBetweenPoints(Vector3f p1, Vector3f p2)
{
float distance;

float x = p1.x - p2.x;
float y = p1.y - p2.y;
distance = (float)Math.sqrt((x*x) + (y*y));

return distance;
}

public float getTrackLength()
{
float length = 0;
for(int i = 0; i < points.size(); i++)
{
length += distBetweenPoints(points.get(i), points.get((i == 0)? points.size()-1:i-1));
}
return length;
}

public int getTrackPart(float distance)
{
float length = 0;
for(int i = 0; i < points.size(); i++)
{
if(length >= distance)
{
return i;
} else {
length += distBetweenPoints(points.get(i), points.get((i == (points.size()-1))? 0:i+1));
}
}
return 0;
}


public float getProgressAlongTrackPart(float distance, int trackPart)
{
float length = 0;
float lastLength = 0;
for(int i = 0; i < trackPart + 1; i++)
{
if(i != 0)
{
length += distBetweenPoints(points.get(i-1), points.get(i));
}
}
return length;
}

public Vector3f getPositionOnTrack(float distance)
{

int trackPart = getTrackPart(distance);
Vector3f p2 = points.get((trackPart == 0)? points.size()-1:trackPart-1);
Vector3f p1 = points.get(trackPart);

float partLength = distBetweenPoints(p2, p1);
float prog = distance - (getProgressAlongTrackPart(distance,(trackPart == 0)? points.size()-1:trackPart-1));
float progress = prog/partLength;
Vector3f finalPos = new Vector3f(lerp(p2.x, p1.x, progress),lerp(p2.y, p1.y, progress), lerp(p2.z, p1.z, progress));

return finalPos;
}

public float lerp(float a, float b, float f)
{
float result = (a+ f*(b - a));

return result;
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/flansmod/client/model/AnimTrackLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.flansmod.client.model;

import com.flansmod.common.RotatedAxes;
import com.flansmod.common.vector.Vector3f;

public class AnimTrackLink {

public Vector3f position;
public Vector3f prevPosition;
public float zRot = 0;
public float prevZRot;
public float progress = 0;
public RotatedAxes rot;

public AnimTrackLink(float prog)
{
progress = prog;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/flansmod/client/model/ModelVehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class ModelVehicle extends ModelDriveable
public ModelRendererTurbo rightTrackWheelModels[] = new ModelRendererTurbo[0]; //These go with the tracks but rotate
public ModelRendererTurbo leftTrackWheelModels[] = new ModelRendererTurbo[0];


public ModelRendererTurbo fancyTrackModel[] = new ModelRendererTurbo[0];


public ModelRendererTurbo leftAnimTrackModel[][] = new ModelRendererTurbo[0][0]; //Unlimited frame track animations
public ModelRendererTurbo rightAnimTrackModel[][] = new ModelRendererTurbo[0][0];

Expand All @@ -37,6 +41,10 @@ public class ModelVehicle extends ModelDriveable
public ModelRendererTurbo drillHeadModel[] = new ModelRendererTurbo[0]; //Drill head. Rotates around
public Vector3f drillHeadOrigin = new Vector3f(); //this point

//recoiling barrel part
public ModelRendererTurbo animBarrelModel[] = new ModelRendererTurbo[0];
public Vector3f barrelAttach = new Vector3f();

public int animFrame = 0;


Expand Down Expand Up @@ -330,6 +338,16 @@ public void renderTurret(float f, float f1, float f2, float f3, float f4, float
}
}

public void renderAnimBarrel(float f, float f1, float f2, float f3, float f4, float f5, EntityVehicle vehicle, float dt)
{
if(vehicle.isPartIntact(EnumDriveablePart.turret))
{
for (ModelRendererTurbo aAnimBarrelModel : animBarrelModel) {
aAnimBarrelModel.render(f5, oldRotateOrder);
}
}
}

public void renderDrillBit(EntityVehicle vehicle, float f)
{
if(vehicle.isPartIntact(EnumDriveablePart.harvester))
Expand All @@ -341,6 +359,13 @@ public void renderDrillBit(EntityVehicle vehicle, float f)
}
}

public void renderFancyTracks(EntityVehicle vehicle, float f)
{
for (ModelRendererTurbo adrillHeadModel : fancyTrackModel) {
adrillHeadModel.render(0.0625F, oldRotateOrder);
}
}

@Override
public void flipAll()
{
Expand All @@ -362,6 +387,7 @@ public void flipAll()
flip(frontWheelModel);
flip(backWheelModel);
flip(drillHeadModel);
flip(fancyTrackModel);
for(ModelRendererTurbo[] latm : leftAnimTrackModel)
flip(latm);
for(ModelRendererTurbo[] ratm : rightAnimTrackModel)
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/com/flansmod/client/model/RenderVehicle.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.flansmod.common.driveables.DriveablePart;
import com.flansmod.common.driveables.DriveablePosition;
import com.flansmod.common.driveables.DriveableType;
import com.flansmod.common.driveables.EntitySeat;
import com.flansmod.common.driveables.EntityVehicle;
import com.flansmod.common.driveables.EnumDriveablePart;
import com.flansmod.common.driveables.ItemVehicle;
Expand Down Expand Up @@ -83,14 +84,42 @@ public void render(EntityVehicle vehicle, double d, double d1, double d2, float
float modelScale = type.modelScale;
GlStateManager.pushMatrix();
{
float recoilDPos = (float)Math.sin(Math.toRadians(vehicle.recoilPos)) - (float)Math.sin(Math.toRadians(vehicle.lastRecoilPos));
float recoilPos = (float)Math.sin(Math.toRadians(vehicle.lastRecoilPos)) + recoilDPos*f1;

GlStateManager.scale(modelScale, modelScale, modelScale);
ModelVehicle modVehicle = (ModelVehicle)type.model;
if(modVehicle != null)
modVehicle.render(vehicle, f1);

for(int i = 0; i < vehicle.trackLinksLeft.length; i++)
{
AnimTrackLink link = vehicle.trackLinksLeft[i];
float rotZ = link.zRot;
GL11.glPushMatrix();
GL11.glTranslatef(link.position.x/16F, link.position.y/16F, link.position.z/16F);
for(; rotZ > 180F; rotZ -= 360F) {}
for(; rotZ <= -180F; rotZ += 360F) {}
GL11.glRotatef(rotZ * (float)(180/Math.PI), 0, 0, 1);
modVehicle.renderFancyTracks(vehicle, f1);
GL11.glPopMatrix();
}

for(int i = 0; i < vehicle.trackLinksRight.length; i++)
{
AnimTrackLink link = vehicle.trackLinksRight[i];
float rotZ = link.zRot;
for(; rotZ > 180F; rotZ -= 360F) {}
for(; rotZ <= -180F; rotZ += 360F) {}
GL11.glPushMatrix();
GL11.glTranslatef(link.position.x/16F, link.position.y/16F, link.position.z/16F);
GL11.glRotatef(rotZ * (float)(180/Math.PI), 0, 0, 1);
modVehicle.renderFancyTracks(vehicle, f1);
GL11.glPopMatrix();
}

GlStateManager.pushMatrix();
if(type.turretOrigin != null && vehicle.isPartIntact(EnumDriveablePart.turret) &&
vehicle.getSeat(0) != null)
if(type.turretOrigin != null && vehicle.isPartIntact(EnumDriveablePart.turret) && vehicle.getSeat(0) != null)
{
dYaw = (vehicle.getSeat(0).looking.getYaw() - vehicle.getSeat(0).prevLooking.getYaw());
while(dYaw > 180F)
Expand All @@ -110,6 +139,18 @@ public void render(EntityVehicle vehicle, double d, double d1, double d2, float
if(modVehicle != null)
modVehicle.renderTurret(0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F, vehicle, f1);

//rotate and render barrel
if(modVehicle != null) {
EntitySeat[] seats = vehicle.getSeats();
GL11.glTranslatef(modVehicle.barrelAttach.x,modVehicle.barrelAttach.y, -modVehicle.barrelAttach.z);
float bPitch = (seats[0].looking.getPitch() - seats[0].prevLooking.getPitch());
float aPitch = seats[0].prevLooking.getPitch() + bPitch * f1;

GL11.glRotatef(-aPitch, 0F, 0F, 1F);
GL11.glTranslatef(recoilPos*-(5F/16F),0F, 0F);
modVehicle.renderAnimBarrel(0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F, vehicle, f1);
}

if(FlansMod.DEBUG)
{
GlStateManager.translate(type.turretOrigin.x, type.turretOrigin.y, type.turretOrigin.z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ public abstract class EntityDriveable extends Entity implements IControllable, I
/** Current gun variables for alternating weapons */
public int currentGunPrimary, currentGunSecondary;

/** Whether each mouse button is held */
public boolean leftMouseHeld = false, rightMouseHeld = false;

/** Angle of harvester aesthetic piece */
public float harvesterAngle;

Expand All @@ -124,6 +127,12 @@ public abstract class EntityDriveable extends Entity implements IControllable, I
public int animCount = 0;
public int animFrame = 0;

// Gun recoil
public boolean isRecoil = false;
public float recoilPos = 0;
public float lastRecoilPos = 0;
public int recoilTimer = 0;

public EntityDriveable(World world)
{
super(world);
Expand Down Expand Up @@ -951,6 +960,24 @@ else if(passenger instanceof EntityWheel)
}
}

//Gun recoil
if(leftMouseHeld){
tryRecoil();
setRecoilTimer();
}
lastRecoilPos = recoilPos;

if(recoilPos > 180-(180/type.recoilTime))
{
recoilPos = 0;
isRecoil = false;
}

if(isRecoil)
recoilPos = recoilPos + (180/type.recoilTime);

if(recoilTimer >= 0)
recoilTimer--;

for(DriveablePart part : getDriveableData().parts.values())
{
Expand Down Expand Up @@ -1078,12 +1105,17 @@ else if(EnumDriveablePart.getPart(
{
throttle *= 0.98F;
primaryShootHeld = secondaryShootHeld = false;
}
}

This comment has been minimized.

Copy link
@Liruxo

Liruxo Oct 3, 2019

Contributor

There shouldn't be an empty space behind the curly bracket

else if(getDriver() != null && getDriver() == getControllingPassenger())
{
reportVehicleError();
}

if(seats[0] != null && seats[0].getRidingEntity() == null)
{
rightMouseHeld = leftMouseHeld = false;
}

// Check if shooting
if(shootDelayPrimary > 0)
shootDelayPrimary--;
Expand Down Expand Up @@ -1175,6 +1207,40 @@ else if(FlansMod.hooks.BuildCraftLoaded && stack.isItemEqual(
}
}

public void tryRecoil()
{
int slot = -1;
DriveableType type = getDriveableType();
for(int i = driveableData.getMissileInventoryStart(); i < driveableData.getMissileInventoryStart() + type.numMissileSlots; i++)
{
ItemStack shell = driveableData.getStackInSlot(i);
if(shell != null && shell.getItem() instanceof ItemBullet && type.isValidAmmo(((ItemBullet)shell.getItem()).type, EnumWeaponType.SHELL))
{
slot = i;
}
}

if(recoilTimer <= 0 && slot != -1)
isRecoil = true;
}

public void setRecoilTimer()
{
int slot = -1;
DriveableType type = getDriveableType();
for(int i = driveableData.getMissileInventoryStart(); i < driveableData.getMissileInventoryStart() + type.numMissileSlots; i++)
{
ItemStack shell = driveableData.getStackInSlot(i);
if(shell != null && shell.getItem() instanceof ItemBullet && type.isValidAmmo(((ItemBullet)shell.getItem()).type, EnumWeaponType.SHELL))
{
slot = i;
}
}

if(recoilTimer <= 0 && slot != -1)
recoilTimer = getDriveableType().shootDelayPrimary;
}

private Vector3f getRandPosInBoundingBox(DriveablePart part)
{
// Pick a random position within the bounding box and spawn a flame there
Expand Down

3 comments on commit 254fa35

@ChrisLane
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format source to use tab indents. There's a lot of inconsistency in this commit.

@ChrisLane
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, "Semi Functional Barrels"? Semi-functional in what way?

@war-monger
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, "Semi Functional Barrels"? Semi-functional in what way?

Well, currently the barrels don't 'recoil back' but the positioning for them is in place. Keep in mind I wasn't the one who ported this code. It was from my friend.

Please sign in to comment.