-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobject.lua
More file actions
214 lines (212 loc) · 4.34 KB
/
object.lua
File metadata and controls
214 lines (212 loc) · 4.34 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Object =
{}
--------------
Object_mt =
{
__index = Object,
__gc =
function(self)
print(self:get_name(), " deleted")
end
}
--------------
if(dokun) then
object_factory = Factory:new()
end
------------------
-- ENVIRONMENTAL OBJECTS
-- Objects are meant to be non-collidable sprites
function Object.new(name)
local object
if dokun then
object = Sprite:new()
else
object = {}
end
object.name = name
object.type = "Environmental"
if not Object.id then
Object.id = 0
end
Object.id = Object.id + 1
if name == nil then
object.name = string.format("%s%s", "Object", Object.id)
end
-- mt
object.mt = {__index = object}
-- copy function
object.new = function()
local new_object
if dokun then
new_object = Sprite:new()
else
new_object = {}
end
setmetatable(new_object, object.mt)
return new_object
end
if (dokun) then
object_factory:store(object)
end
setmetatable(object, Object_mt)
return object
end
--------------
function Object:load(file_name)
if dokun then
return Sprite.load(self, file_name)
end
end
--------------
function Object:draw(x, y)
if dokun then
if not x and not y then
Sprite.draw(self)
else
Sprite.draw(self, x, y)
end
end
end
--------------
function Object:move(x, y)
if dokun then
Sprite.move(self, x, y)
end
end
--------------
function Object:rotate(degree)
if dokun then
Sprite.rotate(self, degree)
end
end
--------------
function Object:scale(sx, sy)
if dokun then
Sprite.scale(self, sx, sy)
end
end
--------------
function Object:animate(player)
-- custom animation function
if type(self.on_animate) == "function" then
self:on_animate(player)
end
end
--------------
-- SETTERS
--------------
function Object:set_position(x, y)
if dokun then
Sprite.set_position(self, x, y)
end
end
--------------
function Object:set_texture(texture)
if dokun then
Sprite.set_texture(self, texture)
end
end -- set movable, fixed, etc
--------------
-- GETTERS
--------------
function Object:get_position()
if dokun then
return Sprite.get_position(self)
end
end
--------------
function Object:get_parent()
-- object of Item
if getmetatable(self) == Object_mt then return Object end
-- clone of Item object
local _G = _G
for _, object in pairs(_G) do
if type(object) == "table" then
if getmetatable(object) == Object_mt then
if getmetatable(self) == object.mt then
return object
end
end
end
end
end
--------------
-- BOOLEAN
--------------
function Object:is_object()
-- Is it a table?
if type(self) ~= "table" then
return false
end
if getmetatable(self) == Object_mt then
return true
end
if (dokun) then
for i = 0, object_factory:get_size() do
if getmetatable(object_factory:get_object(i)) == Player_mt then
if getmetatable(self) == object_factory:get_object(i).mt then
return true
end
end
end
else
if is_copy(self) then
return true
end
end
return false
end
--------------
is_object = Object.is_object
--------------
function Object:is_collidable()
return self.collidable
end
--------------
--------------
-- EVENT
--------------
function Object:is_copy()
local _G = _G
for _, object in pairs(_G) do
if type(object) == "table" then
if getmetatable(object) == Object_mt then
if getmetatable(self) == object.mt then
return true
end
end
end
end
return false
end
--------------
function Object:draw_event(player) -- draws all created objects
for _, object in pairs(_G) do
if type(object) == "table" then
if getmetatable(object) == Object_mt then-- object is an object (Item base mt)
-- animate the object
object:animate(player)
-- draw the object
object:draw()
if getmetatable(object) == object.mt then
-- animate the object
object:animate(player)
-- draw the object
object:draw()
end
end
end
end
end
--------------
function object_event_handler()
local g = _G
for _, obj in pairs(g) do
if is_object(obj) then
end
end
end
--------------
dofile("object/tree.lua")
--dofile("object/fence.lua")
dofile("object/chest.lua")