]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/signal.cpp
Add a common base class for tangible objects
[r2c2.git] / source / libr2c2 / signal.cpp
index a8b0be029799afdcec0c5c379b2788e31919583d..72cf235193e6a9c537e67a003ede7a221b8c7673 100644 (file)
@@ -13,7 +13,7 @@ using namespace Msp;
 namespace R2C2 {
 
 Signal::Signal(Layout &l, const SignalType &t):
-       layout(l),
+       Object(l),
        type(t),
        address(0),
        track(0),
@@ -33,6 +33,14 @@ Signal::~Signal()
        layout.remove_signal(*this);
 }
 
+Signal *Signal::clone(Layout *to_layout) const
+{
+       Signal *sig = new Signal((to_layout ? *to_layout : layout), type);
+       sig->set_position(position);
+       sig->set_rotation(rotation);
+       return sig;
+}
+
 void Signal::set_address(unsigned a)
 {
        address = a;
@@ -53,7 +61,7 @@ void Signal::set_position(const Vector &p)
                        if(d<dist || dist<0)
                        {
                                position = n.pos;
-                               direction = n.dir;
+                               rotation = n.dir;
                                track = *i;
                                dist = d;
                        }
@@ -69,7 +77,7 @@ void Signal::normalize_location()
        unsigned n_endpoints = track->get_type().get_endpoints().size();
        for(unsigned j=0; j<n_endpoints; ++j)
        {
-               float a = track->get_endpoint_direction(j)-direction;
+               float a = track->get_endpoint_direction(j)-rotation;
                while(a<-M_PI/2)
                        a += M_PI*2;
                while(a>M_PI*3/2)
@@ -82,23 +90,37 @@ void Signal::normalize_location()
        }
 }
 
-void Signal::set_direction(float d)
+void Signal::set_rotation(float r)
 {
-       float a = direction-d;
+       float a = rotation-r;
        while(a>M_PI*3/2)
                a -= M_PI*2;
        while(a<-M_PI/2)
                a += M_PI*2;
        if(a>=M_PI/2)
        {
-               direction += M_PI;
-               if(direction>M_PI*2)
-                       direction -= M_PI*2;
+               rotation += M_PI;
+               if(rotation>M_PI*2)
+                       rotation -= M_PI*2;
        }
 
        normalize_location();
 }
 
+bool Signal::collide_ray(const Vector &start, const Vector &ray) const
+{
+       // XXX Totally hardcoded stuff, should be replaced with a geometry system
+       Vector center = position;
+       center.x += sin(rotation)*0.035;
+       center.y -= cos(rotation)*0.035;
+       Vector d(center.x-start.x, center.y-start.y);
+       float x = (d.x*ray.x+d.y*ray.y)/(ray.x*ray.x+ray.y*ray.y);
+       Vector nearest(start.x+ray.x*x-center.x, start.y+ray.y*x-center.y, start.z+ray.z*x-center.z);
+       if(nearest.z<0|| nearest.z>0.12)
+               return false;
+       return nearest.x*nearest.x+nearest.y*nearest.y<0.0001;
+}
+
 void Signal::tick(const Time::TimeDelta &)
 {
        if(check_allocated_blocks)
@@ -176,7 +198,7 @@ void Signal::reset()
 void Signal::save(list<DataFile::Statement> &st) const
 {
        st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
-       st.push_back((DataFile::Statement("direction"), direction));
+       st.push_back((DataFile::Statement("rotation"), rotation));
        if(address)
                st.push_back((DataFile::Statement("address"), address));
 }
@@ -185,9 +207,9 @@ void Signal::save(list<DataFile::Statement> &st) const
 Signal::Loader::Loader(Signal &s):
        DataFile::ObjectLoader<Signal>(s)
 {
-       add("address",   &Loader::address);
-       add("direction", &Loader::direction);
-       add("position",  &Loader::position);
+       add("address",  &Loader::address);
+       add("position", &Loader::position);
+       add("rotation", &Loader::rotation);
 }
 
 void Signal::Loader::address(unsigned a)
@@ -195,14 +217,14 @@ void Signal::Loader::address(unsigned a)
        obj.set_address(a);
 }
 
-void Signal::Loader::direction(float d)
+void Signal::Loader::position(float x, float y, float z)
 {
-       obj.set_direction(d);
+       obj.set_position(Vector(x, y, z));
 }
 
-void Signal::Loader::position(float x, float y, float z)
+void Signal::Loader::rotation(float d)
 {
-       obj.set_position(Vector(x, y, z));
+       obj.set_rotation(d);
 }
 
 } // namespace R2C2