-
Notifications
You must be signed in to change notification settings - Fork 2
/
MBGame.m
296 lines (253 loc) · 6.08 KB
/
MBGame.m
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#import "MBtypes.h"
#import "MBGame.h"
#import "MButil.h"
#import "MBBill.h"
#import "MBBucket.h"
#import "MBComputer.h"
#import "MBCable.h"
#import "MBHorde.h"
#import "MBNetwork.h"
#import "MBOS.h"
#import "MBScorelist.h"
#import "MBSpark.h"
#import "MBUI.h"
#define SCREENSIZE 400
/* Game states */
#define STATE_PLAYING 1
#define STATE_BETWEEN 2
#define STATE_END 3
#define STATE_WAITING 4
/* Score related constants */
#define SCORE_ENDLEVEL -1
#define SCORE_BILLPOINTS 5
static unsigned int state;
static int efficiency;
static int score, level, iteration;
static MBPicture *logo, *icon, *about;
static MBMCursor *defaultcursor, *downcursor;
static MBBill *grabbed;
static int screensize = SCREENSIZE;
@implementation MBGame
// private
- (void)setup_level:(int)newlevel
{
level = newlevel;
[horde Horde_setup];
grabbed = NULL;
[ui UI_set_cursor:defaultcursor];
[network Network_setup];
iteration = 0;
efficiency = 0;
}
// private
- (void)update_info
{
char str[80];
int on_screen = [horde Horde_get_counter:HORDE_COUNTER_ON];
int off_screen = [horde Horde_get_counter:HORDE_COUNTER_OFF];
int base = [network Network_get_counter:NETWORK_COUNTER_BASE];
int off = [network Network_get_counter:NETWORK_COUNTER_OFF];
int win = [network Network_get_counter:NETWORK_COUNTER_WIN];
int units = [network Network_num_computers];
sprintf(str, "Bill:%d/%d System:%d/%d/%d Level:%d Score:%d",
on_screen, off_screen, base, off, win, level, score);
[ui UI_draw_str:str :5 :screensize - 5];
efficiency += ((100 * base - 10 * win) / units);
}
// private
- (void)draw_logo
{
char str[15];
[ui UI_clear];
[ui UI_draw:logo
:(screensize - [ui UI_picture_width:logo]) / 2
:(screensize - [ui UI_picture_height:logo]) / 2];
sprintf(str, "Click to start");
[ui UI_draw_str:str :167 :screensize/7*5];
}
- (void)Game_start:(int)newlevel
{
state = STATE_PLAYING;
score = 0;
[ui UI_restart_timer];
[ui UI_set_pausebutton:1];
[self setup_level:newlevel];
}
- (void)Game_quit
{
[scorelist Scorelist_write];
}
- (void)Game_warp_to_level:(int)lev
{
if (state == STATE_PLAYING) {
if (lev <= level)
return;
[self setup_level:lev];
}
else {
if (lev <= 0)
return;
[self Game_start:lev];
}
}
- (void)Game_add_high_score:(const char *)str
{
[scorelist Scorelist_recalc:str :level :score];
}
- (void)Game_button_press:(int)x :(int)y
{
int counter;
if (state != STATE_PLAYING)
return;
[ui UI_set_cursor:downcursor];
if ([bucket Bucket_clicked:x :y]) {
[bucket Bucket_grab:x :y];
return;
}
grabbed = [horde Horde_clicked_stray:x :y];
if (grabbed != NULL) {
[os OS_set_cursor:grabbed->cargo];
return;
}
counter = [horde Horde_process_click:x :y];
score += (counter * counter * SCORE_BILLPOINTS);
}
- (void)Game_button_release:(int)x :(int)y
{
int i;
[ui UI_set_cursor:defaultcursor];
if (state == STATE_WAITING) {
[self Game_start:1];
return;
}
if (state != STATE_PLAYING)
return;
if (grabbed == NULL) {
[bucket Bucket_release:x :y];
return;
}
for (i = 0; i < [network Network_num_computers]; i++) {
MBComputer *computer = [network Network_get_computer:i];
if ([computer Computer_on:x :y] &&
[computer Computer_compatible:grabbed->cargo] &&
(computer->os == OS_WINGDOWS || computer->os == OS_OFF)) {
int counter;
[network Network_inc_counter:NETWORK_COUNTER_BASE :1];
if (computer->os == OS_WINGDOWS)
counter = NETWORK_COUNTER_WIN;
else
counter = NETWORK_COUNTER_OFF;
[network Network_inc_counter:counter :-1];
computer->os = grabbed->cargo;
[horde Horde_remove_bill:grabbed];
grabbed = NULL;
return;
}
}
[horde Horde_add_bill:grabbed];
grabbed = NULL;
}
- (void)Game_update
{
char str[40];
switch (state) {
case STATE_PLAYING:
[ui UI_clear];
[bucket Bucket_draw];
[network Network_update];
[network Network_draw];
[horde Horde_update:iteration];
[horde Horde_draw];
[self update_info];
if ([horde Horde_get_counter:HORDE_COUNTER_ON] +
[horde Horde_get_counter:HORDE_COUNTER_OFF] == 0) {
score += (level * efficiency / iteration);
state = STATE_BETWEEN;
}
if (([network Network_get_counter:NETWORK_COUNTER_BASE] +
[network Network_get_counter:NETWORK_COUNTER_OFF]) <= 1)
state = STATE_END;
break;
case STATE_END:
[ui UI_set_cursor:defaultcursor];
[ui UI_clear];
[network Network_toasters];
[network Network_draw];
[ui UI_refresh];
[ui UI_popup_dialog:DIALOG_ENDGAME];
if ([scorelist Scorelist_ishighscore:score]) {
[ui UI_popup_dialog:DIALOG_ENTERNAME];
}
[ui UI_popup_dialog:DIALOG_HIGHSCORE];
[self draw_logo];
[ui UI_kill_timer];
[ui UI_set_pausebutton:0];
state = STATE_WAITING;
break;
case STATE_BETWEEN:
[ui UI_set_cursor:defaultcursor];
sprintf(str, "After Level %d:\nScore: %d", level, score);
[ui UI_popup_dialog:DIALOG_SCORE];
state = STATE_PLAYING;
[self setup_level:++level];
break;
}
[ui UI_refresh];
iteration++;
}
- (int)Game_state
{
return state;
}
- (int)Game_score
{
return score;
}
- (int)Game_level
{
return level;
}
- (int)Game_screensize
{
return screensize;
}
- (double)Game_scale:(int)dimensions
{
double scale = (double)screensize / SCREENSIZE;
double d = 1;
for ( ; dimensions > 0; dimensions--)
d *= scale;
return (d);
}
- Game_main
{
[MBBill Bill_class_init:self :horde :network :os :ui];
[MBCable Cable_class_init:self :network :spark :ui];
[MBComputer Computer_class_init:self :network :os :ui];
srandom((unsigned)time(NULL));
[ui UI_make_main_window:screensize];
[ui UI_load_picture:"logo" :0 :&logo];
[ui UI_load_picture:"icon" :0 :&icon];
[ui UI_load_picture:"about" :0 :&about];
[self draw_logo];
[ui UI_refresh];
[scorelist Scorelist_read];
[ui UI_load_cursor:"hand_up" :CURSOR_SEP_MASK :&defaultcursor];
[ui UI_load_cursor:"hand_down" :CURSOR_SEP_MASK :&downcursor];
[ui UI_set_cursor:defaultcursor];
[MBBill Bill_load_pix];
[os OS_load_pix];
[MBComputer Computer_load_pix];
[bucket Bucket_load_pix];
[spark Spark_load_pix];
state = STATE_WAITING;
[ui UI_set_pausebutton:0];
return self;
}
- (void)Game_set_size:(int)size
{
if (size >= SCREENSIZE) {
screensize = size;
}
}
@end