Power Tutorial

From Leadwerks Developer Wiki

Jump to: navigation, search

Contents

Introduction

This tutorial shows how to use effectively all provided tools of LE (official + community). This tutorial is using LE 2.3 and Editor. The old version of this tutorial is using LE 2.28 and Sandbox, and it can be found here: Power_Tutorial_(old).

Tutorial Steps

1) Creating the game source code

Launch LEWizard, and set the parameters as following:
image:pt_pic01.png
The result will look like:
image:pt_pic02.png
Change the working directory for All Configurations to ".":
image:pt_pic07.png


2) Setting up the game media

Create a media folder in the game project folder, and copy the following folders from the SDK under the media folder:
image:pt_pic03.png
The Maps folder contains just a simple Editor scene, with cobblestones terrain texture, a skybox, a directional light, a tree, a waterplane, and a info_playerstart entity:
image:pt_pic04a.png
You can create the main.sbx yourself, or download this example:
media:pt_maps.rar
You need also the PlayerStart model, which you put under the media/Models/Entities/Info/PlayerStart folder. This just adds a visual PlayerStart entity placeholder to Editor, and gamelib reads its properties:
media:pt_playerstart.rar
Change also the game path in Editor to point to your game project's media folder (I use c:\user\letut\media here).


3) Adding gamelib to the game source

Add gamelib.cpp to the source section of your project, and change the source code to look like this:
image:pt_pic05.png
Now we can run the game using Ctrl-F5, and it looks like this:
image:pt_pic06.png


4) Adding the player

After the line with LoadMap(), we can add one line to tell gamelib who the current player is:

	game.scene.SetCurrentPlayer("info_playerstart");

Now we can also add MouseLook after the line with game.Update():

	game.MouseLook(game.scene.cam);

You can run this code, but you can just look around, and there is also a arrow model which gamelib added for us, since we didn't provide a player model in the Editor scene, but we don't see it because it's somewhere under the terrain. Let's make the player movable via keyboard, add these lines after the line with MouseLook():

			game.KeyboardMove();
			game.PositionPlayerModel();
			game.PositionCamera();

Now we can move around, and have also chase cam (use the mouse wheel):
image:pt_pic09.png


5) Adding music

You can place stereo and mono sounds into Editor. Stereo sounds are always independant of any positions, while mono sounds are 3D positional. We add a stereo sound, since it's the game music:
image:pt_pic10a.png
Gamelib supports the following entitykeys for an env_sound class entity:

  • loop = 0 or 1 (default is 1)
  • sound = filename of the sound
  • range = number, how far away the sound can be heard (works for mono sounds only)
  • pitch = 1.0 is normal pitch
  • volume = 0.0 to 1.0

Here is the music for the tutorial, place it under media/Sounds/Music:
media:tattarat.rar
To make your own ogg sounds and music files, I recommend to use NCH WavePad, it's a free professional wave editor which can save also as ogg, and it's much better than Audacity: http://www.nch.com.au/wavepad/masters.html The Master's Edition gives you bookmarks and few more features, but the free edition will do fine for casual musicians.


6) Making models

We can use various 3D modellers to make models for LE. Well, basically all, if you have UU3D Pro. For huge buildings (since it has CSG) and simple shapes Leadwerks 3DWS is still unbeaten. Let's make a simple bamboo crate with 3DWS:
image:pt_pic11.png
Notice that the size of 4x4x4 grids (when you start 3DWS) is 1x1x1 meter in Editor and LE. To convert models to gmf format, there are various tools in the SDK, including 3DW, X, FBX, to gmf. We use the 3dw2gmf.exe tool, but with a little trick to make it easier (the same trick works for the other tools too). We create some folder on the Desktop, I call it 3dw2gmf, and place a renamed 3dw2gmf.exe here. Now you can just drag and drop any 3dw file on top of the 3dw2gmf.cmd and it creates the correct gmf in the same folder as your original 3dw file:
image:pt_pic12.png
The trick cmd file can be downloaded here:
media:3dw2gmfcmd.rar
Now we need to create a mat file for the gmf model. This is simple because we have only a diffuse texture, so we can let LE use all its default settings. Note that the mat file must be according to the texture filename without extension, and not the model filename.
File: bamboo.mat

texture0="abstract::bamboo.dds"

I use Paint.NET (it's free) to convert any texture to DDS, just make sure you save it with DXT5 option when saving as DDS. All the bamboo crate related files go to media/Models/Bamboocrate folder. The final step for every decent model is to add a physics body. For our bamboo crate it's very simple, since it's already in standard scale due to our trick cmd:
image:pt_pic13.png
To make the crate alive in Editor and LE, just add mass=1 to its entitykeys:
image:pt_pic14.png The complete Bamboocrate model can be downloaded here: media:bamboocrate.rar


7) Adding Weapons

I made a simple weapon placeholder model in 3DWS, which you can place in Sandbox. Place these files for the weapon under media/Models/Weapon:
media:pt_weapon.rar
Then you can add some settings to each weapon model:
image:pt_pic15a.png
To assign the weapons to a certain player, we need to name the players first (make sure this is also done in Editor), and then we need to set the initial weapon for the player:

	game.scene.SetCurrentPlayer("player1");
	game.scene.GetCurrentPlayer()->SetWeapon("machinegun");

After the line with game.PositionCamera() we can add some logic to fire the weapons. For weapons which have no autofire, we should use Mouse::IsHit, so that it won't repeat shooting:

			if(game.scene.GetCurrentPlayer()->GetWeapon()->autofire==0)
			{
				if(Mouse::IsHit(LEFT))game.PlayerShoot();
			}
			else
			{
				if(Mouse::IsDown(LEFT))game.PlayerShoot();
			}

After those lines, we can add an option to enable camera zoom using the right mouse button:

			if(Mouse::IsHit(RIGHT))game.SwitchCameraZoom();

And after that line, we can add weapon switching using the number keys 1 and 2. Note that Mouse::IsHit() needs to be called after switching a weapon to avoid buffered mouse hits when switching from an autofire weapon to a single fire weapon:

			if(Keyboard::IsHit(KEY_1))
			{
				game.scene.GetCurrentPlayer()->SetWeapon("machinegun");
				Mouse::IsHit();
			}
			if(Keyboard::IsHit(KEY_2))
			{
				game.scene.GetCurrentPlayer()->SetWeapon("rifle");
				Mouse::IsHit();
			}

8) Physics Joints

Coming soon...


Personal tools