]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.h
Make the axles of vehicles rotate when moving
[r2c2.git] / source / libr2c2 / vehicle.h
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBR2C2_VEHICLE_H_
9 #define LIBR2C2_VEHICLE_H_
10
11 #include "geometry.h"
12
13 namespace R2C2 {
14
15 class Layout;
16 class Track;
17 class VehicleType;
18
19 class Vehicle
20 {
21 public:
22         enum PlaceMode
23         {
24                 CENTER,
25                 FRONT_AXLE,
26                 FRONT_BUFFER,
27                 BACK_AXLE,
28                 BACK_BUFFER
29         };
30
31 private:
32         struct TrackPosition
33         {
34                 Track *track;
35                 unsigned ep;
36                 float offs;
37
38                 TrackPosition();
39                 TrackPosition(Track *, unsigned, float);
40                 void advance(float);
41                 TrackPoint get_point() const;
42         };
43
44         struct Rod
45         {
46                 Point position;
47                 float angle;
48
49                 Rod();
50         };
51
52         Layout &layout;
53         const VehicleType &type;
54         Vehicle *next;
55         Vehicle *prev;
56         TrackPosition track_pos;
57         Point position;
58         float direction;
59         std::vector<float> bogie_dirs;
60         std::vector<std::vector<float> > axle_angles;
61         std::vector<Rod> rods;
62         unsigned front_sensor;
63         unsigned back_sensor;
64
65 public:
66         Vehicle(Layout &, const VehicleType &);
67         ~Vehicle();
68
69         const VehicleType &get_type() const { return type; }
70
71         void attach_back(Vehicle &);
72         void attach_front(Vehicle &);
73         void detach_back();
74         void detach_front();
75         Vehicle *get_next() const { return next; }
76         Vehicle *get_previous() const { return prev; }
77
78         void place(Track &, unsigned, float, PlaceMode = CENTER);
79         void unplace();
80         void advance(float);
81         Track *get_track() const { return track_pos.track; }
82         unsigned get_entry() const { return track_pos.ep; }
83         float get_offset() const { return track_pos.offs; }
84         const Point &get_position() const { return position; }
85         float get_direction() const { return direction; }
86         float get_axle_angle(unsigned) const;
87         float get_bogie_direction(unsigned) const;
88         float get_bogie_axle_angle(unsigned, unsigned) const;
89         const Point &get_rod_position(unsigned) const;
90         float get_rod_angle(unsigned) const;
91 private:
92         void update_position();
93         void update_position_from(const Vehicle &);
94         void propagate_position();
95         void propagate_forward();
96         void propagate_backward();
97         void check_sensor(float, unsigned &);
98         void turn_axles(float);
99         void update_rods();
100
101         void adjust_for_distance(TrackPosition &, TrackPosition &, float, float = 0.5) const;
102         TrackPoint get_point(const Point &, const Point &, float = 0.5) const;
103         TrackPoint get_point(const TrackPosition &, float, float = 0.5) const;
104 };
105
106 } // namespace R2C2
107
108 #endif