forked from t-iwasaki/arduboy-JumpBoy
/
Coins.ino
169 lines (129 loc) · 2.99 KB
/
Coins.ino
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
/*----------------------
Coins:
--------------------- */
const uint8_t CONCURRENT_COIN_MAX = 10;
struct Coin
{
uint8_t spCount = 0;
uint8_t qty = 0;
uint8_t norma = 20;
uint8_t concurrent_coin_max = CONCURRENT_COIN_MAX;
bool is_puzzle = false;
bool is_puzzle_prepare = false;
} coin;
struct Coins
{
float x = 64;
float y = 16;
bool active = false;
uint8_t spCount = 0;
};
Coins coins[CONCURRENT_COIN_MAX];
void initCoin(int concurrent_coin_max,bool is_puzzle)
{
coin.spCount = 0;
coin.qty = 0;
coin.is_puzzle = is_puzzle;
coin.is_puzzle_prepare = false;
coin.norma = (coin.is_puzzle) ? concurrent_coin_max : 20;
coin.concurrent_coin_max = (concurrent_coin_max > CONCURRENT_COIN_MAX ) ? CONCURRENT_COIN_MAX : concurrent_coin_max;
for (int i = 0; i < sizeof(coins); i++) {
coins[i].active = false;
}
}
void updateCurrentCoinMax(int concurrent_coin_max)
{
coin.concurrent_coin_max = (concurrent_coin_max > CONCURRENT_COIN_MAX ) ? CONCURRENT_COIN_MAX : concurrent_coin_max;
}
void resetSpCount()
{
coin.spCount = 0;
}
void moveCoin()
{
if (coin.is_puzzle && !coin.is_puzzle_prepare) {
coin.is_puzzle_prepare = true;
_createCoins();
} else {
//アイテムの有効期限チェック
coin.spCount++;
if (coin.spCount == 200)
{
coin.spCount = 0;
updateCurrentCoinMax(1);
}
_createCoins();
}
_checkCoinsLife();
_collisionCoins();
}
boolean _checkPlayerPosition(int x,int y)
{
if (pX - 8 * 2 < x && x < pX + 8 * 3 &&
pY - 8 * 2 < y && y < pY + 8 * 3 ) {
return false;
}
return true;
}
void _createCoins()
{
for (int i = 0; i < coin.concurrent_coin_max; i++) {
if (coins[i].active == false) {
if (coin.is_puzzle) {
// 高さは同じで横に等間隔にコインを配置
coins[i].x = 10 + i * 10;
coins[i].y = 10 + 30;
}
int x = 10 + random(110);
int y = 10 + random(34);
if (_checkPlayerPosition(x,y)) {
coins[i].x = x;
coins[i].y = y;
// coins[i].spCount = 10 + random(20);
coins[i].spCount = 50;
coins[i].active = true;
}
}
}
}
void _checkCoinsLife()
{
for (int i = 0; i < coin.concurrent_coin_max; i++) {
coins[i].spCount--;
if (coins[i].spCount <= 0) {
coins[i].active = false;
}
}
}
void _collisionCoins()
{
for (int i = 0; i < coin.concurrent_coin_max; i++) {
if (coins[i].active) {
if (coins[i].x < pX + 8 &&
coins[i].x + 8 > pX &&
coins[i].y < pY + 8 &&
8 + coins[i].y > pY)
{
arduboy.tunes.tone(NOTE_G3, 80);
coin.qty++;
coins[i].active = false;
if (coin.qty >= coin.norma) {
stageClear();
return;
}
}
}
}
}
uint8_t getCoinQty()
{
return coin.qty;
}
void drawCoin()
{
for (int i = 0; i < coin.concurrent_coin_max; i++) {
if (coins[i].active) {
arduboy.drawBitmap(coins[i].x, coins[i].y, bCoin, 8, 8, 1);
}
}
}