-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example_-_infinite_tilemap_scroller.lua
65 lines (55 loc) · 1.67 KB
/
Example_-_infinite_tilemap_scroller.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- Infinite Scroller
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
position = vec2(0,0)
map = {}
countdown=0
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This sets the line thickness
strokeWidth(5)
-- Do your drawing here
countdown = countdown - 1
if countdown<0 then
createrandommapsection(math.floor(position.x / 64)+20,0,math.random(2,6),math.random(2,8))
createrandommapsection(math.floor(position.x / 64)+20,0,25,1)
countdown = math.random(10,120)
end
drawmap()
-- increase our position
position.x=position.x+4
text(position.x,WIDTH/2,HEIGHT-50)
end
function drawmap()
-- get our tile position
-- 64s are the tile width and height
tilex = math.floor(position.x / 64)
tiley = math.floor(position.y / 64)
-- get our offset for smooth scrolling
offx = math.floor(tilex*64-position.x)
offy = math.floor(tiley*64-position.y)
-- draw a section of the map on the screen
for x=0,20,1 do
for y=0,20,1 do
if map[x+tilex]==nil==false and map[x+tilex][y+tiley]==nil==false then
fill(255)
stroke(255)
rect(x*64+offx,y*64+offy,64,64)
end
end
end
end
-- create a random set of blocks on the map table
function createrandommapsection(x,y,w,h)
-- make a 50 by 50 map section at -100 x and -100 y
for x1=x,x+w do
if map[x1]==nil then map[x1]={} end
for y1=y,y+h do
map[x1][y1]=2
end
end
end