using System; using System.Collections; using System.Collections.Generic; using BITFALL.Placement; using BITFALL.Player.Movement; using BITKit; using BITKit.Entities; using BITKit.StateMachine; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.InputSystem.Interactions; namespace BITFALL.Player.Equip { public interface IPlacementState:IState{} public sealed class PlacementController : BITEquipBase { [SerializeField] private Material material; [SerializeField] private LayerMask layerMask; [Header(Constant.Header.Input)] [SerializeField] private InputActionReference craftAction; [Inject] private IEntityMovement _movement; private readonly Optional allowCraft = new(); private AssetablePlacement _assetablePlacement=>Item.GetAssetable() as AssetablePlacement; public override bool IsSupportItem(IBasicItem item) { if (item is null) return false; var assetable = item.GetAssetable(); return assetable is IPlacementObject; } public override void OnAwake() { base.OnAwake(); inputActionGroup.RegisterCallback(craftAction, OnCraft); } private void OnCraft(InputAction.CallbackContext obj) { if (BITAppForUnity.AllowCursor.Allow) return; if (obj is not { interaction: PressInteraction, performed: true }) return; if (!allowCraft.Allow) return; var instance = _assetablePlacement.CreateInstance() as MonoBehaviour; instance!.transform.SetPositionAndRotation(allowCraft.Value, allowCraft.Value); } public override void OnUpdate(float deltaTime) { var startPosition = Camera.main.transform.position; var direction = Camera.main.transform.forward; var distance = 3.2f; var position = _movement.Position; var rotation = _movement.Rotation; // ReSharper disable once AssignmentInConditionalExpression if (allowCraft.Allow = Physics.Raycast(startPosition, direction, out var hit, distance, layerMask)) { position = hit.point + hit.normal * 0.32f; Debug.DrawLine(hit.point, hit.point + hit.normal, Color.magenta); rotation = _movement.Rotation; position = MathV.AlignToGrid(position, _assetablePlacement.PositionIncrement); rotation = MathQ.AlignRotation(rotation, _assetablePlacement.RotationIncrement); foreach (var x in _assetablePlacement.Object.As().GetComponentsInChildren()) { Graphics.DrawMesh(x.sharedMesh,position+rotation*x.transform.localPosition,rotation * x.transform.localRotation,material,LayerMask.NameToLayer("Default")); } allowCraft.Value = new Location(position, rotation); } else { Debug.DrawLine(startPosition, startPosition + direction * distance, Color.red); } } } }