Game Development in Android using Libgdx Part VIII

In this tutorial we show how to move the protagonist by tapping near it.

We will make a class which would will handle the gravitational force applied to the protagonist on tapping the screen. For the lack of a better name we will call it ObjectPull.

It would have a location property like other objects on screen but won’t be considered for collision so we won’t add it in the objects manager.

Gravity.java

public class Gravity{
         public Vector2 Position;
         Vector2 impulseDirection;
         public static final float IMPULSE=100;
         float impulseMagnitude;
         TextureObject outerTex;
         TextureObject innerTex;
         CircleObject coreObject;
         public static final float IMPULSE_RADIUS=200;
         public static final float INNER_CORE_RADIUS=15;

         Boolean isActive;
         public Gravity(TextureRegion outerRegion,TextureRegion innerRegion){
              Position=new Vector2();
              forceDirection=new Vector2();
              outerTex=new TextureObject(outerRegion,Position);
              innerTex=new TextureObject(innerRegion,Position);
              coreObject=new CircleObject(INNER_CODE_RADIUS);
              coreObject.Mass=0;
         }
         public void Update(int tx,int ty,boolean active){
              isActive=active;
              if(isActive){ 
                    Position.set(tx,ty);
                    innerTexture.Position.set(Position);
                    outerTexture.Position.set(Position);
              }
         }

         public Vector2 UpdatePlayerImpulse(CircleObject player){
                impulseMagnitude=0;
                if(isActive){
                      float distance=Position.dst(player.Position.x,player.Position.y)-player.Radius;
                      if(distance<INNER_CODE_RADIUS){
                            //Handle Response/Kill Player/Reduce health
                            CollisionHandler.CollisionResponse(player,coreObject);

                      }else if(distance<IMPULSE_RADIUS){
                            impulseMagnitude=1-distance/IMPULSE_RADIUS;
                            impulseMagnitude*=impulseRatio*IMPULSE;
                            impulseDirection.set(Position.x-player.Position.x,Position.y-player.Position.y);
                            impulseDirection.nor();
                      }
                }
                impulseDirection.mult(impulseMagnitude);
                return impulseDirection;
         } 
         public void Draw(SpriteBatch sp){
              if(isActive){
                   outerTexture.Draw(sp);
                   innerTexture.Draw(sp);
              }
         } 
}

So in the initialization code of the game we would just instantiate an object of this class. Then in Update we would have a code similar to this.

A little change to the TapResponse class.

Adding two more fields called CurrentX and CurrentY;

int CurrentX;
int CurrentY;
public void UpdateTouch(int x,int y,boolean IsTouching){
      //...................
      CurrentX=x;
      CurrentY=y;
      //...................
}

Addition in the gameplay screen update function.

public void Update(float dt){
  //................
  ObjectManager.Update(dt);
  if(tapResponse.TouchState==tapResponse.TOUCH_START ||
       tapResponse.TouchState==tapResponse.TOUCH_DRAG){
             GravityObject.Update(tapResponse.CurrentX,touchHandler.CurrentY);
             heroObject.ApplyImpulse(GravityObject.UpdatePlayerImpulse(heroObject));
  }
  //................
 }

Now we will add the enemies. We will call them mob. For levels mode we would be reading enemies from a file, while for score/infinite mode we would be creating a logic to generate enemies randomly in the playing area.

A Small Change to BaseObject class.

We will add a field called Tag which will be useful to identify which group the object belongs to.

public String Tag;

Mob.java

public class Mob extends  CircleObject{
     public static final float MOB_RADIUS=5f;
     public TextureObject texture;
     public Boolean IsActive;
     public Mob(TextureRegion region, float x,float y){
            super(MOB_RADIUS);
            Position=new Vector2(x,y);
            texture=new TextureObject(region,Position);
            IsActive=true;
            IsSensor=false;
            Tag="MOB";
     } 
     public void Draw(SpriteBatch sp){
           if(IsActive)
                      texture.Draw(sp);
     } 
}

Stars:

Stars are the collectibles which the protagonist has to collect to finish the levels. They are similar to the above class, with a small difference that IsSensor property would be true for them so that when protagonist collides with the star, it would just pass through rather than rebounding from it. We would give a different tag so that while handling collision response we can change the state of the game variables easily.

public class Star extends  CircleObject{
     public static final float STAR_RADIUS=5f;
     public TextureObject texture;
     public Boolean IsActive;
     public Mob(TextureRegion region, float x,float y){
            super(STAR_RADIUS);
            Position=new Vector2(x,y);
            texture=new TextureObject(region,Position);
            IsActive=true;
            IsSensor=true;
            Tag="STAR";
     } 
     public void Draw(SpriteBatch sp){
           if(IsActive)
                      texture.Draw(sp);
     } 
}

We will add a draw function to the ObjectManager class and that will then draw all the objects to the screen.  As it has an array of BaseObjects we can add a Draw function which can be implement in the inherited classes. In case there is a need for invisible objects then leave Draw function empty.

BaseObject.java

public abstract void Draw(SpriteBatch sp);

ObjectManager.java

public void Draw(SpriteBatch sp){
       for(int i=0;i<objects.length;i++)
           objects.Draw(sp);
}

LevelManager.java

public class LevelManager{
       public int TotalMobs;
       public int TotalStars;
       public int CurrentMobCount;
       public int CurrentStarCount;
       public float TimeLimit;
       public Timer GameTimer;
       //.....
}

So we have created a mob/enemy for our game. In the object parser where we parse the objects we would be making mob/stars objects while parsing and adding them to the objectmanager list and updating the counts in LevelManager object.

//ObjectParser.java
ObjectManager objectManager;
LevelManager levelManager;
MobManager mobManager;
StarManager starManager;
public static final int MOB_OBJECT=1;
public static final int STAR_OBJECT=2;
//....
public void ParseObject(int level){
     //...
     int x=Integer.parseInt(dataStr[OBJ_POS_X);
     int y=Integer.parseInt(dataStr[OBJ_POS_Y);
     int type=Integer.parseInt(dataStr[OBJ_POS_TYPE);
     switch(type){
           case MOB_OBJECT:Mob mob=new Mob(MobTextureRegion,x,y)
                   manager.AddObject(mob);
                   levelManager.TotalMobCount++;
           case STAR_OBJECT:Star star=new Star(StarTextureRegion,x,y)
                   manager.AddObject(star);
                   levelManager.TotalStarCount++;

     } 

}

So we have read a text file, made objects and added them to an array which would be updated and rendered every frame.

Next Tutorial:

Now we have to check the collisions of objects and handle them respectively.

Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.

Thank you for the patience.

Tagged with: , , , ,
Posted in Tutorials
Try our games



Error: Twitter did not respond. Please wait a few minutes and refresh this page.