aboutsummaryrefslogtreecommitdiff
path: root/src/layers.moon
blob: 6b6de6f4503b3ac3a15e686da8871f0663965d2e (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
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
--Keeps track of layers
state = require "global"
disp = require "dispatch"
graph = require "graph"
mod = ...

class Icon
	new: (squaretype,position,layer) =>
		@type = squaretype
		@node = am.group! ^ {am.translate(position.x + 16, position.y - 16) ^ am.scale(1) ^ am.rotate(0) ^ am.sprite(@type.img_src)}
		layer.node\append(@node)
		@x = position.x
		@y = position.y

	__tostring: =>
		return string.format("<%s at (%d, %d)>",@type.type,@x,@y)


prototypes = {}
class IconPrototype
	new: (squaretype,img,cantoggle) =>
		@type = squaretype
		@img_src = img
		prototypes[@type] = @
		@cantoggle = cantoggle
	totile: (x,y,layer) =>
		ret = Icon(@,vec2(x,y),layer)
		ret

IconPrototype("liquid fuel thruster","data/icon_engine.png",false)
IconPrototype("ion thruster","data/icon_ionthrust.png",false)
IconPrototype("fuel pipe","data/pipe1111.png",true)
IconPrototype("liquid fuel tank","data/icon_fueltank.png",false)
IconPrototype("electric wire","data/icon_wire.png",true)
IconPrototype("generator", "data/icon_generator.png",false)
IconPrototype("battery","data/icon_battery.png",false)
IconPrototype("high volt cabel","data/icon_high_voltage.png",true)
IconPrototype("high volt source","data/icon_hv_source.png",false)
IconPrototype("laser beam","data/icon_laser_turret.png",false)

mod.check_paths = (tbl) ->
	for k, path in pairs(tbl)
		pathresult = graph.path(path[1],path[2],path[3],true,mod.Layer.tiles_adjacent)
		if pathresult == nil
			print("Failed to find path from",path[1], "to",path[2])
			return false
	return true

mod.check_hv_paths = (tbl) ->
	for k, path in pairs(tbl)
		pathresult = graph.path(path[1],path[2],path[3],true,mod.Layer.hv_tiles_adjacent)
		print("pathresult was:",pathresult)
		if pathresult == nil
			print("Failed to find path from",path[1], "to",path[2])
			return false
	return true

electricals = {
	["electric wire"]: true
	["generator"]: true
	["ion thruster"]: true
}
hvs = {
	["high volt cabel"]: true
	["high volt source"]: true
	["laser beam"]: true
}

class Layer
	new: (layername, default_node, node_cost, layer_icon) =>
		@name = layername
		@grid = {}
		@node = am.group!
		@nodelist = {} --A node least to easily remove all of them
		@default = default_node
		@icon = layer_icon
		@cost = node_cost
		print("Setting node action")

	mark_square: (x,y,proto) =>
		print("Marking square:",x,y)
		assert(proto,"Marking a square on a layer must have a type")
		assert(prototypes[proto], "Unkown square type:" .. proto)
		gpos = disp.normal_to_window(vec2(x*32,y*32))
		item = prototypes[proto]\totile(gpos.x, gpos.y, @)
		@grid[x] = @grid[x] or {}
		@grid[x][y] = item
		item
	
	tiles_adjacent: (node, neighbor) ->
		xdist = math.abs(neighbor.x - node.x)
		ydist = math.abs(neighbor.y - node.y)
		dist = ( xdist ^ 2) + (ydist ^ 2)
		ret = false
		if dist == (32 ^ 2)
			ret = true
		ret
	
	hv_tiles_adjacent: (node,neighbor) ->
		if mod.Layer.tiles_adjacent(node,neighbor)
			print("neighbor type.type:",neighbor.type.type)
			if hvs[node.type.type] and electricals[neighbor.type.type]
				error("High volt-normal error")
			return true
		else
			return false

	get_tile: (x,y) =>
		if @grid[x] and @grid[x][y]
			return @grid[x][y]
		return nil

	flat_tiles: =>
		ret = {}
		for _,row in pairs @grid
			for _,peice in pairs row
				table.insert(ret,peice)
		ret

	clear_square: (x,y) =>
		t = @get_tile(x,y)
		@node\remove(t.node)
		@grid[x][y] = nil
		print("Clearing:",t)

	toggle_square: (x,y,t) =>
		t = @get_tile(x,y)
		if t
			@clear_square(x,y)
		else
			@mark_square(x,y,t or @default)

	print_grid: =>
		for row = 1, math.floor((state.win.bottom - state.win.top) / 32)
			this_row = {}
			for col = 1, math.floor((state.win.right - state.win.left) / 32)
				if @grid[row] and @grid[row][col]
					table.insert(this_row,"1")
				else
					table.insert(this_row,"0")
			print(table.concat(this_row))

mod.Layer = Layer

mod.create_layer = (layername) ->
	ret = Layer(layername)

mod.gen_scene = () ->
	mod.node = am.group!

mod