summaryrefslogtreecommitdiff
path: root/game/vehicles/train.cpp
blob: 5bddd612cb779162dfc15422e42a10d3a9c1d1f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "train.h"
#include "game/vehicles/linkHistory.h"
#include "game/vehicles/railVehicle.h"
#include "game/vehicles/railVehicleClass.h"
#include "gfx/renderable.h"
#include "location.h"
#include <algorithm>
#include <functional>
#include <optional>
#include <utility>

template<typename> class Ray;

Location
Train::getBogiePosition(float linkDist, float dist) const
{
	float b2linkDist {};
	const auto b2Link = linkHist.getAt(dist - linkDist, &b2linkDist);
	return b2Link.first->positionAt(b2linkDist, b2Link.second);
}

bool
Train::intersectRay(const Ray<GlobalPosition3D> & ray, BaryPosition & baryPos, RelativeDistance & distance) const
{
	return applyOne(&RailVehicle::intersectRay, ray, baryPos, distance) != end();
}

void
Train::tick(TickDuration dur)
{
	currentActivity->apply(this, dur);
	move(dur);

	float trailBy {0.F};
	apply(&RailVehicle::move, this, std::ref(trailBy));
}

void
Train::doActivity(Go * go, TickDuration dur)
{
	const auto maxSpeed = objects.front()->rvClass->maxSpeed;
	if (go->dist) {
		*go->dist -= speed * dur.count();
		if (*go->dist < (speed * speed) / 60.F) {
			speed -= std::min(speed, 30.F * dur.count());
		}
		else {
			if (speed != maxSpeed) {
				speed += ((maxSpeed - speed) * dur.count());
			}
		}
	}
	else {
		if (speed != maxSpeed) {
			speed += ((maxSpeed - speed) * dur.count());
		}
	}
}

void
Train::doActivity(Idle *, TickDuration dur)
{
	if (speed != 0.F) {
		speed -= std::min(speed, 30.F * dur.count());
	}
}