I'm attempting to make a game where you have a laser (or in this case, a stream of light) that bounces off of mirrors to reach some kind of goal. I'm guessing you've played one of these games before, as they were popular back in the day (Aargon, anyone?).
Anyhow, I'm running into some issues with the light stream. My current system has obj_light, which has a direction set in the creation code (not the event!). It will calculate which direction it is going, determine if it needs to change direction (in the case of a mirror) or stop (a wall), draw the corresponding sprite to what it did, and move to the next spot. Here's the code (in the draw event):
var done, xx, yy, sprite, dirwas;
xx = x //set variables. xx and yy are position of current piece of light stream
yy = y
done = 0 //setting done to 1 will break you out of the loop
dirwas = 0 //used for making sprite not malfunction on straight pieces
sprite = 0 //the image_index of the light stream piece we are placing each iteration of the loop
if dir = 360 //Ensure dir is within readable ranges (0-270)
dir = 0
if dir > 360
dir -= 360
do
{
if dir = 0 //If you are going this direction (left)
{
sprite += 1 //Add 1 to sprite (sprites use binary to determine sprite to use)
dirwas = 0 //Tell the code later what the direction was at first
}
if dir = 90 //If not that direction, check other directions
{
sprite += 2
dirwas = 90
}
if dir = 180
{
sprite += 4
dirwas = 180
}
if dir = 270
{
sprite += 8
dirwas = 270
}
show_error(string(xx),0) //Tell me where you are currently
show_error(string(yy),0)
if instance_place(xx,yy,obj_mirror1) //If we are over a mirror of a certain orientation (mirror1)
{
if dir = 90 or dir = 270 //Determine which way to turn the light
dir += 90
if dir = 0 or dir = 180
dir -= 90
show_error("MIRROR HIT",0) //Tell me that you hit a mirror.
}
if instance_place(xx,yy,obj_wall) //If you hit a wall
{
show_error("WALL HIT",0) //Tell me
done = 1 //Stop the loop (break added for extra protection against infinite loops)
break
}
if dir = 360 //Make sure dir is within acceptable ranges
dir = 0
if dir > 360
dir -= 360
if dir = 0 //If the dir has changed, add to variable sprite
{
if dirwas != 0
sprite += 1
}
if dir = 90
{
if dirwas != 90
sprite += 2
}
if dir = 180
{
if dirwas != 180
sprite += 4
}
if dir = 270
{
if dirwas != 270
sprite += 8
}
if !done
draw_sprite(spr_light,sprite,xx,yy) //If we didn't finish the light stream, draw a piece of it.
show_error(string(sprite),0) //Tell me what the image_index is.
if dir = 0 //Move to the next spot according to the new direction.
{
xx += 32
}
if dir = 90
{
yy -= 32
}
if dir = 180
{
xx -= 32
}
if dir = 270
{
yy += 32
}
if xx < 0 //If we're outside the room, end.
or yy < 0
or xx > room_width
or yy > room_width
{
break
done = 1
}
sprite = 0 //Reset variables
dirwas = 0
}
until (done)(If it helps, I put it up on pastebin here.)It's a little messy, so I commented it so you can tell what I'm having it do.
Edited by Dilbertguy, 21 April 2011 - 04:26 PM.











