You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
400 lines
13 KiB
400 lines
13 KiB
using TMPro; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
using Ability; |
|
using UnityEngine.SceneManagement; |
|
using System; |
|
|
|
#if UNITY_ANDROID || UNITY_IOS |
|
using UnityEngine.Advertisements; |
|
#endif |
|
|
|
public class CommonFunctions |
|
{ |
|
//----------------------------------------------------------------------------------------- |
|
public static void myassert(bool assert, string msg = "") |
|
{ |
|
if (!assert) |
|
{ |
|
Debug.Log(msg); |
|
} |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static SkinsInfo GetSkinInfoByType(SkinType type) |
|
{ |
|
for (int i = 0; i < GlobalsVar.gSkinsInfo.Count; ++i) |
|
{ |
|
if (type == GlobalsVar.gSkinsInfo[i].m_type) |
|
return GlobalsVar.gSkinsInfo[i]; |
|
} |
|
|
|
CommonFunctions.myassert(false); |
|
return null; |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static int GetCoeffForUpgrade(int count) |
|
{ |
|
if (count <= 10) |
|
return 1; |
|
else if (count >= 11 && count <= 20) |
|
return 2; |
|
else if (count >= 21 && count <= 30) |
|
return 3; |
|
else if (count >= 31 && count <= 40) |
|
return 4; |
|
else if (count >= 41 && count <= 50) |
|
return 5; |
|
else |
|
return 6; |
|
|
|
//int coeff = GlobalsVar.gCurrentAbRandomCount / 10; |
|
//return coeff + 1; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static void ChangeDigitsTextVal(GameObject obj, float valueTo, float time, float delay = 0f) |
|
{ |
|
TextMeshProUGUI txtLabel = obj.GetComponent<TextMeshProUGUI>(); |
|
if (!txtLabel) |
|
{ |
|
myassert(false, ""); |
|
return; |
|
} |
|
|
|
string txtStr = txtLabel.text; |
|
float valueFrom = Convert.ToInt32(txtStr); |
|
|
|
LeanTween.value(obj, valueFrom, valueTo, time).setOnUpdate( |
|
(float value) => |
|
{ |
|
int intVal = (int)value; |
|
obj.GetComponent<TextMeshProUGUI>().text = intVal.ToString(); |
|
}).setDelay(delay); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static StoreOffersInfo GetRealPurchaseBySKU(string skuName) |
|
{ |
|
foreach (string key in GlobalsVar.gStoreOffersInfo.Keys) |
|
{ |
|
if (key == skuName) |
|
return GlobalsVar.gStoreOffersInfo[key]; |
|
} |
|
|
|
myassert(false, ""); |
|
return null; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static float GetAnimLength(Animator anim, string animName) |
|
{ |
|
if (anim) |
|
{ |
|
AnimationClip[] clips = anim.runtimeAnimatorController.animationClips; |
|
foreach (AnimationClip ac in clips) |
|
{ |
|
if (ac.name == animName) |
|
return ac.length; |
|
} |
|
} |
|
|
|
myassert(false); |
|
return 0; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static string GetTimeStr(int timeSec) |
|
{ |
|
int hours = timeSec / 60 / 60; |
|
int minutes = timeSec / 60 - hours * 60; |
|
int seconds = timeSec - minutes * 60 - hours * 60 * 60; |
|
|
|
string minStr = minutes.ToString(); |
|
string secStr = seconds.ToString(); |
|
|
|
if (minutes < 10) |
|
minStr = "0" + minutes.ToString(); |
|
|
|
if (seconds < 10) |
|
secStr = "0" + seconds.ToString(); |
|
|
|
if (hours == 0) |
|
return minStr + ":" + secStr; |
|
else |
|
return hours.ToString() + ":" + minStr + ":" + secStr; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static float ClampUp(float value, float upLimit) |
|
{ |
|
if (value > upLimit) |
|
value = upLimit; |
|
|
|
return value; |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static float ClampDown(float value, float downLimit) |
|
{ |
|
if (value < downLimit) |
|
value = downLimit; |
|
|
|
return value; |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static void SetButtonText(GameObject btnObj, string txt) |
|
{ |
|
Transform textStr = btnObj.transform.Find("Text"); |
|
if (textStr) |
|
textStr.GetComponent<TextMeshProUGUI>().text = txt; |
|
else |
|
myassert(false); |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static string GetMainAbilityNameStr(MainAbilityType type) |
|
{ |
|
int textId = 0; |
|
if (type == MainAbilityType.ADD_DAMAGE) |
|
textId = 0; |
|
else if (type == MainAbilityType.ADD_ATTACK_SPEED) |
|
textId = 1; |
|
else if (type == MainAbilityType.ADD_HEALTH) |
|
textId = 2; |
|
else if (type == MainAbilityType.ADD_ARMOR_COLLISION_TRAP) |
|
textId = 3; |
|
else if (type == MainAbilityType.ADD_ARMOR_SHOT) |
|
textId = 4; |
|
else if (type == MainAbilityType.ADD_HEAL) |
|
textId = 5; |
|
else if (type == MainAbilityType.ADD_DROP_GOLD) |
|
textId = 6; |
|
else if (type == MainAbilityType.ADD_DROP_ITEMS) |
|
textId = 7; |
|
else if (type == MainAbilityType.ADD_EQUIP) |
|
textId = 8; |
|
|
|
return GlobalsVar.gGameTextMng.GetGameText(10 + textId); |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static string GetInGameAbilityNameStr(InGameAbType type) |
|
{ |
|
int textId = 0; |
|
if (type == InGameAbType.ADD_DAMAGE) |
|
textId = 0; |
|
else if (type == InGameAbType.ADD_MINOR_DAMAGE) |
|
textId = 1; |
|
else if (type == InGameAbType.ADD_ATTACK_SPEED) |
|
textId = 2; |
|
else if (type == InGameAbType.ADD_MINOR_SPEED) |
|
textId = 3; |
|
else if (type == InGameAbType.ADD_HEALTH) |
|
textId = 4; |
|
else if (type == InGameAbType.ADD_XP) |
|
textId = 5; |
|
else if (type == InGameAbType.ADD_DROP_GOLD) |
|
textId = 6; |
|
else if (type == InGameAbType.ADD_BULLET_AFTER) |
|
textId = 7; |
|
else if (type == InGameAbType.ADD_BULLET_PARALLEL) |
|
textId = 8; |
|
else if (type == InGameAbType.ADD_BULLET_45) |
|
textId = 9; |
|
else if (type == InGameAbType.ADD_BULLET_RICOCHET) |
|
textId = 10; |
|
else if (type == InGameAbType.ADD_HEADSHOT) |
|
textId = 11; |
|
|
|
return GlobalsVar.gGameTextMng.GetGameText(100 + textId); |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static int GetLinearLvlId(int stageId, int roomId) |
|
{ |
|
if (stageId - 1 == 0) |
|
return roomId; |
|
else |
|
{ |
|
int linearLvlId = GlobalsVar.gRoomByStage[0]; |
|
for (int i = 1; i < GlobalsVar.gRoomByStage.Count; ++i) |
|
{ |
|
if (i == stageId - 1) |
|
{ |
|
linearLvlId += roomId; |
|
break; |
|
} |
|
else |
|
linearLvlId += GlobalsVar.gRoomByStage[i]; |
|
} |
|
|
|
return linearLvlId; |
|
} |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static int GetStageId() |
|
{ |
|
string stageName = SceneManager.GetActiveScene().name; |
|
int idx = stageName.IndexOf("_"); |
|
stageName = stageName.Substring(0 + 5, idx - 5); |
|
|
|
return Convert.ToInt32(stageName); |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static int GetRoomId() |
|
{ |
|
string stageName = SceneManager.GetActiveScene().name; |
|
int idx = stageName.IndexOf("_"); |
|
stageName = stageName.Substring(0 + idx + 1, stageName.Length - idx - 1); |
|
|
|
return Convert.ToInt32(stageName); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public static void SetDisableButtonColor(Button button) |
|
{ |
|
ColorBlock colors = button.colors; |
|
colors.disabledColor = Color.white; |
|
button.colors = colors; |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static void WasRealPurchase(string purchaseName, string transactionId) |
|
{ |
|
StoreOffersInfo info = GetRealPurchaseBySKU(purchaseName); |
|
if (info != null) |
|
{ |
|
if (info.m_sku.Contains("coins")) |
|
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Coins, info.m_count); |
|
else if (info.m_sku.Contains("gems")) |
|
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Gems, info.m_count); |
|
|
|
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu; |
|
if (mainMenu != null) |
|
mainMenu.Repaint(); |
|
} |
|
} |
|
|
|
//------------------------------------------------------------------- |
|
public static void UpdateTimeToFreeCoins(float timeDelta) |
|
{ |
|
if (GlobalsVar.gUser == null) |
|
return; |
|
|
|
if (GlobalsVar.gCurrentTimeToFreeStoreCoins <= 0f) |
|
return; |
|
|
|
GlobalsVar.gCurrentTimeToFreeStoreCoins -= timeDelta; |
|
|
|
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu; |
|
if (mainMenu) |
|
mainMenu.UpdateShopAdsTimer(GlobalsVar.gCurrentTimeToFreeStoreCoins); |
|
|
|
if (GlobalsVar.gCurrentTimeToFreeStoreCoins <= 0f) |
|
{ |
|
GlobalsVar.gCurrentTimeToFreeStoreCoins = 0f; |
|
|
|
if (mainMenu) |
|
mainMenu.ReinitShopAdsObj(); |
|
} |
|
} |
|
|
|
//------------------------------------------------------------------- |
|
public static void StartShowAd() |
|
{ |
|
#if UNITY_IOS || UNITY_ANDROID |
|
const string RewardedPlacementId = "rewardedVideo"; |
|
var options = new ShowOptions { resultCallback = HandleShowResult }; |
|
Advertisement.Show(RewardedPlacementId, options); |
|
#endif |
|
} |
|
|
|
#if UNITY_IOS || UNITY_ANDROID |
|
//------------------------------------------------------------------- |
|
private static void HandleShowResult(ShowResult result) |
|
{ |
|
switch (result) |
|
{ |
|
case ShowResult.Finished: |
|
GameObject afterDeathDlg = GlobalsVar.gBoard.GetDialogByType(DialogType.AFTER_DEATH_DIALOG); |
|
if (afterDeathDlg) |
|
{ |
|
afterDeathDlg.GetComponent<Dialogs.AfterDeathDialog>().HideDialog(); |
|
GameBoard.Instance.MainCharacter.Reload(); |
|
break; |
|
} |
|
|
|
GameObject energyDlg = GlobalsVar.gBoard.GetDialogByType(DialogType.NOT_ENOUGH_ENERGY); |
|
if (energyDlg) |
|
{ |
|
energyDlg.GetComponent<NotEnoughEnergyDialog>().HideDialog(); |
|
GlobalsVar.gUser.IncEnergy(5); |
|
break; |
|
} |
|
|
|
GameObject slotDlg = GlobalsVar.gBoard.GetDialogByType(DialogType.SLOT_MACHINE); |
|
if (slotDlg) |
|
{ |
|
slotDlg.GetComponent<Dialogs.SlotMachineDialog>().ChangeSpinButtonSprite(); |
|
break; |
|
} |
|
|
|
MainMenu mainMenu = GlobalsVar.gBoard as MainMenu; |
|
if (mainMenu) |
|
{ |
|
GlobalsVar.gCurrentTimeToFreeStoreCoins = GlobalsVar.gTimeToFreeStoreCoins; |
|
|
|
GlobalsVar.gUser.IncCurrency(Shop.ShopType.Coins, GlobalsVar.gAdsCoinsCount); |
|
mainMenu.ReinitShopAdsObj(); |
|
mainMenu.Repaint(); |
|
} |
|
|
|
break; |
|
case ShowResult.Skipped: |
|
Debug.Log("The ad was skipped before reaching the end."); |
|
break; |
|
case ShowResult.Failed: |
|
Debug.LogError("The ad failed to be shown."); |
|
break; |
|
} |
|
} |
|
#endif |
|
|
|
//------------------------------------------------------------------ |
|
public static bool IsRewardedAdCanBeShown() |
|
{ |
|
#if UNITY_IOS || UNITY_ANDROID |
|
const string RewardedPlacementId = "rewardedVideo"; |
|
if (Advertisement.IsReady(RewardedPlacementId)) |
|
{ |
|
return true; |
|
} |
|
#endif |
|
|
|
return false; |
|
} |
|
|
|
//----------------------------------------------------------------------------------------- |
|
public static void CheckTestKeyPressed() |
|
{ |
|
if (Input.GetKeyDown("f12")) |
|
{ |
|
#if UNITY_EDITOR |
|
string filePath = UnityEditor.EditorUtility.OpenFilePanel("", Application.dataPath + "/Scenes/Stages/", "unity"); |
|
if (filePath.Length != 0) |
|
{ |
|
int idx = filePath.IndexOf("Stages"); |
|
filePath = filePath.Substring(idx); |
|
filePath = filePath.Substring(0, filePath.Length - 6); |
|
|
|
SceneManager.LoadScene("Scenes/" + filePath); |
|
} |
|
#endif |
|
} |
|
} |
|
}
|
|
|