-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.lua
More file actions
112 lines (98 loc) · 2.22 KB
/
Copy pathworld.lua
File metadata and controls
112 lines (98 loc) · 2.22 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
World = {
_width = 0,
_height = 0,
_map = nil,
_speed = 0,
_ground = 0,
_gravity = 0,
_actors = {}
}
World.__index = World
setmetatable (World, {
__call = function (cls, ...)
local self = setmetatable ({}, cls)
self:_init (...)
return self
end })
function World:_init (args)
self._width = args.width or 800
self._height = args.height or 600
self._map = args.map or nil
self._speed = args.speed or 200
self._ground = args.ground or 400
self._gravity = args.gravity or -2400
end
function World:add_actor(actor)
if actor ~= nil then
actor:set_world(self)
table.insert(self._actors, actor)
end
end
function World:set_map(map)
if map ~= nil then
map:set_world(self)
self._map = map
end
end
function World:get_map()
return self._map
end
function World:update(dt)
if self._map then
self._map:update(dt)
end
if self._actors then
for _, actor in pairs(self._actors) do
local ground = self._ground - actor:get_height()
if actor:get_y_velocity() ~= 0 then
actor:set_y(actor:get_y() + actor:get_y_velocity() * dt)
actor:set_y_velocity(actor:get_y_velocity() - self._gravity * dt)
end
if actor:get_y() > ground then
actor:set_y_velocity(0)
actor:set_y(ground)
end
if actor:get_y_velocity() == 0 and actor:get_y() < self._ground then
actor:set_y_velocity(ground - actor:get_y())
end
actor:update(dt)
end
table.sort(self._actors, function (a, b)
return a:get_z_index() < b:get_z_index()
end)
for _, actor in pairs(self._actors) do
actor:check_collision(self._actors)
end
end
end
function World:draw()
if self._map then
self._map:draw()
end
if self._actors then
for _, actor in pairs(self._actors) do
actor:draw()
end
end
end
function World:delete_actor(obj)
if self._actors then
for index, actor in pairs(self._actors) do
if obj == actor then
actor = nil
table.remove(self._actors, index)
collectgarbage()
end
end
end
end
function World:get_speed()
return self._speed
end
function World:get_width()
return self._width
end
function World:get_height()
return self._height
end
return World