Skip to content

Commit

Permalink
wip split out speedometer code
Browse files Browse the repository at this point in the history
  • Loading branch information
toberge committed Dec 23, 2024
1 parent c941bba commit 31c7a70
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
17 changes: 16 additions & 1 deletion Assets/Prefabs/GunParts/BrickBarrel.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ GameObject:
- component: {fileID: 6075777771513326584}
- component: {fileID: 4046340877857086533}
- component: {fileID: 799348183392755439}
- component: {fileID: 8663398469110200193}
- component: {fileID: 419563472744653519}
- component: {fileID: 6222283589665742002}
- component: {fileID: 1739641533833410606}
Expand Down Expand Up @@ -401,8 +402,22 @@ MonoBehaviour:
m_PostInfinity: 2
m_RotationOrder: 4
speedMultiplier: 1.5
maxSpeed: 25
projectileToAreaDamageRatio: 0.6
speedometer: {fileID: 8663398469110200193}
--- !u!114 &8663398469110200193
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8762663949210455680}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2b79d69bc2a48345b0833cae42a1f6f, type: 3}
m_Name:
m_EditorClassIdentifier:
Body: {fileID: 0}
maxSpeed: 25
speedometer: {fileID: 1500347907578133150}
--- !u!114 &419563472744653519
MonoBehaviour:
Expand Down
29 changes: 5 additions & 24 deletions Assets/Scripts/Augment/BulletModifiers/InheritMomentumModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,28 @@ public class InheritMomentumModifier : MonoBehaviour, ProjectileModifier
[SerializeField, Tooltip("The projectile speed will be *this* times faster")]
private float speedMultiplier = 1.5f;

[SerializeField]
private float maxSpeed = 25;

[Range(0, 1)]
[Tooltip("Projectile damage is this amount of total damage")]
[SerializeField]
private float projectileToAreaDamageRatio = .6f;

[SerializeField]
private MeshRenderer speedometer;
private Material speedometerMaterial;
private Speedometer speedometer;

private Rigidbody playerBody;

private const string SpeedPropertyName = "PlayerSpeed";

// TODO connect speedometer!
// TODO speedometer in separate script omg

public void Attach(ProjectileController projectile)
{
if (!projectile.GunController.Player)
return;
// TODO support AIs and network play

playerBody = projectile.GunController.Player.GetComponent<Rigidbody>();
speedometer.Body = playerBody;

projectile.OnNetworkInit += OnNetworkInit;
projectile.OnProjectileInit += OnProjectileInit;

speedometer.materials[0] = Instantiate(speedometer.materials[0]);
speedometerMaterial = speedometer.materials[0];
}

// TODO split away the speedometer stuff
private void FixedUpdate()
{
if (!speedometerMaterial)
return;
var velocity = playerBody.velocity.magnitude;
var targetValue = Mathf.Clamp(velocity, 0f, maxSpeed) / maxSpeed;
var currentValue = speedometerMaterial.GetFloat("_Value");
speedometerMaterial.SetFloat("_Value", Mathf.Lerp(currentValue, targetValue, .3f));
}

private void OnNetworkInit(ref ProjectileFireData data, GunStats _)
Expand All @@ -77,7 +59,6 @@ private void OnProjectileInit(ref ProjectileState state, GunStats stats)
var damage = state.damage * damageCurve.Evaluate(speed);
state.damage = damage * projectileToAreaDamageRatio;
state.additionalProperties[ExplosionModifier.AreaDamagePropertyName] = damage * (1 - projectileToAreaDamageRatio);
Debug.Log($"Split {damage} into {state.damage} and {state.additionalProperties[ExplosionModifier.AreaDamagePropertyName]}");
state.speed = Mathf.Max(1f, speed * speedMultiplier);
}

Expand Down
33 changes: 33 additions & 0 deletions Assets/Scripts/UI/Speedometer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;

public class Speedometer : MonoBehaviour
{
public Rigidbody Body;

[SerializeField]
private float maxSpeed = 25;

[SerializeField]
private MeshRenderer speedometer;

[SerializeField]
private int materialIndex = 0;

private Material speedometerMaterial;

private void Start()
{
speedometer.materials[materialIndex] = Instantiate(speedometer.materials[materialIndex]);
speedometerMaterial = speedometer.materials[materialIndex];
}

private void Update()
{
if (!Body || !speedometerMaterial)
return;
var velocity = Body.velocity.magnitude;
var targetValue = Mathf.Clamp(velocity, 0f, maxSpeed) / maxSpeed;
var currentValue = speedometerMaterial.GetFloat("_Value");
speedometerMaterial.SetFloat("_Value", Mathf.Lerp(currentValue, targetValue, .3f));
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/UI/Speedometer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 31c7a70

Please sign in to comment.