summaryrefslogtreecommitdiff
path: root/data/luaobjects/test/client.lua
blob: 80f085190b6dfebd8f5acc7a61630e1d031a04bb (plain)
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
-- Client-side code of the test lua object

--
-- Some helper functions
--

function split(str, pat)
   local t = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
	 table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

function dump(o)
    if type(o) == 'table' then
        local s = '{ '
        for k,v in pairs(o) do
                if type(k) ~= 'number' then k = '"'..k..'"' end
                s = s .. '['..k..'] = ' .. dump(v) .. ','
        end
        return s .. '} '
    else
        return tostring(o)
    end
end

--
-- Actual code
--

function step(self, dtime)
	-- Some smoother animation could be done here
end

function process_message(self, data)
	--print("client got message: " .. data)

	-- Receive our custom messages

	sp = split(data, " ")
	if sp[1] == "pos" then
		object_set_position(self, sp[2], sp[3], sp[4])
	end
	if sp[1] == "rot" then
		object_set_rotation(self, sp[2], sp[3], sp[4])
	end
end

function initialize(self, data)
	print("client object got initialization: " .. data)

	corners = {
		{-1/2,-1/4, 0},
		{ 1/2,-1/4, 0},
		{ 1/2, 1/4, 0},
		{-1/2, 1/4, 0},
	}
	object_add_to_mesh(self, "rat.png", corners, false)

end