-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.m
More file actions
93 lines (79 loc) · 2.47 KB
/
Copy pathgame.m
File metadata and controls
93 lines (79 loc) · 2.47 KB
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
#import "vector.h"
#import "game.h"
#include <math.h>
@implementation jsGame
@synthesize jsBots, numberOfBots;
-(id) initWithArgc: (int) argc Argv: (char**) argv{
self = [super init];
if(self){
//make bots based off of arguments to the program
numberOfBots = argc-1;
jsBots = [NSArray arrayWithObjects: nil];
for(int i = 1; i<=numberOfBots && i <= 4; i++){
//initialize bullets to null
bullets[i-1] = NULL;
//Get the file name
NSString* botFileName = [NSString stringWithCString:argv[i] encoding:NSUTF8StringEncoding];
//set the initial position
jsVector* position = [[jsVector alloc] initWithX: 20 Y:20];
//make the bot
jsBot* bot = [[jsBot alloc] initWithFile: botFileName Position: position];
bot.game = self;
bot.index = i-1;
//add bot to array
jsBots = [jsBots arrayByAddingObject: bot];
}
//start the bots threads
for(int i=0; i<numberOfBots; i++){
jsBot* bot = [jsBots objectAtIndex:i];
bot.thread = [[NSThread alloc] initWithTarget:bot selector:@selector(main) object:nil];
[bot.thread start];
}
}
return self;
}
- (jsBullet *)getBullet: (int) index{
return bullets[index];
}
//issue will come up when scan are around 0 and 360
- (void) scan: (jsBot*) bot{
bot.returnValues = 0;
for(int i=0; i<numberOfBots; i++){
jsBot* boti = [jsBots objectAtIndex:i];
if(boti != bot){
jsVector* diff = [boti.position sub:bot.position];
int angleOfBot = (int)round(diff.angle*180/M_PI);
if(angleOfBot < bot.scanAngle + bot.scanResolution && angleOfBot > bot.scanAngle - bot.scanResolution){
int length = (int)round(diff.length);
if(bot.returnValues > length || bot.returnValues == 0){
bot.returnValues = length;
}
}
}
}
}
- (void) shoot: (jsBot*) bot{
if(bullets[bot.index] != NULL){
bot.returnValues = -1;
return;
}
bot.returnValues = 1;
bullets[bot.index] = [[jsBullet alloc] init];
bullets[bot.index].startPosition = [bot.position copy];
bullets[bot.index].position = [bot.position copy];
bullets[bot.index].fromBot = bot;
bullets[bot.index].shootArguments = bot.shootArguments;
}
- (void) update{
for(int i=0; i<numberOfBots; i++){
jsBot* bot = [jsBots objectAtIndex:i];
[bot update];
if(bullets[i] != NULL){
bullets[i].position = [bullets[i].position add:[jsVector vectorWithMag: 5 angle:bullets[i].shootArguments.direction]];
if([bullets[i].position sub:bullets[i].startPosition].length >= bullets[i].shootArguments.distance){
bullets[i] = NULL;
}
}
}
}
@end