Today, I learned how to implement Angry Birds and used JBox2D to build the physical world (this is not the focus of this article, but thank you very much for this open-source stuff)
An entity class of an independent object.
Import org. jbox2d. dynamics. Body;
Import org. liky. angrybird. util. Globals;
Import org. liky. angrybird. util. ImageUtils;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. Point;
Public class Bird {
Private Body body;
Private float r;
Private int type;
Public Bird (int type ){
Switch (type ){
Case 1:
R = Globals. PIECE_WIDTH/2;
Break;
}
This. type = type;
}
Public void draw (Canvas canvas, Paint paint, Point nowPosition ){
Canvas. save ();
Canvas. rotate (float) (body. getAngle () * 180/Math. PI ),
Body. getPosition (). x * Globals. RATE + nowPosition. x,
Body. getPosition (). y * Globals. RATE + nowPosition. y );
Canvas. drawBitmap (ImageUtils. getBirdImg (type), body. getPosition (). x
* Globals. RATE-r + nowPosition. x, body. getPosition (). y
* Globals. RATE-r + nowPosition. y, paint );
If (Globals. birdCenterFlag ){
// Ensure that the coordinates of the flying bird are in the center of the screen
NowPosition. x = (int)-(body. getPosition (). x * Globals. RATE-Globals. SCREEN_WIDTH/2 );
NowPosition. y = (int)-(body. getPosition (). y * Globals. RATE-Globals. SCREEN_HEIGHT/2 );
If (nowPosition. x> 0 ){
NowPosition. x = 0;
}
If (nowPosition. y> 0 ){
NowPosition. y = 0;
}
If (nowPosition. x <-Globals. SCREEN_WIDTH ){
NowPosition. x =-Globals. SCREEN_WIDTH;
}
If (nowPosition. y <-Globals. SCREEN_HEIGHT ){
NowPosition. y =-Globals. SCREEN_HEIGHT;
}
}
Canvas. restore ();
}
Public Body getBody (){
Return body;
}
Public void setBody (Body body ){
This. body = body;
This. body. m_userData = this;
}
Public int getType (){
Return type;
}
Public void setType (int type ){
This. type = type;
}
Public float getR (){
Return r;
}
Public void setR (float r ){
This. r = r;
}
}
When we construct multiple objects, if they are common, we propose a public method as an abstract class (at least one abstract method)
Import org. jbox2d. dynamics. Body;
Import org. liky. angrybird. util. Globals;
Import org. liky. angrybird. util. JBoxUtils;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. Point;
Public abstract class Item {
Private Body body;
Private float width;
Private float height;
Private float life = 500;
Private int animIndex = 0;
Private int countDown = 2;
// Record the coordinates and radians at the time of destruction
Private float [] destoryPoint;
Private float destoryAngle;
Public void draw (Canvas canvas, Paint paint, Point nowPosition ){
// Canvas operations before and after are implemented here
// Rotate the canvas
Canvas. save ();
If (life> 0 ){
Canvas. rotate (float) (body. getAngle () * 180/Math. PI ),
Body. getPosition (). x * Globals. RATE + nowPosition. x,
Body. getPosition (). y * Globals. RATE + nowPosition. y );
} Else {
// Canvas. rotate (float) (destoryAngle * 180/Math. PI ),
// Body. getPosition (). x * Globals. RATE + nowPosition. x,
// Body. getPosition (). y * Globals. RATE + nowPosition. y );
}
DrawItem (canvas, paint, nowPosition); // you only need to implement it again.
Canvas. restore ();
}
Public abstract void drawItem (Canvas canvas, Paint paint, Point nowPosition );
Public Body getBody (){
Return body;
}
Public void setBody (Body body ){
This. body = body;
This. body. m_userData = this;
}
Public float getWidth (){
Return width;
}
Public void setWidth (float width ){
This. width = width;
}
Public float getHeight (){
Return height;
}
Public void setHeight (float height ){
This. height = height;
}
Public float getLife (){
Return life;
}
Public void setLife (float life ){
This. life = life;
If (life <= 0 ){
If (destoryPoint = null ){
DestoryPoint = new float [2];
DestoryPoint [0] = body. getPosition (). x * Globals. RATE;
DestoryPoint [1] = body. getPosition (). y * Globals. RATE;
DestoryAngle = body. getAngle ();
}
// When the lifecycle is less than 0, the body must be destroyed.
JBoxUtils. world. destroyBody (body );
}
}
Public int getAnimIndex (){
Return animIndex;
}
Public void setAnimIndex (int animIndex ){
This. animIndex = animIndex;
}
Public int getCountDown (){
Return countDown;
}
Public void setCountDown (int countDown ){
This. countDown = countDown;
}
Public float [] getDestoryPoint (){
Return destoryPoint;
}
Public void setDestoryPoint (float [] destoryPoint ){
This. destoryPoint = destoryPoint;
}
Public float getDestoryAngle (){
Return destoryAngle;
}
Public void setDestoryAngle (float destoryAngle ){
This. destoryAngle = destoryAngle;
}
}
Package org. liky. angrybird. vo;
Import org. jbox2d. dynamics. Body;
Import org. liky. angrybird. util. Globals;
Import org. liky. angrybird. util. ImageUtils;
Import android. graphics. Canvas;
Import android. graphics. Paint;
Import android. graphics. Point;
Public class Wood extends Item {
Public Wood (Body body ){
SetBody (body );
SetWidth (Globals. PIECE_WIDTH * 3 );
SetHeight (Globals. PIECE_HEIGHT/2 );
SetLife (100 );
}
Public void drawItem (Canvas canvas, Paint paint, Point nowPosition ){
If (getLife ()> 300 ){
Canvas. drawBitmap (ImageUtils. getWood (0), getBody (). getPosition (). x
* Globals. RATE-getWidth ()/2 + nowPosition. x, getBody ()
. GetPosition (). y
* Globals. RATE
-GetHeight ()
/2
+ NowPosition. y, paint );
} Else 'if (getLife ()> 100 ){
Canvas. drawBitmap (ImageUtils. getWood (1), getBody (). getPosition (). x
* Globals. RATE-getWidth ()/2 + nowPosition. x, getBody ()
. GetPosition (). y
* Globals. RATE
-GetHeight ()
/2
+ NowPosition. y, paint );
} Else if (getLife ()> 0 ){
Canvas. drawBitmap (ImageUtils. getWood (2), getBody (). getPosition (). x
* Globals. RATE-getWidth ()/2 + nowPosition. x, getBody ()
. GetPosition (). y
* Globals. RATE
-GetHeight ()
/2
+ NowPosition. y, paint );
} Else {
SetCountDown (getCountDown ()-1 );
If (getCountDown () = 0 ){
SetCountDown (2 );
SetAnimIndex (getAnimIndex () + 1 );
If (getAnimIndex () = 15 ){
SetAnimIndex (14 );
}
}
Canvas. drawBitmap (ImageUtils. getWoodDestory (getAnimIndex ()),
GetDestoryPoint () [0]-Globals. PIECE_WIDTH/2
+ NowPosition. x, getDestoryPoint () [1]
-Globals. PIECE_HEIGHT + nowPosition. y, paint );
}
}
}