120 lines
2.4 KiB
Lua
120 lines
2.4 KiB
Lua
function world_init()
|
|
worldtiles = {}
|
|
--test start
|
|
bob = build(3,2,buildings.jam_workshop)
|
|
bob = replace(bob, buildings.city)
|
|
--destroy(get_worldtile(3,2))
|
|
--test end
|
|
end
|
|
|
|
function world_update()
|
|
if (btnp(4)) build(3,2,buildings.jam_workshop)
|
|
if (btnp(5)) compute_effect_array(worldtiles)
|
|
if (time()%1 == 0) compute_effect_array(worldtiles)
|
|
end
|
|
|
|
function world_draw()
|
|
draw_array(worldtiles)
|
|
end
|
|
|
|
|
|
buildings =
|
|
{
|
|
jam_workshop =
|
|
{
|
|
sprite = 1,
|
|
inputs =
|
|
{
|
|
{
|
|
type = "fruit",
|
|
quantity = 2
|
|
}
|
|
},
|
|
outputs =
|
|
{
|
|
{
|
|
type = "jam",
|
|
quantity = 3
|
|
}
|
|
}
|
|
},
|
|
city =
|
|
{
|
|
sprite = 5,
|
|
inputs =
|
|
{
|
|
{
|
|
type = "jam",
|
|
quantity = 1
|
|
}
|
|
},
|
|
outputs =
|
|
{
|
|
{
|
|
type = "money",
|
|
quantity = 2
|
|
}
|
|
}
|
|
},
|
|
}
|
|
|
|
function compute_effect_array(array)
|
|
for obj in all(array) do
|
|
obj.compute_effet(obj)
|
|
end
|
|
end
|
|
|
|
function draw_array(array)
|
|
for obj in all(array) do
|
|
obj.draw(obj)
|
|
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.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
|
|
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
|