@@ -14,26 +14,49 @@ local spriteCenter = 50;
1414local other_players = {}
1515local customers = {}
1616
17- function drawCustomers(change)
18- if table.maxn(room.state.customers) > table.maxn(customers) then
19- created_id = factory.create("default:/customer_factory#factory", vmath.vector3(50,0,0.1), vmath.quat(),{},3.125)
20- customers[table.maxn(customers) +1] = created_id
21- elseif table.maxn(room.state.customers) < table.maxn(customers) then
22- go.delete(customers[table.maxn(customers)])
23- table.remove(customers)
17+ function setContains(set, key)
18+ return set[key] ~= nil
19+ end
20+
21+ function gamestateHasCustomer(id)
22+ for k,v in pairs(room.state.customers) do
23+ if v.id == id then
24+ return true
25+ end
2426 end
25- for pos,customer_id in pairs(customers) do
27+ return false
28+ end
29+
30+ function drawCustomers(change)
31+ --update and add customers from gamestate
32+
33+
34+ for pos, customer in pairs(room.state.customers) do
35+
36+ -- called out of order
37+ if not setContains(customers, customer.id) then
38+ -- create new customer if not yet created
39+ created_id = factory.create("default:/customer_factory#factory", vmath.vector3(50,0,0.1), vmath.quat(),{},3.125)
40+ customers[customer.id] = created_id
41+ customer_go = customers[customer.id]
42+ msg.post(customer_go, "Game State", {sprite_id = customer.sprite_id})
43+
44+
45+ end
46+ --update all customer
2647 --lua starts indexing at 1 instead of 0 like everything else
2748 newy = (pos-1) * scaling + spriteCenter;
28- newtype = nil
29- if room.state.customers[pos].wants["Kolle"] then
30- newtype = "KolleCustomer"
31- elseif room.state.customers[pos].wants["Zotrine"] then
32- newtype = "ZotrineCustomer"
33- elseif room.state.customers[pos].wants["Premium"] then
34- newtype = "PremiumCustomer"
49+ customer_go = customers[customer.id]
50+ msg.post(customer_go, "Game State", {y = newy, order = customer.wants})
51+ end
52+
53+ -- remove customers not in gamestate
54+
55+ for customer_id, customer_go in pairs(customers) do
56+ if not gamestateHasCustomer(customer_id) then
57+ go.delete(customers[customer_id])
58+ customers[customer_id] = nil;
3559 end
36- msg.post(customer_id, "Game State", {y = newy, type = newtype, order = room.state.customers[pos].wants})
3760 end
3861end
3962
86109
87110function onStateChange(change)
88111 Move(change)
112+ for k, v in pairs(change) do
113+ if v['field'] == 'customers' then
114+ drawCustomers(change)
115+ end
116+ end
89117end
90118
91119
@@ -142,10 +170,6 @@ function on_message(self, message_id, message, sender)
142170 room:send("move",message.dir);
143171 elseif (message_id == hash("use")) then
144172 room:send("use")
145- elseif (message_id == hash("drop")) then
146- room:send("drop");
147- elseif (message_id == hash("refill")) then
148- room:send("refill",message.item);
149173 end
150174
151175end
0 commit comments