From 31c7a70f5740bc54e092c39b8d034a7674793729 Mon Sep 17 00:00:00 2001 From: Tore Bergebakken Date: Mon, 23 Dec 2024 20:56:29 +0100 Subject: [PATCH] wip split out speedometer code --- Assets/Prefabs/GunParts/BrickBarrel.prefab | 17 +++++++++- .../InheritMomentumModifier.cs | 29 +++------------- Assets/Scripts/UI/Speedometer.cs | 33 +++++++++++++++++++ Assets/Scripts/UI/Speedometer.cs.meta | 11 +++++++ 4 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 Assets/Scripts/UI/Speedometer.cs create mode 100644 Assets/Scripts/UI/Speedometer.cs.meta diff --git a/Assets/Prefabs/GunParts/BrickBarrel.prefab b/Assets/Prefabs/GunParts/BrickBarrel.prefab index cbccbde2..4b76741d 100644 --- a/Assets/Prefabs/GunParts/BrickBarrel.prefab +++ b/Assets/Prefabs/GunParts/BrickBarrel.prefab @@ -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} @@ -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: diff --git a/Assets/Scripts/Augment/BulletModifiers/InheritMomentumModifier.cs b/Assets/Scripts/Augment/BulletModifiers/InheritMomentumModifier.cs index 8ab67220..45276fa4 100644 --- a/Assets/Scripts/Augment/BulletModifiers/InheritMomentumModifier.cs +++ b/Assets/Scripts/Augment/BulletModifiers/InheritMomentumModifier.cs @@ -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(); + 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 _) @@ -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); } diff --git a/Assets/Scripts/UI/Speedometer.cs b/Assets/Scripts/UI/Speedometer.cs new file mode 100644 index 00000000..2bbc167c --- /dev/null +++ b/Assets/Scripts/UI/Speedometer.cs @@ -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)); + } +} diff --git a/Assets/Scripts/UI/Speedometer.cs.meta b/Assets/Scripts/UI/Speedometer.cs.meta new file mode 100644 index 00000000..fa472892 --- /dev/null +++ b/Assets/Scripts/UI/Speedometer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d2b79d69bc2a48345b0833cae42a1f6f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: