X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Fsimplecontroller.cpp;h=df828d5498832bf480cea9767518dae92dc6014a;hb=66a30fdf63df6ae092fb3d335c30a37e4e6ef40d;hp=7f8b3362087ed0cee7a37eb03c55875cc31a4f20;hpb=1ff06c5bc46a677fa389ef86c6b26664368f1653;p=r2c2.git diff --git a/source/libr2c2/simplecontroller.cpp b/source/libr2c2/simplecontroller.cpp index 7f8b336..df828d5 100644 --- a/source/libr2c2/simplecontroller.cpp +++ b/source/libr2c2/simplecontroller.cpp @@ -1,11 +1,4 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - -#include +#include #include #include "simplecontroller.h" @@ -23,17 +16,27 @@ SimpleController::SimpleController(): target_speed.set(0); } +const char *SimpleController::enumerate_controls(unsigned index) const +{ + if(index==0) + return target_speed.name.c_str(); + else if(index==1) + return reverse.name.c_str(); + else + return 0; +} + void SimpleController::set_control(const string &name, float v) { - if(name=="speed") + if(name==target_speed.name) { target_speed.set(v); signal_control_changed.emit(target_speed); } - else if(name=="reverse") + else if(name==reverse.name) { if(target_speed.value || speed) - throw InvalidState("Must be stopped to change reverse"); + throw logic_error("Must be stopped to change reverse"); reverse.set(v); signal_control_changed.emit(reverse); } @@ -41,12 +44,12 @@ void SimpleController::set_control(const string &name, float v) const Controller::Control &SimpleController::get_control(const string &name) const { - if(name=="speed") + if(name==target_speed.name) return target_speed; - else if(name=="reverse") + else if(name==reverse.name) return reverse; else - throw KeyError("Unknown control", name); + throw key_error(name); } float SimpleController::get_braking_distance() const @@ -58,17 +61,9 @@ void SimpleController::tick(const Time::TimeDelta &dt) { float secs = dt/Time::sec; if(speedtarget_speed.value) - speed = target_speed.value; - } + speed = min(speed+secs*accel, target_speed.value); else if(speed>target_speed.value) - { - speed -= secs*accel; - if(speed