NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using System;
using Crescive.BoolSystem;
using Crescive.ResourceSystem.UI;
using Sirenix.OdinInspector;
using UnityAtoms.BaseAtoms;
using UnityEngine;
using UnityEngine.Events;

namespace Crescive.ResourceSystem
{
[CreateAssetMenu(fileName = "Resource Channel", menuName = "Crescive/Resource System/Channels/Resource Channel")]
public class ResourceChannel : ScriptableObject
{
#region INSPECTOR FIELDS

[field: SerializeField] public StringReference TypeId { get; private set; }
[SerializeField] private ResourceAnimation defaultResourceAnimation;
[SerializeField] private TimedBoolVariable infiniteVariable;
[SerializeField] private float initialValue = 0f;

#endregion

#region PRIVATE FIELDS

private float _sessionStartValue;
private string _playerPrefsKey;

#endregion

#region PUBLIC EVENTS

[FoldoutGroup("Events")] public UnityEvent<ResourceValueEventArgs> OnSet;
[FoldoutGroup("Events")] public UnityEvent<ResourceValueEventArgs> OnIncreased;
[FoldoutGroup("Events")] public UnityEvent<ResourceValueEventArgs> OnDecreased;
[FoldoutGroup("Spend Events")] public UnityEvent<ResourceValueEventArgs> OnSpendSuccessful;
[FoldoutGroup("Spend Events")] public UnityEvent<ResourceValueEventArgs> OnSpendFailed;
[FoldoutGroup("Animation Events")] public UnityEvent<ResourceValueEventArgs> OnCoinIncreaseAnimationTriggered;
[FoldoutGroup("Animation Events")] public UnityEvent<ResourceValueEventArgs> OnCoinDecreaseAnimationTriggered;

#endregion

#region PUBLIC PROPERTIES

public ResourceAnimation DefaultResourceAnimation => defaultResourceAnimation;
public bool IsInfinite => infiniteVariable.IsActive;
public TimedBoolVariable InfiniteVariable => infiniteVariable;

public float Value
{
get => PlayerPrefs.GetFloat(_playerPrefsKey, initialValue);
private set
{
PlayerPrefs.SetFloat(_playerPrefsKey, value);
PlayerPrefs.Save();
}
}

public float SessionStartValue => _sessionStartValue;
public int IntValue => Mathf.FloorToInt(Value);
public int SessionStartIntValue => Mathf.FloorToInt(SessionStartValue);
public UnityEvent<bool> IsInfiniteChanged => infiniteVariable.IsActiveChanged;

#endregion

#region UNITY LIFECYCLE

private void OnEnable()
{
// Generate a unique key for PlayerPrefs based on TypeId
_playerPrefsKey = $"Resource_{TypeId.Value}";

// Store the value at session start
_sessionStartValue = Value;
}

#endregion

#region PUBLIC METHODS

[FoldoutGroup("Methods")]
[Button]
public void SetValue(ResourceValueEventArgs args)
{
Value = args.Value;
OnSet?.Invoke(args);
}

[FoldoutGroup("Methods")]
[Button]
public void IncreaseValue(ResourceValueEventArgs args)
{
Value += args.Value;
OnIncreased?.Invoke(args);
TriggerCoinIncreaseAnimation(args);
}

[FoldoutGroup("Methods")]
[Button]
public void DecreaseValue(ResourceValueEventArgs args)
{
if (IsInfinite)
return;

var currentValue = Value;
var newValue = currentValue - args.Value;

if (newValue < 0)
{
newValue = 0;
Debug.LogException(new Exception($"Negative decrease attempt on: {name}"));
}

Value = newValue;
OnDecreased?.Invoke(args);
TriggerCoinDecreaseAnimation(args);
}

public void SetValue(float amount) => SetValue(new ResourceValueEventArgs(amount));

public void IncreaseValue(float amount)
{
var args = new ResourceValueEventArgs(amount);
IncreaseValue(args);
}

public void DecreaseValue(float amount)
{
var args = new ResourceValueEventArgs(amount);
DecreaseValue(args);
}

public void SetValue(int amount) => SetValue((float)amount);
public void IncreaseValue(int amount) => IncreaseValue((float)amount);
public void DecreaseValue(int amount) => DecreaseValue((float)amount);

public bool CanSpendAmount(float amount) => IsInfinite || Value >= amount;

public bool TrySpend(ResourceValueEventArgs args)
{
if (IsInfinite)
{
// Debug.Log($"{TypeId} infinite. {args.Value}");
OnSpendSuccessful?.Invoke(args);
return true;
}

if (!CanSpendAmount(args.Value))
{
// Debug.Log($"{TypeId} spend failed. {args.Value}");
OnSpendFailed?.Invoke(args);
return false;
}

// Debug.Log($"{TypeId} spend successful. {args.Value}");
DecreaseValue(args);
OnSpendSuccessful?.Invoke(args);

return true;
}

public void SetIsInfinite(bool infinite) => infiniteVariable.SetIsPermanent(infinite);

public int GetTotalTransactionAmount() => IntValue;

public int GetSessionTransactionAmount()
{
var totalTransactionAmount = GetTotalTransactionAmount();
var sessionStartTransactionAmount = SessionStartIntValue;

return totalTransactionAmount - sessionStartTransactionAmount;
}

public void TriggerCoinIncreaseAnimation(ResourceValueEventArgs args) =>
OnCoinIncreaseAnimationTriggered?.Invoke(args);

public void TriggerCoinDecreaseAnimation(ResourceValueEventArgs args) =>
OnCoinDecreaseAnimationTriggered?.Invoke(args);

// Method to reset PlayerPrefs value for this resource
public void ResetValue()
{
PlayerPrefs.DeleteKey(_playerPrefsKey);
PlayerPrefs.Save();
_sessionStartValue = initialValue;
}

#endregion
}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.