ag.import("std") td = {mx = 0, my = 0, picking = false, pick_x = 0, pick_y = 0} function init_event() ground = std.createPlanePointNormal(0, 0, 0, 0, 0, 1) ground:setColor(0, 1, 0) grid_texture = ag.loadTexture("grid.png") grid = ag.createQuad(0, 0, 0, 8, 0, 0, 0, 8, 0) grid:setTexture(grid_texture) grid:setOffset(1) grid_selector = ag.createQuad(0, 0, 0, 0.5, 0, 0, 0, 0.5, 0) grid_selector:setVisible(false) grid_selector:setColor(0, 0, 1, 0.5) grid_selector:setBlending(true) grid_selector:setOffset(2) ag.setCamera(2, -12, 10) ag.setCursorVisible(true) end function key_down_event(k) if (k == "q") then ag.exit() end end function update_event() hit = ag.pickOne(td.mx, td.my, ground) if (hit) then local px = ground.pick_pos[1] local py = ground.pick_pos[2] local x_coord = math.floor(px + 8) local y_coord = math.floor(py + 8) td.picking = (x_coord >= 0 and x_coord <= 15 and y_coord >= 0 and y_coord <= 15) if (td.picking) then grid_selector:setPosition(x_coord - 7.5, y_coord - 7.5, 0) td.pick_x = x_coord td.pick_y = y_coord end end grid_selector:setVisible(td.picking) end function mouse_motion_event(x, y) td.mx = x td.my = y end function mousebutton_down_event(button, x, y) if (button == 1) then if td.picking then local s = ag.createSphere(0.48) s:setColor(math.random(), math.random(), math.random()) s:setPosition(td.pick_x - 7.5, td.pick_y - 7.5, 0.6) end end end