Mote Warrior Part 2 - Magic-Mouse.Net

Stepping it up a notch

Now we need a mote, a mote is per definition something small, battery operated drone or a drink made from grains. Both have inspired the name. Warrior, well to explore fighting aspects of a game, which is pretty basic in all games beside games like tetris and bejewled.

So to to prevent redundant code we are making a new class named Mote.

public class Mote extends Thing{

}

Notice that i extends Thing. This makes us reuse the code in Thing. Making sure that if you decide to change somthing in the original class, every class is getting refactored.

In the class you have to make a constructor you call:

    public Mote(int x, int y,int width, int length, double deg1, double deg2)
           {
                      super(x, y, 0, 0, width, length, deg1, deg2);
                      // TODO Auto-generated constructor stub
                      this.x = x;
                      this.y = y;
                      this.z = 0;
                      this.length = length;
                      this.width = width;
                      this.height = 0;
                      this.deg1 = deg1;
                      this.deg2 = deg2;
           }

This will make an error in the code, since the variables in Thing is created as private properties. To solve the bug you need to refactor Thing and make the variables like this:

    protected int x;
    protected int y;
    protected int z;
    protected int height;
    protected int width;
    protected int length;
    protected double deg1;
    protected double deg2;

In the end it gives the same result, only this one is working.

Remember like in thing we have a 2d constructior, what super does is to fill in to the parent class the information, thats why in super, we have x and height to zero as default.

           public Mote(int x, int y, int z, int height, int width, int length,
                                 double deg1, double deg2) {
                      super(x, y, z, height, width, length, deg1, deg2);
                      // TODO Auto-generated constructor stub
           }

If you want to make use of the 3d constructor, you just add the following text insted. Though with Java supporting overload i suggest you add both.

Having the getters and setters from Thing we don't need to think about that, to show how that works we can make a little example code:

            Mote car = new Mote(1, 2, 3, 4, 0, 90);
                   System.out.print(house.getX() + "\n");
           // Returns 1

We make a mote, thats a mobile object that can move on a two dimentional surface. In this case a car, notice that even though we didn't make any functions in the class. All are extended from the superclass: Thing.

Now we allready agreed that Thing was a static object like a house, and a mote is a movable object like a character or a car in our last example.

Lets make it move.

This is where we make the change from thing. We add a new function to the Mote Class, called move, so in this case it is:

           public void move(int velocity)
           {
          
           }

           public void jump(int velocity)
           {
          
           }

Now this is nothing special about this, though since we are at this point focusing on a 2d game with 3d posiblities, we are gonna forget about jump and just focus about the move class, then later return to the jump function.

To get the correct distance to move we need to know how fast we move. So we enter the velocity in to the function. Now if we could only move up, down, left and right, there would be no problem. We would just check what direction we were moving and then add the value to the current position, some thing like this.

           public void move(int velocity)
           {
                      if(deg1 = 0){x = x + velocity;}
                      if(deg1 = 180){x = x - velocity;}
                      if(deg1 = 90){y = y + velocity;}
                      if(deg1 = 270){y = y - velocity;}
           }

Or if you wanted to make the result even prettier, you could add it in to a select, but, what if you got a free moving object that can move to any direction, what do you do then?

The result is a bit more mathematical, but is fairly simple and is actually shorter codewise:

           public void move(int velocity)
           {
                      if(deg1 = 0){x = x + velocity;}
                      if(deg1 = 180){x = x - velocity;}
                      if(deg1 = 90){y = y + velocity;}
                      if(deg1 = 270){y = y - velocity;}
           }

Now calling function move(10); will make your Mote move 10 in the given direction. Note that the accuracy of the movements increases the larger the ”playing board” is, lets say that we are moving 1000 pixels, rounding down to nearst whole number isn't as affected as if we move 2 pixels where rounding down to nearst whole number could be an offset of up to 25%. So keep the scale large considered movements.

So now we can go straight ahead (or backwards feeding in a negative number, move(-10); now we need to turn our movable object.

We start out like the other times by defining the function. We don't need any feedback and we need to use it from where ever so public void, and a describing name: rotateDeg. With a double as parameter because it would be posible to turn it 20,2 degrees.

           public void rotateDeg(double deg){

           }

The basic mathematics here is pretty easy. We just add or substract the amount of degrees to our current degrees, so the math looks like this deg – deg1. Where deg is the parameter and deg1 is object's current heading.

In code it looks something like this:

           public void rotateDeg(double deg){
                      double intDeg = this.deg1 + deg;
           }

Though we have  a problem. If we are having a heading of 7° and we are moving 20° to the left (counter clock wise) we get a result of -13° and thats not posible since we know that a degree is somewhere between 1 and 360. And always positive.

In code it looks like this:

           public void rotateDeg(double deg){
                      double intDeg = this.deg1 + deg;
                      if(intDeg >= 360)
                      {this.deg1 = Math.abs(intDeg - 360);}
                      else if( intDeg < 0)
                      {this.deg1 = Math.abs(360 + intDeg);}      
                      else
                      {this.deg1 = Math.abs(intDeg);}
           }

 

 

Content is copyright www.magic-mouse.net | System is copyright Magic-Mouse.Net | Design By Snorri Debess