This is FlyLogic.m in view mode; [Download] [Up]
#import "FlyLogic.h"
#import "FlyCommon.h"
@implementation FlyLogic
- setBoundsRect:(NXRect *)r
{
NXSetRect(&bounds, r->origin.x, r->origin.y, r->size.width, r->size.height);
return self;
}
- getNewHeading:(Fly *)fly
{
long rand;
long head;
rand = random() % 100;
head = [fly heading];
if (rand < [fly noTurn]) {
return self;
} else if (rand < [fly leftTurn]) {
if (head == kMinDir) {
[fly setHeading:kMaxDir];
} else {
[fly setHeading:--head];
}
} else {
if (head == kMaxDir) {
[fly setHeading:kMinDir];
} else {
[fly setHeading:++head];
}
}
return self;
}
- getStateAndTurn:(Fly *)fly
{
float speed = 0;
long noTurn = 0;
long leftTurn = 0;
long statePeriod = 0;
speed = [fly speed];
if (speed <= 2) {
noTurn = 95;
leftTurn = 97;
statePeriod = 100;
} else if (speed <= 4) {
noTurn = 86;
leftTurn = 93;
statePeriod = 75;
} else if (speed <= 10) {
noTurn = 80;
leftTurn = 90;
statePeriod = 50;
} else if (speed <= 20) {
noTurn = 70;
leftTurn = 85;
statePeriod = 37;
} else if (speed <= 30) {
noTurn = 60;
leftTurn = 80;
statePeriod = 25;
}
[fly setPeriod:statePeriod];
[fly setNoTurn:noTurn andLeftTurn:leftTurn];
return self;
}
- adjustPosition:(Fly *)fly
{
NXPoint *pos;
long heading;
pos = [fly position];
heading = [fly heading];
switch(heading) {
case kEast:
case kNorthEast:
case kSouthEast:
pos->x += [fly speed];
break;
case kWest:
case kNorthWest:
case kSouthWest:
pos->x -= [fly speed];
break;
default:
break;
}
if (pos->x > bounds.size.width) {
pos->x = bounds.origin.x - (kFlyCellWidth - 1);
} else if (pos->x < bounds.origin.x - (kFlyCellWidth - 1)) {
pos->x = bounds.size.width;
}
switch(heading) {
case kNorth:
case kNorthEast:
case kNorthWest:
pos->y += [fly speed];
break;
case kSouth:
case kSouthEast:
case kSouthWest:
pos->y -= [fly speed];
break;
default:
break;
}
if (pos->y > bounds.size.height) {
pos->y = bounds.origin.y - (kFlyCellHeight - 1);
} else if (pos->y < bounds.origin.y - (kFlyCellHeight - 1)) {
pos->y = bounds.size.height;
}
return self;
}
- randomPosition:(Fly *)fly
{
NXPoint p;
p.x = random() % (long)bounds.size.width;
p.y = random() % (long)bounds.size.height;
[fly setPosition:p];
return self;
}
@end
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.