• No se han encontrado resultados

Seguridad Social, Salubridad e Higiene en el Trabajo Maritimo

DERECHO LABORAL MARITIMO INTERNACIONAL

H. Seguridad Social, Salubridad e Higiene en el Trabajo Maritimo

Here is the BaseWeaponController.cs script in its completed form:

using UnityEngine; using System.Collections;

public class BaseWeaponController : MonoBehavior {

public GameObject[] weapons; public int selectedWeaponSlot; public int lastSelectedWeaponSlot; public Vector3 offsetWeaponSpawnPosition; public Transform forceParent;

private ArrayList weaponSlots; private ArrayList weaponScripts; private BaseWeaponScript TEMPWeapon; private Vector3 TEMPvector3;

private Quaternion TEMProtation; private GameObject TEMPgameObject; private Transform myTransform; private int ownerNum;

public bool useForceVectorDirection; public Vector3 forceVector;

123 6.1 Building the Scripts

public void Start () {

// default to the first weapon slot

selectedWeaponSlot= 0;

lastSelectedWeaponSlot= -1;

// initialize weapon list ArrayList weaponSlots= new ArrayList();

// initialize weapon scripts ArrayList weaponScripts= new ArrayList();

// cache a reference to the transform (looking up a

// transform each step can be expensive, so this is important!)

myTransform= transform;

if(forceParent==null) {

forceParent= myTransform;

}

// rather than look up the transform position and rotation // of the player each iteration of the loop below,

// we cache them first into temporary variables

TEMPvector3= forceParent.position;

TEMProtation= forceParent.rotation;

// we instantiate all of the weapons and hide them so that // we can activate and use them

// when needed.

for( int i=0; i<weapons.Length; i++ ) {

// Instantiate the item from the weapons list

TEMPgameObject= (GameObject) Instantiate( weapons[i], TEMPvector3 + offsetWeaponSpawnPosition,

TEMProtation );

// make this gameObject that our weapon controller // script is attached to, to be the parent of the // weapon so that the weapon will move around // with the player

// NOTE: if you need projectiles to be on a

// different layer from the main gameObject, set the // layer of the

// forceParent object to the layer you want // projectiles to be on TEMPgameObject.transform.parent= forceParent; TEMPgameObject.layer= forceParent.gameObject.layer; TEMPgameObject.transform.position= forceParent. position; TEMPgameObject.transform.rotation= forceParent. rotation;

// store a reference to the gameObject in an ArrayList

weaponSlots.Add( TEMPgameObject );

// grab a reference to the weapon script attached to // the weapon and store the reference in an ArrayList

TEMPWeapon= TEMPgameObject.GetComponent<BaseWeapon

Script>();

weaponScripts.Add( TEMPWeapon );

// disable the weapon

TEMPgameObject.SetActive( false );

}

// now we set the default selected weapon to visible SetWeaponSlot(0);

}

public void SetOwner(int aNum) {

// used to identify the object firing, if required

ownerNum= aNum;

}

public virtual void SetWeaponSlot (int slotNum) {

// if the selected weapon is already this one, drop out! if(slotNum==lastSelectedWeaponSlot)

return;

// disable the current weapon DisableCurrentWeapon();

// set our current weapon to the one passed in

selectedWeaponSlot= slotNum;

// make sure sensible values are getting passed in if(selectedWeaponSlot<0)

selectedWeaponSlot= weaponSlots.Count-1;

// make sure that the weapon slot isn’t higher than the // total number of weapons in our list

if(selectedWeaponSlot>weaponSlots.Count-1) selectedWeaponSlot=weaponSlots.Count-1;

// we store this selected slot to use to prevent duplicate // weapon slot setting

125 6.1 Building the Scripts

// enable the newly selected weapon EnableCurrentWeapon();

}

public virtual void NextWeaponSlot (bool shouldLoop) {

// disable the current weapon DisableCurrentWeapon();

// next slot

selectedWeaponSlot++;

// make sure that the slot isn’t higher than the total // number of weapons in our list

if(selectedWeaponSlot==weaponScripts.Count) { if(shouldLoop) { selectedWeaponSlot= 0; } else { selectedWeaponSlot= weaponScripts.Count-1; } }

// we store this selected slot to use to prevent duplicate // weapon slot setting

lastSelectedWeaponSlot=selectedWeaponSlot;

// enable the newly selected weapon EnableCurrentWeapon();

}

public virtual void PrevWeaponSlot (bool shouldLoop) {

// disable the current weapon DisableCurrentWeapon();

// prev slot

selectedWeaponSlot--;

// make sure that the slot is a sensible number if( selectedWeaponSlot<0 ) { if(shouldLoop) { selectedWeaponSlot= weaponScripts.Count-1; } else { selectedWeaponSlot= 0; } }

// we store this selected slot to use to prevent duplicate // weapon slot setting

lastSelectedWeaponSlot=selectedWeaponSlot;

// enable the newly selected weapon EnableCurrentWeapon();

}

public virtual void DisableCurrentWeapon () {

if(weaponScripts.Count==0) return;

// grab reference to currently selected weapon script TEMPWeapon= ( BaseWeaponScript )weaponScripts[selectedWeapon Slot];

// now tell the script to disable itself TEMPWeapon.Disable();

// grab reference to the weapon’s gameObject and disable // that, too

TEMPgameObject= ( GameObject )weaponSlots[selectedWeaponSlot]; TEMPgameObject.SetActive( false );

}

public virtual void EnableCurrentWeapon () {

if( weaponScripts.Count==0 ) return;

// grab reference to currently selected weapon

TEMPWeapon= ( BaseWeaponScript )weaponScripts[selectedWeapon Slot];

// now tell the script to enable itself TEMPWeapon.Enable();

TEMPgameObject= ( GameObject )weaponSlots[selectedWeapon Slot];

TEMPgameObject.SetActive( true ); }

public virtual void Fire () {

if(weaponScripts==null) return;

127 6.1 Building the Scripts

if(weaponScripts.Count==0) return;

// find the weapon in the currently selected slot

TEMPWeapon= ( BaseWeaponScript )weaponScripts[selectedWeapon Slot];

theDir = myTransform.forward;

if( useForceVectorDirection )

theDir = forceVector;

// fire the projectile

TEMPWeapon.Fire( theDir, ownerNum ); }

}

6.1.1.1 Script Breakdown

The BaseWeaponController class derives from MonoBehavior, utilizing the usual built-in Unity calls (Start(), Update(), FixedUpdate(), etc.):

public class BaseWeaponController : MonoBehavior {

Skipping down, past the declarations, to the Start() function, default values are set for the currently selected weapon slot, and the last selected weapon and ArrayList arrays are initialized ready to hold different weapon(s). When the lastSelectedWeaponSlot variable is set to −1, it just lets the weapon system know that it needs to set the weapon (to avoid duplicate calls later on, whenever the weapon slot is set, it checks to make sure that the new weapon it is trying to set is different to the one currently set up—hence the −1 to make the values of selectedWeaponSlot versus lastSelectedWeaponSlot different on initialization):

// default to the first weapon slot

selectedWeaponSlot= 0;

lastSelectedWeaponSlot= -1;

// initialize weapon list ArrayList weaponSlots= new ArrayList();

// initialize weapon scripts ArrayList weaponScripts= new ArrayList();

Transforms are cached into myTransform, and the variable forceParent is populated with myTransform if it has not been set in the Unity editor Inspector window. The idea here is that there may be occasions where the required parent transform of the weapon may not always be the transform to which this script is attached. The weapon’s parent transform may be set by dragging a reference into forceParent from within the Unity editor:

myTransform= transform; if(forceParent==null) {

forceParent= myTransform;

}

The position and rotation of the parent transform will be used repeatedly in the weap- ons setup code. To avoid having to look them up over and over again, their values are saved in the variables TEMPvector3 and TEMProtation:

TEMPvector3= forceParent.position;

TEMProtation= forceParent.rotation;

When the game starts, the prefab references held by the ArrayList array are instanti- ated and positioned at the parent transform’s position.

The for loop uses weapons.Length to get the length of the weapons array and iterate through it:

// we instantiate all of the weapons and hide them so that // we can activate and use them when needed.

for( int i=0; i<weapons.Length; i++ ) {

The weapon at position i in the array is instantiated at the position of TEMPvector3 with the rotation from TEMProtation:

// Instantiate the item from the weapons list

TEMPgameObject= (GameObject) Instantiate( weapons[i], TEMPvector3 + offsetWeaponSpawnPosition,

TEMProtation );

The instantiated weapon is then parented to the transform held in the variable force- Parent so that it inherits its position and rotation. In this game, the layer of the parent will also be used to identify where projectiles come from. We’ll look at this layer-based identification a little more as the function unfolds, but for now, just make a little note that the layer of the weapon’s parent will be copied down to the weapon. Individual weapons scripts will also copy the layer setting onto their projectiles:

TEMPgameObject.transform.parent= forceParent;

TEMPgameObject.layer= forceParent.gameObject.layer;

TEMPgameObject.transform.position= TEMPvector3;

TEMPgameObject.transform.rotation= TEMProtation;

These new objects are going to be the ones that the script manages, enabling and dis- abling them as required and calling on them to act as required. Each new weapon is added to a new ArrayList called weaponSlots:

// store a reference to the gameObject in an ArrayList

weaponSlots.Add( TEMPgameObject );

The BaseWeaponScript instance attached to each weapon in those slots is found (using GameObject.GetComponent()) and stored in another ArrayList named weaponScripts. By putting them in an array, it saves having to look them up every time:

129 6.1 Building the Scripts

// grab a reference to the weapon script attached to // the weapon and store the reference in an ArrayList

TEMPWeapon= TEMPgameObject.GetComponent<BaseWeapon

Script>();

weaponScripts.Add( TEMPWeapon );

The weapons need to start out disabled; otherwise, they will all appear on top of each other and all active. They are disabled here using the GameObject.SetActive() Unity function:

// disable the weapon

TEMPgameObject.SetActive( false );

}

Now that all of the weapons are instantiated, set up, and hidden, the last thing needed for initialization is to set the default weapon slot to the first weapon (index of 0). To set the slot, use the SetWeaponSlot() function:

// now we set the default selected weapon to visible SetWeaponSlot(0);

}

When a projectile hits an enemy or collides with a player, the object being hit needs to know where the projectile came from in order for it to know how to react to the col- lision. In the example game Lazer Blast Survival, projectile identification is made simply by layer—all player projectiles are set to layer 9, and all enemy projectiles are on layer 17. Set the layer on the gun-mounting-point object (the object that will be the parent to the weapon), and this weapon script will use the same layer when assigning layers to the projectiles.

Another method for checking where projectiles have come from is to assign each player with a unique ID number and to use the SetOwner() function to tell the projectiles who owns each one. This method is employed in the Interstellar Paranoids and Tank Battle example games. The ID number is passed on to projectiles and stored in the script that each projectile has attached to it, the ProjectileController.cs script:

public void SetOwner(int aNum) {

// used to identify the object firing, if required

ownerNum= aNum;

}

The function SetWeaponSlot takes an integer to use as an index number to refer to the weapons stored in the ArrayList named weaponSlots. The weaponSlots ArrayList will be in the same order as weapons added to the original weapons array in the Unity editor Inspector window.

The SetWeaponSlot() function starts out by making sure that the weapon we are try- ing to set (the variable slotNum) is not the weapon that is already active (the variable lastSelectedWeaponSlot). This is done to avoid duplicate calls to activate the same already activated weapon:

public virtual void SetWeaponSlot (int slotNum) {

// if the selected weapon is already this one, drop out! if(slotNum==lastSelectedWeaponSlot)

return;

As a new weapon is about to be made active, the current weapon is disabled by a call to the function DisableCurrentWeapon():

// disable the current weapon DisableCurrentWeapon();

selectedWeaponSlot is an index used to store the currently active weapon slot. Here it is set to the incoming parameter value of slotNum:

// set our current weapon to the one passed in

selectedWeaponSlot= slotNum;

The new value of selectedWeaponSlot is then validated to make sure that it is not less than zero or more than the total number of occupied slots taking away one (since the weaponSlots array of weapons starts at zero, we take one off the total to find the last entry). During the validation, if the value of slotNum is found to be outside what is allowed, it will be set to the nearest valid number—that way, the function will not fail in setting the weapon because of a bad value in slotNum but will choose the wrong weapon instead:

// make sure sensible values are getting passed in if(selectedWeaponSlot<0)

selectedWeaponSlot= weaponSlots.Count-1;

// make sure that the weapon slot isn’t higher than the // total number of weapons in our list

if(selectedWeaponSlot>weaponSlots.Count-1) selectedWeaponSlot=weaponSlots.Count-1;

Now that selectedWeaponSlot has been set, lastSelectedWeaponSlot needs updating to the latest slot number:

// we store this selected slot to use to prevent duplicate // weapon slot setting

lastSelectedWeaponSlot= selectedWeaponSlot;

A call to EnableCurrentWeapon() will take the value of selectedWeaponSlot and acti- vate the currently selected weapon:

// enable the newly selected weapon EnableCurrentWeapon();

}

The NextWeaponSlot() takes a Boolean parameter to tell the function whether or not to loop around from the last weapon to the first. If the shouldLoop parameter is false, and the current weapon is already set to the last available weapon, it will return the same weapon instead of looping around to zero and returning the weapon from the first slot:

public virtual void NextWeaponSlot (bool shouldLoop) {

131 6.1 Building the Scripts

First, the current weapon is disabled in anticipation of setting a new one:

// disable the current weapon DisableCurrentWeapon();

selectedWeaponSlot index number is incremented and validated to make sure that it now contains a valid index number (this is where the slot number will be looped back to zero if it goes over the total number of available weapons):

// next slot

selectedWeaponSlot++;

// make sure that the slot isn’t higher than the total // number of weapons in our list

if(selectedWeaponSlot==weaponScripts.Count) { if(shouldLoop) { selectedWeaponSlot= 0; } else { selectedWeaponSlot= weaponScripts.Count-1; } }

The lastSelectedWeaponSlot gets updated before the new current weapon is enabled, completing the function:

// we store this selected slot to use to prevent duplicate // weapon slot setting

lastSelectedWeaponSlot=selectedWeaponSlot; // enable the newly selected weapon EnableCurrentWeapon();

}

PrevWeaponSlot works in almost exactly the same way as NextWeaponSlot(). It has a Boolean parameter to state whether or not to loop around if the current weapon slot drops below zero, and the most obvious difference is that the selectedWeaponSlot variable is decremented rather than incremented:

public virtual void PrevWeaponSlot (bool shouldLoop) {

// disable the current weapon DisableCurrentWeapon();

// prev slot

selectedWeaponSlot--;

// make sure that the slot is a sensible number if( selectedWeaponSlot<0 )

{

{ selectedWeaponSlot= weaponScripts.Count-1; } else { selectedWeaponSlot= 0; } }

// we store this selected slot to use to prevent duplicate // weapon slot setting

lastSelectedWeaponSlot=selectedWeaponSlot; // enable the newly selected weapon EnableCurrentWeapon();

}

Disabling the current weapon means that it will be both invisible and unable to fire:

public virtual void DisableCurrentWeapon () {

To disable the weapon, the script will need to talk to the BaseWeaponScript.cs attached to it. Since weaponScripts and weaponSlots ArrayLists were created in the same order, the selectedWeaponSlot can be used as the index for either array.

To make sure that there are scripts in the weaponScripts array, its Count property is checked before going any further:

if(weaponScripts.Count==0) return;

A reference to the currently selected weapon slot’s weapon script (BaseWeaponScript. cs) is stored in the variable TEMPWeapon as it is retrieved from the weaponScripts ArrayList:

// grab reference to currently selected weapon script TEMPWeapon= ( BaseWeaponScript )weaponScripts[selectedWeapon Slot];

To tell the weapon to disable, it’s a call to its Disable() function:

// now tell the script to disable itself TEMPWeapon.Disable();

As well as disabling the actual weapon script, it now needs to be hidden from view. First, the variable TEMPgameObject receives a reference to the weapon’s gameObject from the weaponSlots ArrayList. The TEMPgameObject variable is then set to inactive by GameObject.SetActive():

// grab reference to the weapon’s gameObject and disable // that, too

TEMPgameObject= ( GameObject )weaponSlots[selectedWeaponSlot]; TEMPgameObject.SetActive( false );

133 6.1 Building the Scripts

Enabling the currently selected weapon is done by the EnableCurrentWeapon() func- tion, which works almost exactly the same as DisableCurrentWeapon() except that it calls the Enable() function of the BaseWeaponScript instead of Disable():

public virtual void EnableCurrentWeapon () {

if( weaponScripts.Count==0 ) return;

// grab reference to currently selected weapon

TEMPWeapon= ( BaseWeaponScript )weaponScripts[selectedWeapon Slot];

// now tell the script to enable itself TEMPWeapon.Enable();

TEMPgameObject= ( GameObject )weaponSlots[selectedWeapon Slot];

TEMPgameObject.SetActive( true ); }

By default, the Fire() function will launch projectiles along the transform’s forward axis, but there is support for firing along a fixed vector by setting the useForceVectorDi- rection to true.

The function takes no parameters, making it easy to call from any other script:

public virtual void Fire () {

The weaponScripts ArrayList is checked to make sure that it has been properly initial- ized and that it contains entries (a quick count check):

if(weaponScripts==null) return;

if(weaponScripts.Count==0) return;

TEMPWeapon gets a reference to the currently selected weapon’s script from the weaponScripts array:

// find the weapon in the currently selected slot

TEMPWeapon= ( BaseWeaponScript )weaponScripts[selectedWeapon Slot];

By default, the firing direction is along the transform’s forward vector:

When useForceVectorDirection is set to true, theDir is set to the Vector from the variable forceVector, which should be set in the Unity editor Inspector window on the gameObject:

if( useForceVectorDirection )

theDir = forceVector;

The ownerNum variable was mentioned earlier in this section, where it is set by the SetOwner() function. When the call to the currently selected weapon’s Fire() function goes out, it takes a Vector3 and the owner ID:

// fire the projectile

TEMPWeapon.Fire( theDir, ownerNum ); }

}