summaryrefslogtreecommitdiff
path: root/game/network/rail.h
blob: 3b3da426afc72dfac04a0e5f88ea9e838d4dab6f (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
67
68
69
70
71
72
73
74
75
#ifndef RAILLINKS_H
#define RAILLINKS_H

#include "game/worldobject.h"
#include "gfx/models/mesh.h"
#include "gfx/renderable.h"
#include "link.h"
#include "network.h"
#include <glm/glm.hpp>
#include <location.hpp>
#include <maths.h>
#include <memory>
#include <span>
#include <utility>

class Shader;
class Vertex;

// A piece of rail track
class RailLink : public Link, public Renderable {
public:
	using Link::Link;

	void render(const Shader &) const override;

protected:
	[[nodiscard]] static MeshPtr defaultMesh(const std::span<Vertex> vertices);

	MeshPtr mesh;
};

class RailLinkStraight : public RailLink {
public:
	RailLinkStraight(const NodePtr &, const NodePtr &);
	[[nodiscard]] Location positionAt(float dist, unsigned char start) const override;

private:
	RailLinkStraight(NodePtr, NodePtr, const glm::vec3 & diff);
};

class RailLinkCurve : public RailLink {
public:
	RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec2);
	[[nodiscard]] Location positionAt(float dist, unsigned char start) const override;

private:
	RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec3, const Arc);
	glm::vec3 centreBase;
	float radius;
	Arc arc;
};

template<typename T> concept RailLinkConcept = std::is_base_of_v<RailLink, T>;

class RailLinks : public NetworkOf<RailLink>, public WorldObject {
public:
	RailLinks();
	template<RailLinkConcept T, typename... Params>
	std::shared_ptr<T>
	addLink(glm::vec3 a, glm::vec3 b, Params &&... params)
	{
		const auto node1 = nodeAt(a), node2 = nodeAt(b);
		auto l {links.create<T>(node1, node2, std::forward<Params>(params)...)};
		joinLinks(l);
		return l;
	}

	std::shared_ptr<RailLink> addLinksBetween(glm::vec3 start, glm::vec3 end);

private:
	void tick(TickDuration elapsed) override;
	void joinLinks(const LinkPtr &) const;
};

#endif