summaryrefslogtreecommitdiff
path: root/game/network/network.h
blob: d561e558054b1583de8cb9d433186561f58d33b5 (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
#pragma once

#include "link.h"
#include <collection.hpp>
#include <gfx/renderable.h>
#include <glm/glm.hpp>
#include <memory>
#include <set>
#include <sorting.hpp>
#include <string>
#include <utility>
#include <variant>

class Texture;
class Shader;
class Ray;

class Network {
public:
	using LinkEnd = std::pair<LinkPtr, unsigned char>;
	explicit Network(const std::string & textureName);
	virtual ~Network() = default;

	[[nodiscard]] NodePtr findNodeAt(glm::vec3) const;
	[[nodiscard]] NodePtr nodeAt(glm::vec3);
	[[nodiscard]] std::pair<NodePtr, bool> newNodeAt(glm::vec3);
	[[nodiscard]] virtual LinkPtr intersectRayLinks(const Ray &) const = 0;
	[[nodiscard]] virtual NodePtr intersectRayNodes(const Ray &) const;

	[[nodiscard]] Link::Nexts routeFromTo(const Link::End &, glm::vec3) const;
	[[nodiscard]] Link::Nexts routeFromTo(const Link::End &, const NodePtr &) const;

	virtual LinkCPtr addStraight(glm::vec3, glm::vec3) = 0;
	virtual CLinks addJoins(glm::vec3, glm::vec3) = 0;

protected:
	static void joinLinks(const LinkPtr & l, const LinkPtr & ol);

	using Nodes = std::set<NodePtr, PtrSorter<NodePtr>>;
	Nodes nodes;
	std::shared_ptr<Texture> texture;
};

template<typename T> class NetworkOf : public Network, public Renderable {
protected:
	using Network::Network;

	Collection<T> links;
	void joinLinks(const LinkPtr &) const;

protected:
	[[nodiscard]] LinkPtr intersectRayLinks(const Ray &) const override;

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

	LinkCPtr addStraight(glm::vec3 n1, glm::vec3 n2) override;
	CLinks addJoins(glm::vec3, glm::vec3) override;

	void render(const Shader &) const override;
};