-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.gd
92 lines (77 loc) · 2.35 KB
/
world.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
extends Node2D
const tile = preload("res://tile.tscn")
const unit_test = preload("res://units/unit_test.tscn")
const unit = preload("res://units/unit.tscn")
const TILE_SIZE = 32
onready var as = AStar.new()
#just for test
var map = []
var map_tiles = []
func _ready():
var i = 0
var f_in = File.new()
f_in.open("res://map.json",File.READ)
var line = {}
line.parse_json(f_in.get_line())
map = line['map']
map_tiles = line['map_tiles']
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.set_pos(Vector2(x*TILE_SIZE+TILE_SIZE/2,y*TILE_SIZE+TILE_SIZE/2))
tile_new.get_node("tiles").set_frame(map_tiles[y][x])
add_child(tile_new)
as.add_point(i,Vector3(x,y,0))
i += 1
#Connects cells right and down. Possible duplicates ?
i = 0
for y in range(map.size()):
for x in range(map[0].size()):
if y > 0 and x > 0 and y < map.size() and x < map[0].size():
if map[y][x] == 0 and map[y][x+1] == 0:
as.connect_points(i,i+1)
if map[y][x] == 0 and map[y+1][x] == 0:
as.connect_points(i,i+map[0].size())
if map[y][x] == 0 and map[y+1][x+1] == 0:
as.connect_points(i,i+1+map[0].size())
i += 1
add_unit_to_id(33)
add_unit(66)
set_process_input(true)
func add_unit(id):
var unit_new = unit.instance()
unit_new.set_pos(id_to_px(id))
unit_new.set_z(2)
fill_cell(id)
add_child(unit_new)
func add_unit_to_id(id):
var unit_new = unit_test.instance()
unit_new.set_pos(id_to_px(id))
unit_new.set_z(2)
add_child(unit_new)
func fill_cell(id):
pass
func px_to_id(pos):
var x = floor(pos.x/TILE_SIZE)
var y = floor(pos.y/TILE_SIZE)
var id = y * map[0].size() + x
return id
func id_to_px(id):
id = int(id)
var x = id % map[0].size()
var y = id / map[0].size()
return Vector2(x*TILE_SIZE+TILE_SIZE/2,y*TILE_SIZE+TILE_SIZE/2)
func id_to_cell(id):
id = int(id)
var x = id % map[0].size()
var y = id / map[0].size()
return Vector2(x,y)
func _input(event):
if event.type == InputEvent.MOUSE_BUTTON && event.button_index == 1 && event.pressed && not event.is_echo():
if get_node("unit_test").path.size() == 0:
get_node("unit_test").end_id = px_to_id(event.pos)
get_node("unit_test").path = as.get_id_path(get_node("unit_test").start_id,get_node("unit_test").end_id)
get_node("unit_test").walk()
else:
print("still walking")