diff --git a/choco.lua b/choco.lua index a124595..a632e7f 100644 --- a/choco.lua +++ b/choco.lua @@ -1,10 +1,10 @@ function _init() + world_init() playercontroler_init() - worldtiles = {} end function _update() - if (btnp(5)) compute_effect_array(worldtiles) + world_update() playercontroler_update() end @@ -12,7 +12,37 @@ function _draw() cls(0) map(0, 0, 0, 8, 64, 64) camera(0,0) - draw_array(worldtiles) draw_ressource_bar() + world_draw() playercontroler_draw() +end + +ressources = +{ + fruit = + { + quantity = 55, + sprite = 33, + x = 70 + }, + money = + { + quantity = 100, + sprite = 35, + x = 0 + }, + jam = + { + quantity = 100, + sprite = 34, + x = 100 + } +} + +function draw_ressource_bar() + rectfill(0, 0, 127, 7, 1) + for _,res in pairs(ressources) do + spr(res.sprite, res.x, 0) + print(res.quantity, res.x + 8, 1, 6) + end end \ No newline at end of file diff --git a/world.lua b/world.lua index 3101de0..66e4fa3 100644 --- a/world.lua +++ b/world.lua @@ -1,57 +1,62 @@ +function world_init() + worldtiles = {} + --test start + bob = build(3,2,buildings.jam_workshop) + bob = replace(bob, buildings.city) + destroy(bob) + --test end +end -ressources = +function world_update() + if (btnp(4)) build(3,2,buildings.jam_workshop) + if (btnp(5)) compute_effect_array(worldtiles) +end + +function world_draw() + draw_array(worldtiles) +end + + +buildings = { - fruits = - { - quantity = 5, - sprite = 33, - x = 70 - }, - money = - { - quantity = 100, - sprite = 35, - x = 0 - }, - jam = - { - quantity = 0, - sprite = 34, - x = 100 - } -} - -buildings = { jam_workshop = { sprite = 1, - inputs = { - {fruit = 2} + inputs = + { + { + type = "fruit", + quantity = 2 + } }, - outputs = {jam = 3} + outputs = + { + { + type = "jam", + quantity = 3 + } + } }, city = { sprite = 5, - inputs = {}, - outputs = {} + inputs = + { + { + type = "jam", + quantity = 1 + } + }, + outputs = + { + { + type = "money", + quantity = 2 + } + } }, } - -function draw_ressource_bar() - rectfill(0, 0, 127, 7, 1) - for _,res in pairs(ressources) do - spr(res.sprite, res.x, 0) - print(res.quantity, res.x + 8, 1, 6) - end -end - - -function updatecycle() - -end - function compute_effect_array(array) for obj in all(array) do obj.compute_effet(obj) @@ -64,20 +69,50 @@ function draw_array(array) end end - - function new_worldtile(x, y, building) local worldtile = {} worldtile.x = x worldtile.y = y worldtile.building = building worldtile.draw = function(this) - spr(worldtile.bulding.sprite,worldtile.x*2*8,8+worldtile.y*2*8, 2, 2) + spr(worldtile.building.sprite,worldtile.x*2*8,8+worldtile.y*2*8, 2, 2) end worldtile.compute_effet = function(this) + has_ressource = true for _,input in pairs(worldtile.building.inputs) do - --input. + if (ressources[input.type].quantity < input.quantity) has_ressource = false + end + if (has_ressource) then + for _,input in pairs(worldtile.building.inputs) do + ressources[input.type].quantity-=input.quantity + end + for _,output in pairs(worldtile.building.outputs) do + ressources[output.type].quantity+=output.quantity + end end end return worldtile end + +function build(x, y, building) + newtile = new_worldtile(x,y,building) + add(worldtiles, newtile) + return newtile +end + +function destroy(worldtile) + del(worldtiles, worldtile) +end + +function replace(worldtile, building) + destroy(worldtile) + return build(worldtile.x, worldtile.y, building) +end + +function get_worldtile(x,y) + for _,tile in pairs(worldtiles) do + if (tile.x == x and tile.y == y) then + return tile + end + end +end