-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.lua
More file actions
151 lines (136 loc) · 3.42 KB
/
world.lua
File metadata and controls
151 lines (136 loc) · 3.42 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function circleAngleToPoint(circleAngle)
local x = circleCenterX + circleRadius * math.cos(circleAngle)
local y = circleCenterY + circleRadius * math.sin(circleAngle)
return x, y
end
function circlePointToAngle(x, y)
return math.atan2(y - circleCenterY, x - circleCenterX)
end
function circleGetOppositePoint(x, y)
local opX = x + (circleCenterX - x) * 2
local opY = y + (circleCenterY - y) * 2
return opX, opY
end
function circleIsPlayerOnAngle(angle)
for i=1,playerCount,1 do
local delta = math.abs(getPlayerPosition(i) - angle)
if delta < playerEdgeDistance or delta > math.pi * 2 - playerEdgeDistance then
return true, i
end
end
return false, 0
end
function isPointInCircle(x, y)
local dx = x - circleCenterX
local dy = y - circleCenterY
local dist = math.sqrt(dx*dx + dy*dy)
return dist < circleRadius
end
function setBlock(x, y, b)
if x < 1 or y < 1 or x > blocksXCount or y > blocksYCount then
return
end
blocks[blocksXCount * y + x] = b
end
function setBlockArea(x, y, size, b)
for posY=-size,size,1 do
for posX=-size,size,1 do
setBlock(x + posX, y + posY, b)
end
end
end
function getBlock(x, y)
if x < 1 or y < 1 or x > blocksXCount or y > blocksYCount then
return 0
end
return blocks[blocksXCount * y + x]
end
function setBlockAtPos(x, y, b)
local tx = math.floor((x - blocksXStart) / blocksWidth)
local ty = math.floor((y - blocksYStart) / blocksHeight)
return setBlock(tx, ty, b)
end
function setBlockAreaAtPos(x, y, size, b)
local tx = math.floor((x - blocksXStart) / blocksWidth)
local ty = math.floor((y - blocksYStart) / blocksHeight)
return setBlockArea(tx, ty, size, b)
end
function getBlockAtPos(x, y)
local tx = math.floor((x - blocksXStart) / blocksWidth)
local ty = math.floor((y - blocksYStart) / blocksHeight)
return getBlock(tx, ty)
end
function makeRaycastHit(x, y, isVertical)
local hit = {}
hit.x = x
hit.y = y
hit.isVertical = isVertical
return hit
end
function blockRaycast(x1, y1, x2, y2)
local dx = x2 - x1
local dy = y2 - y1
local steps = 0
if math.abs(dx) > math.abs(dy) then
steps = math.ceil(math.abs(dx))
else
steps = math.ceil(math.abs(dy))
end
local xInc = dx / steps
local yInc = dy / steps
local xStep = 0
local yStep = 0
for i=0,steps,1 do
if getBlockAtPos(x1, y1 + yStep) > 0 then
return makeRaycastHit(x1, y1 + yStep, true)
elseif getBlockAtPos(x1 + xStep, y1) > 0 then
return makeRaycastHit(x1 + xStep, y1, false)
end
xStep = xStep + xInc
yStep = yStep + yInc
end
return makeRaycastHit(0, 0, false)
end
function loadLevel(filename)
genBlocks()
local y = 1
local x = 1
for line in love.filesystem.lines("levels/" .. filename) do
for block in string.gmatch(line, "%S+") do
print(block)
setBlock(x, y, tonumber(block))
x=x+1
end
x=1
y=y+1
end
end
--todo: load level from a file
function genBlocks()
for y=1,blocksYCount,1 do
for x=1,blocksXCount,1 do
setBlock(x, y, 0)
end
end
end
function drawBlocks()
love.graphics.setColor(0, 255, 0)
local block = 0
for y=1,blocksYCount,1 do
for x=1,blocksXCount,1 do
block = getBlock(x, y)
if block > 0 then
if block == 1 then
love.graphics.setColor(255, 0, 0)
elseif block == 2 then
love.graphics.setColor(0, 255, 0)
elseif block == 3 then
love.graphics.setColor(0, 0, 255)
end
local xPos = x * blocksWidth + blocksXStart
local yPos = y * blocksHeight + blocksYStart
love.graphics.draw(images["brick"], xPos, yPos)
end
end
end
end