-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.lua
138 lines (122 loc) · 3.2 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
local bump = require "bump"
local Dungeon = require "dungeon"
local Enemy = require "enemy"
local Powerup = require "powerup"
local SoundFX = require "soundfx"
local tilesize = require "tilesize"
local World = {}
function World.aabb(a, x, y, w, h)
return a.x < x + w and a.x + a.width > x and
a.y < y + h and a.y + a.height > y
end
function World:initialize(foreground, background)
if self.world then
local items, len = self.world:getItems()
for i = 1, len do
self.world:remove(items[i])
end
end
self.enemies = {}
self.world = bump.newWorld()
self:addBlocksToWorld(foreground, background)
end
local remove = table.remove
function World:update(dt, list, x, y, player)
for i = #self.enemies, 1, -1 do
local enemy = self.enemies[i]
enemy:update(dt, self.world, list, player)
enemy:fireBullets(x, y, self.world)
if enemy.health < 0 then
enemy:removeBullets(self.world)
remove(self.enemies, i)
self.world:remove(enemy)
SoundFX:play("killed", .5)
break
end
end
end
function World:addBlocksToWorld(foreground, background)
for i = 1, #background do
local p = background[i]
if p.num == 1 then
self:add(p.x * tilesize, p.y * tilesize, tilesize, tilesize)
end
if p.num == 6 then
self:add(p.x * tilesize, p.y * tilesize, tilesize, tilesize, "exit")
end
end
for i = 1, #foreground do
local p = foreground[i]
if p.num == 2 then
local n = math.random(0, 1)
local name = ""
if n == 0 then
name = "health"
elseif n == 1 then
name = "speed"
end
local x, y = p.x * tilesize, p.y * tilesize
local powerup = Powerup:new(name, x, y)
powerup.kind = "powerup"
self.world:add(powerup, x, y, powerup.width, powerup.height)
elseif p.num == 3 then
local room = Dungeon:getRoomFromPosition(p.x, p.y)
local enemy = Enemy:new(p.x * tilesize, p.y * tilesize, room)
self.enemies[#self.enemies+1] = enemy
self.world:add(enemy, enemy.x, enemy.y, tilesize, tilesize)
elseif p.num == 4 then
local x, y = p.x * tilesize, p.y * tilesize
local powerup = Powerup:new("ammunition", x, y)
powerup.kind = "powerup"
self.world:add(powerup, x, y, 32, 32)
end
end
end
function World:add(x, y, width, height, kind)
local block = {
kind = kind or "solid",
x = x,
y = y,
width = width,
height = height
}
self.world:add(block, x, y, width, height)
end
function World:getWorld()
return self.world
end
function World:remove(item)
self.world:remove(item)
end
function World:drawShadows()
local items, len = self.world:getItems()
for i = 1, len do
local item = items[i]
if item.kind == "player" or item.kind == "enemy" then
local x = (item.x or item.x1) + item.width / 2
local y = (item.y or item.y1) + item.height - 4
local rw, rh = item.width / 1.5, item.height / 2.25
love.graphics.setColor(0, 0, 0, 100)
love.graphics.ellipse("fill", x, y, rw, rh)
end
end
end
function World:draw()
local items, len = self.world:getItems()
for i = 1, len do
local item = items[i]
if item.kind == "powerup" then
item:draw()
end
end
for i = 1, #self.enemies do
self.enemies[i]:draw()
end
end
function World:drawListNums(list)
for i = 1, #list do
local p = list[i]
love.graphics.print(p.num, p.x*tilesize, p.y*tilesize)
end
end
return World