From 2bbe5c4c452b4a3186273124bfc330933a1ba563 Mon Sep 17 00:00:00 2001 From: "kirill.saidov" Date: Sat, 4 Sep 2021 17:48:06 +0600 Subject: [PATCH] lesson#26 video script finished --- .../ourGame/source/data.d | 79 ------------------- ...wing a player | Let's learn Dlang game dev | 77 ++++++++++++++++++ 2 files changed, 77 insertions(+), 79 deletions(-) delete mode 100644 lesson#26 - Drawing a player/ourGame/source/data.d create mode 100644 video_scripts/learn-dlang/#26 | Drawing a player | Let's learn Dlang game dev diff --git a/lesson#26 - Drawing a player/ourGame/source/data.d b/lesson#26 - Drawing a player/ourGame/source/data.d deleted file mode 100644 index 3208e53..0000000 --- a/lesson#26 - Drawing a player/ourGame/source/data.d +++ /dev/null @@ -1,79 +0,0 @@ -module data; - -// mostly used libraries -public import raylib; -public import std.stdio: writeln, write; - -// window dimensions -immutable windowWidth = 720; -immutable windowHeight = 640; - -// Game states -enum GameState { - MainMenu, - Play, - Exit -} - -// state interface -interface IState { - void run(); -} - -// entity interface -class Entity { - private { - Texture2D texture; - Rectangle frame; - Vector2 position; - } - - this(const ref Texture2D texture, const Rectangle frame, const Vector2 Position) { - this.texture = texture; - this.frame = frame; - this.position = position; - } - - void move(const float x, const float y) { - position.x += x; - position.y += y; - } - - void draw() { - DrawTextureRec(texture, frame, position, Colors.WHITE); - } - - abstract void update(); -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/video_scripts/learn-dlang/#26 | Drawing a player | Let's learn Dlang game dev b/video_scripts/learn-dlang/#26 | Drawing a player | Let's learn Dlang game dev new file mode 100644 index 0000000..f1b898a --- /dev/null +++ b/video_scripts/learn-dlang/#26 | Drawing a player | Let's learn Dlang game dev @@ -0,0 +1,77 @@ +#26 | Drawing a player | Let's learn Dlang game dev + +Hello and welcome. Today we are going learn how to draw textures to the +screen. More specifically, we are going to draw a player figure that the user +will use to interact with our game world. All the resources I use, are +freely available for download from my github repo. The link is in the +description section. + +Alright, the first thing we are going to do, is to move all files to a +game package to avoid clutter. Then we need to rename the modules and, +finally, fix the imports. Compile and run to check if all is well. + +Now we can start working on new functionality. + +We have a player, and we want to display it in our game window. +A player has certain size, position and a texture. It may also have a +draw and move functions. + +Now suppose we have an enemy instead. It has the same properties as the +player, doesn't it? + +What about an obstacle? A building, tree, rock? They too have the same +properties, but the only difference is they are stationary. + +So instead of creating a player, monster and an obstacle class, we can +generalize it into an Entity and implement the base functionality only +once. Thus, we will be able to inherit that base functionality later on +in our game and expand any further functionality if needed. For instance, an +attack or a heal function for the player or enemy. + +Let's implement what we have just discussed. + +I am going to create an Entity class. It will have the following private +members: a texture, a frame for animations and a position. + +Then I am going to implement a constructor taking in texture, frame and +position as arguments and just copying those values. + +We also need to implement a move and a draw function that we will be able to +override in the future. + +Finally, we may need an update and a processEvents function. Since their +implementations strongly depend on the type of object, they are going to +be abstract. This will enforce custom implementation upon inheritance. + +That's it! Now we can go ahead and create our player class. It is going +to inherit all the default behaviour from the Entity class. It will also use +the entity's constructor upon initialization. We also must implement the +abstract update and processEvents functions. I am going to leave them +empty for now. + +Alright, let's go to our play state. I am going to load player texture. +Here it is. As you can see, it has multiple frames that can be used to +animate the player. Each frame is 80 by 110 pixels. +So, let's create a player instance and initialize it. + +Finally, all that is left to do is to draw the player to the screen using +the draw method we defined earlier. Compile and run. + +Now, do not forget to press 'P' in order to switch to the player state. Here +is our player! + +In the next video we are going to implements player movement and basic +animation. + +Have a nice day. + + + + + + + + + + +