5 This class is designed to provide a better way to manage objects than
6 the (now deprecated) resource_manager. The iterator provided is
9 Functions of ObjectList:
10 ------------------------
11 new(o) --- constructor
12 push_front(obj) --- adds an object to the front of the list
13 push_back(obj) --- adds an object to the back of the list
14 front() --- returns the element at the front of the
16 back() --- returns the element at the back of the
18 pop_front() --- removes the front element from the list
19 pop_back() --- removes the back element from the list
20 clear() --- clears the list
21 iterator() --- returns an iterator for use in a generic
23 update_all(delta) --- updates all Object instances in the list, delta is optional
24 draw_all() --- draws all Object instances in the list
31 ObjectListNode
.data
= nil
32 ObjectListNode
.next = nil
35 function ObjectListNode
:new(o
)
51 function ObjectList
:new(o
)
58 --adds an object to the front of the list
59 function ObjectList
:push_front(obj
)
60 if self
.head
== nil then
61 self
.head
= ObjectListNode
:new()
65 self
.temp
= ObjectListNode
:new()
67 self
.temp
.next = self
.head
71 self
.size
= self
.size
+ 1
74 --adds an object to the back of the list
75 function ObjectList
:push_back(obj
)
76 if self
.head
== nil then
77 self
.head
= ObjectListNode
:new()
81 self
.temp
= ObjectListNode
:new()
83 self
.tail
.next = self
.temp
87 self
.size
= self
.size
+ 1
90 --returns the first element in the list (so it can be used like a stack)
91 function ObjectList
:front()
92 if self
.head
~= nil then
99 --returns the last element in the list
100 function ObjectList
:back()
101 if self
.head
~= nil then
102 return self
.tail
.data
108 --removes the first element
109 function ObjectList
:pop_front()
110 if self
.head
~= nil then
111 self
.head
= self
.head
.next
112 self
.size
= self
.size
- 1
116 --removes the last element
117 function ObjectList
:pop_back()
118 if self
.head
~= nil then
119 self
.temp
= self
.head
120 while self
.temp
.next.next ~= nil do
121 self
.temp
= self
.temp
.next
123 self
.tail
= self
.temp
125 self
.size
= self
.size
- 1
131 function ObjectList
:clear()
138 --iterator for traversing the list
139 function ObjectList
:iterator()
142 if i
== nil and r
~= true then
143 i
= ObjectListNode
:new()
156 --runs the update function of each Object contained within,
157 -- collecting any Objects that need to be collected
158 function ObjectList
:update_all(delta
)
159 if self
.head
and self
.head
.data
and self
.head
.data
.collect
== true then
160 self
.head
= self
.head
.next
161 self
.size
= self
.size
- 1
164 self
.temp
= self
.head
166 if self
.temp
.next and self
.temp
.next.data
and self
.temp
.next.data
.collect
== true then
167 self
.temp
.next.data
= nil
168 self
.temp
.next = self
.temp
.next.next
169 self
.size
= self
.size
- 1
171 if self
.temp
.data
.update
then self
.temp
.data
:update(delta
) end
172 self
.temp
= self
.temp
.next
176 --calls draw on all of the Objects in the list
177 function ObjectList
:draw_all()
178 for obj
in self
:iterator() do
179 if obj
.draw
then obj
:draw() end
183 --looks to see if an object exists already in the list
184 function ObjectList
:exists(o
)
185 for obj
in self
:iterator() do
186 if obj
== o
then return true end