summaryrefslogtreecommitdiffstats
path: root/MCServer/Plugins/MagicCarpet/objects.lua
blob: 8d81623a557b75955e375cadde72c6dc766a2fa7 (plain) (blame)
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
-- Location object
cLocation = {}
function cLocation:new( x, y, z )
	local object = { x = x, y = y, z = z }
	setmetatable(object, { __index = cLocation })
	return object
end

-- Offsets
cFibers = { }
function cFibers:new()
	local object = {
						cLocation:new( 2, -1, 2 ),
						cLocation:new( 2, -1, 1 ),
						cLocation:new( 2, -1, 0 ),
						cLocation:new( 2, -1, -1 ),
						cLocation:new( 2, -1, -2 ),
						cLocation:new( 1, -1, 2 ),
						cLocation:new( 1, -1, 1 ),
						cLocation:new( 1, -1, 0 ),
						cLocation:new( 1, -1, -1 ),
						cLocation:new( 1, -1, -2 ),
						cLocation:new( 0, -1, 2 ),
						cLocation:new( 0, -1, 1 ),
						cLocation:new( 0, -1, 0 ),
						cLocation:new( 0, -1, -1 ),
						cLocation:new( 0, -1, -2 ),
						cLocation:new( -1, -1, 2 ),
						cLocation:new( -1, -1, 1 ),
						cLocation:new( -1, -1, 0 ),
						cLocation:new( -1, -1, -1 ),
						cLocation:new( -1, -1, -2 ),
						cLocation:new( -2, -1, 2 ),
						cLocation:new( -2, -1, 1 ),
						cLocation:new( -2, -1, 0 ),
						cLocation:new( -2, -1, -1 ),
						cLocation:new( -2, -1, -2 ),
						imadeit = false,
					}
	setmetatable(object, { __index = cFibers })
	return object;
end

-- Carpet object
cCarpet = {}
function cCarpet:new()
	local object = {	Location = cLocation:new(0,0,0),
						Fibers = cFibers:new(),
					}
	setmetatable(object, { __index = cCarpet })
	return object
end

function cCarpet:remove()
	local World = cRoot:Get():GetDefaultWorld()
	for i, fib in ipairs( self.Fibers ) do
		local x = self.Location.x + fib.x
		local y = self.Location.y + fib.y
		local z = self.Location.z + fib.z
		local BlockID = World:GetBlock( x, y, z )
		if( fib.imadeit == true and BlockID == E_BLOCK_GLASS ) then
			World:SetBlock( x, y, z, 0, 0 )
			fib.imadeit = false
		end
	end
end

function cCarpet:draw()
	local World = cRoot:Get():GetDefaultWorld()
	for i, fib in ipairs( self.Fibers ) do
		local x = self.Location.x + fib.x
		local y = self.Location.y + fib.y
		local z = self.Location.z + fib.z
		local BlockID = World:GetBlock( x, y, z )
		if( BlockID == 0  ) then
			fib.imadeit = true
			World:SetBlock( x, y, z, E_BLOCK_GLASS, 0 )
		else
			fib.imadeit = false
		end
	end
end

function cCarpet:moveTo( NewPos )
	local x = math.floor( NewPos.x )
	local y = math.floor( NewPos.y )
	local z = math.floor( NewPos.z )
	if( self.Location.x ~= x or self.Location.y ~= y or self.Location.z ~= z ) then
		self:remove()
		self.Location = cLocation:new( x, y, z )
		self:draw()
	end
end

function cCarpet:getY()
	return self.Location.y
end