-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactor.lua
More file actions
161 lines (129 loc) · 2.81 KB
/
Copy pathactor.lua
File metadata and controls
161 lines (129 loc) · 2.81 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
152
153
154
155
156
157
158
159
160
161
local Actor = {
_world = nil,
_type = nil,
_collision_list = {},
_x = 0,
_y = 0,
_z_index = 0,
_width = 0,
_height = 0,
_max_height = 0,
_y_velocity = 0,
_collision_cb = nil,
_prevent_collision = {}
}
Actor.__index = Actor
setmetatable (Actor, {
__call = function (cls, ...)
local self = setmetatable (cls, ...)
self:_init(...)
return self
end
})
function Actor:_init (x, y)
self._x = x
self._y = y
end
function Actor:get_world ()
return self._world
end
function Actor:set_world (world)
self._world = world
end
function Actor:get_type ()
return self._type
end
function Actor:set_type (t)
self._type = t
end
function Actor:get_collision_list ()
return self._collision_list
end
function Actor:set_collision_list (coll_list)
self._collision_list = coll_list
end
function Actor:set_collision_cb(func)
self._collision_cb = func
end
function Actor:can_collide (obj)
if obj and self:get_type() and self:get_collision_list() then
for _, coll_type in pairs(self:get_collision_list()) do
if obj:get_type() == coll_type then
for _, actor in pairs(self._prevent_collision) do
if actor == obj then
return false
end
end
return true
end
end
end
return false
end
function Actor:get_dimensions()
return self:get_x(), self:get_y(), self:get_x() + self:get_width(), self:get_y() + self:get_height()
end
function Actor:is_inside_boundaries(actor)
x1, y1, xw1, yh1 = self:get_dimensions()
x2, y2, xw2, yh2 = actor:get_dimensions()
return x1 < xw2 and xw1 > x2 and y1 < yh2 and yh1 > y2
end
function Actor:check_collision (actors)
if actors then
for _, actor in pairs(actors) do
if self ~= actor and self:can_collide(actor) and self:is_inside_boundaries(actor) then
table.insert(self._prevent_collision, actor)
self._collision_cb()
end
end
end
end
function Actor:get_x ()
return self._x
end
function Actor:set_x (x)
self._x = x
end
function Actor:get_y ()
return self._y
end
function Actor:set_y (y)
self._y = y
end
function Actor:get_z_index ()
return self._z_index
end
function Actor:set_z_index (z_index)
self._z_index = z_index
end
function Actor:get_width ()
return self._width
end
function Actor:set_width (width)
self._width = width
end
function Actor:get_height ()
return self._height
end
function Actor:set_height (height)
self._height = height
end
function Actor:get_max_height ()
return self._max_height
end
function Actor:set_max_height (max_height)
self._max_height = max_height
end
function Actor:get_y_velocity ()
return self._y_velocity
end
function Actor:set_y_velocity (y_velocity)
self._y_velocity = y_velocity
end
function Actor:draw ()
local _dummy = nil
end
function Actor:update (dt)
local _dummy = nil
end
return Actor