![]() |
Game behaviour and the advantages - Printable Version +- GFX Forum (https://forum.gfx-engine.org) +-- Forum: Resources and Tutorials (https://forum.gfx-engine.org/forumdisplay.php?fid=20) +--- Forum: Tutorials and Guides (https://forum.gfx-engine.org/forumdisplay.php?fid=21) +--- Thread: Game behaviour and the advantages (/showthread.php?tid=3) |
Game behaviour and the advantages - Andy - 06-30-2024 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 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) 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 Now that we have created our GameBehavior, we need to assign it to our player sprite in the main class. Code: // Create an player We have now created our first behaviour ![]() |