DNP_2DDemo.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if ENABLE_INPUT_SYSTEM && DNP_NewInputSystem
  5. using UnityEngine.InputSystem;
  6. #endif
  7. namespace DamageNumbersPro.Demo
  8. {
  9. public class DNP_2DDemo : MonoBehaviour
  10. {
  11. float nextShotTime;
  12. void Start()
  13. {
  14. nextShotTime = 0;
  15. Cursor.visible = true;
  16. Cursor.lockState = CursorLockMode.None;
  17. }
  18. void Update()
  19. {
  20. HandleShooting();
  21. }
  22. void HandleShooting()
  23. {
  24. if (DNP_InputHandler.GetLeftClick())
  25. {
  26. Shoot();
  27. nextShotTime = Time.time + 0.3f;
  28. }
  29. else if (DNP_InputHandler.GetRightHeld() && Time.time > nextShotTime)
  30. {
  31. Shoot();
  32. nextShotTime = Time.time + 0.06f;
  33. }
  34. }
  35. void Shoot()
  36. {
  37. Vector2 mousePosition = Vector2.zero;
  38. #if ENABLE_INPUT_SYSTEM && DNP_NewInputSystem
  39. if (Mouse.current != null) {
  40. mousePosition = Mouse.current.position.ReadValue();
  41. }
  42. #else
  43. mousePosition = Input.mousePosition;
  44. #endif
  45. // Raycast
  46. Vector3 worldPosition = Camera.main.ScreenToWorldPoint(mousePosition);
  47. worldPosition.z = -5;
  48. RaycastHit hit;
  49. Physics.Raycast(worldPosition, Vector3.forward, out hit, 10f);
  50. // Select Damage Number
  51. DNP_PrefabSettings settings = DNP_DemoManager.instance.GetSettings();
  52. DamageNumber prefab = DNP_DemoManager.instance.GetCurrent();
  53. // Number
  54. float number = 1 + Mathf.Pow(Random.value, 2.2f) * settings.numberRange;
  55. if (prefab.digitSettings.decimals == 0)
  56. {
  57. number = Mathf.Floor(number);
  58. }
  59. // Create Damage Number
  60. DamageNumber newDamageNumber = prefab.Spawn(worldPosition, number);
  61. if (hit.collider != null)
  62. {
  63. DNP_Target dnpTarget = hit.collider.GetComponent<DNP_Target>();
  64. if(dnpTarget != null)
  65. {
  66. dnpTarget.Hit();
  67. }
  68. newDamageNumber.SetFollowedTarget(hit.collider.transform);
  69. }
  70. // Apply Demo Settings
  71. settings.Apply(newDamageNumber);
  72. }
  73. }
  74. }