|
|
| More advanced weapon actions This lesson covers common TA actions such as alternating firepoints and restore animations, and not so common disable-able weapons. These could have caused confusion if I had not covered the basics of aiming and firing in the previous lesson. Now we're just building on those simple themes with a few tweaks to each function, as well as using a few more. Load example4.bos and let's go. RestoreAfterDelay() & restore_delay You may have noticed all TA units will turn their weapons back to the default position after a while, so they don't go around with barrels aiming backwards etc. This is done by starting the function RestoreAfterDelay() after aiming, and it waits for restore_delay amount of ticks (milliseconds) before turning all the turrets and barrels back. Therefore the first thing you'll notice as you go down the .bos is that var1 has changed to restore_delay, and we're actually going to use a static-var now. We define it's starting value in Create() to be 3000, 3 seconds. Then comes SetMaxReloadTime(time). This function seems to be TA's way of tweaking the delay time, and seems to work, so lets use it. Now we've reached the main part of this feature, the function which sleeps for restore_delay ticks, defaulting to about 3 seconds, before turning all turrets and barrels back to <0>, their original angle. Simple enough. We don't need to wait for these actions to finish, as we're not doing anything else after them. If you find that having more that one weapon starting this function causes it to begin to turn all the barrels back while a weapon is aiming, you can avoid any graphical problems by having separate RestoreAfterDelay() functions for each weapon. Remember to use another variable as the sleep constant. Lastly you can see that after aiming the barrels, AimPrimary() starts RestoreAfterDelay(), making sure an instance of it will restore the barrels after a while. Alternating firepoints This section is about when you have two separate firepoints for a weapon, both close together and so aimed in the same direction (e.g. A Peewee's arms). Load example5.bos and note the doubling up of barrel and flare pieces. The first part of an alternating firepoint feature is creating a variable whose value will alter as we cycle around the firepoints. In this case it's gun_1, but can have any name. We start it with the value of 0, but in this example it could have started with 1, as here they simply mean left or right respectively. The main parts of this feature are the if(gun_1 == x) statements which will limit the animations to the left or right barrels and flares. Both barrels are aimed the same as they are on the same turret and this will usually be the visual effect you want. The feature really started when the weapon is fired. If we're dealing with the left side, the left flare is the piecenum passed in QueryPrimary(piecenum), and the left barrel is recoiled as the left flare flashes in FirePrimary(). After this the weapon has fired and we want to change to the next one, so here we can simple tell gun_1 to alternate from 1 to 0, or 0 to 1 (gun_1 = !gun_1;). If we had 3 or more firepoints, we'd increment the value of gun_1, and then if it's higher than the number of barrels this weapon has, we can reset it to the first value. The following code would do this:
gun_1 = gun_1 + 1;
if( gun_1 == 4 ) { gun_1 = 1; } Disable-able weapons There are various circumstances where you may want to stop a weapon firing until a condition is fulfilled, such as not stationary, out of a fire arc, or heavily damaged. In all these cases you can prevent a weapon firing by adding the following code after the signal statements in the AimPrimary() function:
While( test_var1 == 1 )
The test_var can have any name, such as bMoving, flagexplode1, whatever, and you can test for any value, but the point of it is that the aiming function will not complete while the test_var is in a certain state, instead it'll loop through the sleep delay statement.{ sleep 100; } Enough destruction, now lets rebuild We've cover pretty much all there is with weapons. Once you get used to these simple features you'll be able to add more barrels and turrets, and fancy actions like unfolding/reloaded missiles. Read through some Cavedog or quality 3rd Party units to learn exactly how they've created a feature, and you'll see how they've just enhanced the things described in the last two lessons. For more about damageable weapons and armour, see Lesson8, dedicated to the explodeparts.h include from Nikola Genkov. Otherwise we can move on to the topic of other unit animations, Lesson5. Also, you may be wondering how to create fire arcs, where say the left side turret of a ship is used if the enemy's in that direction, and similarly for the right side. This is done in a similar way to the alternating firepoints, except instead of alternating/cycling the gun_1 variable after we've fired the weapon, we decide it's state in the AimPrimary function, and then perform the correct aiming:
if( heading < <180> )
{ gun_1 = 1; turn side2 to y-axis heading speed <50>; turn side2 to x-axis <0> - pitch speed <25>; wait-for-turn side2 around y-axis; wait-for-turn side2 around x-axis; } if( heading >= <180> ) { gun_1 = 0; turn side1 to y-axis heading speed <50>; turn side1 to x-axis <0> - pitch speed <25>; wait-for-turn side1 around y-axis; wait-for-turn side1 around x-axis; } Again you'll soon notice if you've mixed up which value of gun_1 related to left or right.
|
| © Proud like a god |