Skip to content

Commit 254fa35

Browse files
committed
Animated Tracks and Semi Functional Barrels
Ports from 1.7.10 FM+
1 parent 839435d commit 254fa35

File tree

7 files changed

+358
-12
lines changed

7 files changed

+358
-12
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.flansmod.client.model;
2+
3+
import java.util.ArrayList;
4+
5+
import net.minecraft.item.ItemStack;
6+
7+
import com.flansmod.common.vector.Vector3f;
8+
9+
10+
public class AnimTankTrack {
11+
12+
public ArrayList<Vector3f> points = new ArrayList<Vector3f>();
13+
public float trackLinkLength = 0;
14+
15+
public AnimTankTrack (ArrayList<Vector3f> trackPoints, float linkLength)
16+
{
17+
points = trackPoints;
18+
trackLinkLength = linkLength;
19+
}
20+
21+
public void setLinkLength(float length)
22+
{
23+
trackLinkLength = length;
24+
}
25+
26+
public float distBetweenPoints(Vector3f p1, Vector3f p2)
27+
{
28+
float distance;
29+
30+
float x = p1.x - p2.x;
31+
float y = p1.y - p2.y;
32+
distance = (float)Math.sqrt((x*x) + (y*y));
33+
34+
return distance;
35+
}
36+
37+
public float getTrackLength()
38+
{
39+
float length = 0;
40+
for(int i = 0; i < points.size(); i++)
41+
{
42+
length += distBetweenPoints(points.get(i), points.get((i == 0)? points.size()-1:i-1));
43+
}
44+
return length;
45+
}
46+
47+
public int getTrackPart(float distance)
48+
{
49+
float length = 0;
50+
for(int i = 0; i < points.size(); i++)
51+
{
52+
if(length >= distance)
53+
{
54+
return i;
55+
} else {
56+
length += distBetweenPoints(points.get(i), points.get((i == (points.size()-1))? 0:i+1));
57+
}
58+
}
59+
return 0;
60+
}
61+
62+
63+
public float getProgressAlongTrackPart(float distance, int trackPart)
64+
{
65+
float length = 0;
66+
float lastLength = 0;
67+
for(int i = 0; i < trackPart + 1; i++)
68+
{
69+
if(i != 0)
70+
{
71+
length += distBetweenPoints(points.get(i-1), points.get(i));
72+
}
73+
}
74+
return length;
75+
}
76+
77+
public Vector3f getPositionOnTrack(float distance)
78+
{
79+
80+
int trackPart = getTrackPart(distance);
81+
Vector3f p2 = points.get((trackPart == 0)? points.size()-1:trackPart-1);
82+
Vector3f p1 = points.get(trackPart);
83+
84+
float partLength = distBetweenPoints(p2, p1);
85+
float prog = distance - (getProgressAlongTrackPart(distance,(trackPart == 0)? points.size()-1:trackPart-1));
86+
float progress = prog/partLength;
87+
Vector3f finalPos = new Vector3f(lerp(p2.x, p1.x, progress),lerp(p2.y, p1.y, progress), lerp(p2.z, p1.z, progress));
88+
89+
return finalPos;
90+
}
91+
92+
public float lerp(float a, float b, float f)
93+
{
94+
float result = (a+ f*(b - a));
95+
96+
return result;
97+
}
98+
99+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.flansmod.client.model;
2+
3+
import com.flansmod.common.RotatedAxes;
4+
import com.flansmod.common.vector.Vector3f;
5+
6+
public class AnimTrackLink {
7+
8+
public Vector3f position;
9+
public Vector3f prevPosition;
10+
public float zRot = 0;
11+
public float prevZRot;
12+
public float progress = 0;
13+
public RotatedAxes rot;
14+
15+
public AnimTrackLink(float prog)
16+
{
17+
progress = prog;
18+
}
19+
}

src/main/java/com/flansmod/client/model/ModelVehicle.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class ModelVehicle extends ModelDriveable
2626
public ModelRendererTurbo rightTrackWheelModels[] = new ModelRendererTurbo[0]; //These go with the tracks but rotate
2727
public ModelRendererTurbo leftTrackWheelModels[] = new ModelRendererTurbo[0];
2828

29+
30+
public ModelRendererTurbo fancyTrackModel[] = new ModelRendererTurbo[0];
31+
32+
2933
public ModelRendererTurbo leftAnimTrackModel[][] = new ModelRendererTurbo[0][0]; //Unlimited frame track animations
3034
public ModelRendererTurbo rightAnimTrackModel[][] = new ModelRendererTurbo[0][0];
3135

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

44+
//recoiling barrel part
45+
public ModelRendererTurbo animBarrelModel[] = new ModelRendererTurbo[0];
46+
public Vector3f barrelAttach = new Vector3f();
47+
4048
public int animFrame = 0;
4149

4250

@@ -330,6 +338,16 @@ public void renderTurret(float f, float f1, float f2, float f3, float f4, float
330338
}
331339
}
332340

341+
public void renderAnimBarrel(float f, float f1, float f2, float f3, float f4, float f5, EntityVehicle vehicle, float dt)
342+
{
343+
if(vehicle.isPartIntact(EnumDriveablePart.turret))
344+
{
345+
for (ModelRendererTurbo aAnimBarrelModel : animBarrelModel) {
346+
aAnimBarrelModel.render(f5, oldRotateOrder);
347+
}
348+
}
349+
}
350+
333351
public void renderDrillBit(EntityVehicle vehicle, float f)
334352
{
335353
if(vehicle.isPartIntact(EnumDriveablePart.harvester))
@@ -341,6 +359,13 @@ public void renderDrillBit(EntityVehicle vehicle, float f)
341359
}
342360
}
343361

362+
public void renderFancyTracks(EntityVehicle vehicle, float f)
363+
{
364+
for (ModelRendererTurbo adrillHeadModel : fancyTrackModel) {
365+
adrillHeadModel.render(0.0625F, oldRotateOrder);
366+
}
367+
}
368+
344369
@Override
345370
public void flipAll()
346371
{
@@ -362,6 +387,7 @@ public void flipAll()
362387
flip(frontWheelModel);
363388
flip(backWheelModel);
364389
flip(drillHeadModel);
390+
flip(fancyTrackModel);
365391
for(ModelRendererTurbo[] latm : leftAnimTrackModel)
366392
flip(latm);
367393
for(ModelRendererTurbo[] ratm : rightAnimTrackModel)

src/main/java/com/flansmod/client/model/RenderVehicle.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.flansmod.common.driveables.DriveablePart;
2626
import com.flansmod.common.driveables.DriveablePosition;
2727
import com.flansmod.common.driveables.DriveableType;
28+
import com.flansmod.common.driveables.EntitySeat;
2829
import com.flansmod.common.driveables.EntityVehicle;
2930
import com.flansmod.common.driveables.EnumDriveablePart;
3031
import com.flansmod.common.driveables.ItemVehicle;
@@ -83,14 +84,42 @@ public void render(EntityVehicle vehicle, double d, double d1, double d2, float
8384
float modelScale = type.modelScale;
8485
GlStateManager.pushMatrix();
8586
{
87+
float recoilDPos = (float)Math.sin(Math.toRadians(vehicle.recoilPos)) - (float)Math.sin(Math.toRadians(vehicle.lastRecoilPos));
88+
float recoilPos = (float)Math.sin(Math.toRadians(vehicle.lastRecoilPos)) + recoilDPos*f1;
89+
8690
GlStateManager.scale(modelScale, modelScale, modelScale);
8791
ModelVehicle modVehicle = (ModelVehicle)type.model;
8892
if(modVehicle != null)
8993
modVehicle.render(vehicle, f1);
9094

95+
for(int i = 0; i < vehicle.trackLinksLeft.length; i++)
96+
{
97+
AnimTrackLink link = vehicle.trackLinksLeft[i];
98+
float rotZ = link.zRot;
99+
GL11.glPushMatrix();
100+
GL11.glTranslatef(link.position.x/16F, link.position.y/16F, link.position.z/16F);
101+
for(; rotZ > 180F; rotZ -= 360F) {}
102+
for(; rotZ <= -180F; rotZ += 360F) {}
103+
GL11.glRotatef(rotZ * (float)(180/Math.PI), 0, 0, 1);
104+
modVehicle.renderFancyTracks(vehicle, f1);
105+
GL11.glPopMatrix();
106+
}
107+
108+
for(int i = 0; i < vehicle.trackLinksRight.length; i++)
109+
{
110+
AnimTrackLink link = vehicle.trackLinksRight[i];
111+
float rotZ = link.zRot;
112+
for(; rotZ > 180F; rotZ -= 360F) {}
113+
for(; rotZ <= -180F; rotZ += 360F) {}
114+
GL11.glPushMatrix();
115+
GL11.glTranslatef(link.position.x/16F, link.position.y/16F, link.position.z/16F);
116+
GL11.glRotatef(rotZ * (float)(180/Math.PI), 0, 0, 1);
117+
modVehicle.renderFancyTracks(vehicle, f1);
118+
GL11.glPopMatrix();
119+
}
120+
91121
GlStateManager.pushMatrix();
92-
if(type.turretOrigin != null && vehicle.isPartIntact(EnumDriveablePart.turret) &&
93-
vehicle.getSeat(0) != null)
122+
if(type.turretOrigin != null && vehicle.isPartIntact(EnumDriveablePart.turret) && vehicle.getSeat(0) != null)
94123
{
95124
dYaw = (vehicle.getSeat(0).looking.getYaw() - vehicle.getSeat(0).prevLooking.getYaw());
96125
while(dYaw > 180F)
@@ -110,6 +139,18 @@ public void render(EntityVehicle vehicle, double d, double d1, double d2, float
110139
if(modVehicle != null)
111140
modVehicle.renderTurret(0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F, vehicle, f1);
112141

142+
//rotate and render barrel
143+
if(modVehicle != null) {
144+
EntitySeat[] seats = vehicle.getSeats();
145+
GL11.glTranslatef(modVehicle.barrelAttach.x,modVehicle.barrelAttach.y, -modVehicle.barrelAttach.z);
146+
float bPitch = (seats[0].looking.getPitch() - seats[0].prevLooking.getPitch());
147+
float aPitch = seats[0].prevLooking.getPitch() + bPitch * f1;
148+
149+
GL11.glRotatef(-aPitch, 0F, 0F, 1F);
150+
GL11.glTranslatef(recoilPos*-(5F/16F),0F, 0F);
151+
modVehicle.renderAnimBarrel(0.0F, 0.0F, -0.1F, 0.0F, 0.0F, 0.0625F, vehicle, f1);
152+
}
153+
113154
if(FlansMod.DEBUG)
114155
{
115156
GlStateManager.translate(type.turretOrigin.x, type.turretOrigin.y, type.turretOrigin.z);

src/main/java/com/flansmod/common/driveables/EntityDriveable.java

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public abstract class EntityDriveable extends Entity implements IControllable, I
105105
/** Current gun variables for alternating weapons */
106106
public int currentGunPrimary, currentGunSecondary;
107107

108+
/** Whether each mouse button is held */
109+
public boolean leftMouseHeld = false, rightMouseHeld = false;
110+
108111
/** Angle of harvester aesthetic piece */
109112
public float harvesterAngle;
110113

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

130+
// Gun recoil
131+
public boolean isRecoil = false;
132+
public float recoilPos = 0;
133+
public float lastRecoilPos = 0;
134+
public int recoilTimer = 0;
135+
127136
public EntityDriveable(World world)
128137
{
129138
super(world);
@@ -951,6 +960,24 @@ else if(passenger instanceof EntityWheel)
951960
}
952961
}
953962

963+
//Gun recoil
964+
if(leftMouseHeld){
965+
tryRecoil();
966+
setRecoilTimer();
967+
}
968+
lastRecoilPos = recoilPos;
969+
970+
if(recoilPos > 180-(180/type.recoilTime))
971+
{
972+
recoilPos = 0;
973+
isRecoil = false;
974+
}
975+
976+
if(isRecoil)
977+
recoilPos = recoilPos + (180/type.recoilTime);
978+
979+
if(recoilTimer >= 0)
980+
recoilTimer--;
954981

955982
for(DriveablePart part : getDriveableData().parts.values())
956983
{
@@ -1078,12 +1105,17 @@ else if(EnumDriveablePart.getPart(
10781105
{
10791106
throttle *= 0.98F;
10801107
primaryShootHeld = secondaryShootHeld = false;
1081-
}
1108+
}
10821109
else if(getDriver() != null && getDriver() == getControllingPassenger())
10831110
{
10841111
reportVehicleError();
10851112
}
10861113

1114+
if(seats[0] != null && seats[0].getRidingEntity() == null)
1115+
{
1116+
rightMouseHeld = leftMouseHeld = false;
1117+
}
1118+
10871119
// Check if shooting
10881120
if(shootDelayPrimary > 0)
10891121
shootDelayPrimary--;
@@ -1175,6 +1207,40 @@ else if(FlansMod.hooks.BuildCraftLoaded && stack.isItemEqual(
11751207
}
11761208
}
11771209

1210+
public void tryRecoil()
1211+
{
1212+
int slot = -1;
1213+
DriveableType type = getDriveableType();
1214+
for(int i = driveableData.getMissileInventoryStart(); i < driveableData.getMissileInventoryStart() + type.numMissileSlots; i++)
1215+
{
1216+
ItemStack shell = driveableData.getStackInSlot(i);
1217+
if(shell != null && shell.getItem() instanceof ItemBullet && type.isValidAmmo(((ItemBullet)shell.getItem()).type, EnumWeaponType.SHELL))
1218+
{
1219+
slot = i;
1220+
}
1221+
}
1222+
1223+
if(recoilTimer <= 0 && slot != -1)
1224+
isRecoil = true;
1225+
}
1226+
1227+
public void setRecoilTimer()
1228+
{
1229+
int slot = -1;
1230+
DriveableType type = getDriveableType();
1231+
for(int i = driveableData.getMissileInventoryStart(); i < driveableData.getMissileInventoryStart() + type.numMissileSlots; i++)
1232+
{
1233+
ItemStack shell = driveableData.getStackInSlot(i);
1234+
if(shell != null && shell.getItem() instanceof ItemBullet && type.isValidAmmo(((ItemBullet)shell.getItem()).type, EnumWeaponType.SHELL))
1235+
{
1236+
slot = i;
1237+
}
1238+
}
1239+
1240+
if(recoilTimer <= 0 && slot != -1)
1241+
recoilTimer = getDriveableType().shootDelayPrimary;
1242+
}
1243+
11781244
private Vector3f getRandPosInBoundingBox(DriveablePart part)
11791245
{
11801246
// Pick a random position within the bounding box and spawn a flame there

0 commit comments

Comments
 (0)