First, add the getangle and ReDir functions:
- Code: Select all
double getangle(double A, double B)
{
double result;
result = B - A;
if(result>180)
{
result = 360 - result;
result *= -1;
}
else if (result < -180)
{
result = 360 + result;
}
return result;
}
double ReDir(double A, double B, double mag)
{
double ang = getangle(A, B);
if(abs(mag)<=abs(ang))
{
if(ang>0)
{
return A + mag;
}
else if (ang < 0)
{
return A - mag;
}
else
{
return 0;
}
}
else
{
return B;
}
}
add this in a section of Global Code.
Another function you will find useful is:
- Code: Select all
void angWrap(double *ang)
{
while (*ang >= 360)
{
*ang -= 360;
}
while (*ang < 0)
{
*ang += 360;
}
}
This automatically 'wraps' a real variable to resemble an angle, eg. if I have an angle, 350, and I ncrease it by ten, making it 360, and use angWrap, it will make it equal to 0, which is the same as 360 degrees.
Now, you need a variable to represent the angle you are aiming at. Use the 'variables' panel (at the bottom of the script editor) to create a real variable, call it dir. (You might also want to make it an actor variable, depending on what else you have.) For this example, I'll also add how to make different weapons react differently. So create an integer variable called weapon. 0 will be a pistol, whcih you can aim quickly, with a reasonable amount of recoil, 1, a machine-gun, with slower aiming and heavier recoil, and 2 a shotgun with slow aiming and large recoil. Create another integer called aimspeed. This is for how fast the current weapon aims. Also, another integer called recoil, for how much recoil the current weapon has.
Now it will be silly if you are aiming right and you want to aim left to have to aim all the way up and over the top, or around the bottom to get there. For example if I am aiming north-east, and I move to aim at west, I should be able to immediately flip to north-west, and then aim down to straight west. So we will allow for this. Add the aprx function to Global Code:
- Code: Select all
int aprx(double i, double j, double dif)
{
if(j<=i+dif && j>=i-dif)
{
return 1;
}
else
{
return 0;
}
}
this makes it easier to find if one value is aproximately equal to another.
Now, we need to make our character aim. Add a script editor draw actor event. Now, add this code:
- Code: Select all
double aimang = direction(xscreen, yscreen, xmouse, ymouse);
//get the direction to the mouse
//to make the 'turn' effect:
char cursid = 1; //the 'side' of the current aim
char aimsid = 1; //the 'side' of the desired aim
if (aprx(dir, 180, 90))
{
cursid = 0;//if dir is aproximately 180 (straight left), with 90 degrees variation, you are aiming left
}
if (aprx(aimang, 180, 90))
{
aimsid = 0;//same for the disired aim
}
if (cursid != aimsid)//if the disired aim is on the opposite side from the current aim
{
dir += getangle(dir, 90) * 2;//change aim by the angle size to the top times two
}
//make normal aiming
dir = ReDir(dir, aimang, aimspeed);
angWrap(&dir);
animpos = dir;//or any other script for the animpos
All very well for the aiming. Now you want to make different guns work. To start, add the script on your players create actor:
- Code: Select all
weapon = 0;//pistol
aimspeed = 10;
recoil = 20;
//adjust these two variables as suits; I haven't tested them, but make sure you change them everywhere
Now, we want script to change weapon. Say you press keys 1 - 3 to select. Key 1 should change weapon to 0, 2 to 1, and 3 to 2. Add a keydown on your player, for keys, 1, 2 and 3, make sure it is 'at least one key is pressed', and repeat is disabled. Add this script:
- Code: Select all
switch(getLastKey())
{
case KEY_1:
weapon = 0;
aimspeed = 10;
recoil = 20;//see note in previous section
break;
case KEY_2:
weapon = 1;
aimspeed = 10;
recoil = 15;
break;
case KEY_3:
weapon = 2;
aimspeed = 7;
recoil = 20;
break;
}
Have a mouse sensor actor that sends activation events to your character for every shot. You'll need to have a way of limiting how often you can shoot, and for making weapons automatic. I'll leave that to you, but you'll probably want to change more variables in the keydown event, above.
Add this script for the activation event:
- Code: Select all
//any script you want for creating bullet and playing sound etc.
//the recoil script:
dir = ReDir(dir, 90, recoil);
angWrap(&dir);
animpos = dir;//or any other script for the animpos
I think this should do what you want. If you have problems, just ask, and I'll see if I can fix them.
One piece of advice, which applies to most code you get from anywhere: try to understand what it does, and why it works. It''l make it easier to find problems, make it easier to addapt it to your own use, increase your learning and knowledge of coding and it will make you feel better knowing, not only that someone-gave-me-this-code-and-it-does-what-I-want, but This-code-that-someone-gave-me-does-what-I-want-and-I-understand-why.
If, when you write code, you put lots of comments throught it, it will help others to understand, and it can sometimes make it easier for you to figure out, either as you go, or looking back on it.