Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layered turret top rendering #2769

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace CombatExtended
{
public class TurretDrawExtension : DefModExtension
{
public GraphicData TurretBottomGraphicData;

public GraphicData TurretTopGraphicData;

public List<TurretDrawExtension_BarrelOffsetPair> Barrels = new List<TurretDrawExtension_BarrelOffsetPair>();
}

public class TurretDrawExtension_BarrelOffsetPair
{
public Vector2 BarrelOffset;
public Material BarrelMaterial;
public GraphicData BarrelGraphicData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class Building_TurretGunCE : Building_Turret
private bool everSpawned = false;
public GlobalTargetInfo globalTargetInfo = GlobalTargetInfo.Invalid;

public Material TurretTopBaseMaterial;
public Material TurretTopTopMaterial;
public List<TurretDrawExtension_BarrelOffsetPair> BarrelOffsetPairs = new List<TurretDrawExtension_BarrelOffsetPair>();

#endregion

#region Properties
Expand Down Expand Up @@ -193,6 +197,21 @@ public override void SpawnSetup(Map map, bool respawningAfterLoad) //Add ma
}
}

TurretDrawExtension turretDrawExtension = this.def.GetModExtension<TurretDrawExtension>();
if (turretDrawExtension != null)
{
TurretTopBaseMaterial = turretDrawExtension.TurretBottomGraphicData?.Graphic.MatSingle;
TurretTopTopMaterial = turretDrawExtension.TurretTopGraphicData?.Graphic.MatSingle;
if (turretDrawExtension.Barrels.Any())
{
foreach (var barrel in turretDrawExtension.Barrels)
{
TurretDrawExtension_BarrelOffsetPair barrelCache = barrel;
barrelCache.BarrelMaterial = barrel.BarrelGraphicData.Graphic.MatSingle;
BarrelOffsetPairs.Add(barrelCache);
}
}
}
// if (CompAmmo == null || CompAmmo.Props == null || CompAmmo.Props.ammoSet == null || CompAmmo.Props.ammoSet.ammoTypes.NullOrEmpty())
// return;

Expand Down Expand Up @@ -587,10 +606,55 @@ public override void Draw()
{
CE_Utility.Recoil(def.building.turretGunDef, AttackVerb, out drawOffset, out angleOffset, top.CurRotation, false);
}
top.DrawTurret(drawOffset, angleOffset);
if (TurretTopBaseMaterial != null)
{
DrawTurretComponents(TurretTopBaseMaterial);
}
if (BarrelOffsetPairs.Any())
{
for (int i = 0; i < BarrelOffsetPairs.Count(); i++)
{
if (i == AttackVerb.burstShotsLeft % BarrelOffsetPairs.Count())
{
DrawTurretComponentRecoiled(BarrelOffsetPairs[i].BarrelMaterial, BarrelOffsetPairs[i].BarrelOffset, drawOffset, angleOffset);
}
else
{
DrawTurretComponentRecoiled(BarrelOffsetPairs[i].BarrelMaterial, BarrelOffsetPairs[i].BarrelOffset, Vector3.zero, 0);
}
}
}
else
{
top.DrawTurret(drawOffset, angleOffset);
}
if (TurretTopTopMaterial != null)
{
DrawTurretComponents(TurretTopTopMaterial, true);
}
base.Draw();
}

public void DrawTurretComponents(Material mat, bool above = false)
{
Vector3 v = new Vector3(def.building.turretTopOffset.x, 0f, def.building.turretTopOffset.y).RotatedBy(top.CurRotation);
float turretTopDrawSize = def.building.turretTopDrawSize;
float num = CurrentEffectiveVerb?.AimAngleOverride ?? top.CurRotation;
Matrix4x4 matrix = default(Matrix4x4);
matrix.SetTRS(DrawPos + (above ? 2 * Altitudes.AltIncVect : Altitudes.AltIncVect / 2) + v, (-90 + num).ToQuat(), new Vector3(turretTopDrawSize, 1f, turretTopDrawSize));
Graphics.DrawMesh(MeshPool.plane10, matrix, mat, 0);
}

public void DrawTurretComponentRecoiled(Material mat, Vector2 offset, Vector3 recoilDrawOffset, float recoilAngleOffset)
{
float turretTopDrawSize = def.building.turretTopDrawSize;
Vector3 v = recoilDrawOffset;
float num = CurrentEffectiveVerb?.AimAngleOverride ?? top.CurRotation;
Matrix4x4 matrix = default(Matrix4x4);
matrix.SetTRS(DrawPos + Altitudes.AltIncVect + v + new Vector3(offset.x, 0f, offset.y).RotatedBy(top.CurRotation), (-90 + num).ToQuat(), new Vector3(turretTopDrawSize, 1f, turretTopDrawSize));
Graphics.DrawMesh(MeshPool.plane10, matrix, mat, 0);
}

public override void DrawExtraSelectionOverlays() // Draw at range less than 1.42 tiles
{
float range = this.GunCompEq.PrimaryVerb.verbProps.range;
Expand Down
Loading