-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathenvironment.gd
More file actions
170 lines (138 loc) · 5.01 KB
/
environment.gd
File metadata and controls
170 lines (138 loc) · 5.01 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
extends Node2D
var map: TileMap
enum MAP_LAYER {
BACKGROUND,
WATERLOGGED,
SURFACE,
}
enum TERRAINS {
BLANK = -1,
GRASS,
WATER,
}
enum WATERLOGGED {
BLANK = -1,
BARRIER,
ROCK,
}
enum SURFACE {
BLANK = -1,
ROCK,
TREE,
}
const BARRIER_SOURCE: int = 2
const BARRIER_ATLAS_COORDS: Vector2i = Vector2i(9, 14)
const WATERLOGGED_ROCK_SOURCE: int = 3
const WATERLOGGED_ROCK_ATLAS_COORDS: Vector2i = Vector2i(0,0)
const SURFACE_ROCK_SOURCE: int = 3
const SURFACE_ROCK_ATLAS_COORDS: Vector2i = Vector2i(0, 0)
const TREE_SOURCE: int = 4
const TREE_ATLAS_COORDS: Vector2i = Vector2i(0, 0)
func _ready():
map = get_node("TileMap")
func get_tile_coords(coords: Vector2) -> Vector2i:
return map.local_to_map(to_local(coords))
func get_mouse_tile_coords() -> Vector2i:
return map.local_to_map(map.get_local_mouse_position())
func get_background(coords: Vector2i) -> int:
var tile: TileData = map.get_cell_tile_data(MAP_LAYER.BACKGROUND, coords)
if tile == null:
return TERRAINS.BLANK
return tile.get_terrain()
func get_waterlogged(coords: Vector2i) -> int:
# var waterlogged: TileData = map.get_cell_tile_data(MAP_LAYER.WATERLOGGED, coords)
var source: int = map.get_cell_source_id(MAP_LAYER.WATERLOGGED, coords)
var atlas_coords: Vector2i = map.get_cell_atlas_coords(MAP_LAYER.WATERLOGGED, coords)
match [source, atlas_coords]:
[-1, _]:
return WATERLOGGED.BLANK
[BARRIER_SOURCE, BARRIER_ATLAS_COORDS]:
return WATERLOGGED.BARRIER
[WATERLOGGED_ROCK_SOURCE, WATERLOGGED_ROCK_ATLAS_COORDS]:
return WATERLOGGED.ROCK
return -2
func get_surface(coords: Vector2i) -> int:
# var waterlogged: TileData = map.get_cell_tile_data(MAP_LAYER.WATERLOGGED, coords)
var source: int = map.get_cell_source_id(MAP_LAYER.SURFACE, coords)
var atlas_coords: Vector2i = map.get_cell_atlas_coords(MAP_LAYER.SURFACE, coords)
match [source, atlas_coords]:
[-1, _]:
return SURFACE.BLANK
[SURFACE_ROCK_SOURCE, SURFACE_ROCK_ATLAS_COORDS]:
return SURFACE.ROCK
[TREE_SOURCE, TREE_ATLAS_COORDS]:
return SURFACE.TREE
return -2
func set_background(coords: Vector2i, key: int, force: bool = false) -> bool:
var background: int = get_background(coords)
var waterlogged: int = get_waterlogged(coords)
match key:
TERRAINS.BLANK:
# If there is water, check for waterlogged object
if background == TERRAINS.WATER:
if waterlogged != WATERLOGGED.BARRIER and not force:
return false
map.set_cell(MAP_LAYER.WATERLOGGED, coords, -1, Vector2i(-1, -1))
# Remove Terrain, with tileset matching
map.set_cells_terrain_connect(MAP_LAYER.BACKGROUND, [coords], 0, TERRAINS.BLANK)
TERRAINS.GRASS:
# If there is water, check for waterlogged object
if background == TERRAINS.WATER:
if waterlogged != WATERLOGGED.BARRIER and not force:
return false
map.set_cell(MAP_LAYER.WATERLOGGED, coords, -1, Vector2i(-1, -1))
# Set grass, with tileset matching
map.set_cells_terrain_connect(MAP_LAYER.BACKGROUND, [coords], 0, TERRAINS.GRASS, false)
TERRAINS.WATER:
if waterlogged == WATERLOGGED.BLANK:
map.set_cell(MAP_LAYER.WATERLOGGED, coords, BARRIER_SOURCE, BARRIER_ATLAS_COORDS)
map.set_cells_terrain_connect(MAP_LAYER.BACKGROUND, [coords], 0, TERRAINS.WATER, false)
return true
func set_waterlogged(coords: Vector2i, key: int, force: bool = false) -> bool:
var background: int = get_background(coords)
match key:
WATERLOGGED.BLANK:
# If tile isn't water or if force is enabled,
# set waterlogged tile to blank
if background != TERRAINS.WATER or force:
map.set_cell(MAP_LAYER.WATERLOGGED, coords, -1, Vector2i(-1, -1))
else:
return false
WATERLOGGED.BARRIER:
# If tile is water or if force is enabled,
# set waterlogged tile to water
if background == TERRAINS.WATER or force:
map.set_cell(MAP_LAYER.WATERLOGGED, coords, BARRIER_SOURCE, BARRIER_ATLAS_COORDS)
else:
return false
WATERLOGGED.ROCK:
# If tile is water or if force is enabled,
# set waterlogged tile to rock
if background == TERRAINS.WATER or force:
map.set_cell(MAP_LAYER.WATERLOGGED, coords, WATERLOGGED_ROCK_SOURCE, WATERLOGGED_ROCK_ATLAS_COORDS)
else:
return false
return true
## May be removed if these will be implemented as sprites
func set_surface(coords: Vector2i, key: int, force: bool = false) -> bool:
var background: int = get_background(coords)
var surface: int = get_surface(coords)
match key:
SURFACE.BLANK:
# Set blank
map.set_cell(MAP_LAYER.SURFACE, coords, -1, Vector2i(-1, -1))
SURFACE.ROCK:
# If tile isn't water and space is empty, or if force is enabled,
# set tile to rock
if (background != TERRAINS.WATER and surface == SURFACE.BLANK) or force:
map.set_cell(MAP_LAYER.SURFACE, coords, SURFACE_ROCK_SOURCE, SURFACE_ROCK_ATLAS_COORDS)
else:
return false
SURFACE.TREE:
# If tile isn't water and space is empty, or if force is enabled,
# set tile to tree
if (background != TERRAINS.WATER and surface == SURFACE.BLANK) or force:
map.set_cell(MAP_LAYER.SURFACE, coords, TREE_SOURCE, TREE_ATLAS_COORDS)
else:
return false
return true