This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:guides:ufo:main [2018/02/13 20:47 (7 years ago)] – ↷ Page moved from guides:ufo:main to en:guides:ufo:main iarwain | en:guides:ufo:main [2020/08/19 19:22 (5 years ago)] (current) – ↷ Links adapted because of a move operation sausage | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | <WRAP center round box 80%> | + | ====== |
- | This is a document in progress. Intended to be both a video tutorial, and and article served from gamedev.net | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | ====== | + | |
- | + | ||
- | + | ||
- | Welcome | + | |
- | + | ||
- | Hello, my name is Wayne, and my aim for this tutorial is to take you through all the steps to build a UFO game from scratch. | + | |
- | + | ||
- | Before I begin, I should openly acknowledge that this series is cheekily inspired by the 2D UFO tutorial written for Unity. | + | |
- | + | ||
- | This was chosen as it makes an excellent comparison of the approaches between Orx and Unity. It is also a perfect way to highlight one of the major parts that makes Orx unique among other game engines, its Configuration System. | + | |
- | + | ||
- | You'll get very familiar with this system very soon. It's at the very heart of just about every game written using Orx. | + | |
- | + | ||
- | If you are very new to game development, | + | |
- | + | ||
- | The aim of our game is to allow the player to control a UFO by applying physical forces to move it around. The player must collect pickups to increase their score to win. | + | |
- | + | ||
- | + | ||
- | ====== | + | |
- | + | ||
- | + | ||
- | You will need a blank project for Visual Studio 2015, CodeLite or Code:: | + | |
- | + | ||
- | Once you have that, unzip the package to somewhere on your harddrive. | + | |
- | + | ||
- | Inside the '' | + | |
- | + | ||
- | Open the folder for the IDE you would like to use. Click MyGame.sln to open in Visual Studio, MyGame.workspace for CodeLite, or MyGame.workspace for Code:: | + | |
- | + | ||
- | When the blank template loads, there are two main folders to note in your solution: | + | |
- | + | ||
- | * config | + | |
- | * src | + | |
- | + | ||
- | Firstly, the '' | + | |
- | + | ||
- | + | ||
- | ===== What is config? ===== | + | |
- | + | ||
- | Orx is a data driven 2D game engine. Many of the elements in your game, like objects, spawners, music etc, do not need to defined in code. They can be defined (or configured) using config files. | + | |
- | + | ||
- | You can make a range of complex multi-part objects with special behaviours and effects in Orx, and bring them into your game with a single line of code. You'll see this in the following chapters of this guide. | + | |
- | + | ||
- | There are three config files in the config folder, but only one will actually be used in our game. This is: | + | |
- | + | ||
- | MyGame.ini | + | |
- | + | ||
- | All our game configuration will be done there. | + | |
- | + | ||
- | There are two other config files: | + | |
- | + | ||
- | * CreationTemplate.ini | + | |
- | * SettingsTemplate.ini | + | |
- | + | ||
- | These are example configs and they list all the properties and values that are available to you. We will mainly concentrate on referring to the CreationTemplate.ini, | + | |
- | + | ||
- | + | ||
- | ===== The code template ===== | + | |
- | + | ||
- | + | ||
- | Now to take a look at the basic MyGame.cpp and see what is contained there. | + | |
- | + | ||
- | The first function is the Init() function. | + | |
- | + | ||
- | This function will execute when the game starts up. Here you can create objects have been defined in the config, or perform other set up tasks like handlers. We'll do both of these soon. | + | |
- | + | ||
- | The Run() function is executed every main clock cycle. This is a good place to continually perform a task. Though there are better alternatives for this, and we will cover those later. This is mainly used to check for the quit key. | + | |
- | + | ||
- | The Exit() function is where things are cleaned up when your game quits. Orx cleans up nicely after itself. We won't use this function as part of this guide. | + | |
- | + | ||
- | The Bootstrap() function is an optional function to use. This is used to tell Orx where to find the first config file for use in our game (MyGame.ini). There is another way to do this, but for now, we'll use this function to inform Orx of the config. | + | |
- | + | ||
- | Then of course, the main() function. We do not need to use this function in this guide. | + | |
- | + | ||
- | You should be able to compile successfully. Run the program and an Orx logo will appear slowly rotating. | + | |
- | + | ||
- | Great. So now you have everything you need to start building the UFO game. | + | |
- | + | ||
- | + | ||
- | ====== 2. Setting up the game assets ====== | + | |
- | + | ||
- | Our game will have a background, a UFO which the player will control, and some pickups that the player can collect. | + | |
- | + | ||
- | The UFO will be controlled by the player using the cursor keys. | + | |
- | + | ||
- | First you'll need the assets to make the game. You can download the gameassets.zip file which will contain: | + | |
- | + | ||
- | The background file (background.png): | + | |
- | + | ||
- | {{ : | + | |
- | + | ||
- | The UFO and Pickup sprite images (ufo.png and pickup.png): | + | |
- | + | ||
- | {{: | + | |
- | + | ||
- | And a pickup sound effect (pickup.ogg): | + | |
- | + | ||
- | Unzip this file to a temporary location on your harddrive. | + | |
- | + | ||
- | Copy the three .png files into your data/object folder | + | |
- | + | ||
- | Copy the .ogg file into your data/sound folder. | + | |
- | + | ||
- | Now these files can be accessed by your project and be included in the game. | + | |
- | + | ||
- | + | ||
- | ====== Setting up the Playfield ====== | + | |
- | + | ||
- | + | ||
- | We will start by setting up the background object. This is done using config. | + | |
- | + | ||
- | Open the MyGame.ini config file in your editor and add the following: | + | |
- | + | ||
- | <code ini> | + | |
- | [BackgroundGraphic] | + | |
- | Texture = background.png | + | |
- | Pivot = center | + | |
- | </ | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | The Orx library know where to find this image, due to the properties set in the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [Resource] | + | |
- | Texture = ../ | + | |
- | </ | + | |
- | + | ||
- | So any texture files that are required (just like in our [BackgroundGraphic] section) will be located in the '' | + | |
- | + | ||
- | The second parameter is '' | + | |
- | + | ||
- | There are other values available for Pivot. To see the list of values, open the CreationTemplate.ini file in your editor. Scroll to the [GraphicTemplate] section and find Pivot in the list. There you can see all the possible values that could be used. | + | |
- | + | ||
- | '' | + | |
- | + | ||
- | We need to define an object that will make use of this graphic. This will be the actual entity that is used in the game: | + | |
- | + | ||
- | <code ini> | + | |
- | [BackgroundObject] | + | |
- | Graphic = BackgroundGraphic | + | |
- | Position = (0, 0, 0) | + | |
- | </ | + | |
- | + | ||
- | The Graphic property is the section '' | + | |
- | + | ||
- | The second property is the '' | + | |
- | + | ||
- | To make the object appear in our game, we will add a line of code in our source file to create it. | + | |
- | + | ||
- | In the Init() function of MyGame.cpp, remove the default line: | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | and replace it with: | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | The old spinning logo is now replaced with a nice tiled background object. | + | |
- | + | ||
- | Next, the ufo object is required. This is what the player will control. | + | |
- | + | ||
- | First create the configuration for the ufo object: | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoObject] | + | |
- | Graphic = UfoGraphic | + | |
- | Position = (0, 0, -0.1) | + | |
- | </ | + | |
- | + | ||
- | Then the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoGraphic] | + | |
- | Texture = ufo.png | + | |
- | Pivot = center | + | |
- | </ | + | |
- | + | ||
- | Unlike the background object, our ufo object will need to be assigned to a variable. This will make it possible to affect the ufo using code: | + | |
- | + | ||
- | Create the variable for our ufo object just under the orx.h include line: | + | |
- | + | ||
- | <code c> | + | |
- | #include " | + | |
- | orxOBJECT *ufo; | + | |
- | </ | + | |
- | + | ||
- | And in the Init() function, create an instance of the ufo object with: | + | |
- | + | ||
- | <code c> | + | |
- | ufo = orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | Compile and run. You'll see a ufo object in front of the background. Excellent. | + | |
- | + | ||
- | Let's talk a little about the Z axis positions of the ufo, the background and the camera. It's worth explaining to get a good understand of how cameras and frustums work. | + | |
- | + | ||
- | The following diagram will illustrate how our camera and objects are laid out: | + | |
- | + | ||
- | {{ : | + | |
- | + | ||
- | Note that the Camera z-position in the config is: -1.0 | + | |
- | The UFO z-position is: -0.1 | + | |
- | The Background z-position is: 0.0 | + | |
- | + | ||
- | So 0.0 is the maximum distance in our game, and -1.0 is the potential minimum distance (the position of the camera' | + | |
- | + | ||
- | Also note the camera' | + | |
- | + | ||
- | That's good, because our ufo and background are in that range: -0.1 and 0.0. As long as any objects have a z-position less than 0.0, they will be sure to be in front of the background. | + | |
- | + | ||
- | And as long as any objects have a z-position greater than -1.0, the camera will show them. | + | |
- | + | ||
- | Time to move to something a little more fun, moving the ufo. | + | |
- | ====== Controlling the UFO ====== | + | Gamedev.net is hosting |
- | + | ||
- | + | ||
- | The ufo is going to be controlled using the cursor arrow keys on the keyboard. | + | |
- | + | ||
- | The ufo will be moved by applying forces. Physics will be set up in the project in order to do this. | + | |
- | + | ||
- | Also, we will use a clock to call an update function every frame (?) which will read and respond to key presses. | + | |
- | + | ||
- | Defining the keys is very straight forward. In the config file, expand the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [MainInput] | + | |
- | KEY_ESCAPE = Quit | + | |
- | KEY_UP = GoUp | + | |
- | KEY_DOWN = GoDown | + | |
- | KEY_LEFT = GoLeft | + | |
- | KEY_RIGHT = GoRight | + | |
- | </ | + | |
- | + | ||
- | Each key is being given a label name. These label names are available in our code to test against. | + | |
- | + | ||
- | The next step is to create an update callback function in our code where we will continually check if the keys are pressed: | + | |
- | + | ||
- | <code c> | + | |
- | void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo, | + | |
- | { | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | And in order to tie this function to a clock (the clock will execute this function over and over), add the following to the Init() function: | + | |
- | + | ||
- | <code c> | + | |
- | orxClock_Register(orxClock_FindFirst(orx2F(-1.0f), orxCLOCK_TYPE_CORE), | + | |
- | </ | + | |
- | + | ||
- | That looks very scary and intimidating, | + | |
- | + | ||
- | Let's test a key to ensure that all is working well. Add the following code into the Update function: | + | |
- | + | ||
- | <code ini> | + | |
- | void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo, | + | |
- | { | + | |
- | if (ufo) { | + | |
- | + | ||
- | if (orxInput_IsActive(" | + | |
- | orxLOG(" | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Every time Update is run, ufo is tested to ensure it exists, and then moves to check the input system for the label " | + | |
- | + | ||
- | If that condition is true, send "LEFT PRESSED!" | + | |
- | + | ||
- | Soon we'll replace the orxLOG line with a function that places force on the ufo. But before that, we need to add physics to the ufo. | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | Press the left arrow key and take note of the console window. Every time you press or hold the key, the message is printed. Good, so key presses are working. | + | |
- | + | ||
- | + | ||
- | ====== Physics ====== | + | |
- | + | ||
- | + | ||
- | In order to affect the ufo using forces, physics need to be enabled. | + | |
- | + | ||
- | Begin by adding a Physics config section and setting Gravity with: | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 980, 0) | + | |
- | </ | + | |
- | + | ||
- | In order for an object in Orx to be affected by physics, it needs both a dynamic body, and at least one bodypart. Give the ufo a body with the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoObject] | + | |
- | Graphic = UfoGraphic | + | |
- | Position = (0, 0, -0.1) | + | |
- | Body = UfoBody | + | |
- | </ | + | |
- | + | ||
- | Next, create the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBody] | + | |
- | Dynamic = true | + | |
- | PartList = UfoBodyPart | + | |
- | </ | + | |
- | + | ||
- | The body part is set to '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBodyPart] | + | |
- | Type = sphere | + | |
- | Solid = true | + | |
- | </ | + | |
- | + | ||
- | The body part '' | + | |
- | + | ||
- | Compile and Run. | + | |
- | + | ||
- | The ufo falls through the floor. This is because of the gravity setting of 980 in the y axis which simulates world gravity. | + | |
- | + | ||
- | Our game is a top down game. So change the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 0, 0) | + | |
- | </ | + | |
- | + | ||
- | Re-run (no compile needed) and the ufo should remain in the centre of the screen. | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 0, 0) | + | |
- | ShowDebug = true | + | |
- | </ | + | |
- | + | ||
- | Re-run, and you will see a pinkish sphere outline automatically sized around the ufo object. For now we'll turn that off again. You can do this by changing the value to false, adding a '';'' | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 0, 0) | + | |
- | ShowDebug = false | + | |
- | </ | + | |
- | + | ||
- | Let's add some force to the ufo if the left cursor key is pressed. Change the code in the Update function to be: | + | |
- | + | ||
- | <code c> | + | |
- | void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo, | + | |
- | { | + | |
- | if (ufo) { | + | |
- | + | ||
- | const orxFLOAT FORCE = 0.8; | + | |
- | orxVECTOR leftForce= { -FORCE, 0, 0 }; | + | |
- | + | ||
- | if (orxInput_IsActive(" | + | |
- | orxObject_ApplyForce(ufo, | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | Compile and re-run. | + | |
- | + | ||
- | If you press and release the left arrow key, the ufo will move to the left. If you hold the left key down, the ufo will increase its speed and move out the left hand side of the screen. | + | |
- | + | ||
- | Even if you tap the left key once quickly, the ufo will still eventually travel out of the left of the screen. There is no friction yet to slow it down, or any barriers to stop it going out of the screen. | + | |
- | + | ||
- | Even though the background looks it has a border, it is really only a picture. In order to create a barrier for the ufo, we will need to wrap the edges using some body parts. | + | |
- | + | ||
- | This means, the background object will also be given a body, and four body parts, one for each wall. Start with adding a body to the object: | + | |
- | + | ||
- | <code ini> | + | |
- | [BackgroundObject] | + | |
- | Graphic = BackgroundGraphic | + | |
- | Position = (0, 0, 0) | + | |
- | Body = WallBody | + | |
- | </ | + | |
- | + | ||
- | And then the body itself: | + | |
- | + | ||
- | <code ini> | + | |
- | [WallBody] | + | |
- | Dynamic = false | + | |
- | PartList = WallTopPart # WallRightPart # WallBottomPart # WallLeftPart | + | |
- | </ | + | |
- | + | ||
- | This is different to the ufo body. This one is not dynamic. This means that it is a static body, one that cannot be affected by gravity. But dynamic objects can still collide with it. Also, there are four parts to this body, unlike the ufo which only had one. | + | |
- | + | ||
- | Start with the WallTopPart first: | + | |
- | + | ||
- | <code ini> | + | |
- | [WallTopPart] | + | |
- | Type = box | + | |
- | Solid = true | + | |
- | TopLeft = (-400, -300, 0) | + | |
- | BottomRight = (400, -260, 0) | + | |
- | </ | + | |
- | + | ||
- | In this part, use a '' | + | |
- | + | ||
- | Stretch the box to cover the region from (-400,-300) to (400, -260) | + | |
- | + | ||
- | At this point, it might be a good idea to turn on the physics debugging to check our work: | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 0, 0) | + | |
- | ShowDebug = true | + | |
- | </ | + | |
- | + | ||
- | Re-run the project. | + | |
- | + | ||
- | The top wall region should cover the top barrier squares: | + | |
- | + | ||
- | [] | + | |
- | + | ||
- | Great. Next, we'll do the right hand side. But rather than copy all the same values, we'll reuse some from the top wall: | + | |
- | + | ||
- | <code ini> | + | |
- | [WallRightPart@WallTopPart] | + | |
- | TopLeft = (360, -260,0) | + | |
- | BottomRight = (400, 260, 0) | + | |
- | </ | + | |
- | + | ||
- | Notice the '' | + | |
- | + | ||
- | Therefore, use the '' | + | |
- | + | ||
- | This is called " | + | |
- | + | ||
- | Re-run the project, and there will now be two walls. | + | |
- | + | ||
- | Define the last two walls using the same technique: | + | |
- | + | ||
- | <code ini> | + | |
- | [WallBottomPart@WallTopPart] | + | |
- | TopLeft = (-400, | + | |
- | BottomRight = (400, 300, 0) | + | |
- | + | ||
- | [WallLeftPart@WallTopPart] | + | |
- | TopLeft = (-400, | + | |
- | BottomRight = (-360, 260, 0) | + | |
- | </ | + | |
- | + | ||
- | Now there are four walls for the ufo to collide with. | + | |
- | + | ||
- | Re-run and try moving the ufo left into the wall. | + | |
- | + | ||
- | Oops, it doesn' | + | |
- | + | ||
- | This is done with flags and masks. | + | |
- | + | ||
- | Make a change to the ufo's body part by adding '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBodyPart] | + | |
- | Type = sphere | + | |
- | Solid = true | + | |
- | SelfFlags = ufo | + | |
- | CheckMask = wall | + | |
- | </ | + | |
- | + | ||
- | '' | + | |
- | + | ||
- | These labels don't have to match the names you give objects, however it will help you stay clean and organised. | + | |
- | + | ||
- | So above we are saying, the UfoBodyPart is a " | + | |
- | + | ||
- | But we haven' | + | |
- | + | ||
- | <code ini> | + | |
- | [WallTopPart] | + | |
- | Type = box | + | |
- | Solid = true | + | |
- | SelfFlags = wall | + | |
- | CheckMask = ufo | + | |
- | TopLeft = (-400, -300, 0) | + | |
- | BottomRight = (400, -260, 0) | + | |
- | </ | + | |
- | + | ||
- | Remember, that the other three wall parts inherit the values from '' | + | |
- | + | ||
- | Re-run and notice the ufo's debug region becomes mauve rather than pink. | + | |
- | + | ||
- | Press the left arrow key and drive the ufo into the left wall. It collides! And it stops. | + | |
- | + | ||
- | Now that the collision is working, we can flesh out the rest of the keyboard controls and test all four walls: | + | |
- | + | ||
- | <code c> | + | |
- | void orxFASTCALL Update(const orxCLOCK_INFO *_pstClockInfo, | + | |
- | { | + | |
- | if (ufo) { | + | |
- | + | ||
- | const orxFLOAT FORCE = 0.8; | + | |
- | + | ||
- | orxVECTOR rightForce = { FORCE, 0, 0 }; | + | |
- | orxVECTOR leftForce = { -FORCE, 0, 0 }; | + | |
- | orxVECTOR upForce = { 0, -FORCE, 0 }; | + | |
- | orxVECTOR downForce = { 0, FORCE, 0 }; | + | |
- | + | ||
- | if (orxInput_IsActive(" | + | |
- | orxObject_ApplyForce(ufo, | + | |
- | } | + | |
- | if (orxInput_IsActive(" | + | |
- | orxObject_ApplyForce(ufo, | + | |
- | } | + | |
- | if (orxInput_IsActive(" | + | |
- | orxObject_ApplyForce(ufo, | + | |
- | } | + | |
- | if (orxInput_IsActive(" | + | |
- | orxObject_ApplyForce(ufo, | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Now is a good time to turn off the physics debug as we did earlier on. | + | |
- | + | ||
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | Try all four keys, and you should be able to move the ufo around the screen. The ufo can also collide with each wall. | + | |
- | + | ||
- | The ufo is a little boring in the way that it doesn' | + | |
- | + | ||
- | We need to ensure the UfoBody is not using fixed rotation. While this value defaults to false when not supplied, it will make things more readable if we explicitly set it: | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBody] | + | |
- | Dynamic = true | + | |
- | PartList = UfoBodyPart | + | |
- | FixedRotation = false | + | |
- | </ | + | |
- | + | ||
- | The active ingredient here is to ensure that both the wall bodypart and the ufo bodypart both have a little friction applied. This way when they collide, they will drag against each other and produce some spin: | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBodyPart] | + | |
- | Type = sphere | + | |
- | Solid = true | + | |
- | SelfFlags = ufo | + | |
- | CheckMask = wall | + | |
- | Friction = 1.2 | + | |
- | + | ||
- | [WallTopPart] | + | |
- | Type = box | + | |
- | Solid = true | + | |
- | SelfFlags = wall | + | |
- | CheckMask = ufo | + | |
- | TopLeft = (-400, -300, 0) | + | |
- | BottomRight = (400, -260, 0) | + | |
- | Friction = 1.2 | + | |
- | </ | + | |
- | + | ||
- | Re-run that and give it a try. Run against a wall on angle to get some spin on the ufo. | + | |
- | + | ||
- | The next thing to notice is that both the movement of the ufo and the spin never slow down. There is no friction to slow those down. | + | |
- | + | ||
- | We'll deal with the spin first. By adding some '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBody] | + | |
- | Dynamic = true | + | |
- | PartList = UfoBodyPart | + | |
- | AngularDamping = 2 | + | |
- | FixedRotation = false | + | |
- | </ | + | |
- | + | ||
- | Re-run and check the spin. Should be slowing down after leaving the wall. | + | |
- | + | ||
- | Now for the movement. That can be done with '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBody] | + | |
- | Dynamic = true | + | |
- | PartList = UfoBodyPart | + | |
- | LinearDamping = 5 | + | |
- | AngularDamping = 2 | + | |
- | FixedRotation = false | + | |
- | </ | + | |
- | + | ||
- | Re-run and the speed will slow down after releasing the arrow keys. But it's slower overall as well. Not 100% what we want. | + | |
- | + | ||
- | You can increase the FORCE value in code, in the Update function to compensate: | + | |
- | + | ||
- | <code c> | + | |
- | const orxFLOAT FORCE = 1.8; | + | |
- | </ | + | |
- | + | ||
- | Compile and run. The speed should be more what we expect. | + | |
- | + | ||
- | Another thing to notice about the ufo is that during mid-rotation, | + | |
- | + | ||
- | This is achieved with '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoObject] | + | |
- | Graphic = UfoGraphic | + | |
- | Position = (0, 0, -0.1) | + | |
- | Body = UfoBody | + | |
- | Smoothing = true | + | |
- | </ | + | |
- | + | ||
- | Another thing that would be nice, is for the ufo to be already spinning a little when the game starts. For this, add a little '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoObject] | + | |
- | Graphic = UfoGraphic | + | |
- | Position = (0, 0, -0.1) | + | |
- | Body = UfoBody | + | |
- | Smoothing = true | + | |
- | AngularVelocity = 200 | + | |
- | </ | + | |
- | + | ||
- | Run this and the ship will have a small amount of spin at the start until the '' | + | |
- | + | ||
- | + | ||
- | ====== Following the ufo with the camera ====== | + | |
- | + | ||
- | + | ||
- | While we can simply move the ufo around with the keys on a fixed background, it will be a more pleasant experience to have the ufo fixed and have the screen scroll around instead. | + | |
- | + | ||
- | This effect can be achieved by parenting the camera to the ufo so that wherever the ufo goes, the camera goes. | + | |
- | + | ||
- | Currently, our project is set up so that the viewport has a camera configured to it. But the camera is not available to our code. | + | |
- | + | ||
- | We will require the camera to be available in a variable so that it can be parented to the ufo object. | + | |
- | + | ||
- | To fix this add a line in the Init() function to extract the camera from the viewport into a variable: | + | |
- | + | ||
- | <code ini> | + | |
- | orxVIEWPORT *viewport = orxViewport_CreateFromConfig(" | + | |
- | camera = orxViewport_GetCamera(viewport); | + | |
- | </ | + | |
- | + | ||
- | And because the ' | + | |
- | + | ||
- | <code c> | + | |
- | #include " | + | |
- | orxOBJECT *ufo; | + | |
- | orxCAMERA *camera; | + | |
- | </ | + | |
- | + | ||
- | Now it is time to parent the camera to the ufo: | + | |
- | + | ||
- | <code c> | + | |
- | ufo = orxObject_CreateFromConfig(" | + | |
- | orxCamera_SetParent(camera, | + | |
- | </ | + | |
- | + | ||
- | Compile and Run. | + | |
- | + | ||
- | Woah, hang on. That's crazy, the whole screen just rotated around when ufo. And it continues to rotate when hitting the ufo against the walls. See how the camera is a child of the ufo now? Not only does the camera move with the ufo, it rotates with it as well. | + | |
- | + | ||
- | We certainly want it to move with the ufo, but it would be nice ignore the rotation from the parent ufo. Add the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [Camera] | + | |
- | FrustumWidth | + | |
- | FrustumHeight = 600 | + | |
- | FrustumFar | + | |
- | FrustumNear | + | |
- | Position | + | |
- | IgnoreFromParent = rotation | + | |
- | </ | + | |
- | + | ||
- | Re-run. That's got it fixed. | + | |
- | + | ||
- | <WRAP center round box 60%> | + | |
- | Content to Delete: | + | |
- | + | ||
- | Wait the screen went blank. That not good. What happened? | + | |
- | + | ||
- | Something changed when we made the camera a parent of the ufo. When we did this, the camera' | + | |
- | + | ||
- | Now the camera' | + | |
- | + | ||
- | <code ini> | + | |
- | [Camera] | + | |
- | FrustumWidth | + | |
- | FrustumHeight = 600 | + | |
- | FrustumFar | + | |
- | FrustumNear | + | |
- | Position | + | |
- | </ | + | |
- | + | ||
- | Re-run. | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | + | ||
- | Now when you move around, the playfield will appear to scroll rather than it being the ufo that moves. This makes for a more dramatic and interesting effect. | + | |
- | + | ||
- | + | ||
- | + | ||
- | ====== Creating Pickup Objects ====== | + | |
- | + | ||
- | In our game, the ufo will be required to collect objects scattered around the playfield. | + | |
- | + | ||
- | When the ufo collides with one, the object will disappear, giving the impression that it has been picked up. | + | |
- | + | ||
- | Begin by creating a config section for the graphic, and then the pickup object: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupGraphic] | + | |
- | Texture = pickup.png | + | |
- | Pivot = center | + | |
- | + | ||
- | [PickupObject] | + | |
- | Graphic = PickupGraphic | + | |
- | </ | + | |
- | + | ||
- | The graphic will use the image pickup.png which is located in the project' | + | |
- | + | ||
- | It will also be pivoted in the center which will be handy for a rotation effect later. | + | |
- | + | ||
- | Finally, the pickup object uses the pickup graphic. Nice and easy. | + | |
- | + | ||
- | Our game will have eight pickup objects. We need a simple way to have eight of these objects in various places. | + | |
- | + | ||
- | We will employ a nice trick to handle this. We will make an empty object, called '' | + | |
- | + | ||
- | That way, wherever the parent is moved, the children move with it. | + | |
- | + | ||
- | Add that now: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupObjects] | + | |
- | ChildList = PickupObject1 # PickupObject2 # PickupObject3 # PickupObject4 # PickupObject5 # PickupObject6 # PickupObject7 # PickupObject8 | + | |
- | Position = (-400, -300, -0.1) | + | |
- | </ | + | |
- | + | ||
- | This object will have no graphic. That's ok. It can still act like any other object. | + | |
- | + | ||
- | Notice the position. It is being positioned in the top left hand corner of the screen. All of the child objects PickupObject1 to PickupObject8 will be positioned relative to the parent in the top left corner. | + | |
- | + | ||
- | Now to create the actual children. We'll use the inheritance trick again, and just use '' | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupObject1@PickupObject] | + | |
- | Position = (370, 70, -0.1) | + | |
- | + | ||
- | [PickupObject2@PickupObject] | + | |
- | Position = (210, 140, -0.1) | + | |
- | + | ||
- | [PickupObject3@PickupObject] | + | |
- | Position = (115, 295, -0.1) | + | |
- | + | ||
- | [PickupObject4@PickupObject] | + | |
- | Position = (215, 445, -0.1) | + | |
- | + | ||
- | [PickupObject5@PickupObject] | + | |
- | Position = (400, 510, -0.1) | + | |
- | + | ||
- | [PickupObject6@PickupObject] | + | |
- | Position = (550, 420, -0.1) | + | |
- | + | ||
- | [PickupObject7@PickupObject] | + | |
- | Position = (660, 290, -0.1) | + | |
- | + | ||
- | [PickupObject8@PickupObject] | + | |
- | Position = (550, 150, -0.1) | + | |
- | </ | + | |
- | + | ||
- | Each of the PickupObject* objects uses the properties defined in '' | + | |
- | + | ||
- | The last thing to do is to create an instance '' | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | Compile and Run. | + | |
- | + | ||
- | Eight pickup objects should appear on screen. Looking good. | + | |
- | + | ||
- | It would look good if the pickups rotated slowly on screen, just to make them more interesting. This is very easy to achieve in Orx using FX. | + | |
- | + | ||
- | FX can also be defined in config. | + | |
- | + | ||
- | FX allows you to affect an object' | + | |
- | + | ||
- | Change the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupObject] | + | |
- | Graphic = PickupGraphic | + | |
- | FXList = SlowRotateFX | + | |
- | </ | + | |
- | + | ||
- | Clearly being an '' | + | |
- | + | ||
- | An FX is a collection of FX Slots. FX Slots are the actual effects themselves. Confused? Let's work through it. First, the FX: | + | |
- | + | ||
- | <code ini> | + | |
- | [SlowRotateFX] | + | |
- | SlotList = SlowRotateFXSlot | + | |
- | Loop = true | + | |
- | </ | + | |
- | + | ||
- | This simply means, use some effect called '' | + | |
- | + | ||
- | Next the slot (or effect): | + | |
- | + | ||
- | <code ini> | + | |
- | [SlowRotateFXSlot] | + | |
- | Type = rotation | + | |
- | StartTime = 0 | + | |
- | EndTime = 10 | + | |
- | Curve = linear | + | |
- | StartValue = 0 | + | |
- | EndValue = 360 | + | |
- | </ | + | |
- | + | ||
- | That's a few properties. First, the '' | + | |
- | + | ||
- | The total time for the FX is 10 seconds, which comes from the '' | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | And the values which the curve uses over the 10 second period starts from 0 and climbs to 360. | + | |
- | + | ||
- | Re-run and notice the pickups now turning slowly for 10 seconds and then repeating. | + | |
- | + | ||
- | The rotation redraw is a little rough, just like the ufo one was. What is a good solution? Add smoothing: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupObject] | + | |
- | Graphic = PickupGraphic | + | |
- | FXList = SlowRotateFX | + | |
- | Smoothing = true | + | |
- | </ | + | |
- | + | ||
- | Re-run. Much better. | + | |
- | + | ||
- | + | ||
- | + | ||
- | ====== Picking up the collectable objects ====== | + | |
- | + | ||
- | + | ||
- | Time to make the ufo collide with the pickups. In order for this to work (just like for the walls) the pickups need a body. | + | |
- | + | ||
- | And the body needs to be set to collide with a ufo and vice versa. | + | |
- | + | ||
- | First a body for the pickup template: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupObject] | + | |
- | Graphic = PickupGraphic | + | |
- | FXList = SlowRotateFX | + | |
- | Smoothing = true | + | |
- | Body = PickupBody | + | |
- | </ | + | |
- | + | ||
- | Then the body section itself: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupBody] | + | |
- | Dynamic = false | + | |
- | PartList = PickupPart | + | |
- | </ | + | |
- | + | ||
- | Just like the wall, the pickup are not dynamic. We don't want them bouncing and traveling around as a result of being hit by the ufo. They are static and need to stay in place if they are hit. | + | |
- | + | ||
- | Next to define the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupPart] | + | |
- | Type = sphere | + | |
- | Solid = false | + | |
- | SelfFlags = pickup | + | |
- | CheckMask = ufo | + | |
- | </ | + | |
- | + | ||
- | The pickup is sort of roundish, so we're going with a spherical type. | + | |
- | + | ||
- | It is not solid. We want the ufo to able to pass through it when it collides. It should not influence the ufo's travel at all. | + | |
- | + | ||
- | The pickup is given a label of '' | + | |
- | + | ||
- | The ufo must reciprocate this arrangement (just like a good date) by adding pickup to its list of bodypart check masks: | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoBodyPart] | + | |
- | Type = sphere | + | |
- | Solid = true | + | |
- | Friction = 1.2 | + | |
- | SelfFlags = ufo | + | |
- | CheckMask = wall # pickup | + | |
- | </ | + | |
- | + | ||
- | This is a static bodypart, and we have specified collision actions to occur if the ufo collides with a pickup. But it's a little difficult to test this right now. However you can turn on the debug again to check the body parts: | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 0, 0) | + | |
- | ShowDebug = true | + | |
- | </ | + | |
- | + | ||
- | Re-run to see the body parts. | + | |
- | + | ||
- | Switch off again: | + | |
- | + | ||
- | <code ini> | + | |
- | [Physics] | + | |
- | Gravity = (0, 0, 0) | + | |
- | ShowDebug = false | + | |
- | </ | + | |
- | + | ||
- | To cause a code event to occur when the ufo hits a pickup, we need something new: a physics hander. The hander will run a function of ours whenever two objects collide. | + | |
- | + | ||
- | We can test for these two objects to see if they are the ones we are interested in, and run some code if they are. | + | |
- | + | ||
- | First, add the physics hander to the end of the Init() function: | + | |
- | + | ||
- | <code c> | + | |
- | orxClock_Register(orxClock_FindFirst(orx2F(-1.0f), | + | |
- | orxEvent_AddHandler(orxEVENT_TYPE_PHYSICS, | + | |
- | </ | + | |
- | + | ||
- | This will create a physics handler, and should any physics event occur, (like two objects colliding) then a function called '' | + | |
- | + | ||
- | Our new function will start as: | + | |
- | + | ||
- | <code c> | + | |
- | orxSTATUS orxFASTCALL PhysicsEventHandler(const orxEVENT *_pstEvent) | + | |
- | { | + | |
- | + | ||
- | if (_pstEvent-> | + | |
- | orxOBJECT *pstRecipientObject, | + | |
- | + | ||
- | /* Gets colliding objects */ | + | |
- | pstRecipientObject = orxOBJECT(_pstEvent-> | + | |
- | pstSenderObject = orxOBJECT(_pstEvent-> | + | |
- | + | ||
- | const orxSTRING recipientName = orxObject_GetName(pstRecipientObject); | + | |
- | const orxSTRING senderName = orxObject_GetName(pstSenderObject); | + | |
- | + | ||
- | orxLOG(" | + | |
- | + | ||
- | return orxSTATUS_SUCCESS; | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Every handler function passes an orxEVENT object in. This structure contains a lot of information about the event. | + | |
- | + | ||
- | The eID is tested to ensure that the type of physics event that has occurred is a orxPHYSICS_EVENT_CONTACT_ADD which indicates when objects collide. | + | |
- | + | ||
- | If true, then two orxOBJECT variables are declared, then set from the orxEVENT structure. They are passed in as the hSender and hRecipient objects. | + | |
- | + | ||
- | Next, two orxSTRINGs are declared and are set by getting the names of the objects using the orxObject_GetName function. The name that is returned is the section name from the config. | + | |
- | + | ||
- | Potential candidates are: '' | + | |
- | + | ||
- | The names are then sent to the console. | + | |
- | + | ||
- | Finally, the function returns orxSTATUS_SUCCESS which is required by an event function. | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | If you drive the ufo into a pickup or the edge of the playfield, a message will display on the console. So we know that all is working. | + | |
- | + | ||
- | Next is to add code to remove a pickup from the playfield if the ufo collides with it. Usually we could compare the name of one object to another and perform the action. | + | |
- | + | ||
- | In our case, however, the pickups are named different things: PickupObject1, | + | |
- | + | ||
- | So we will need to actually just check if the name contains " | + | |
- | + | ||
- | In fact, we don't need to test for the " | + | |
- | + | ||
- | First, remove the orxLOG line. We don't need that anymore. | + | |
- | + | ||
- | Change the function to become: | + | |
- | + | ||
- | <code c> | + | |
- | orxSTATUS orxFASTCALL PhysicsEventHandler(const orxEVENT *_pstEvent) | + | |
- | { | + | |
- | + | ||
- | if (_pstEvent-> | + | |
- | orxOBJECT *pstRecipientObject, | + | |
- | + | ||
- | /* Gets colliding objects */ | + | |
- | pstRecipientObject = orxOBJECT(_pstEvent-> | + | |
- | pstSenderObject = orxOBJECT(_pstEvent-> | + | |
- | + | ||
- | const orxSTRING recipientName = orxObject_GetName(pstRecipientObject); | + | |
- | const orxSTRING senderName = orxObject_GetName(pstSenderObject); | + | |
- | + | ||
- | if (orxString_SearchString(recipientName, | + | |
- | orxObject_SetLifeTime(pstRecipientObject, | + | |
- | } | + | |
- | + | ||
- | if (orxString_SearchString(senderName, | + | |
- | orxObject_SetLifeTime(pstSenderObject, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | return orxSTATUS_SUCCESS; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | You can see the new code additions after the object names. | + | |
- | + | ||
- | If an object name contains the word " | + | |
- | + | ||
- | This will ensure the object is removed instantly and deleted by Orx in a safe manner. | + | |
- | + | ||
- | Notice that the test is performed twice. Once, if the pickup object is the sender, and again if the object is the recipient. | + | |
- | + | ||
- | Therefore we need to check and handle both. | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | Move the ufo over the pickups and they should disappear nicely. | + | |
- | + | ||
- | ====== Sounds ====== | + | |
- | + | ||
- | It's great that collecting the pickups work, but a silent game is pretty bland. It would be great to have a sound play whenever a pickup is collected. | + | |
- | + | ||
- | Start by configuring a sound: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupSound] | + | |
- | Sound = pickup.ogg | + | |
- | KeepInCache = true | + | |
- | </ | + | |
- | + | ||
- | Then as part of the collision detection in the '' | + | |
- | + | ||
- | <code c> | + | |
- | if (orxString_SearchString(recipientName, | + | |
- | orxObject_SetLifeTime(pstRecipientObject, | + | |
- | orxObject_AddSound(pstSenderObject, | + | |
- | } | + | |
- | + | ||
- | if (orxString_SearchString(senderName, | + | |
- | orxObject_SetLifeTime(pstSenderObject, | + | |
- | orxObject_AddSound(pstRecipientObject, | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | In code above, if the recipient is a pickup object, then use the '' | + | |
- | + | ||
- | And of course, if the pickup object is the sender, we add the sound to the recipient object. Also, the '' | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | Hit the pickups and a sound will play. | + | |
- | + | ||
- | You can also use sounds without code. There is an '' | + | |
- | + | ||
- | We can use this sound on the ufo when it first appears in the game. | + | |
- | + | ||
- | This is as simple | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoObject] | + | |
- | Graphic = UfoGraphic | + | |
- | Position = (0, 0, -0.1) | + | |
- | Body = UfoBody | + | |
- | Smoothing = true | + | |
- | AngularVelocity = 200 | + | |
- | SoundList = SoundAppear | + | |
- | </ | + | |
- | + | ||
- | Re-run and a nice sound plays at the start of the game. | + | |
- | + | ||
- | ====== Adding a score ====== | + | |
- | + | ||
- | + | ||
- | What's a game without a score? | + | |
- | + | ||
- | The great thing about Orx objects is that they don't have to contain a texture as a graphic. They can contain a font and text rendered to a graphic instead. This is perfect for making a score object. | + | |
- | + | ||
- | Start by adding some config for the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [ScoreObject] | + | |
- | Graphic = ScoreTextGraphic | + | |
- | Position = (-380, -280, 0) | + | |
- | </ | + | |
- | + | ||
- | Next, to add the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [ScoreTextGraphic] | + | |
- | Text = ScoreText | + | |
- | </ | + | |
- | + | ||
- | Now to define the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [ScoreText] | + | |
- | String = 10000 | + | |
- | </ | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | Let's now create an instance of the '' | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | So far, the Init() function should look like this: | + | |
- | + | ||
- | <code c> | + | |
- | orxSTATUS orxFASTCALL Init() | + | |
- | { | + | |
- | orxVIEWPORT *viewport = orxViewport_CreateFromConfig(" | + | |
- | camera = orxViewport_GetCamera(viewport); | + | |
- | + | ||
- | orxObject_CreateFromConfig(" | + | |
- | ufo = orxObject_CreateFromConfig(" | + | |
- | + | ||
- | orxObject_CreateFromConfig(" | + | |
- | + | ||
- | orxObject_CreateFromConfig(" | + | |
- | + | ||
- | orxClock_Register(orxClock_FindFirst(orx2F(-1.0f), | + | |
- | orxEvent_AddHandler(orxEVENT_TYPE_PHYSICS, | + | |
- | + | ||
- | return orxSTATUS_SUCCESS; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | There should be a score object in the top left hand corner displaying: 10000 | + | |
- | + | ||
- | The score is pretty small. And it's fixed into the top left corner of the playfield. That's not really what we want. | + | |
- | + | ||
- | A score is an example of a User Interface (UI) element. It should be fixed in the same place on the screen. Not move around when the screen scrolls. | + | |
- | + | ||
- | The score should in fact, be fixed as a child to the Camera. So that wherever the Camera goes, the score object goes with it. | + | |
- | + | ||
- | This can be achieved with the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [ScoreObject] | + | |
- | Graphic = ScoreTextGraphic | + | |
- | Position = (-380, -280, 0) | + | |
- | ParentCamera = Camera | + | |
- | UseParentSpace = false | + | |
- | </ | + | |
- | + | ||
- | With these changes, we've stated that we want the Camera to be the parent of the '' | + | |
- | + | ||
- | By saying that we don't want to '' | + | |
- | + | ||
- | And '' | + | |
- | + | ||
- | Re-run and you'll see the score in much the same position as before, but when you move the ufo around, and the screen scrolls, the score object remains fixed in the same place. | + | |
- | + | ||
- | The only thing, it's still a little small. We can double its size using '' | + | |
- | + | ||
- | <code ini> | + | |
- | [ScoreObject] | + | |
- | Graphic = ScoreTextGraphic | + | |
- | Position = (-380, -280, 0) | + | |
- | ParentCamera = Camera | + | |
- | UseParentSpace = false | + | |
- | Scale = 2.0 | + | |
- | </ | + | |
- | + | ||
- | Re-run. That looks a lot better. | + | |
- | + | ||
- | To actually make use of the score object, we will need a variable in code of type int to keep track of the score. | + | |
- | + | ||
- | Every clock cycle, we'll take that value and change the text on the ScoreObject. | + | |
- | + | ||
- | That is another cool feature of Orx text objects. The text can be changed any time, and the object will re-render. | + | |
- | + | ||
- | Finally, when the ufo collides with the pickup, and the pickup is destroyed, the score variable will be increased. The clock will pick up the variable value and set the score object. | + | |
- | + | ||
- | Begin by creating a '' | + | |
- | + | ||
- | <code c> | + | |
- | #include " | + | |
- | orxOBJECT *ufo; | + | |
- | orxCAMERA *camera; | + | |
- | int score = 0; | + | |
- | </ | + | |
- | + | ||
- | Change the comparison code inside the '' | + | |
- | + | ||
- | <code c> | + | |
- | if (orxString_SearchString(recipientName, | + | |
- | orxObject_SetLifeTime(pstRecipientObject, | + | |
- | orxObject_AddSound(pstSenderObject, | + | |
- | score += 150; | + | |
- | } | + | |
- | + | ||
- | if (orxString_SearchString(senderName, | + | |
- | orxObject_SetLifeTime(pstSenderObject, | + | |
- | orxObject_AddSound(pstRecipientObject, | + | |
- | score += 150; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Now we need a way to change the text of the score object. We declared the score object in the Init() function as: | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | But we really need to create it using an orxOBJECT variable: | + | |
- | + | ||
- | <code c> | + | |
- | scoreObject = orxObject_CreateFromConfig(" | + | |
- | </ | + | |
- | + | ||
- | And then declare the '' | + | |
- | + | ||
- | <code c> | + | |
- | #include " | + | |
- | orxOBJECT *ufo; | + | |
- | orxCAMERA *camera; | + | |
- | orxOBJECT *scoreObject; | + | |
- | int score = 0; | + | |
- | </ | + | |
- | + | ||
- | Now it is possible to update the score object using our score variable. At the bottom of the Update() function, add the following code: | + | |
- | + | ||
- | <code c> | + | |
- | if (scoreObject) { | + | |
- | orxCHAR formattedScore[5]; | + | |
- | orxString_Print(formattedScore, | + | |
- | + | ||
- | orxObject_SetTextString(scoreObject, | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | First, the block will only execute if there is a valid scoreObject. | + | |
- | + | ||
- | If so, then create a 5 character string. Then print into the string with the score value, effectively converting an int into a string. | + | |
- | + | ||
- | Finally set the score text to the scoreObject using the '' | + | |
- | + | ||
- | Compile and Run. | + | |
- | + | ||
- | Move the ufo around and collect the pickups to increase the score 150 points at a time. | + | |
- | + | ||
- | + | ||
- | ====== Winning the game ====== | + | |
- | + | ||
- | + | ||
- | 1200 is the maximum amount of points that can be awarded, and that is what will make us win the game. | + | |
- | + | ||
- | If we do win, we want a text label to appear above the ufo, saying "You win!" | + | |
- | + | ||
- | Like the score object, we need to define a '' | + | |
- | + | ||
- | <code ini> | + | |
- | [YouWinObject] | + | |
- | Graphic = YouWinTextGraphic | + | |
- | Position = (0, -60, 0.0) | + | |
- | Scale = 2.0 | + | |
- | </ | + | |
- | + | ||
- | Just like the camera, the '' | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | Next, the actual '' | + | |
- | + | ||
- | <code ini> | + | |
- | [YouWinTextGraphic] | + | |
- | Text = YouWinText | + | |
- | Pivot = center | + | |
- | </ | + | |
- | + | ||
- | And the text to render into the YouWinTextGraphic: | + | |
- | + | ||
- | <code ini> | + | |
- | [YouWinText] | + | |
- | String = You Win! | + | |
- | </ | + | |
- | + | ||
- | We'll test it by creating an instance of the '' | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_CreateFromConfig(" | + | |
- | scoreObject = orxObject_CreateFromConfig(" | + | |
- | + | ||
- | ufoYouWinTextObject = orxObject_CreateFromConfig(" | + | |
- | orxObject_SetParent(ufoYouWinTextObject, | + | |
- | </ | + | |
- | + | ||
- | Then the variable: | + | |
- | + | ||
- | <code c> | + | |
- | #include " | + | |
- | orxOBJECT *ufo; | + | |
- | orxCAMERA *camera; | + | |
- | orxOBJECT *ufoYouWinTextObject; | + | |
- | orxOBJECT *scoreObject; | + | |
- | int score = 0; | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | Compile and Run. | + | |
- | + | ||
- | The "You win" text should appear above the ufo. Not bad, but the text it rotating with the ufo much like the camera did. We can ignore the rotation from the parent on this object too: | + | |
- | + | ||
- | <code ini> | + | |
- | [YouWinObject] | + | |
- | Graphic = YouWinTextGraphic | + | |
- | Position = (0, -60, 0.0) | + | |
- | Scale = 2.0 | + | |
- | IgnoreFromParent = rotation | + | |
- | </ | + | |
- | + | ||
- | Re-run. Interesting. It certainly isn't rotating with the ufo, but its position is still being taken from the ufo's rotation. We need to ignore this as well: | + | |
- | + | ||
- | <code ini> | + | |
- | [YouWinObject] | + | |
- | Graphic = YouWinTextGraphic | + | |
- | Position = (0, -60, 0.0) | + | |
- | Scale = 2.0 | + | |
- | IgnoreFromParent = position.rotation rotation | + | |
- | </ | + | |
- | + | ||
- | Good that's working right. | + | |
- | + | ||
- | We want the "You Win!" to appear once all pickups are collected. | + | |
- | + | ||
- | The '' | + | |
- | + | ||
- | <code c> | + | |
- | orxObject_Enable(ufoYouWinTextObject, | + | |
- | </ | + | |
- | + | ||
- | Finally, all that is left to do is add a small check in the '' | + | |
- | + | ||
- | <code c> | + | |
- | if (orxString_SearchString(recipientName, | + | |
- | orxObject_SetLifeTime(pstRecipientObject, | + | |
- | orxObject_AddSound(pstSenderObject, | + | |
- | score += 150; | + | |
- | } | + | |
- | + | ||
- | if (orxString_SearchString(senderName, | + | |
- | orxObject_SetLifeTime(pstSenderObject, | + | |
- | orxObject_AddSound(pstRecipientObject, | + | |
- | score += 150; | + | |
- | } | + | |
- | + | ||
- | if (orxObject_IsEnabled(ufoYouWinTextObject) == orxFALSE && score == 1200) { | + | |
- | orxObject_Enable(ufoYouWinTextObject, | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | We are checking two things: that the '' | + | |
- | + | ||
- | If both conditions are met, enable the '' | + | |
- | + | ||
- | Compile and run. | + | |
- | + | ||
- | Move the ufo around and collect all the pickups. When all are picked up and 1200 is reached, the "You Win!" text should appear above the ufo signifying that the game is over and we have won. | + | |
- | + | ||
- | And that brings us to the end! We have created a simple and complete game with some configuration and minimal code. | + | |
- | + | ||
- | Congratulations! | + | |
- | + | ||
- | I hope you enjoyed working through making the ufo game. Of course, there are many little extras you can add to give your game that little extra polish. So there a couple more chapters that you can follow along with if you wish. | + | |
- | + | ||
- | + | ||
- | ===== Shadows ===== | + | |
- | + | ||
- | There are many ways to do shadows. One method is to use shaders... though this method is a little beyond this simple guide. | + | |
- | + | ||
- | Another method, when making your graphics, would be to add an alpha shadow underneath. This is a good method if your object does not need to rotate or flip. | + | |
- | + | ||
- | The method I will show you in this chapter is to have a separate shadow object as a child of an object. And in order to remain independent of rotations is to ignore them from the parent. | + | |
- | + | ||
- | First a shadow graphic for the ufo, and one for the pickups: | + | |
- | + | ||
- | {{: | + | |
- | + | ||
- | + | ||
- | Save these both into the ''/ | + | |
- | + | ||
- | Then create config for the ufo shadow: | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoShadowGraphic] | + | |
- | Texture = ufo-shadow.png | + | |
- | Alpha = 0.3 | + | |
- | Pivot = center | + | |
- | </ | + | |
- | + | ||
- | The only interesting part is the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoShadowObject] | + | |
- | Graphic | + | |
- | Position = (20, 20, 0.05) | + | |
- | </ | + | |
- | + | ||
- | Set the '' | + | |
- | + | ||
- | Next, add the '' | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoObject] | + | |
- | Graphic | + | |
- | Position | + | |
- | Body = UfoBody | + | |
- | AngularVelocity = 200 | + | |
- | Smoothing | + | |
- | UseParentSpace | + | |
- | SoundList | + | |
- | ChildList | + | |
- | </ | + | |
- | + | ||
- | Run the project. | + | |
- | + | ||
- | The shadow child is sitting properly behind the ufo but it rotates around the ufo, which is not right. We'll need to ignore the rotation from the parent: | + | |
- | + | ||
- | <code ini> | + | |
- | [UfoShadowObject] | + | |
- | Graphic | + | |
- | Position | + | |
- | IgnoreFromParent | + | |
- | </ | + | |
- | + | ||
- | Not only do we need to ignore the rotation of ufo, we also need to ignore the rotation position of the ufo. | + | |
- | + | ||
- | Re-run and the shadow sits nice and stable to the bottom right of the ufo. | + | |
- | + | ||
- | Now to do the same with the pickup shadow: | + | |
- | + | ||
- | <code ini> | + | |
- | [PickupShadowGraphic] | + | |
- | Texture = pickup-shadow.png | + | |
- | Alpha = 0.3 | + | |
- | Pivot = center | + | |
- | + | ||
- | [PickupShadowObject] | + | |
- | Graphic | + | |
- | Position | + | |
- | IgnoreFromParent = position.rotation | + | |
- | </ | + | |
- | + | ||
- | The only difference between this object and the ufo shadow, is that we want the pickup shadow to take the rotation value from the parent. But we do not want to take the position rotation. | + | |
- | + | ||
- | That way, the pickup shadow will remain in the bottom right of the pickup, but will rotate nicely in place. | + | |
- | Now attach as a child to the pickup object: | + | {{ guides:ufo: |
- | <code ini> | + | You can jump to the five parts here: |
- | [PickupObject] | + | |
- | Graphic | + | |
- | FXList | + | |
- | Smoothing = true | + | |
- | Body = PickupBody | + | |
- | ChildList = PickupShadowObject | + | |
- | </ | + | |
- | Re-run, and the shadows should all be working correctly. | + | - [[https:// |
+ | - [[https:// | ||
+ | - [[https:// | ||
+ | - [[https:// | ||
+ | - [[https:// |