-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomView.m
308 lines (248 loc) · 7.9 KB
/
CustomView.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
297
298
299
300
301
302
303
304
305
306
307
308
//
// CustomView.m
// NSStatusItemTest
//
// Created by Matt Gemmell on 04/03/2008.
// Copyright 2008 Magic Aubergine. All rights reserved.
//
#import "CustomView.h"
#import "AppController.h"
#import <QuartzCore/QuartzCore.h>
#define HEIGHT 20
#define WIDTH 60
#define PADDLE_HEIGHT 8
#define PADDLE_WIDTH 2
#define BALL_SIZE 3
#define WINDOW_OFFSET 3
#define FPS (24.0)
@implementation CustomView
- (id)initWithFrame:(NSRect)frame controller:(AppController *)ctrlr{
if (self = [super initWithFrame:frame]) {
controller = ctrlr; // deliberately weak reference.
[self startMatchAndP1HasBall:YES];
}
return self;
}
- (void)dealloc{
controller = nil;
}
- (void)drawRect:(NSRect)rect {
[[NSColor blackColor] setFill];
NSRect bg =NSMakeRect(0, 0, WIDTH, HEIGHT+WINDOW_OFFSET);
NSRectFill(bg);
[[NSColor whiteColor] setFill];
NSRect middle =NSMakeRect(30, 0, 0.5, HEIGHT+WINDOW_OFFSET);
NSRectFill(middle);
NSRect p1 =NSMakeRect(2, p1Y, PADDLE_WIDTH, PADDLE_HEIGHT);
NSRectFill(p1);
NSRect p2 =NSMakeRect(WIDTH-PADDLE_WIDTH-2, p2Y, PADDLE_WIDTH, PADDLE_HEIGHT);
NSRectFill(p2);
NSRect ball =NSMakeRect(ballX, ballY, BALL_SIZE, BALL_SIZE);
NSRectFill(ball);
NSMutableParagraphStyle * paragraphStyleH = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyleH setAlignment:NSRightTextAlignment];
NSFont * font = [NSFont fontWithName:@"04b03" size:12];
if (font==nil) {
NSLog(@"Please install 04b03 font!");
[NSApp terminate:self];
}
NSDictionary* attributesH = @{NSParagraphStyleAttributeName: paragraphStyleH,
NSForegroundColorAttributeName: [NSColor whiteColor],
NSFontAttributeName: font};
NSString * Hstr = [[NSString alloc] initWithFormat:@"%d", h];
[Hstr drawAtPoint:NSMakePoint(17, 10) withAttributes:attributesH];
NSMutableParagraphStyle * paragraphStyleM = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyleM setAlignment:NSLeftTextAlignment];
NSDictionary* attributesM = @{NSParagraphStyleAttributeName: paragraphStyleM,
NSForegroundColorAttributeName: [NSColor whiteColor],
NSFontAttributeName: font};
NSString * Mstr = [[NSString alloc] initWithFormat:@"%d", m];
[Mstr drawAtPoint:NSMakePoint(33, 10) withAttributes:attributesM];
}
-(void)refresh{
[self moveBall];
[self moveP1:p1ShouldLose];
[self moveP2:p2ShouldLose];
if(someoneScored) {
[timer invalidate];
h=[[self getCurrentDateTime] hour];
m=[[self getCurrentDateTime] minute];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
[self startMatchAndP1HasBall:p1ShouldLose];
});
}
[self setNeedsDisplay:YES];
}
- (void) moveBall{
ballX+=ballDeltaX;
ballY+=ballDeltaY;
if(p1ShouldLose){
if(ballX<0){
//NSLog(@"GAME OVER");
someoneScored=YES;
ballDeltaX=0;
}else{
if(ballX>WIDTH-BALL_SIZE-PADDLE_WIDTH-2){
ballX=WIDTH-BALL_SIZE-PADDLE_WIDTH-2;
ballDeltaX=-ballDeltaX;
}
}
}else if(p2ShouldLose) {
if(ballX>WIDTH-BALL_SIZE){
//NSLog(@"GAME OVER");
someoneScored=YES;
ballDeltaX=0;
}else{
if(ballX<PADDLE_WIDTH+2){
ballX=PADDLE_WIDTH+2;
ballDeltaX=-ballDeltaX;
}
}
}else{
if(ballX>WIDTH-BALL_SIZE-PADDLE_WIDTH-2){
ballX=WIDTH-BALL_SIZE-PADDLE_WIDTH-2;
ballDeltaX=-ballDeltaX;
[self checkClock];
}
if(ballX<PADDLE_WIDTH+2){
ballX=PADDLE_WIDTH+2;
ballDeltaX=-ballDeltaX;
[self checkClock];
}
}
if(ballY+BALL_SIZE>HEIGHT+WINDOW_OFFSET){
ballY=HEIGHT+WINDOW_OFFSET-BALL_SIZE;
ballDeltaY=-ballDeltaY;
}
if(ballY<0){
ballY=0;
ballDeltaY=-ballDeltaY;
}
}
-(void)moveP1:(bool)shouldLose{
float err;
if (ballDeltaX<0) {
if(shouldLose){
err=ballY+1.5-p1Y-(PADDLE_HEIGHT/2.0);
if(abs(err)>0.1){
if(ballX< WIDTH/3.0){
err*=0.1;
}else if(ballX< WIDTH/3.0*2.0){
err*=0.05;
}else{
err*=0.01;
}
p1Y+=err;
}
}else{
err=ballY+1.5-p1Y-(PADDLE_HEIGHT/2.0);
if(abs(err)>0.1){
if(ballX< WIDTH/3.0){
err*=0.4;
}else if(ballX< WIDTH/3.0*2.0){
err*=0.2;
}else{
err*=0.08;
}
p1Y+=err;
}
}
}else{
err=ballY+1.5-p1Y-(PADDLE_HEIGHT/2.0);
if(abs(err)>0.1){
if(ballX>=WIDTH/3.0*2.0){
err*=0.02;
p1Y+=err;
}
}
}
if (p1Y<0) p1Y=0;
if (p1Y+PADDLE_HEIGHT>HEIGHT+WINDOW_OFFSET) p1Y=HEIGHT-PADDLE_HEIGHT+WINDOW_OFFSET;
}
-(void)moveP2:(bool)shouldLose{
float err;
if (ballDeltaX>0) {
if(shouldLose){
err=ballY+1.5-p2Y-(PADDLE_HEIGHT/2.0);
if(abs(err)>0.1){
if(ballX< WIDTH/3.0){
err*=0.1;
}else if(ballX< WIDTH/3.0*2.0){
err*=0.05;
}else{
err*=0.01;
}
p2Y+=err;
}
}else{
err=ballY-p2Y-(PADDLE_HEIGHT/2.0);
if(abs(err)>0.1){
if(ballX> WIDTH/3.0*2){
err*=0.4;
}else if(ballX> WIDTH/3.0){
err*=0.2;
}else{
err*=0.08;
}
p2Y+=err;
}
}
}else{
err=ballY+1.5-p2Y-(PADDLE_HEIGHT/2.0);
if(abs(err)>0.1){
if(ballX>=WIDTH<3.0){
err*=0.02;
p2Y+=err;
}
}
}
if (p2Y<0) p2Y=0;
if (p2Y+PADDLE_HEIGHT>HEIGHT+WINDOW_OFFSET) p2Y=HEIGHT-PADDLE_HEIGHT+WINDOW_OFFSET;
}
- (void) startMatchAndP1HasBall:(bool)p1Starting{
p1Y=0;
p2Y=HEIGHT-PADDLE_HEIGHT+WINDOW_OFFSET;
if(p1Starting){
ballX=PADDLE_WIDTH+BALL_SIZE+1;
}else{
ballX=WIDTH-PADDLE_WIDTH-BALL_SIZE-1;
}
ballY=PADDLE_WIDTH/2.0;
ballDeltaX=1.0+ (arc4random()%20)/20.0;
ballDeltaY=0.5+ (arc4random()%15)/15.0;
h=[[self getCurrentDateTime] hour];
m=[[self getCurrentDateTime] minute];
p1ShouldLose=NO;
p2ShouldLose=NO;
someoneScored=NO;
//NSLog(@"%d:%d", h ,m);
//[self performSelector:@selector(refresh) withObject:nil afterDelay:1];
timer=[NSTimer scheduledTimerWithTimeInterval:1.0/FPS
target:self
selector:@selector(refresh)
userInfo:nil
repeats:YES];
}
- (void) checkClock{
int newH;
int newM;
if(p1ShouldLose || p2ShouldLose){
return;
}
newH=[[self getCurrentDateTime] hour];
newM=[[self getCurrentDateTime] minute];
if (m!=newM) {
//m=newM;
p1ShouldLose=YES;
//NSLog(@"P2 IS GONNA LOSE!");
if(h!=newH){
//h=newH;
//NSLog(@"P1 IS GONNA LOSE!");
p2ShouldLose=YES;
}
}
}
-(NSDateComponents *)getCurrentDateTime{
return [ [NSCalendar currentCalendar] components:NSHourCalendarUnit + NSMinuteCalendarUnit + NSSecondCalendarUnit fromDate:[NSDate date]];
}
@end