Fixed symlinks. Added quit event to demos.
[luagame.git] / base / ObjectList.lua
blob7fe58ae2cbd739e313e8eca420c038b4e0eebcc8
1 --[[
2 ObjectList class
3 ----------------
5 This class is designed to provide a better way to manage objects than
6 the (now deprecated) resource_manager. The iterator provided is
7 particularly useful.
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
15 list, else nil
16 back() --- returns the element at the back of the
17 list, else nil
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
22 for loop
23 update_all(delta) --- updates all Object instances in the list, delta is optional
24 draw_all() --- draws all Object instances in the list
25 --]]
28 --object list node
30 ObjectListNode = {}
31 ObjectListNode.data = nil
32 ObjectListNode.next = nil
34 --constructor
35 function ObjectListNode:new(o)
36 o = o or {}
37 setmetatable(o, self)
38 self.__index = self
39 return o
40 end
43 --object list
44 ObjectList = {}
45 ObjectList.head = nil
46 ObjectList.tail = nil
47 ObjectList.temp = nil
48 ObjectList.size = 0
50 --constructor
51 function ObjectList:new(o)
52 o = o or {}
53 setmetatable(o, self)
54 self.__index = self
55 return o
56 end
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()
62 self.tail = self.head
63 self.head.data = obj
64 else
65 self.temp = ObjectListNode:new()
66 self.temp.data = obj
67 self.temp.next = self.head
68 self.head = self.temp
69 self.temp = nil
70 end
71 self.size = self.size + 1
72 end
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()
78 self.tail = self.head
79 self.head.data = obj
80 else
81 self.temp = ObjectListNode:new()
82 self.temp.data = obj
83 self.tail.next = self.temp
84 self.tail = self.temp
85 self.temp = nil
86 end
87 self.size = self.size + 1
88 end
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
93 return self.head.data
94 else
95 return nil
96 end
97 end
99 --returns the last element in the list
100 function ObjectList:back()
101 if self.head ~= nil then
102 return self.tail.data
103 else
104 return nil
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
124 self.temp.next = nil
125 self.size = self.size - 1
126 self.temp = nil
130 --clears the list
131 function ObjectList:clear()
132 self.head = nil
133 self.tail = nil
134 self.temp = nil
135 self.size = 0
138 --iterator for traversing the list
139 function ObjectList:iterator()
140 local i, r
141 return function ()
142 if i == nil and r ~= true then
143 i = ObjectListNode:new()
144 i.next = self.head
145 r = true
147 i = i.next
148 if i then
149 return i.data
150 else
151 return nil
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
165 while self.temp do
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
188 return false