-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.gd
219 lines (186 loc) · 5.05 KB
/
main.gd
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
extends Node2D
const tile = preload("res://tile.tscn")
const TILE_SIZE = 32
var current_button = 0
var mouse_down = false
var map = []
var map_tiles = []
var file_name = "map_template"
func _ready():
if global.edit:
file_name = global.file_name
var f_in = File.new()
f_in.open("res://maps/"+file_name+".json",File.READ)
var line = {}
line.parse_json(f_in.get_line())
map = line['map']
map_tiles = line['map_tiles']
get_node("player_1").set_pos(Vector2(line['player_1_x'],line['player_1_y']))
get_node("player_2").set_pos(Vector2(line['player_2_x'],line['player_2_y']))
set_process_input(true)
get_node("gui/buttons/tile_grass").set_disabled(true)
for y in range(map.size()):
for x in range(map[0].size()):
var tile_new = tile.instance()
tile_new.set_name("tile_"+str(y)+"_"+str(x))
tile_new.x = x
tile_new.y = y
tile_new.set_pos(Vector2(x*TILE_SIZE+TILE_SIZE/2+100,y*TILE_SIZE+TILE_SIZE/2))
tile_new.get_node("tiles").set_frame(map_tiles[y][x])
add_child(tile_new)
func buttons_enable():
for button in get_node("gui/buttons").get_button_list():
button.set_disabled(false)
func _on_tile_grass_pressed():
current_button = 0
buttons_enable()
get_node("gui/buttons/tile_grass").set_disabled(true)
func _on_tile_cliff_pressed():
current_button = 1
buttons_enable()
get_node("gui/buttons/tile_cliff").set_disabled(true)
func _on_tile_p1_pressed():
current_button = 2
buttons_enable()
get_node("gui/buttons/tile_p1").set_disabled(true)
func _on_tile_p2_pressed():
current_button = 3
buttons_enable()
get_node("gui/buttons/tile_p2").set_disabled(true)
func _input(event):
if event.type == InputEvent.MOUSE_BUTTON && event.button_index == 1:
mouse_down = event.pressed
func neighbours_adjust(x,y):
if y > 0:
if map[y-1][x] == 1:
set_tile(x,y-1,false)
if x < map[0].size()-2:
if map[y][x+1] == 1:
set_tile(x+1,y,false)
if y < map.size()-2:
if map[y+1][x] == 1:
set_tile(x,y+1,false)
if x > 0:
if map[y][x-1] == 1:
set_tile(x-1,y,false)
func set_tile(x,y,adjust):
var tile_new = get_node("tile_"+str(y)+"_"+str(x)+"/tiles")
if map[y-1][x] == 1 \
and map[y][x+1] == 1 \
and map[y+1][x] == 1 \
and map[y][x-1] == 1:
tile_new.set_frame(5)
map_tiles[y][x] = 5
elif map[y-1][x] == 0 \
and map[y][x+1] == 1 \
and map[y+1][x] == 1 \
and map[y][x-1] == 1:
tile_new.set_frame(2)
map_tiles[y][x] = 2
elif map[y-1][x] == 1 \
and map[y][x+1] == 0 \
and map[y+1][x] == 1 \
and map[y][x-1] == 1:
tile_new.set_frame(6)
map_tiles[y][x] = 6
elif map[y-1][x] == 1 \
and map[y][x+1] == 1 \
and map[y+1][x] == 0 \
and map[y][x-1] == 1:
tile_new.set_frame(8)
map_tiles[y][x] = 8
elif map[y-1][x] == 1 \
and map[y][x+1] == 1 \
and map[y+1][x] == 1 \
and map[y][x-1] == 0:
tile_new.set_frame(4)
map_tiles[y][x] = 4
elif map[y-1][x] == 0 \
and map[y][x+1] == 0 \
and map[y+1][x] == 1 \
and map[y][x-1] == 1:
tile_new.set_frame(3)
map_tiles[y][x] = 3
elif map[y-1][x] == 0 \
and map[y][x+1] == 1 \
and map[y+1][x] == 1 \
and map[y][x-1] == 0:
tile_new.set_frame(1)
map_tiles[y][x] = 1
elif map[y-1][x] == 1 \
and map[y][x+1] == 1 \
and map[y+1][x] == 0 \
and map[y][x-1] == 0:
tile_new.set_frame(7)
map_tiles[y][x] = 7
elif map[y-1][x] == 1 \
and map[y][x+1] == 0 \
and map[y+1][x] == 0 \
and map[y][x-1] == 1:
tile_new.set_frame(9)
map_tiles[y][x] = 9
elif map[y-1][x] == 0 \
and map[y][x+1] == 0 \
and map[y+1][x] == 0 \
and map[y][x-1] == 0:
tile_new.set_frame(10)
map_tiles[y][x] = 10
elif map[y-1][x] == 1 \
and map[y][x+1] == 0 \
and map[y+1][x] == 0 \
and map[y][x-1] == 0:
tile_new.set_frame(11)
map_tiles[y][x] = 11
elif map[y-1][x] == 0 \
and map[y][x+1] == 0 \
and map[y+1][x] == 1 \
and map[y][x-1] == 0:
tile_new.set_frame(12)
map_tiles[y][x] = 12
elif map[y-1][x] == 1 \
and map[y][x+1] == 0 \
and map[y+1][x] == 1 \
and map[y][x-1] == 0:
tile_new.set_frame(13)
map_tiles[y][x] = 13
elif map[y-1][x] == 0 \
and map[y][x+1] == 1 \
and map[y+1][x] == 0 \
and map[y][x-1] == 0:
tile_new.set_frame(14)
map_tiles[y][x] = 14
elif map[y-1][x] == 0 \
and map[y][x+1] == 1 \
and map[y+1][x] == 0 \
and map[y][x-1] == 1:
tile_new.set_frame(15)
map_tiles[y][x] = 15
elif map[y-1][x] == 0 \
and map[y][x+1] == 0 \
and map[y+1][x] == 0 \
and map[y][x-1] == 1:
tile_new.set_frame(16)
map_tiles[y][x] = 16
else:
tile_new.set_frame(5)
map_tiles[y][x] = 5
if adjust:
map[y][x] = 1
neighbours_adjust(x,y)
func _on_tile_save_pressed():
var a = 3
var f_out = File.new()
var out = {
player_1_x=get_node("player_1").get_pos().x,
player_1_y=get_node("player_1").get_pos().y,
player_2_x=get_node("player_2").get_pos().x,
player_2_y=get_node("player_2").get_pos().y,
map=map,
map_tiles=map_tiles
}
f_out.open("res://maps/"+global.file_name+".json",File.WRITE)
f_out.store_line(out.to_json())
f_out.close()
get_node("gui/AcceptDialog").popup()
func _on_tile_menu_pressed():
get_tree().change_scene("menu.tscn")