
/* Generated by Interface Builder */

#import "Car_main.h"
#import "Controller.h"
#import "Car.h"

@implementation Controller

- init
{
    [super init];
    controller = self;
    return self;
}

- read:(NXTypedStream *)stream
{
    [super read:stream];
    NXReadTypes(stream,"fff",&mass,&shiftCeiling,&shiftFloor);
    return self;
}

- write:(NXTypedStream *)stream
{
    [super write:stream];
    NXWriteTypes(stream,"fff",&mass,&shiftCeiling,&shiftFloor);
    return self;
}

- (float)mass
{
    return mass;
}

- setMass:(float)aNumber
{
    mass = aNumber;
    return self;
}

- (float)shiftCeiling
{
    return shiftCeiling;
}

- setShiftCeiling:(float)aNumber
{
    shiftCeiling = aNumber;
    return self;
}

- (float)shiftFloor
{
    return shiftFloor;
}

- setShiftFloor:(float)aNumber
{
    shiftFloor = aNumber;
    return self;
}

- powerRequired:(float)power
{
float enginePower;
float motorPower;

    if ( [car currentVelocity] * 3.6 > shiftCeiling )		// Gotta remember to change m/s to km/h
        runningMode = ENGINE;
    if ( [car currentVelocity] * 3.6 < shiftFloor )
        runningMode = MOTOR;
   
    if ( runningMode == MOTOR )
        {
        motorPower = power;
        enginePower = 0;
        }
    if ( runningMode == ENGINE )
        {
        motorPower = 0;
        enginePower = power;	
        }
    if ( runningMode == BOTH )
        {
        // Not implemented yet.
        }
    if ( power < 0 )
        {
        motorPower = power;
        enginePower = 0;
        }
    [engine powerRequired:enginePower];
    [motor powerRequired:motorPower];
    
    return self;
}

@end
