-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.lua
120 lines (96 loc) · 2 KB
/
object.lua
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
-- BLAST FLOCK source files
-- by TRASEVOL_DOG (https://trasevol.dog/)
require("ttable")
function init_object_mgr(...)
objs={
to_update={},
to_draw0={},
to_draw1={},
to_draw2={},
to_draw3={},
to_draw4={}
}
local args={...}
for v in all(args) do
objs[v]={}
end
end
--collision stuff
function collide_objgroup(obj,group)
for obj2 in all(objs[group]) do
if obj2~=obj then
local bl=collide_objobj(obj,obj2)
if bl then
return obj2
end
end
end
return false
end
function collide_objobj(obj1,obj2)
return (abs(obj1.x-obj2.x)<(obj1.w+obj2.w)/2
and abs(obj1.y-obj2.y)<(obj1.h+obj2.h)/2)
end
--object managing
function update_objects(dt)
local uobjs=objs.to_update
for obj in all(uobjs) do
obj:update(dt)
end
end
function draw_objects()
for i=0,4 do
local dobjs=objs["to_draw"..i]
for obj in all(dobjs) do
obj:draw()
end
end
end
function register_object(o)
for reg in all(o.regs) do
if not objs[reg] then
objs[reg] = {}
end
add(objs[reg],o)
end
o.registered = (o.registered or 0) + 1
o.__registered = true
end
function deregister_object(o)
for reg in all(o.regs) do
del(objs[reg],o)
end
o.__registered = false
end
function group_add(group,o)
add(o.regs,group)
add(objs[group],o)
end
function group_del(group,o)
del(o.regs,group)
del(objs[group],o)
end
function clear_group(group)
objs[group]={}
end
function clear_all_groups()
for n,v in pairs(objs) do
clear_group(n)
end
end
function eradicate_group(grp)
--local a = objs[group]
--if a then
while #objs[grp] > 0 do
deregister_object(objs[grp][1])
end
--end
-- for o in group(grp) do
-- deregister_object(o)
-- end
end
function new_group(name) objs[name] = {} end
function group_exists(name) return objs[name] ~= nil end
function group(name) return all(objs[name]) end
function group_size(name) return #objs[name] end
function group_member(grp,pos) return objs[grp][pos] end