knorke wrote:http://tvo.github.io/spring/2010/04/19/ ... the_stumpy
It says "This example makes the weapon of the stumpy fully functional. Here is the complete code:"
so from that page as one block...
Code: Select all
-------------------------------------------------------------------------------------------
-- Here piece numbers are retrieved (using the piece function) and assigned to the 
-- variables base, flare, turret and barrel.
-------------------------------------------------------------------------------------------
local base, flare, turret, barrel = piece('base', 'flare', 'turret', 'barrel')
local SIG_AIM = {}
-------------------------------------------------------------------------------------------
-- The call-in Create is called right after the unit is created. Because all pieces 
-- are visible by default, we need to hide the flare piece here, as it should 
-- only be briefly shown after firing.
-------------------------------------------------------------------------------------------
function script.Create()
	Hide(flare)
end
-------------------------------------------------------------------------------------------
-- This is a common pattern present in (nearly) all unit scripts you’ll read and write. 
-- AimWeapon is the call-in that is started by Spring when an enemy is nearby and the unit 
-- needs to aim at this enemy. Spring pre-calculates the heading and pitch and passes these to 
-- this function. Spring calls this function very often though, which is why Signal and 
-- SetSignalMask are used to ensure only one aim thread (per unit) is alive at any one time.
-- The next statements define the actual animation: the turret piece is turned towards the 
-- target at a horizontal speed of 0.5 rad/sec and a vertical speed of 0.25 rad/sec. 
-- The thread is then suspended until the animation has finished before it shouts ‘FIRE!’ 
-- to Spring by means of the statement return true.
-------------------------------------------------------------------------------------------
function script.AimWeapon1(heading, pitch)
	Signal(SIG_AIM)
	SetSignalMask(SIG_AIM)
	Turn(turret, y_axis, heading, 0.5)
	Turn(barrel, x_axis, -pitch, 0.25)
	WaitForTurn(turret, y_axis)
	WaitForTurn(barrel, x_axis)
	return true
end
-------------------------------------------------------------------------------------------
-- Once the weapon is fired (even if AimWeapon returns true it’s possible the unit does not 
-- fire because e.g. friendly units or trees block its line of fire) Spring calls FireWeapon 
-- for that weapon. Typically, this displays a flare and plays a recoil animation. That is 
-- exactly what is done in this example. First, the flare piece is shown and the barrel is 
-- positioned 2.4 elmos backward. Then the call to Sleep suspends the thread for 150 
-- milliseconds before the flare piece is hidden again. Immediately the barrel starts moving 
-- back to it’s initial position at a speed of 3 elmos/sec.
-------------------------------------------------------------------------------------------
function script.FireWeapon1()
	Show(flare)
	Move(barrel, z_axis, -2.4)
	Sleep(150)
	Hide(flare)
	Move(barrel, z_axis, 0, 3)
end
-------------------------------------------------------------------------------------------
-- Last but not least these two call-ins define which pieces Spring shall use as ‘origin’ 
-- in all the aiming calculations. If you are new to unit scripting, just follow the pattern 
-- presented here: AimFromWeapon should return the turret piece and QueryWeapon should return a 
-- piece at the end of the barrel. Later on you may use these to implement weapons that fire 
-- from multiple barrels on the same turret.
-------------------------------------------------------------------------------------------
function script.AimFromWeapon1()
	return turret
end
function script.QueryWeapon1()
	return flare
end





 