love: added square grid example;added hex example
This commit is contained in:
parent
14a36ae366
commit
dc86672f5a
20
love/from_forums/draw_hex_example/main.lua
Normal file
20
love/from_forums/draw_hex_example/main.lua
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
hex = {}
|
||||
hex.centerX = 200
|
||||
hex.centerY = 200
|
||||
hex.size = 45
|
||||
function love.draw()
|
||||
love.graphics.setColor({255,255,255})
|
||||
local lastX = nil
|
||||
local lastY = nil
|
||||
for i=0,6 do
|
||||
local angle = 2 * math.pi / 6 * (i + 0.5)
|
||||
local x = hex.centerX + hex.size * math.cos(angle)
|
||||
local y = hex.centerY + hex.size * math.sin(angle)
|
||||
if i > 0 then
|
||||
love.graphics.line(lastX, lastY, x, y)
|
||||
end
|
||||
lastX = x
|
||||
lastY = y
|
||||
end
|
||||
end
|
81
love/from_forums/square_grid_example/main.lua
Normal file
81
love/from_forums/square_grid_example/main.lua
Normal file
@ -0,0 +1,81 @@
|
||||
local camera = {0, 0}
|
||||
local screen = {800,600}
|
||||
local squares = nil
|
||||
local squaresize = 50
|
||||
local maxx, maxy = 100, 100
|
||||
--love.window.setMode( 1024, 768 )
|
||||
function love.load()
|
||||
-- build the map
|
||||
squares = {}
|
||||
for y = 1, maxy do
|
||||
local row = {}
|
||||
for x = 1, maxx do
|
||||
local sq = {set = false}
|
||||
table.insert(row, sq)
|
||||
end
|
||||
table.insert(squares, row)
|
||||
end
|
||||
screen[1] = love.graphics.getWidth()
|
||||
screen[2] = love.graphics.getHeight()
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
-- draw the currently visible portion of the map
|
||||
-- this draws every partially visible square and relies
|
||||
-- on love drawing code to clip the edges
|
||||
local camx, camy = unpack(camera)
|
||||
local scrx, scry = unpack(screen)
|
||||
|
||||
--Prevent camera from going off screen
|
||||
local vleft = math.floor(camx/squaresize)
|
||||
local vup = math.floor(camy/squaresize)
|
||||
|
||||
|
||||
local horc = math.ceil(scrx/squaresize) + 1
|
||||
local verc = math.ceil(scry/squaresize) + 1
|
||||
|
||||
-- Until the number of vertical squares
|
||||
for y = 1, verc do
|
||||
-- Until the number of horizontal squares
|
||||
for x = 1, horc do
|
||||
if squares[vup+y] and squares[vup+y][vleft+x] then
|
||||
local mode = "line"
|
||||
if squares[vup+y][vleft+x].set then
|
||||
mode = "fill"
|
||||
end
|
||||
love.graphics.rectangle(mode, (vleft+x-1)*squaresize - camx,
|
||||
(vup+y-1)*squaresize - camy, squaresize, squaresize)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local scrollspeed = 200
|
||||
local edge = 100
|
||||
|
||||
function love.update(dt)
|
||||
-- scroll the screen if mouse is near edges
|
||||
local scrx, scry = unpack(screen)
|
||||
local mx, my = love.mouse.getPosition()
|
||||
if mx < edge/2 then camera[1] = camera[1] - scrollspeed * dt end
|
||||
if mx > scrx - edge/2 then camera[1] = camera[1] + scrollspeed * dt end
|
||||
if my < edge/2 then camera[2] = camera[2] - scrollspeed * dt end
|
||||
if my > scry - edge/2 then camera[2] = camera[2] + scrollspeed * dt end
|
||||
-- limit the camera position from wandering off
|
||||
if camera[1] < -edge then camera[1] = -edge end
|
||||
if camera[2] < -edge then camera[2] = -edge end
|
||||
local xlimit, ylimit = maxx*squaresize + edge - scrx, maxy*squaresize + edge - scry
|
||||
if camera[1] > xlimit then camera[1] = xlimit end
|
||||
if camera[2] > ylimit then camera[2] = ylimit end
|
||||
end
|
||||
|
||||
function love.mousepressed(x, y, button)
|
||||
-- toggle the clicked square on left button click
|
||||
if button ~= 'l' then return end
|
||||
local wx, wy = x + camera[1], y + camera[2]
|
||||
local x, y = math.floor(wx/squaresize)+1, math.floor(wy/squaresize)+1
|
||||
if squares[y] and squares[y][x] then
|
||||
local value = squares[y][x].set
|
||||
squares[y][x].set = not value -- toggle bool flag
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user