1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
| using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;
using System;
public class Game : MonoBehaviour
{
[Header("Текст, отвечающий за отображение денег")]
public Text scoreText;
[Header("Магазин")]
public List<Item> shopItems = new List<Item>();
[Header("Текст на кнопках товаров")]
public Text[] shopItemsText;
[Header("Кнопки товаров")]
public Button[] shopBttns;
[Header("Панелька магазина")]
public GameObject shopPan;
[Header("Top")]
public GameObject Top;
private int score; //Игровая валюта
private int scoreIncrease = 1; //Бонус при клике
private Save sv = new Save();
private void Awake()
{
if (PlayerPrefs.HasKey("SV"))
{
int totalBonusPerSec = 0;
sv = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("SV"));
score = sv.score;
for (int i = 0; i < shopItems.Count; i++)
{
shopItems[i].levelOfItem = sv.levelOfItem[i];
shopItems[i].bonusCounter = sv.bonusCounter[i];
if (shopItems[i].needCostMultiplier) shopItems[i].cost *= (int)Mathf.Pow(shopItems[i].costMultiplier, shopItems[i].levelOfItem);
if (shopItems[i].levelOfItem != 0) scoreIncrease += (int)Mathf.Pow(shopItems[i].bonusIncrease, shopItems[i].levelOfItem);
totalBonusPerSec += shopItems[i].bonusPerSec * shopItems[i].bonusCounter;
}
DateTime dt = new DateTime(sv.date[0], sv.date[1], sv.date[2], sv.date[3], sv.date[4], sv.date[5]);
TimeSpan ts = DateTime.Now - dt;
int offlineBonus = (int)ts.TotalSeconds * totalBonusPerSec;
score += offlineBonus;
print("Ваши рабочии заработали:" + offlineBonus + "$");
}
}
private void Start()
{
updateCosts(); //Обновить текст с ценами
StartCoroutine(BonusPerSec()); //Запустить просчёт бонуса в секунду
}
private void Update()
{
scoreText.text = score + "$";//Отображаем деньги
}
public void BuyBttn(int index) //Метод при нажатии на кнопку покупки товара (индекс товара)
{
int cost = shopItems[index].cost * shopItems[shopItems[index].itemIndex].bonusCounter; //Рассчитываем цену в зависимости от кол-ва рабочих (к примеру)
if (shopItems[index].itsBonus && score >= cost) //Если товар нажатой кнопки - это бонус, и денег >= цены(е)
{
if (cost > 0) // Если цена больше чем 0, то:
{
score -= cost; // Вычитаем цену из денег
StartCoroutine(BonusTimer(shopItems[index].timeOfBonus, index)); //Запускаем бонусный таймер
}
else print("Нечего улучшать то!"); // Иначе выводим в консоль текст.
}
else if (score >= shopItems[index].cost) // Иначе, если товар нажатой кнопки - это не бонус, и денег >= цена
{
if (shopItems[index].itsItemPerSec) shopItems[index].bonusCounter++; // Если нанимаем рабочего (к примеру), то прибавляем кол-во рабочих
else scoreIncrease += shopItems[index].bonusIncrease; // Иначе бонусу при клике добавляем бонус товара
score -= shopItems[index].cost; // Вычитаем цену из денег
if (shopItems[index].needCostMultiplier) shopItems[index].cost *= shopItems[index].costMultiplier; // Если товару нужно умножить цену, то умножаем на множитель
shopItems[index].levelOfItem++; // Поднимаем уровень предмета на 1
}
else print("Не хватает денег!"); // Иначе если 2 проверки равны false, то выводим в консоль текст.
updateCosts(); //Обновить текст с ценами
}
private void updateCosts() // Метод для обновления текста с ценами
{
for (int i = 0; i < shopItems.Count; i++) // Цикл выполняется, пока переменная i < кол-ва товаров
{
if (shopItems[i].itsBonus) // Если товар является бонусом, то:
{
int cost = shopItems[i].cost * shopItems[shopItems[i].itemIndex].bonusCounter; // Рассчитываем цену в зависимости от кол-ва рабочих (к примеру)
shopItemsText[i].text = shopItems[i].name + "\n" + cost + "$"; // Обновляем текст кнопки с рассчитанной ценой
}
else shopItemsText[i].text = shopItems[i].name + "\n" + shopItems[i].cost + "$"; // Иначе если товар не является бонусом, то обновляем текст кнопки с обычной ценой
}
}
IEnumerator BonusPerSec() // Бонус в секунду
{
while (true) // Бесконечный цикл
{
for (int i = 0; i < shopItems.Count; i++) score += (shopItems[i].bonusCounter * shopItems[i].bonusPerSec); // Добавляем к игровой валюте бонус рабочих (к примеру)
yield return new WaitForSeconds(1); // Делаем задержку в 1 секунду
}
}
IEnumerator BonusTimer(float time, int index) // Бонусный таймер (длительность бонуса (в сек.),индекс товара)
{
shopBttns[index].interactable = false; // Выключаем кликабельность кнопки бонуса
shopItems[shopItems[index].itemIndex].bonusPerSec *= 2; // Удваиваем бонус рабочих в секунду (к примеру)
yield return new WaitForSeconds(time); // Делаем задержку на столько секунд, сколько указали в параметре
shopItems[shopItems[index].itemIndex].bonusPerSec /= 2; // Возвращаем бонус в нормальное состояние
shopBttns[index].interactable = true; // Включаем кликабельность кнопки бонуса
}
#if UNITY_ANDROID && !UNITY_EDITOR
private void OnApplicationPause()
{
sv.score = score;
sv.levelOfItem = new int[shopItems.Count];
sv.bonusCounter = new int[shopItems.Count];
for (int i = 0; i < shopItems.Count; i++)
{
sv.levelOfItem[i] = shopItems[i].levelOfItem;
sv.bonusCounter[i] = shopItems[i].bonusCounter;
}
PlayerPrefs.SetString("SV", JsonUtility.ToJson(sv));
PlayerPrefs.Save();
}
#else
private void OnApplicationQuit()
{
sv.score = score;
sv.levelOfItem = new int[shopItems.Count];
sv.bonusCounter = new int[shopItems.Count];
for (int i = 0; i < shopItems.Count; i++)
{
sv.levelOfItem[i] = shopItems[i].levelOfItem;
sv.bonusCounter[i] = shopItems[i].bonusCounter;
}
sv.date[0] = DateTime.Now.Year;
sv.date[1] = DateTime.Now.Month;
sv.date[2] = DateTime.Now.Day;
sv.date[3] = DateTime.Now.Hour;
sv.date[4] = DateTime.Now.Minute;
sv.date[5] = DateTime.Now.Second;
PlayerPrefs.SetString("SV", JsonUtility.ToJson(sv));
PlayerPrefs.Save();
}
#endif
public void showShopPan() // Рассказывал в уроке #2
{
shopPan.SetActive(!shopPan.activeSelf);
Top.SetActive(!Top.activeSelf);// Рассказывал в уроке #2
}
public void OnClick() // Рассказывал в уроке #1
{
score += scoreIncrease; // К игровой валюте прибавляем бонус при клике
}
void OnMouseUpAsButton()
{
bool proverka = true;
if (gameObject.name == "Hram" && proverka == true)
{
for (int i = 0; i < shopItems.Count; i++) score += 100; // Добавляем к игровой валюте бонус рабочих (к примеру)
yield return new WaitForSeconds(1);
}
}
}
[Serializable]
public class Item // Класс товара
{
[Tooltip("Кол-во рабочих")]
public Text TextCounter;
[Tooltip("Название используется на кнопках")]
public string name;
[Tooltip("Цена товара")]
public int cost;
[Tooltip("Бонус, который добавляется к бонусу при клике")]
public int bonusIncrease;
[HideInInspector]
public int levelOfItem; // Уровень товара
[Space]
[Tooltip("Нужен ли множитель для цены?")]
public bool needCostMultiplier;
[Tooltip("Множитель для цены")]
public int costMultiplier;
[Space]
[Tooltip("Этот товар даёт бонус в секунду?")]
public bool itsItemPerSec;
[Tooltip("Бонус, который даётся в секунду")]
public int bonusPerSec;
[HideInInspector]
public int bonusCounter;// Кол-во рабочих (к примеру)
[Space]
[Tooltip("Это временный бонус?")]
public bool itsBonus;
[Tooltip("Множитель товара, который управляется бонусом (Умножается переменная bonusPerSec)")]
public int itemMultiplier;
[Tooltip("Индекс товара, который будет управляться бонусом (Умножается переменная bonusPerSec этого товара)")]
public int itemIndex;
[Tooltip("Длительность бонуса")]
public float timeOfBonus;
}
[Serializable]
public class Save
{
public int score;
public int[] levelOfItem;
public int[] bonusCounter;
public int[] date = new int[6];
} |