Initial commit: Unity WordConnect project
This commit is contained in:
@ -0,0 +1,69 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using WordsToolkit.Scripts.Data;
|
||||
using WordsToolkit.Scripts.Gameplay.Managers;
|
||||
using WordsToolkit.Scripts.Settings;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons
|
||||
{
|
||||
public class BaseGUIButton : CustomButton, IFadeable
|
||||
{
|
||||
protected LevelManager levelManager;
|
||||
protected GameSettings gameSettings;
|
||||
protected ResourceManager resourceManager;
|
||||
protected ButtonViewController buttonViewController;
|
||||
public RectTransform rectTransform;
|
||||
public Vector2 savePosition;
|
||||
public Vector2 targetPosition;
|
||||
|
||||
[Inject]
|
||||
public void Construct(LevelManager levelManager, GameSettings gameSettings, ResourceManager resourceManager, ButtonViewController buttonViewController)
|
||||
{
|
||||
this.gameSettings = gameSettings;
|
||||
this.levelManager = levelManager;
|
||||
this.resourceManager = resourceManager;
|
||||
this.buttonViewController = buttonViewController;
|
||||
this.buttonViewController.RegisterButton(this);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
animator.enabled = false;
|
||||
rectTransform.DOAnchorPos( targetPosition, 0.5f);
|
||||
}
|
||||
|
||||
public void InstantHide()
|
||||
{
|
||||
rectTransform.anchoredPosition = targetPosition;
|
||||
}
|
||||
|
||||
public void HideForWin()
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
animator.enabled = true;
|
||||
rectTransform.DOAnchorPos(savePosition, 0.5f).OnComplete(ShowCallback);
|
||||
}
|
||||
|
||||
protected virtual void ShowCallback()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eae64b3976cb4849bb325653a674bfd0
|
||||
timeCreated: 1748758607
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4baa2b47c0445f7837eea134618de67
|
||||
timeCreated: 1748527950
|
||||
@ -0,0 +1,139 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using System.Linq;
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using WordsToolkit.Scripts.Data;
|
||||
using WordsToolkit.Scripts.System;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons.Boosts
|
||||
{
|
||||
public abstract class BaseBoostButton : BaseGUIButton
|
||||
{
|
||||
public ResourceObject resourceToPay;
|
||||
public ResourceObject resourseToHoldBoost;
|
||||
public TextMeshProUGUI price;
|
||||
public TextMeshProUGUI countText;
|
||||
public GameObject priceObject;
|
||||
public GameObject countTextObject;
|
||||
protected int count;
|
||||
|
||||
[SerializeField]
|
||||
private ParticleSystem waves;
|
||||
|
||||
private CanvasGroup canvasGroup;
|
||||
private bool isActive;
|
||||
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
InitializePrice();
|
||||
UpdatePriceDisplay();
|
||||
onClick.AddListener(OnClick);
|
||||
var main = waves.main;
|
||||
main.prewarm = true; // Start particles in grown state
|
||||
main.loop = true;
|
||||
waves.Stop(); // Ensure it's not auto-playing
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
onClick.RemoveListener(OnClick);
|
||||
}
|
||||
|
||||
protected abstract void InitializePrice();
|
||||
|
||||
public virtual void UpdatePriceDisplay()
|
||||
{
|
||||
// If we have resources in the hold boost, show that count
|
||||
if (resourseToHoldBoost != null && resourseToHoldBoost.GetValue() > 0)
|
||||
{
|
||||
countTextObject.gameObject.SetActive(true);
|
||||
priceObject.gameObject.SetActive(false);
|
||||
countText.text = resourseToHoldBoost.GetValue().ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
countTextObject.gameObject.SetActive(false);
|
||||
priceObject.gameObject.SetActive(true);
|
||||
price.text = count.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
protected void OnClick()
|
||||
{
|
||||
if (isActive)
|
||||
{
|
||||
Refund();
|
||||
DeactivateBoost();
|
||||
return;
|
||||
}
|
||||
if (resourceManager.Consume(resourseToHoldBoost, 1))
|
||||
{
|
||||
ActivateBoost();
|
||||
}
|
||||
// If not, consume from the regular resource
|
||||
else if (resourceManager.ConsumeWithEffects(resourceToPay, count))
|
||||
{
|
||||
resourseToHoldBoost.Add(gameSettings.countOfBoostsToBuy);
|
||||
UpdatePriceDisplay();
|
||||
}
|
||||
}
|
||||
|
||||
private void Refund()
|
||||
{
|
||||
resourseToHoldBoost.Add(1);
|
||||
UpdatePriceDisplay();
|
||||
}
|
||||
|
||||
protected virtual void ActivateBoost(bool hideButtons = true)
|
||||
{
|
||||
UpdatePriceDisplay();
|
||||
if(hideButtons)
|
||||
buttonViewController.HideOtherButtons(this);
|
||||
PulseAnimation();
|
||||
waves.Play();
|
||||
isActive = true;
|
||||
priceObject.SetActive(false);
|
||||
countTextObject.SetActive(false);
|
||||
}
|
||||
protected virtual void DeactivateBoost()
|
||||
{
|
||||
isActive = false;
|
||||
buttonViewController.ShowButtons();
|
||||
waves.Clear();
|
||||
waves.Stop();
|
||||
DOTween.Complete(transform);
|
||||
DOTween.Kill(transform);
|
||||
transform.localScale = Vector3.one;
|
||||
UpdatePriceDisplay();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void PulseAnimation()
|
||||
{
|
||||
animator.enabled = false;
|
||||
transform.DOScale(Vector3.one * 0.9f, 0.5f)
|
||||
.SetLoops(-1, LoopType.Yoyo)
|
||||
.SetEase(Ease.InOutSine);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48df3644dc80e4cb9bcf75be337f8fc4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,49 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using WordsToolkit.Scripts.Enums;
|
||||
using WordsToolkit.Scripts.Gameplay;
|
||||
using WordsToolkit.Scripts.System;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons.Boosts
|
||||
{
|
||||
public class HammerBoostButton : BaseBoostButton
|
||||
{
|
||||
protected override void InitializePrice()
|
||||
{
|
||||
count = gameSettings.hammerBoostPrice;
|
||||
UpdatePriceDisplay();
|
||||
}
|
||||
|
||||
protected override void ActivateBoost(bool hideButtons = true)
|
||||
{
|
||||
base.ActivateBoost(hideButtons);
|
||||
if (levelManager != null && !levelManager.hammerMode)
|
||||
{
|
||||
levelManager.hammerMode = true;
|
||||
EventManager.GetEvent<Tile>(EGameEvent.TileSelected).Subscribe(OnTileSelected);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTileSelected(Tile obj)
|
||||
{
|
||||
DeactivateBoost();
|
||||
}
|
||||
|
||||
protected override void DeactivateBoost()
|
||||
{
|
||||
levelManager.hammerMode = false;
|
||||
EventManager.GetEvent<Tile>(EGameEvent.TileSelected).Unsubscribe(OnTileSelected);
|
||||
base.DeactivateBoost();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cafdd253e27364743a17c8c205d36c4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,37 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using WordsToolkit.Scripts.Gameplay.Managers;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons.Boosts
|
||||
{
|
||||
public class TipBoostButton : BaseBoostButton
|
||||
{
|
||||
protected override void InitializePrice()
|
||||
{
|
||||
count = gameSettings.hintBoostPrice;
|
||||
UpdatePriceDisplay();
|
||||
}
|
||||
|
||||
protected override void ActivateBoost(bool hideButtons = true)
|
||||
{
|
||||
base.ActivateBoost(false);
|
||||
FindObjectOfType<FieldManager>().OpenRandomTile();
|
||||
DeactivateBoost();
|
||||
}
|
||||
|
||||
protected override void DeactivateBoost()
|
||||
{
|
||||
base.DeactivateBoost();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8c9628c9e141543b6a064c5d3cb68180
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -0,0 +1,88 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons
|
||||
{
|
||||
public interface IShowable
|
||||
{
|
||||
void Show();
|
||||
}
|
||||
|
||||
public interface IHideable
|
||||
{
|
||||
void Hide();
|
||||
void InstantHide();
|
||||
}
|
||||
|
||||
public interface IHideableForWin
|
||||
{
|
||||
void HideForWin();
|
||||
}
|
||||
|
||||
public interface IFadeable : IShowable, IHideable{}
|
||||
|
||||
public class ButtonViewController
|
||||
{
|
||||
private HashSet<IShowable> buttons;
|
||||
|
||||
public void RegisterButton(IShowable button)
|
||||
{
|
||||
if (buttons == null)
|
||||
{
|
||||
buttons = new HashSet<IShowable>();
|
||||
}
|
||||
buttons.Add(button);
|
||||
if (button is IHideable hideable)
|
||||
{
|
||||
hideable.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
public void HideOtherButtons(IShowable except)
|
||||
{
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
if (!ReferenceEquals(button, except) && button is IHideable hideable)
|
||||
{
|
||||
hideable.Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ShowButtons()
|
||||
{
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
button.Show();
|
||||
}
|
||||
}
|
||||
|
||||
public void HideAllForWin()
|
||||
{
|
||||
if (buttons == null) return;
|
||||
|
||||
foreach (var button in buttons)
|
||||
{
|
||||
if (button is IHideableForWin buttonForWin)
|
||||
{
|
||||
buttonForWin.HideForWin();
|
||||
}
|
||||
else if(button is IHideable hideable)
|
||||
{
|
||||
hideable.Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 717db02c9e994b8ea966b55ece00dc54
|
||||
timeCreated: 1748601128
|
||||
@ -0,0 +1,116 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
using VContainer;
|
||||
using WordsToolkit.Scripts.Audio;
|
||||
using WordsToolkit.Scripts.Enums;
|
||||
using WordsToolkit.Scripts.System;
|
||||
using WordsToolkit.Scripts.System.Haptic;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons
|
||||
{
|
||||
[RequireComponent(typeof(Animator))]
|
||||
public class CustomButton : Button
|
||||
{
|
||||
public AudioClip overrideClickSound;
|
||||
public RuntimeAnimatorController overrideAnimatorController;
|
||||
private bool isClicked;
|
||||
private readonly float cooldownTime = .5f; // Cooldown time in seconds
|
||||
public new ButtonClickedEvent onClick;
|
||||
private new Animator animator;
|
||||
public bool noSound;
|
||||
|
||||
private static bool blockInput;
|
||||
|
||||
public static CustomButton latestClickedButton;
|
||||
private IAudioService audioService;
|
||||
[Inject]
|
||||
public void Construct(IAudioService audioService)
|
||||
{
|
||||
this.audioService = audioService;
|
||||
}
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
isClicked = false;
|
||||
// run only in runtime
|
||||
if (Application.isEditor)
|
||||
{
|
||||
return;
|
||||
}
|
||||
base.OnEnable();
|
||||
animator = GetComponent<Animator>();
|
||||
if (overrideAnimatorController != null)
|
||||
{
|
||||
animator.runtimeAnimatorController = overrideAnimatorController;
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnPointerClick(PointerEventData eventData)
|
||||
{
|
||||
if (blockInput || isClicked || !interactable)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (transition != Transition.Animation)
|
||||
{
|
||||
Pressed();
|
||||
}
|
||||
|
||||
isClicked = true;
|
||||
if(!noSound)
|
||||
audioService.PlayClick(overrideClickSound);
|
||||
HapticFeedback.TriggerHapticFeedback(HapticFeedback.HapticForce.Light);
|
||||
// Start cooldown
|
||||
if (gameObject.activeInHierarchy)
|
||||
{
|
||||
StartCoroutine(Cooldown());
|
||||
}
|
||||
|
||||
base.OnPointerClick(eventData);
|
||||
}
|
||||
|
||||
public void Pressed()
|
||||
{
|
||||
if (blockInput || !interactable)
|
||||
{
|
||||
return;
|
||||
}
|
||||
latestClickedButton = this;
|
||||
onClick?.Invoke();
|
||||
EventManager.GetEvent<CustomButton>(EGameEvent.ButtonClicked).Invoke(this);
|
||||
}
|
||||
|
||||
private IEnumerator Cooldown()
|
||||
{
|
||||
yield return new WaitForSeconds(cooldownTime);
|
||||
isClicked = false;
|
||||
}
|
||||
|
||||
|
||||
private bool IsAnimationPlaying()
|
||||
{
|
||||
var stateInfo = animator.GetCurrentAnimatorStateInfo(0);
|
||||
return stateInfo.loop || stateInfo.normalizedTime < 1;
|
||||
}
|
||||
|
||||
public static void BlockInput(bool block)
|
||||
{
|
||||
blockInput = block;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c9659348afa04bc6bb30eb6900f94b65
|
||||
timeCreated: 1725694504
|
||||
@ -0,0 +1,47 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons
|
||||
{
|
||||
public class ExtraWordsButton : BaseGUIButton
|
||||
{
|
||||
private Coroutine occasionalPulseCoroutine;
|
||||
private bool pulseEnabled;
|
||||
private bool animated;
|
||||
|
||||
public void PulseAnimation(bool b)
|
||||
{
|
||||
pulseEnabled = b;
|
||||
if (b)
|
||||
{
|
||||
animator.Play($"PulseLoop");
|
||||
}
|
||||
else
|
||||
{
|
||||
animator.SetTrigger($"Pulse");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override void ShowCallback()
|
||||
{
|
||||
base.ShowCallback();
|
||||
if (pulseEnabled)
|
||||
{
|
||||
PulseAnimation(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c3f8f7c7b8644ec9c726828e284a7dd
|
||||
timeCreated: 1748687127
|
||||
208
Assets/WordConnectGameToolkit/Scripts/GUI/Buttons/GiftButton.cs
Normal file
208
Assets/WordConnectGameToolkit/Scripts/GUI/Buttons/GiftButton.cs
Normal file
@ -0,0 +1,208 @@
|
||||
// // ©2015 - 2025 Candy Smith
|
||||
// // All rights reserved
|
||||
// // Redistribution of this software is strictly not allowed.
|
||||
// // Copy of this software can be obtained from unity asset store only.
|
||||
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// // THE SOFTWARE.
|
||||
|
||||
using DG.Tweening;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using VContainer;
|
||||
using WordsToolkit.Scripts.Data;
|
||||
using WordsToolkit.Scripts.Enums;
|
||||
using WordsToolkit.Scripts.Popups;
|
||||
using WordsToolkit.Scripts.Settings;
|
||||
using WordsToolkit.Scripts.System;
|
||||
|
||||
namespace WordsToolkit.Scripts.GUI.Buttons
|
||||
{
|
||||
public class GiftButton : BaseGUIButton
|
||||
{
|
||||
private const string SAVE_KEY = "GiftButton_CollectedItems";
|
||||
|
||||
[Header("Counter Settings")]
|
||||
[SerializeField] private TextMeshProUGUI counterText;
|
||||
[SerializeField] private GameObject counterContainer;
|
||||
[SerializeField] private int collectedItems = 0;
|
||||
|
||||
[Header("Visual Feedback")]
|
||||
[SerializeField] private float pulseScale = 1.2f;
|
||||
[SerializeField] private float pulseDuration = 0.3f;
|
||||
|
||||
[Header("Resources")]
|
||||
[SerializeField] private ResourceObject resource;
|
||||
[Tooltip("Optional override for gems awarded per gift (if not set, uses value from GameSettings)")]
|
||||
[SerializeField] private int gemsPerGift = 0;
|
||||
[SerializeField] private TextMeshProUGUI gemsValueText;
|
||||
[SerializeField] private GameObject gemsLabelObject;
|
||||
|
||||
[Header("Visual Appearance")]
|
||||
[SerializeField] private Image mainBoxSprite;
|
||||
[SerializeField] private Sprite lightModeSprite;
|
||||
[SerializeField] private Sprite darkModeSprite;
|
||||
|
||||
[Inject]
|
||||
private MenuManager menuManager;
|
||||
|
||||
[SerializeField]
|
||||
private Popup giftPopup;
|
||||
|
||||
[Inject]
|
||||
private GameManager gameManager;
|
||||
|
||||
|
||||
|
||||
private bool isActive;
|
||||
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
EventManager.GetEvent(EGameEvent.SpecialItemCollected).Subscribe(OnSpecialItemCollected);
|
||||
onClick.AddListener(ConsumeResource);
|
||||
}
|
||||
|
||||
protected override void OnDisable()
|
||||
{
|
||||
base.OnDisable();
|
||||
EventManager.GetEvent(EGameEvent.SpecialItemCollected).Unsubscribe(OnSpecialItemCollected);
|
||||
onClick.RemoveListener(ConsumeResource);
|
||||
}
|
||||
|
||||
protected override void Start()
|
||||
{
|
||||
base.Start();
|
||||
if(!Application.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Load saved collected items
|
||||
collectedItems = PlayerPrefs.GetInt(SAVE_KEY, 0);
|
||||
|
||||
UpdateCounterDisplay();
|
||||
UpdateGemsValueDisplay();
|
||||
UpdateState();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the displayed gems value
|
||||
/// </summary>
|
||||
private void UpdateGemsValueDisplay()
|
||||
{
|
||||
if (gemsValueText != null)
|
||||
{
|
||||
int gemsAmount = gemsPerGift > 0 ?
|
||||
gemsPerGift :
|
||||
gameSettings.gemsForGift;
|
||||
|
||||
gemsValueText.text = gemsAmount.ToString();
|
||||
}
|
||||
|
||||
// Update visibility of gems label
|
||||
if (gemsLabelObject != null)
|
||||
{
|
||||
gemsLabelObject.SetActive(collectedItems > 0);
|
||||
}
|
||||
else if (gemsValueText != null)
|
||||
{
|
||||
// If no specific label object is assigned, control the text object directly
|
||||
gemsValueText.gameObject.SetActive(collectedItems > 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the visual state of the button based on collection status
|
||||
/// </summary>
|
||||
private void UpdateState()
|
||||
{
|
||||
isActive = collectedItems > 0;
|
||||
interactable = isActive;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increments the counter when a special item is collected
|
||||
/// </summary>
|
||||
public void CollectItem()
|
||||
{
|
||||
collectedItems++;
|
||||
// Save the updated count
|
||||
PlayerPrefs.SetInt(SAVE_KEY, collectedItems);
|
||||
PlayerPrefs.Save();
|
||||
|
||||
UpdateCounterDisplay();
|
||||
UpdateGemsValueDisplay();
|
||||
UpdateState();
|
||||
PlayCollectionFeedback();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the counter UI text
|
||||
/// </summary>
|
||||
private void UpdateCounterDisplay()
|
||||
{
|
||||
if (counterText != null)
|
||||
{
|
||||
counterText.text = collectedItems.ToString();
|
||||
}
|
||||
|
||||
// Show counter only if we've collected items
|
||||
if (counterContainer != null)
|
||||
{
|
||||
counterContainer.SetActive(collectedItems > 0);
|
||||
}
|
||||
}
|
||||
|
||||
private void ConsumeResource()
|
||||
{
|
||||
// Get the gems amount from GameSettings or use the override if set
|
||||
int gemsAmount = gemsPerGift > 0 ?
|
||||
gemsPerGift :
|
||||
gameSettings.gemsForGift;
|
||||
|
||||
// Attempt to consume the resource and add gems
|
||||
if (resource != null && resourceManager.ConsumeWithEffects(resource,gemsAmount))
|
||||
{
|
||||
// Decrease the counter
|
||||
collectedItems--;
|
||||
// Save the updated count
|
||||
PlayerPrefs.SetInt(SAVE_KEY, collectedItems);
|
||||
PlayerPrefs.Save();
|
||||
|
||||
// Update the display
|
||||
UpdateCounterDisplay();
|
||||
UpdateGemsValueDisplay();
|
||||
UpdateState();
|
||||
|
||||
menuManager.ShowPopup(giftPopup);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Play visual feedback when item is collected
|
||||
/// </summary>
|
||||
private void PlayCollectionFeedback()
|
||||
{
|
||||
// Simple pulse animation
|
||||
mainBoxSprite.transform.DOScale(pulseScale, pulseDuration / 2)
|
||||
.SetEase(Ease.OutQuad)
|
||||
.OnComplete(() => {
|
||||
mainBoxSprite.transform.DOScale(1f, pulseDuration / 2).SetEase(Ease.InQuad);
|
||||
});
|
||||
}
|
||||
|
||||
// Event handler for when a special item is collected
|
||||
private void OnSpecialItemCollected()
|
||||
{
|
||||
CollectItem();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d3677e4b5b644088ad9a552da7f8f1b
|
||||
timeCreated: 1742478154
|
||||
Reference in New Issue
Block a user