First Steps - RotcivOcnarb/libgdx-helper-library GitHub Wiki
Introduction
This library is a collection of helper functions that makes programming in LibGDX faster and more visual.
It works based on Tiled to run and design scenes (or States, as they are called here), but it isn't required.
It is highly recommended that you have minimum knowledge of LibGDX before starting to use this library.
Setting up the project
Simply clone this repository, and import in eclipse as a Gradle project (File > Import > Existing Gradle project).
Structure
The first thing you can notice is that there are a LOT of classes, but they are simply wrapers of already existing classes that makes thing be written faster (1 line of code instead of 3 billion)
You see that "test" package over there? You can delete it completely, this is just an example and its not supposed to influence the library classes (or you may want to look at it for some example usage of the library)
For now, the only important Class is the StateOne
class inside the states package
It has a few things there, because i am currently still working on the project, and i have made some games to test the functionalities, but the "Empty" class should be something like this:
package com.mygdx.game.states;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class StateOne extends State{
public StateOne(StateManager manager) {
super(manager);
}
public void create() {
}
public void render(SpriteBatch sb) {
}
public void update(float delta) {
}
public void dispose() {
}
}
Now we can work on things.
The first thing you may want to enable is Physics (if you want a physics scene), you can do this on the create
method with:
enablePhysics(new StateOneListener(this));
The parameter passed is a class with EmptyContact
class inherited, and it works like a ContactListener
, but with a few upgrades. For now lets put this aside (StateOneListener is already setup to work properly). The thing is for each State you create that has physics enabled, it must be paired with a EmptyContact class that handles collision (it is highly recommended that you keep those classes SEPARATED for organization purposes).
You can also set the Gravity with
setGravity(Vector2 gravity);
Now lets add some interactivity in the game. Currently there are 2 Player Classes, PlatformPlayer
and TopDownPlayer
, both are GameObjects that has built in player logic, like simple movement, and for the Platform Player, simple jump logic. Because they are abstract classes, if you want to create a player, you need to create another class that inherits one of those classes. Let's begin with a simple Platform player.
First create a class called MyPlayer
(or any other name you like). I will create this class in the test
package, for organization purposes. Add the extends PlatformPlayer
code on the class declaration, and implement constructors and abstract methods (use the constructor that takes the 2 Vector2 parameters).
public class MyPlayer extends PlatformPlayer{
public MyPlayer(ObjectInfo info, Vector2 position, Vector2 size) {
super(info, position, size);
}
public void create() {
}
public void dispose() {
}
public void render(SpriteBatch sb, ShapeRenderer sr, OrthographicCamera camera) {
}
}
And that's all for now. The only reason why PlatformPlayer
is an abstract class, is for the render
method that allows you to render anything you want for a player, but we are not going on this right now, the physics render for us is enough.
Now that you have a player class created, lets put a player on the game! Go back to StateOne
class and create a new player with that created class:
public class StateOne extends State{
MyPlayer player;
public StateOne(StateManager manager) {
super(manager);
}
public void create() {
enablePhysics(new StateOneListener(this));
setGravity(new Vector2(0, -20));
player = new MyPlayer(new ObjectInfo(this, 0, 1f), Position.CENTER, new Vector2(30, 40));
}
public void render(SpriteBatch sb) {
}
public void update(float delta) {
}
public void dispose() {
}
}
The ObjectInfo
parameter is simply a structure that gives information about the GameObject, the 3 parameters you should pass to it are:
-
State state
: This is the state owner of the object. Because we are working inside a State class, simply pass thethis
keyword that references to this state -
int z
this is the rendering order of objects, you can pass lower numbers to render Objects on the back of the screen, and high numbers to render objects on the front. -
float scale
this is mainly for Tiled own management, because we are not using Tiled for now, it is irrelevant, you can simply pass 1 to it
The next two parameters, are simply the Position and Size of the player. A lot of times i see myself using positions like:
new Vector2(Gdx.graphics.getWidth()/2f, Gdx.graphics.getHeigth()/2f)
wich is simply the way of describing "the center of the screen". That's why i create a few static Vectors to fasten the process. When i call Position.CENTER
, its just this vector above. There are other premade vectors too, like Position.CENTERX
, Position.CENTERY
, Size.SCREEN
, Size.HALFWIDTH
and others.
Now the last thing you need to do to put the player in the game is call this function:
putInScene(player);
This put the object in the rendering / updating queue.
If you run the game, the player should fall from the center of the screen.
Let's put a platform so he can stand on.
I made a lot of Physics helper classes to, so you don't need to create all those lines of code to make a BodyDef, and PolygonShape and stuff. A rectangular static body can be simply added in the game with:
addStaticRectangleBody(Position.CENTERX, Size.WIDTH.cpy().add(0, 100));
So the StateOne
class is now like this:
public class StateOne extends State{
MyPlayer player;
public StateOne(StateManager manager) {
super(manager);
}
public void create() {
enablePhysics(new StateOneListener(this));
enableDebugDraw();
setGravity(new Vector2(0, -20));
player = new MyPlayer(new ObjectInfo(this, 0, 1f), Position.CENTER, new Vector2(30, 40));
putInScene(player);
addStaticRectangleBody(Position.CENTERX, Size.WIDTH.cpy().add(0, 100));
}
public void render(SpriteBatch sb) {
}
public void update(float delta) {
}
public void dispose() {
}
}
We then can move our character on top of the platform. But movement seem a little too aggressive. Lets smooth things out.
Luckly the PlatformPlayer
class has a few methods that can control how the player movement works. First lets make the jumps a little lower:
player.setJumpStrength(10);
Now the player jumps are allright, but the speed is still too fast, lets slow things down a little:
player.setSpeed(7);
And the last thing, you can also change how many jumps the player can do (single jumps, double jumps, triple jumps, etc). Let's reduce the player to a single jump (the default is doublejump):
player.setTotalJumps(1);
You can also call those methods inside the MyPlayer
class, to make things more transparent.
Now you have a basic player movement! The next steps is understanding more deeply how Physics can be used and how to integrate Tiled maps in your game!
You can continue following the tutorials for Tiled and Physics