aboutsummaryrefslogtreecommitdiff
path: root/src/layers.moon
diff options
context:
space:
mode:
Diffstat (limited to 'src/layers.moon')
-rw-r--r--src/layers.moon151
1 files changed, 151 insertions, 0 deletions
diff --git a/src/layers.moon b/src/layers.moon
new file mode 100644
index 0000000..6b6de6f
--- /dev/null
+++ b/src/layers.moon
@@ -0,0 +1,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