Heres how I put it together (screenshot link at bottom of post):
First you'll need to create a spot light image. I used Photoshop.
Create a new document at the size of the resolution your game is going to use (or various sizes for multi res).
Then ensure that the background is transparent. Make a circular selection in the middle of the document the size you want the spot light. Feather the selection so that it has a nice faded edge.
Select the contents of the spot light layer and create an alpha channel of the contents. To avoid the spot light from being extremely bright I then faded the alpha channel down 40% and added a weak spot with the black brush for the centre.
Save as a 32Bit alpha channeled Tga.
For the next bit you'll need an entity placed in WED somewhere.
In your script for the level add the following variable declarations and actions:
var torchEnergy=1000;
var gotFlashlight=0;
entity* lightBlob;
panel flashlight
{
pos_X =0; pos_Y=0;
bmap=<lightBeam.tga>; //load tga image
flags=d3d;
}
panel* flashLightEnt = flashLight; //assign panel to a pointer
action flashlight_control
{
while (player == null) {wait (1);}
//wait for pickup
while (vec_dist (player.y, my.y) > 150) {
wait (1);
}
gotFlashlight = 1; //player has now got the flashlight
my.invisible = on; //hide entity
my.passable = on;
//prepare the bmap panel
// the 302 is half the width of the lightspot image
var sx;
sx = (screen_size.x/2)-302;
// the 300 is half the height of the lightspot image
var sy;
sy = (screen_size.y/2)-300;
//position the image in the panel ready to show
flashLightEnt.pos_x = sx;
flashLightEnt.pos_y = sy;
var torchOn;
while (1)
{
if(key_f==1){ //has the F key been pressed?
while(key_f==1){wait(1);} //ensure the F Key has been released
if(torchOn==0){torchOn=1;}else{torchOn=0;}
}
while (torchEnergy > 0 && torchOn==1) // press the "F" key to use the flashlight
{
flashLightEnt.visible=on; //show the torch
torchEnergy -= 0.5 * time; //reduce battery power in torch
wait (1);
if(key_f==1){ //if the F key is pressed again break the loop
while(key_f==1){wait(1);}
break;
}
}
torchOn=0;
flashLightEnt.visible=off; //switch light off and wait for F key again
wait (1);
}
}
The battery for the torch uses the following action taken from an AUM issue:
action battery_pickup {
var battery=1000;
my.passable = on;
while (player == null) {wait (1);}
while (vec_dist (player.x, my.x) > 100) {wait (1);}
torchEnergy += battery;
ent_remove (me);
}
and thats about it. Assign the 'flashlight_control' action to your flashlight entity, then when testing pick it up and press F to switch it on and off.
You can see the implementation in a rough screen grab below:
http://www.arsoftware.co.uk/products/asylum/asylum9.jpgBecause this method uses a panel you may need to change the zOrders of your HUD elements to ensure they appear over the spot light, which I'm assuming is possible (I haven't got to that point of my game development yet). Also, theres no collision problems as its a 2D trick.
Hope it helps, have fun.
Paul Huckstepp