Coding the basic transform

In this project we will do a better job of the Transform class. In the Scrolling Shooter project, the Transform was packed full of variables and methods that many of the objects didn't need. It served its purpose to prevent the project getting even bigger and we got away with it because there was a limited number of objects in the game.

What we will code now is a very simple version of the Transform and it will contain only the members and methods needed by all game objects. When we need a Transform that does more specific things we can then extend this class and add the required extra members and methods.

Add a new class called Transform. Code the member variables and the constructor as shown next.

import android.graphics.PointF;
import android.graphics.RectF;

public class Transform {
    RectF mCollider;
    private PointF mLocation;
    private float mSpeed;
    private float mObjectHeight;
    private float mObjectWidth;
    private PointF mStartingPosition;
    private boolean mHeadingUp = false;
    private boolean mHeadingDown = false;

    private boolean mFacingRight = true;
    private boolean mHeadingLeft = false;
    private boolean mHeadingRight = false;

    Transform(float speed, float objectWidth, 
              float objectHeight, 
                  PointF startingLocation) {

        mCollider = new RectF();
        mSpeed = speed;
        mObjectHeight = objectHeight;
        mObjectWidth = objectWidth;
        mLocation = startingLocation;

        // This tells movable blocks their starting position
        mStartingPosition = new PointF(
                      mLocation.x, mLocation.y);
    }
}

The members should look quite familiar and self-explanatory. We have a RectF to represent the collider (mCollider), PointF instances for the current location and the starting location (mLocation and mStartingPosition), float variables for speed, height and width (mSpeed, mObjectHeight and mObjectWidth) and boolean variables for each direction the object could be heading.

In the constructor, all the variables representing position, size and speed are initialized. All the values come from the method's parameters. We will see later in the chapter that the GameObjectFactory class will have access to all the specifications needed for a level and will instantiate all the Tranform instances by passing the data we have just seen to the Transform constructor. Just as it was in the previous project except this time GameObjectFactory will have more than one type of Transform to choose from.

Code updateCollider and getCollider methods shown next.

public void updateCollider() {
   mCollider.top = mLocation.y;
   mCollider.left = mLocation.x ;
   mCollider.bottom = 
         (mCollider.top + mObjectHeight);
      
   mCollider.right = 
         (mCollider.left + mObjectWidth);
}


public RectF getCollider() {
   return mCollider;
}

The updateCollider method uses the position of the object along with its width and height to make sure the collider is up-to-date so that collision detection can be done on its precise position. The getCollider method makes the collider available to the PhysicsEngine class so collision detection can be performed.

Add this bunch of getters and setters shown next.

void headUp() {
   mHeadingUp = true;
   mHeadingDown = false;
}

void headDown() {
   mHeadingDown = true;
   mHeadingUp = false;
}

boolean headingUp() {
   return mHeadingUp;
}

boolean headingDown() {
   return mHeadingDown;
}

float getSpeed() {
   return mSpeed;
}

PointF getLocation() {
   return mLocation;
}

PointF getSize() {
   return new PointF(
         (int) mObjectWidth, 
         (int) mObjectHeight);
}

Although quite a long list the methods we just added are quite simple. They allow the instances of the Transform class to share and set the values of some of its private member variables.

Add these getters and setters next.

void headRight() {
   mHeadingRight = true;
   mHeadingLeft = false;
   mFacingRight = true;

}

void headLeft() {
   mHeadingLeft = true;
   mHeadingRight = false;
   mFacingRight = false;
}

boolean headingRight() {
   return mHeadingRight;
}

boolean headingLeft() {
   return mHeadingLeft;
}

void stopHorizontal() {
   mHeadingLeft = false;
   mHeadingRight = false;
}

void stopMovingLeft() {
   mHeadingLeft = false;
}

void stopMovingRight() {
   mHeadingRight = false;
}

boolean getFacingRight() {
   return mFacingRight;
}

PointF getStartingPosition(){
   return mStartingPosition;
}

These are the final methods for the Transform class. They make available to get and set, more of the members of the Transform class. Familiarize yourself with the method names and the variables they work with.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.68.236