码迷,mamicode.com
首页 > 其他好文 > 详细

抽象公共类,复用公共方法为多个对象

时间:2014-06-05 01:00:42      阅读:394      评论:0      收藏:0      [点我收藏+]

标签:des   android   c   style   class   a   

今天学习如何实现愤怒的小鸟,用到了JBox2D构建物理世界(这个不是这篇文章重点,但是很谢谢这个开源玩意)

一般一个独立的物体的实体类

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) {
// 保证正在飞行的鸟的坐标在屏幕正中
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;
}


}

当我们构建多个物体的时候,假如它们有共同性,提出公共方法为抽象类(至少有一个抽象方法)



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;


// 记录销毁时的坐标,以及弧度
private float[] destoryPoint;
private float destoryAngle;


public void draw(Canvas canvas, Paint paint, Point nowPosition) {
// 前后的画布操作,在这里实现
// 旋转画布 
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);//只要重新实现这里就可以了


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();
}


// 当生命小于0时,需要将body销毁
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);
}
}
}

抽象公共类,复用公共方法为多个对象,布布扣,bubuko.com

抽象公共类,复用公共方法为多个对象

标签:des   android   c   style   class   a   

原文地址:http://blog.csdn.net/needkane/article/details/27092747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!