⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediator.cpp

📁 C++ Source code from a tutorial
💻 CPP
字号:
#include "mediator.h"
#include "carparts.h"
#include <iostream>

using namespace std;

Mediator::Mediator() {
    MyEngine = new Engine(this);
    MyElectric = new Electric(this);
    MyRadio = new Radio(this);
    MyWheels = new Wheels(this);
    MyBrakes = new Brakes(this);
    MyHeadlights = new Headlights(this);
    MyAirConditioner = new AirConditioner(this);
    MyRoad = new Road(this);
}

void Mediator::PartChanged(CarPart *part) {
    if (part == MyEngine) {
        if (MyEngine->RPM == 0) {
            MyWheels->Speed = 0;
            return;
        }
        if (MyEngine->Revamount == 0) {
            return;
        }
        // If engine increases, increase the electric output
        MyElectric->ChangeOutputBy
            (MyEngine->Revamount / 10);
        if (MyEngine->Revamount > 0) {
            MyWheels->Accelerate(
                MyEngine->Revamount / 50);
        }
    }
    else if (part == MyElectric) {
        // Dim or brighten the headlights
        if (MyHeadlights->Brightness > 0)
          MyHeadlights->Adjust(MyElectric->ChangedBy / 20);
        if (MyRadio->Volume > 0)
          MyRadio->AdjustVolume(MyElectric->ChangedBy / 30);
    }
    else if (part == MyBrakes) {
        MyWheels->Decelerate(MyBrakes->Pressure / 5);
    }
    else if (part == MyAirConditioner) {
        MyElectric->ChangeOutputBy(
            0 - MyAirConditioner->ChangedBy * 2);
    }
    else if (part == MyRoad) {
        if (MyRoad->ClimbAngle > 0) {
            MyWheels->Decelerate(MyRoad->ClimbAngle * 2);
            MyRoad->ClimbAngle = 0;
        }
        else if (MyRoad->ClimbAngle < 0) {
            MyWheels->Accelerate(MyRoad->ClimbAngle * -4);
            MyRoad->ClimbAngle = 0;
        }
    }
}

void CarControls::StartCar() {
    MyEngine->Start();
}

void CarControls::StopCar() {
    MyEngine->Stop();
}

void CarControls::PushGasPedal(int amount) {
    MyEngine->PushGasPedal(amount);
}

void CarControls::ReleaseGasPedal(int amount) {
    MyEngine->ReleaseGasPedal(amount);
}

void CarControls::PressBrake(int amount) {
    MyBrakes->Apply(amount);
}

void CarControls::TurnOnRadio() {
    MyRadio->SetVolume(100);
}

void CarControls::TurnOffRadio() {
    MyRadio->SetVolume(0);
}

void CarControls::AdjustRadioVolume(int amount) {
    MyRadio->AdjustVolume(amount);
}

void CarControls::TurnOnHeadlights() {
    MyHeadlights->TurnOn();
}

void CarControls::TurnOffHeadlights() {
    MyHeadlights->TurnOff();
}

void CarControls::ClimbHill(int angle) {
    MyRoad->ClimbDescend(angle);
}

void CarControls::DescendHill(int angle) {
    MyRoad->ClimbDescend( 0 - angle );
}

int CarControls::GetSpeed() {
    return MyWheels->Speed;
}

void CarControls::TurnOnAC() {
    MyAirConditioner->TurnOn();
}

void CarControls::TurnOffAC() {
    MyAirConditioner->TurnOff();
}

void CarControls::AdjustAC(int amount) {
    MyAirConditioner->SetLevel(amount);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -