For a basic mine, the script usually looks something like this:
Code:
function Create(self)
self.scanTimer = Timer();
end
function Update(self)
if self.scanTimer:IsPastSimMS( 500 ); -- half second delay in between detection
self.scanTimer:Reset();
-- some method of detecting actors or appropriate targets
if <appropriate target is detected> then
-- gib self, or create explosion effects and then delete self
end
end
end
There are two main ways to do detection.
You have the basic distance check, which goes through every actor on the map and does a distance check with them. If an actor is within a specified distance, the mine explodes.
Then there are ray casts, which check pixels forming a line in some direction, which are more efficient than distance checks but have a narrower "vision."
Multiple ray casts can form a decent area check, or you can make your own pattern with the pixel checks ( SceneMan:GetMOIDPixel( x, y) ).
Fermat's spiral works nicely.
Team checks should also be done during this part, so the mine ignores allied actors.
The timer and timer check is for putting a time delay in between detection runs. Depending on how costly your detection system is, you want to only run it every so milliseconds instead of every frame, unless if it's fast and simple (a single line ray cast).
Detonating the mine can be simply forcing it to gib using Lua ( self:GibThis() ) or spawning a separate explosion effect and then deleting the mine.
An example of a simple mine script that can be attached to an MO that is fired from a gun:
Code:
function Create(self)
self.scanTimer = Timer();
end
function Update(self)
if self.scanTimer:IsPastSimMS( 500 ); -- half second delay in between detections
self.scanTimer:Reset();
-- go through all actors
for actor in MovableMan.Actors do
-- if the actor's team is not the mine's team, and if the distance between the mine and the actor is less than 100 pixels...
if actor.Team ~= self.Team and SceneMan:ShortestDistance( self.Pos, actor.Pos, SceneMan.SceneWrapsX).Magnitude < 100;
-- make the mine gib itself
self:GibThis();
end
end
end
end
Since a projectile's Team value is inherited from the gun, you can have a plain unscripted HDFirearm for the mine launcher.