Time for action - building a computer terminal

  1. Add a new class called ComputerTerminal to the Robot Rampage project.
  2. Add the following using directives to the top of the class file:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
  3. Add declarations to the ComputerTerminal class:
    #region Declarations
    private Sprite activeSprite;
    private Sprite inactiveSprite;
    public Vector2 MapLocation;
    public bool Active = true;
    public float LastSpawnCounter = 0;
    public float minSpawnTime = 6.0f;
    #endregion
    
    
  4. Add a constructor to the ComputerTerminal class:
    #region Constructor
    public ComputerTerminal(
    Sprite activeSprite,
    Sprite inactiveSprite,
    Vector2 mapLocation)
    {
    MapLocation = mapLocation;
    this.activeSprite = activeSprite;
    this.inactiveSprite = inactiveSprite;
    }
    #endregion
    
  5. Add public methods to the ComputerTerminal class:
    #region Public Methods
    public bool IsCircleColliding(Vector2 otherCenter, float radius)
    {
    if (!Active)
    {
    return false;
    }
    return activeSprite.IsCircleColliding(otherCenter, radius);
    }
    public void Deactivate()
    {
    Active = false;
    }
    public void Update(GameTime gameTime)
    {
    if (Active)
    {
    float elapsed = (float)gameTime.ElapsedGameTime. TotalSeconds;
    LastSpawnCounter += elapsed;
    if (LastSpawnCounter > minSpawnTime)
    {
    LastSpawnCounter = 0;
    }
    activeSprite.Update(gameTime);
    }
    else
    {
    inactiveSprite.Update(gameTime);
    }
    }
    public void Draw(SpriteBatch spriteBatch)
    {
    if (Active)
    {
    activeSprite.Draw(spriteBatch);
    }
    else
    {
    inactiveSprite.Draw(spriteBatch);
    }
    }
    player goals implementation, robot rampage projectplayer goals implementation, robot rampage projectcomputer terminals, building#endregion
    

What just happened?

The two possible states of each ComputerTerminal are represented by the two sprites activeSprite and inactiveSprite. We also cache the map-square-based location of the ComputerTerminal in the MapLocation vector. We will use this variable when we spawn enemy robots.

Other than Update() and Draw(), we have two public methods. IsCircleColliding() returns false if the terminal is not active. Otherwise, it passes the call on to the IsCircleColliding() method of activeSprite. The Deactivate() method simply sets Active to false.

During the Update() method, we check to see if the sprite is active. If it is, the spawn time mechanism is updated. Currently, when it is time to spawn a new robot our Update() code just resets the timer. We will be revisiting this method after we have constructed our enemy robots. Either the activeSprite or the inactiveSprite (depending on the state of the Active variable) is passed the Update() call before the method exits.

Similarly, the Draw() method uses the Active variable to determine which of the Sprite objects to pass the Draw() call to.

Spawning computer terminals

Now that we have defined the ComputerTerminal class, we need to create a way for them to appear on the game map. We will do this with a new class called GoalManager, which will be responsible for spawning computer terminals in accessible locations and monitoring to see when the player has shut down a computer terminal.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.111.183