Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Game behaviour and the advantages
#1
In the last tutorial we looked at how to create a game project with GFX, now we want to look at how to create the game logic.

For this we will use GameBehaviors. But what is a GameBehaviour? Simply put, it is a class that contains various functions that are called by the engine at certain times. For example, when the game is initialised, every time the game is updated, during rendering and many more. You can read more about the functions in the documentation on the following page.

Class IGameBehavior | GFX (gfx-engine.org)

The best way to work with the behaviours is to create your own or use the ones that are already there. In this tutorial we will create a simple behaviour for the player control. We will create a new class and make it inherit the IGameBehaviour.

Code:
public class PlayerBehavior : IGameBehavior
{
    public override void OnDestroy(Game game, GameElement parent)
    {
       
    }

    public override void OnInit(Game game, GameElement parent)
    {

    }

    public override void OnRender(Game game, GameElement parent)
    {
       
    }

    public override void OnUpdate(Game game, GameElement parent)
    {
       
    }
}

As you can see, we only have several functions with which we can work. For our behaviour we want to use the Init and Update function, that means once when the game element is initialised and once at every update.

On initialisation we want our behaviour to place the player on the left edge of the screen.

Code:
public override void OnInit(Game game, GameElement parent)
{
    var startX = this.Parent.Location.X - (game.Viewport.Width / 2) + 64;
    this.Parent.Location.X = startX;
    this.Parent.Rotation.Z = Utils.ToRadians(-90.0f);
}

If we now go through the code, we can see that our behaviours have a Parent property, which is the GameElement to which the behaviour is assigned. We also have a parameter of the class Game, which contains the current viewport. With the help of these two properties we can now calculate the location vector. It is important to note that in OpenGL the point X:0 Y:0 is in the middle of the screen. For this reason we need to move the character to the left by the size of the viewport. We also rotate the player by 90°. Again, note that we are not directly assigning 90 degrees, but rather converting the 90 degrees into radii and then assigning them.

Once we have moved our player, the result should look like this

   

Now we come to the movement control, this is quite simple, we check in the update function, also in every frame, if the user has made an input with the WASD keys. It is important to note that I have done two if checks, so that the user can move on the X and Y axis at the same time.

I also calculated the speed and the movement time using DeltaTime, which ensures that the speed is the same for each frame rate.

When the user makes an input, a variable is incremented or decremented to reflect the new position on the screen.

To prevent the user from leaving the screen, I added a function that checks if the player is in the viewport area. If so, the position is changed in the parent, otherwise not.

Code:
public class PlayerBehavior : IGameBehavior
{
    public override void OnDestroy(Game game, GameElement parent)
    {
       
    }

    public override void OnInit(Game game, GameElement parent)
    {
        var startX = this.Parent.Location.X - (game.Viewport.Width / 2) + 64;
        this.Parent.Location.X = startX;
        this.Parent.Rotation.Z = Utils.ToRadians(-90.0f);
    }

    public override void OnRender(Game game, GameElement parent)
    {
       
    }

    public override void OnUpdate(Game game, GameElement parent)
    {
        /// Calculate the speed for the player
        float speed = (float)(game.DeltaTime) * 0.5f;
        float newX = this.Parent.Location.X;
        float newY = this.Parent.Location.Y;


        /// Move the player on the Y axis
        if(Input.IsKeyDown(System.Windows.Forms.Keys.W))
        {
            newY += speed;
        }
        else if (Input.IsKeyDown(System.Windows.Forms.Keys.S))
        {
            newY -= speed;
        }

        /// Move the player on the X axis
        if (Input.IsKeyDown(System.Windows.Forms.Keys.A))
        {
            newX -= speed;
        }
        else if (Input.IsKeyDown(System.Windows.Forms.Keys.D))
        {
            newX += speed;
        }

        /// Check if the new position from the player is on the viewport
        if(IsPlayerOnViewport(game.Viewport,newX, newY, this.Parent))
        {
            this.Parent.Location.Set(newX, newY, 0f);
        }
    }

    private bool IsPlayerOnViewport(Viewport viewport, float x, float y, GameElement element)
    {
        float vpStartX = -(viewport.Width / 2);
        float vpHalfWidth = viewport.Width / 2;
        float vpStartY = -(viewport.Height / 2);
        float vpHalfHeight = viewport.Height / 2;
        float playerX = x - (element.Size.X / 2);
        float playerY = y - (element.Size.Y / 2);

        if(playerX >= vpStartX && playerX + element.Size.X <= vpHalfWidth && playerY >= vpStartY && playerY + element.Size.Y <= vpHalfHeight)
        {
            return true;
        }
        return false;
    }
}

Now that we have created our GameBehavior, we need to assign it to our player sprite in the main class.

Code:
// Create an player
m_player = new Sprite("player", new Vec3(0, 0), new Vec3(64, 64), m_game.AssetManager.GetTexture("f_004.png"));
m_player.AddBehavior(new PlayerBehavior());
scene.AddGameElement("BaseLayer", m_player);

We have now created our first behaviour Smile Congratulations
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)