-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.lua
194 lines (161 loc) · 4.23 KB
/
world.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
-- World
-- Holds and manipulates the tiles that represent the world
require "loader"
require "tile"
World = {width = 0, height = 0}
bg_img = love.graphics.newImage("img/bg.png")
function World:new (o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
text = ""
last_pos = {x=0,y=0}
last_pos2 = {x=0, y=0}
function World:drawBackground(x,y)
love.graphics.draw(bg_img, x, y)
end
function World:draw (x,y)
for lx=1,self.width do
for ly = 1,self.height do
local tile = self.map[ly][lx]
tile:draw((lx*32-32)-x, (ly*32-32)-y)
end
end
love.graphics.print(text, 300, 300)
love.graphics.rectangle("fill", last_pos.x-x-3,last_pos.y-y-3,6,6)
love.graphics.rectangle("fill", last_pos2.x-x-3,last_pos2.y-y-3,6,6)
end
function World:with(map)
self.map = map
self.width = #self.map[1]
self.height = #self.map
self.physics = love.physics.newWorld(0,0,self.width*32,self.height*32)
self.physics:setGravity(0, 0)
self.determine = function(a, b)
local player, battery, wall, book, brain
if a.type == "player" then
player = a
elseif b.type == "player" then
player = b
end
if a.type == "battery" then
battery = a
elseif b.type == "battery" then
battery = b
end
if a.type == "book" then
book = a
elseif b.type == "book" then
book = b
end
if a.type == "brain" then
brain = a
elseif b.type == "brain" then
brain = b
end
if a.type == "wall" then
wall = a
elseif b.type == "wall" then
wall = b
end
return player,battery,wall,book,brain
end
self.add = function(a, b, coll)
local player, battery, wall, book, brain = self.determine(a,b)
if battery and player then
if battery.visible == true then
battery.visible = false
player.energy = player.energy + 25
end
end
if book and player then
if book.visible == true then
book.visible = false
player.books = player.books + 1
end
end
if brain and player then
if brain.visible == true then
brain.visible = false
player.brain = 1
end
end
end
self.persist = function(a, b, coll)
end
self.remove = function(a, b, coll)
end
self.result = function(a, b, coll)
end
self.physics:setCallbacks(self.add, self.persist, self.remove, self.result)
-- Set up rigid bodies
for lx=1,self.width do
for ly = 1,self.height do
local tile = self.map[ly][lx]
if tile.tile_type ~= Type.EMPTY or tile.stop then
tile.body = love.physics.newBody(self.physics, lx*32, ly*32, 0, 0)
local x, y = -16,-16
if tile.tile_type == Type.STAIR_RIGHT then
tile.shape = love.physics.newPolygonShape(tile.body, -32, -32, -24, -32, -24, -24, -16, -24, -16, -16, -8, -16, -8, -8, 0, -8, 0, 0, -32, 0)
else
tile.shape = love.physics.newRectangleShape(tile.body, -16, -16, 32, 32, 0)
end
tile.shape:setSensor(true)
tile.shape:setFriction(1.0)
tile.type = "wall"
tile.shape:setData(tile)
end
end
end
return self
end
function World:foo(bar)
text = bar
end
function World:find_stop(x, y)
for lx=1,self.width do
for ly = 1,self.height do
local tile = self.map[ly][lx]
if tile.stop and tile.visible then
if tile.shape:testPoint(x,y) then
return tile
end
end
end
end
end
function World:find_wall(x, y)
for lx=1,self.width do
for ly = 1,self.height do
local tile = self.map[ly][lx]
if tile.tile_type ~= Type.EMPTY and tile.visible then
if tile.shape:testPoint(x,y) then
return tile
end
end
end
end
end
function World:find_deadly(l, r, y)
local left = false
local right = false
for lx=1,self.width do
for ly = 1,self.height do
local tile = self.map[ly][lx]
if tile.tile_type ~= Type.EMPTY and tile.visible then
if tile.shape:testPoint(l,y) and tile.tile_type == Type.DEADLY then
left = true
end
if tile.shape:testPoint(r,y) and tile.tile_type == Type.DEADLY then
right = true
end
if left and right then
return true
end
end
end
end
return left and right
end