chore: initial commit
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class BallDemoBall : MonoBehaviour
|
||||
{
|
||||
public bool HapticsEnabled = true;
|
||||
public ParticleSystem HitParticles;
|
||||
public ParticleSystem HitPusherParticles;
|
||||
public LayerMask WallMask;
|
||||
public LayerMask PusherMask;
|
||||
public MMUIShaker LogoShaker;
|
||||
public AudioSource EmphasisAudioSource;
|
||||
|
||||
protected Rigidbody2D _rigidBody;
|
||||
protected float _lastRaycastTimestamp = 0f;
|
||||
protected Animator _ballAnimator;
|
||||
protected int _hitAnimationParameter;
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
_rigidBody = this.gameObject.GetComponent<Rigidbody2D>();
|
||||
_ballAnimator = this.gameObject.GetComponent<Animator>();
|
||||
_hitAnimationParameter = Animator.StringToHash("Hit");
|
||||
}
|
||||
|
||||
protected virtual void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (WallMask == (WallMask | (1 << collision.gameObject.layer)))
|
||||
{
|
||||
HitWall();
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
float raycastLength = 5f;
|
||||
|
||||
Debug.DrawLine(this.transform.position, Vector3.down * raycastLength, Color.red);
|
||||
|
||||
if (Time.time - _lastRaycastTimestamp > 1f)
|
||||
{
|
||||
_lastRaycastTimestamp = Time.time;
|
||||
RaycastHit2D hit = Physics2D.Raycast(this.transform.position, Vector2.down, raycastLength, WallMask);
|
||||
if (hit.collider != null)
|
||||
{
|
||||
HitBottom();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void HitBottom()
|
||||
{
|
||||
_rigidBody.AddForce(Vector2.up * 2500f);
|
||||
StartCoroutine(LogoShaker.Shake(0.2f));
|
||||
}
|
||||
|
||||
protected virtual void HitWall()
|
||||
{
|
||||
float amplitude = _rigidBody.velocity.magnitude / 100f;
|
||||
HapticPatterns.PlayEmphasis(amplitude, 0.7f);
|
||||
EmphasisAudioSource.volume = amplitude;
|
||||
StartCoroutine(LogoShaker.Shake(0.2f));
|
||||
EmphasisAudioSource.Play();
|
||||
_ballAnimator.SetTrigger(_hitAnimationParameter);
|
||||
}
|
||||
|
||||
public virtual void HitPusher()
|
||||
{
|
||||
HitPusherParticles.Play();
|
||||
HapticController.fallbackPreset = HapticPatterns.PresetType.Selection;
|
||||
HapticPatterns.PlayEmphasis(0.85f, 0.05f);
|
||||
EmphasisAudioSource.volume = 0.1f;
|
||||
StartCoroutine(LogoShaker.Shake(0.2f));
|
||||
EmphasisAudioSource.Play();
|
||||
_ballAnimator.SetTrigger(_hitAnimationParameter);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59fe36bd4834fab44a9223e70f5fc73e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class BallDemoManager : DemoManager
|
||||
{
|
||||
|
||||
[Header("Ball")]
|
||||
public Vector2 Gravity = new Vector2(0, -30f);
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
Physics2D.gravity = Gravity;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 108a3476b6d855545b26c23b7e92e890
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class BallDemoWall : MonoBehaviour
|
||||
{
|
||||
protected RectTransform _rectTransform;
|
||||
protected BoxCollider2D _boxCollider2D;
|
||||
|
||||
protected virtual void OnEnable()
|
||||
{
|
||||
_rectTransform = this.gameObject.GetComponent<RectTransform>();
|
||||
_boxCollider2D = this.gameObject.GetComponent<BoxCollider2D>();
|
||||
|
||||
_boxCollider2D.size = new Vector2(_rectTransform.rect.size.x, _rectTransform.rect.size.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4615302ba7af13645aec1ad743d65dcf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class BallPusher : MonoBehaviour
|
||||
{
|
||||
public float Force = 5f;
|
||||
public BallDemoBall TargetBall;
|
||||
protected Vector2 _direction;
|
||||
|
||||
protected virtual void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
if (collider.gameObject != TargetBall.gameObject)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_direction = (collider.transform.position - this.transform.position).normalized;
|
||||
_direction.y = 1f;
|
||||
collider.attachedRigidbody.velocity = Vector2.zero;
|
||||
collider.attachedRigidbody.AddForce(_direction * Force);
|
||||
TargetBall.HitPusher();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf5e4b668334cb04282cd8a30200b42c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,73 @@
|
||||
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace Lofelt.NiceVibrations
|
||||
{
|
||||
public class BallTouchZone : MonoBehaviour, IPointerExitHandler, IPointerEnterHandler
|
||||
{
|
||||
public RenderMode ParentCanvasRenderMode { get; protected set; }
|
||||
public RectTransform BallMover;
|
||||
protected bool _holding = false;
|
||||
protected PointerEventData _pointerEventData;
|
||||
protected Vector3 _newPosition;
|
||||
protected Canvas _canvas;
|
||||
protected float _initialZPosition;
|
||||
protected Vector2 _workPosition;
|
||||
|
||||
|
||||
protected virtual void Start()
|
||||
{
|
||||
Initialization();
|
||||
}
|
||||
|
||||
protected virtual void Initialization()
|
||||
{
|
||||
ParentCanvasRenderMode = GetComponentInParent<Canvas>().renderMode;
|
||||
_canvas = GetComponentInParent<Canvas>();
|
||||
_initialZPosition = transform.position.z;
|
||||
}
|
||||
|
||||
protected virtual void Update()
|
||||
{
|
||||
if (_holding)
|
||||
{
|
||||
_newPosition = GetWorldPosition(_pointerEventData.position);
|
||||
}
|
||||
else
|
||||
{
|
||||
_newPosition = Vector3.one * 5000f;
|
||||
}
|
||||
|
||||
_newPosition.z = _initialZPosition;
|
||||
BallMover.position = _newPosition;
|
||||
}
|
||||
protected virtual Vector3 GetWorldPosition(Vector3 testPosition)
|
||||
{
|
||||
if (ParentCanvasRenderMode == RenderMode.ScreenSpaceCamera)
|
||||
{
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(_canvas.transform as RectTransform, testPosition, _canvas.worldCamera, out _workPosition);
|
||||
return _canvas.transform.TransformPoint(_workPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
return testPosition;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void OnPointerEnter(PointerEventData data)
|
||||
{
|
||||
_holding = true;
|
||||
_pointerEventData = data;
|
||||
}
|
||||
|
||||
public virtual void OnPointerExit(PointerEventData data)
|
||||
{
|
||||
_holding = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b52f664c08fd13941b0167b90d8fb49a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user