Add FloorVisibilityManager for dynamic floor visibility management and update material properties for transparency effects

This commit is contained in:
2025-12-13 01:35:22 +01:00
parent 189bbb7ae7
commit 7b1eb645ef
13 changed files with 348 additions and 24 deletions

View File

@@ -0,0 +1,83 @@
using System.Collections;
using System.Collections.Generic;
using Core.Domain;
using UnityEngine;
namespace Infrastructure.Unity
{
public class FloorVisibilityManager : MonoBehaviour
{
[SerializeField] private float fadeSpeed = 5f;
[SerializeField] private float hiddenAlpha = 0.1f;
[SerializeField] private float visibleAlpha = 1.0f;
private GameSession _gameSession;
private List<List<TileViewAdapter>> _floors;
private int _currentFloorIndex = -1;
public void Initialize(GameSession gameSession, List<Tile> allTiles, Dictionary<string, TileViewAdapter> tileViews, int totalFloors)
{
_gameSession = gameSession;
_floors = new List<List<TileViewAdapter>>();
for (var i = 0; i < totalFloors; i++)
{
_floors.Add(new List<TileViewAdapter>());
}
foreach (var tile in allTiles)
{
if (tileViews.TryGetValue(tile.Id, out var view))
{
// Safety check for array bounds
if (tile.Floor < _floors.Count)
{
_floors[tile.Floor].Add(view);
}
}
}
}
private void Update()
{
if (_gameSession == null) return;
// Check if player changed floors
// We read the private field _playerFloorIndex via a public getter we need to add,
// OR we just track it locally if you updated GameSession to expose it.
// Assuming GameSession doesn't expose it publically yet, let's rely on GameBootstrap passing it or just hack it:
// Ideally, GameSession should emit an event 'OnFloorChanged'.
// For now, let's assume we can get it or we passed the player reference.
}
// Call this from GameBootstrap.Update()
public void UpdateFloorVisibility(int playerFloorIndex)
{
if (_currentFloorIndex == playerFloorIndex) return;
_currentFloorIndex = playerFloorIndex;
StopAllCoroutines();
StartCoroutine(UpdateFloorsRoutine(playerFloorIndex));
}
private IEnumerator UpdateFloorsRoutine(int playerFloor)
{
for (var i = 0; i < _floors.Count; i++)
{
var targetAlpha = (i < playerFloor) ? hiddenAlpha : visibleAlpha;
if (_floors[i].Count == 0) continue;
foreach (var tile in _floors[i])
{
if (tile && tile.gameObject.activeInHierarchy)
{
tile.SetAlpha(targetAlpha);
}
}
if (i % 2 == 0) yield return null;
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f836b4958c2c40e4b631f86b35e9f63b
timeCreated: 1765585554

View File

@@ -21,6 +21,7 @@ namespace Infrastructure.Unity
[SerializeField] private CameraController cameraController;
[SerializeField] private NpcController npcPrefab;
[SerializeField] private HunterNpcController hunterNpcPrefab;
[SerializeField] private FloorVisibilityManager floorVisibilityManager;
[Header("Level Generation")]
[SerializeField] private int floorsCount = 3;
@@ -68,7 +69,17 @@ namespace Infrastructure.Unity
// Set Theme based on High Score
ThemeManager.CurrentTheme = ThemeManager.GetTheme(_gameSession.HighScore);
if (levelGenerator) levelGenerator.Generate(soundManager, _allTiles, _tileViews, cameraController, rumbleManager);
if (levelGenerator)
{
levelGenerator.Generate(soundManager, _allTiles, _tileViews, cameraController, rumbleManager);
if (!floorVisibilityManager)
{
floorVisibilityManager = gameObject.AddComponent<FloorVisibilityManager>();
}
floorVisibilityManager.Initialize(_gameSession, _allTiles, _tileViews, floorsCount);
}
SpawnDeathPlane();
SpawnPlayer();
@@ -95,11 +106,18 @@ namespace Infrastructure.Unity
if (_playerInstance)
{
var playerY = _playerInstance.transform.position.y;
var currentFloor = Mathf.RoundToInt(-playerY / floorHeightDistance);
currentFloor = Mathf.Clamp(currentFloor, 0, floorsCount - 1);
// Calculate current floor index based on Y height (inverse logic from Generator)
// Note: Generator uses negative offsets: 0, -15, -30.
// So Floor 0 is at Y=0. Floor 1 is at Y=-15.
// Math to get positive index:
var rawFloor = Mathf.RoundToInt(-playerY / floorHeightDistance);
var currentFloor = Mathf.Clamp(rawFloor, 0, floorsCount - 1);
_gameSession.SetPlayerFloor(currentFloor);
// UPDATE VISIBILITY
floorVisibilityManager.UpdateFloorVisibility(currentFloor);
}
var dt = Time.deltaTime;

View File

@@ -29,14 +29,11 @@ namespace Infrastructure.Unity
var dilation = _timeDilationProvider?.Invoke() ?? 1f;
if (IsGrounded())
{
var direction = (_target.transform.position - transform.position).normalized;
direction.y = 0;
direction.Normalize();
var direction = (_target.transform.position - transform.position).normalized;
direction.y = 0;
direction.Normalize();
rb.MovePosition(rb.position + direction * (moveSpeed * dilation * Time.fixedDeltaTime));
}
rb.MovePosition(rb.position + direction * (moveSpeed * dilation * Time.fixedDeltaTime));
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 2f, tileLayer))
{

View File

@@ -43,7 +43,7 @@ namespace Infrastructure.Unity
{
var dilation = _timeDilationProvider?.Invoke() ?? 1f;
if (IsGrounded()) rb.MovePosition(rb.position + _currentDir * (moveSpeed * dilation * Time.fixedDeltaTime));
rb.MovePosition(rb.position + _currentDir * (moveSpeed * dilation * Time.fixedDeltaTime));
if (Physics.Raycast(transform.position, Vector3.down, out var hit, 2.0f, tileLayer))
{

View File

@@ -1,5 +1,6 @@
using System;
using Core.Domain.Status;
using Core.Domain.Status.Effects;
using KBCore.Refs;
using UnityEngine;
using UnityEngine.InputSystem;
@@ -23,10 +24,16 @@ namespace Infrastructure.Unity
[SerializeField] private float groundCheckDistance = 1.5f;
[Self][SerializeField] private Rigidbody rb;
[Self][SerializeField] private MeshRenderer meshRenderer;
private InputSystem_Actions _actions;
private Vector2 _moveInput;
private Transform _camTransform;
private MaterialPropertyBlock _propBlock;
private static readonly int ColorProperty = Shader.PropertyToID("_BaseColor");
private static readonly int EmissionColorProperty = Shader.PropertyToID("_EmissionColor");
private Color _defaultColor = Color.white;
public Rigidbody Rigidbody => rb;
public StatusManager Status { get; private set; }
@@ -58,15 +65,21 @@ namespace Infrastructure.Unity
}
rb.freezeRotation = true;
// RB gravity is controlled by capabilities
_propBlock = new MaterialPropertyBlock();
if (meshRenderer.material.HasProperty(ColorProperty))
{
_defaultColor = meshRenderer.material.GetColor(ColorProperty);
}
}
private void Update()
{
Status.Tick(Time.deltaTime);
// Apply Status logic
rb.useGravity = !Status.CurrentCapabilities.CanHover;
UpdateVisuals();
}
private void FixedUpdate()
@@ -171,5 +184,40 @@ namespace Infrastructure.Unity
{
_moveInput = Vector2.zero;
}
private void UpdateVisuals()
{
var targetColor = _defaultColor;
var emissionColor = Color.black;
var caps = Status.CurrentCapabilities;
if (caps.CanHover)
{
targetColor = EffectColors.HoverColor;
emissionColor = EffectColors.HoverColor * 0.5f;
}
else if (!caps.CanTriggerDecay)
{
targetColor = EffectColors.LightFootedColor;
}
else if (caps.SpeedMultiplier > 1.2f)
{
targetColor = EffectColors.SpeedBoostColor;
emissionColor = EffectColors.SpeedBoostColor * 0.5f;
}
meshRenderer.GetPropertyBlock(_propBlock);
var currentColor = _propBlock.GetColor(ColorProperty);
if (currentColor.a == 0) currentColor = _defaultColor;
var newColor = Color.Lerp(currentColor, targetColor, Time.deltaTime * 5f);
_propBlock.SetColor(ColorProperty, newColor);
_propBlock.SetColor(EmissionColorProperty, emissionColor);
meshRenderer.SetPropertyBlock(_propBlock);
}
}
}

View File

@@ -31,6 +31,7 @@ namespace Infrastructure.Unity
private static readonly int EmissionColorProperty = Shader.PropertyToID("_EmissionColor");
private Action<TileViewAdapter> _onReturnToPool;
private float _targetAlpha = 1f;
private void Awake()
{
@@ -115,9 +116,23 @@ namespace Infrastructure.Unity
StartCoroutine(PulseScaleRoutine());
}
public void SetAlpha(float alpha)
{
_targetAlpha = alpha;
meshRenderer.GetPropertyBlock(_propBlock);
var currentColor = _propBlock.GetColor(ColorProperty);
currentColor.a = _targetAlpha;
_propBlock.SetColor(ColorProperty, currentColor);
meshRenderer.SetPropertyBlock(_propBlock);
}
private void SetColor(Color color)
{
meshRenderer.GetPropertyBlock(_propBlock);
color.a = _targetAlpha;
_propBlock.SetColor(ColorProperty, color);
meshRenderer.SetPropertyBlock(_propBlock);
}