aboutsummaryrefslogtreecommitdiff
path: root/client/map/tiles/floor_mesher.gd
blob: e424435d559653ef18151705fb48d5f4c91963bd (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# Hurry Curry! - a game about cooking
# Copyright 2024 nokoe
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
class_name FloorMesher
extends Object

const H := 0.25

const RECTANGLE: Array[Vector3] = [
	Vector3.ZERO,
	Vector3.RIGHT,
	Vector3.BACK + Vector3.RIGHT,
	Vector3.BACK, Vector3.ZERO,
	Vector3.BACK + Vector3.RIGHT,
]
const UV: Array[Vector2] = [
	Vector2.ZERO,
	Vector2.RIGHT,
	Vector2.DOWN + Vector2.RIGHT,
	Vector2.DOWN,
	Vector2.ZERO,
	Vector2.DOWN + Vector2.RIGHT,
]

var tiles: Dictionary[String, Vector2i]
var mesh_instance: MeshInstance3D

func _init(mesh: MeshInstance3D) -> void:
	mesh_instance = mesh

func add_tile(pos: Vector2i):
	tiles[str(pos)] = pos

func remove_tile(pos: Vector2i):
	tiles.erase(str(pos))

func flush() -> void:
	if tiles.is_empty(): return
	var tiles_queue = tiles.duplicate()
	var st := SurfaceTool.new()
	st.begin(Mesh.PRIMITIVE_TRIANGLES)

	while not tiles_queue.is_empty():
		var key: String = tiles_queue.keys()[0]
		var tile_position: Vector2i = tiles_queue[key]
		var shift := Vector2i.ZERO
		# move the origin of the rectangle to the upper left corner
		while true:
			#if tiles_queue.has(str(tile_position + shift + Vector2i.UP + Vector2i.LEFT)):
			#	shift += Vector2i.UP + Vector2i.LEFT
			if tiles_queue.has(str(tile_position + shift + Vector2i.UP)):
				shift += Vector2i.UP
			elif tiles_queue.has(str(tile_position + shift + Vector2i.LEFT)):
				shift += Vector2i.LEFT
			else:
				break

		var rectangle_origin := tile_position + shift
		var rectangle := Vector2i.ONE

		# go down as long as possible
		while tiles_queue.has(str(rectangle_origin + Vector2i(0, rectangle.y))):
			rectangle.y += 1

		# try to go right as long as possible by checking if whole columns are long enough
		while true:
			var column_possible := true
			for i in range(0, rectangle.y):
				column_possible = tiles_queue.has(str(rectangle_origin + Vector2i(rectangle.x, i)))
				if not column_possible: break
			if column_possible:
				rectangle.x += 1
			else: break

		# floor
		for i in range(UV.size()):
			st.set_normal(Vector3.UP)
			st.set_uv(UV[i] * Vector2(rectangle))
			st.add_vertex(RECTANGLE[i] * Vector3(rectangle.x, 0., rectangle.y) + Vector3(rectangle_origin.x, 0., rectangle_origin.y))

		# the faces on the side (should be improved...)
		# back
		for i in range(UV.size()):
			st.set_normal(Vector3.BACK)
			st.set_uv(UV[i] * Vector2(rectangle.x, H))
			st.add_vertex((RECTANGLE[i] * Vector3(rectangle.x, 0., -H)).rotated(Vector3.RIGHT, 0.5 * PI)
				+ Vector3(rectangle_origin.x, -H, rectangle_origin.y))
		# right
		for i in range(UV.size()):
			st.set_normal(Vector3.RIGHT)
			st.set_uv(UV[i] * Vector2(H, rectangle.y))
			st.add_vertex((RECTANGLE[i] * Vector3(H, 0., rectangle.y)).rotated(Vector3.BACK, 0.5 * PI)
				+ Vector3(rectangle_origin.x, -H, rectangle_origin.y))
		# left
		for i in range(UV.size()):
			st.set_normal(Vector3.LEFT)
			st.set_uv(UV[i] * Vector2(H, rectangle.y))
			st.add_vertex((RECTANGLE[i] * Vector3(H, 0., -rectangle.y)).rotated(Vector3.BACK, 0.5 * PI)
				+ Vector3(rectangle_origin.x + rectangle.x, -H, rectangle_origin.y + rectangle.y))
		# back
		for i in range(UV.size()):
			st.set_normal(Vector3.BACK)
			st.set_uv(UV[i] * Vector2(rectangle.x, H))
			st.add_vertex((RECTANGLE[i] * Vector3(-rectangle.x, 0., -H)).rotated(Vector3.RIGHT, 0.5 * PI)
				+ Vector3(rectangle_origin.x + rectangle.x, -H, rectangle_origin.y + rectangle.y))

		# remove the rectangle from the "queue"
		for x in range(rectangle_origin.x, rectangle_origin.x + rectangle.x):
			for y in range(rectangle_origin.y, rectangle_origin.y + rectangle.y):
				tiles_queue.erase(str(Vector2i(x, y)))

	st.index()
	st.optimize_indices_for_cache()
	mesh_instance.mesh = st.commit()