-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.m
188 lines (162 loc) · 4.58 KB
/
Level.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
//
// Level.m
// hiddenObjectTest
//
// Created by Jonathan Langens on 28/03/15.
// Copyright (c) 2015 Jonathan Langens. All rights reserved.
//
#import "Level.h"
#import "HOLabel.h"
@implementation Level
@synthesize background = _background;
-(UIImageView*) getBackground
{
if(!self.background)
{
self.background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
self.background.frame = self.superview.bounds;;
[self addSubview:self.background];
}
return self.background;
}
-(instancetype)initWithObjects:(NSArray *)objects
{
self = [super init];
self.objects = [[NSMutableArray alloc] initWithArray:objects];
[self setup];
return self;
}
-(void)awakeFromNib
{
[self setup];
}
-(void)drawRect:(CGRect)rect
{
[self setBackgroundImage];
}
-(void) setup
{
self.objects = [[NSMutableArray alloc] initWithArray:[objectFactory getObjectsForLevel:@"test"]];
[self setBackgroundImage];
[self createLevel];
[self selectObjects];
[self drawObjects];
[self drawButtons];
}
-(void) setBackgroundImage
{
[self setBackgroundColor:[UIColor darkGrayColor]];
[self getBackground].frame = self.superview.bounds;
}
-(void) createLevel
{
// int counter = 0;
for(id object in self.objects)
{
Object *o = (Object*)object;
ObjectInstance *instance = nil;
bool notSelected = true;
while(notSelected)
{
int index = arc4random() % o.instances.count;
notSelected = false;
instance = [o.instances objectAtIndex:index];
for(Object *o in self.objects)
{
if(o.selInstance)
{
if([instance collidesWithRect:o.selInstance.rect])
{
notSelected = true;
}
}
}
}
if(instance)[o setSelectedInstance: instance];
}
}
-(void) selectObjects
{
int countArray[self.objects.count];
for(int i=0;i<self.objects.count;++i)countArray[i] = i;
// randomizing
for(int i=0;i < self.objects.count * 15; ++i)
{
int p1 = arc4random() % self.objects.count;
int p2 = arc4random() % self.objects.count;
int tmp = countArray[p1];
countArray[p1] = countArray[p2];
countArray[p2] = tmp;
}
// now selecting the top 5
NSMutableArray *sel = [[NSMutableArray alloc] init];
[sel addObject:[self.objects objectAtIndex:countArray[0]]];
[sel addObject:[self.objects objectAtIndex:countArray[1]]];
[sel addObject:[self.objects objectAtIndex:countArray[2]]];
[sel addObject:[self.objects objectAtIndex:countArray[3]]];
[sel addObject:[self.objects objectAtIndex:countArray[4]]];
[sel addObject:[self.objects objectAtIndex:countArray[5]]];
self.selection = sel;
}
-(void) drawButtons
{
self.labels = [[NSMutableArray alloc] init];
for(int i = 0;i < 6; ++i)
{
int xp = 100 + (120 * (i));
int yp = 680;
CGRect nrect = CGRectMake(xp, yp, 100, 50);
// UILabel *b = [[UILabel alloc] initWithFrame:nrect];
Object *object = (Object*)[self.selection objectAtIndex:i];
HOLabel *b = [[HOLabel alloc] initWithFrame:nrect andObject:object andLocale:@"NL"];
//[b setText:object.name];
[b setTextColor:[UIColor blackColor]];
[self.labels addObject:b];
[self addSubview:b];
}
}
-(void) drawObjects
{
for(id idview in self.objects)
{
Object *o = (Object*) idview;
[self addSubview:o];
}
}
-(BOOL) removeFromSelection:(Object*)object
{
for(id l in self.labels)
{
if([((HOLabel*)l).trueName isEqualToString:object.name])
{
((HOLabel*)l).text = @"";
return TRUE;
}
}
return FALSE;
}
-(void) clickedAtPosition:(CGPoint) point
{
for(id obj in self.objects)
{
Object *object = (Object*)obj;
if([self containsPoint:object.selInstance.rect Point:point])
{
// removing the object from the list
if([self removeFromSelection:object])
{
[object removeFromSuperview];
[self setNeedsDisplay];
return;
}
}
}
}
-(BOOL) containsPoint:(CGRect) rect Point: (CGPoint) point
{
return ((rect.origin.x <= point.x) &&
((rect.origin.x + rect.size.width) >= point.x) &&
(rect.origin.y <= point.y) &&
((rect.origin.y + rect.size.height) >= point.y));
}
@end