User:DracoBlue/DGTAsa

From Multi Theft Auto: Wiki
< User:DracoBlue
Revision as of 08:53, 10 May 2007 by DracoBlue (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Implementation

function inheritsFrom( baseClass )

	local new_class = {}
	local class_mt = { __index = new_class }
	
	function new_class:create()
		local newinst = {}
		setmetatable( newinst, class_mt )
		return newinst
	end
	
	if nil ~= baseClass then
		setmetatable( new_class, { __index = baseClass } )
	end
	
	-- Implementation of additional OO properties starts here --
	
	-- Return the class object of the instance
	function new_class:class()
		return new_class
	end
	
	-- Return the super class object of the instance
	function new_class:superClass()
		return baseClass
	end
	
	-- Return true if the caller is an instance of theClass
	function new_class:isa( theClass )
		local b_isa = false
	
		local cur_class = new_class
	
		while ( nil ~= cur_class ) and ( false == b_isa ) do
		if cur_class == theClass then
			b_isa = true
		else
			cur_class = cur_class:superClass()
		end
		end
	
		return b_isa
	end
	
	return new_class
end

GTAElement=inheritsFrom(nil)
GTAElement.ID=nil
function GTAElement:className() return "GTAElement" end
function GTAElement:setData(key,value)
	setElementData(self.ID,key,value)
end
function GTAElement:getData(key)
	return getElementData(self.ID,key)
end
function GTAElement:Position()
	return getElementPosition(self.ID)
end

GTAPlayer=inheritsFrom(GTAElement)
function GTAPlayer:className() return "GTAPlayer" end
function GTAPlayer:giveWeapon(w_id,ammo)
	giveWeapon(self.ID,w_id,ammo)
end
function GTAPlayer:Ping()
	return getPlayerPing(self.ID)
end
function GTAPlayer:Name()
	return getClientName(self.ID)
end
function GTAPlayer:Rotation()
	return getPlayerRotation(self.ID)
end
function GTAPlayer:Health()
	return getPlayerHealth(self.ID)
end
function GTAPlayer:setHealth(val)
	return setPlayerHealth(self.ID,val)
end
function GTAPlayer:die(attacker,weapon,bodypart)
	return killPlayer(self.ID,attacker,weapon,bodypart)
end
function GTAPlayer:isDead()
	return isPlayerDead(self.ID)
end
function GTAPlayer:isAlive()
	return not isPlayerDead(self.ID)
end
function GTAPlayer:isDucked()
	return isPlayerDucked(self.ID)
end

Usage

p = GTAPlayer:create()
p.ID=source;
p:setHealth(p:Health()-10)