Space-Smash-Out/Assets/Plugins/PrimeTween/Demo/Scripts/HighlightedElementController.cs
Jakob Feldmann 64162cb4a1 feat: whole project restructuring
This can be seen as the initial state of the project after the released demo.

The changes include:
- New ship models
- Singleton manager structure to keep project scaleable in the future
     - Managing players, their settings, character choices, statistics, match setups, controls etc. in a separate decoupled scene
- Main menu with transitions to the arena scene
- Beginnings of a custom audio solution
- Logging with Log4Net

It is really a complete overhaul of the projects structure and management.
2024-04-01 23:06:39 +02:00

65 lines
2.7 KiB
C#

#if PRIME_TWEEN_INSTALLED
using JetBrains.Annotations;
using PrimeTween;
using UnityEngine;
namespace PrimeTweenDemo {
public class HighlightedElementController : MonoBehaviour {
[SerializeField] Camera mainCamera;
[SerializeField] CameraProjectionMatrixAnimation cameraProjectionMatrixAnimation;
[CanBeNull] public HighlightableElement current { get; private set; }
void Awake() {
#if UNITY_2019_1_OR_NEWER && !PHYSICS_MODULE_INSTALLED
Debug.LogError("Please install the package needed for Physics.Raycast(): 'Package Manager/Packages/Built-in/Physics' (com.unity.modules.physics).");
#endif
}
void Update() {
if (cameraProjectionMatrixAnimation.IsAnimating) {
return;
}
if (Input.touchSupported && !Input.GetMouseButton(0)) {
SetCurrentHighlighted(null);
return;
}
var ray = mainCamera.ScreenPointToRay(Input.mousePosition);
var highlightableElement = RaycastHighlightableElement(ray);
SetCurrentHighlighted(highlightableElement);
if (current != null && Input.GetMouseButtonDown(0)) {
current.GetComponent<Animatable>().OnClick();
}
}
[CanBeNull]
static HighlightableElement RaycastHighlightableElement(Ray ray) {
#if !UNITY_2019_1_OR_NEWER || PHYSICS_MODULE_INSTALLED
// If you're seeing a compilation error on the next line, please install the package needed for Physics.Raycast(): 'Package Manager/Packages/Built-in/Physics' (com.unity.modules.physics).
return Physics.Raycast(ray, out var hit) ? hit.collider.GetComponentInParent<HighlightableElement>() : null;
#else
return null;
#endif
}
void SetCurrentHighlighted([CanBeNull] HighlightableElement newHighlighted) {
if (newHighlighted != current) {
if (current != null) {
AnimateHighlightedElement(current, false);
}
current = newHighlighted;
if (newHighlighted != null) {
AnimateHighlightedElement(newHighlighted, true);
}
}
}
static void AnimateHighlightedElement([NotNull] HighlightableElement highlightable, bool isHighlighted) {
Tween.LocalPositionZ(highlightable.highlightAnchor, isHighlighted ? 0.08f : 0, 0.3f);
foreach (var model in highlightable.models) {
Tween.MaterialColor(model.material, Shader.PropertyToID("_EmissionColor"), isHighlighted ? Color.white * 0.25f : Color.black, 0.2f, Ease.OutQuad);
}
}
}
}
#endif