LibCamera.java:
package game.pak;
import game.CGame;
import game.config.dConfig;
import game.object.CHero;
import game.object.CObject;
import android.graphics.Point;
import android.util.Log;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.clmobi.SinCity.en.EngineActivity;
import com.clmobi.SinCity.en.MyGame;
public class LibCamera {
private Rectangle glViewport;
public OrthographicCamera cam;
private SpriteBatch LibCameraBatch; //add by mxh 2013-10-8
public CObject AniCameraObject;//设置被放大对象 (用于对该对象坐标调整)
// public static float zoom;
public LibCamera(float S_W,float S_Y,SpriteBatch batch){
cam = new OrthographicCamera(S_W, S_Y);
glViewport = new Rectangle(0, 0, S_W, S_Y);
cam.position.set(S_W / 2, S_Y / 2, 0);
LibCameraBatch=batch;
}
public LibCamera(float x,float y,float z){
cam = new OrthographicCamera(dConfig.S_WIDTH, dConfig.S_HEIGHT);
cam.position.set(dConfig.S_WIDTH / 2, dConfig.S_HEIGHT / 2, 0);
}
/**
* 镜头中心位置设定
* @param x
* @param y
* @param z
*/
public void set_position(float x,float y,float z){
cam.position.set(x,y,z);
}
/**
* 设置镜头旋转角度
* @param angle
*/
public void set_rotate(float angle){
cam.rotate(angle);
}
/**
* 设置zoom
* @param zoom
*/
public void set_zoom(float zoom){
cam.zoom=zoom;
}
/**
* 设置zoom
* @param zoom
*/
public float get_zoom(){
return cam.zoom;
}
/**
* 改变(增加)zoom
* @param czoom
*/
public void change_zoom(float czoom){
cam.zoom+=czoom;
}
/**
* 设置被放大对象 (用于对该对象坐标调整)
* @param anicCameraObject
*/
public void set_AniObject(CObject anicCameraObject){
AniCameraObject=anicCameraObject;
}
//**********************************************************************************
// LibCamera 缩放效果处理
//**********************************************************************************
private float cam_zoom0;//记录原始幅度
private float cam_sw;//单帧缩放幅度
private float cam_az;//单帧缩放加速度
private int cam_time;//缩放时间 (帧数)
private int cam_timehalf;//缩放时间中点 (内部使用变量)
// private boolean cam_ismoment;//是否瞬间缩放
private boolean cam_ismomentflg;//标志是否瞬间缩放(内部使用变量)
private int cam_effecttype;//镜头缩放类型
private boolean IsAuto;//镜头缩放类型
/**
*
* 镜头缩放设置
* add by mxh 2013-8-29
* @param sw 单次(一帧)缩放幅度
* @param az 缩放加速度
* @param time 缩放时间(/帧)
* @param type 标记镜头放大效果种类 0:瞬间缩放 1:渐变缩放 2:渐放瞬缩 3:瞬放渐缩
* @param isauto 是否自动缩放镜头
*/
public void setcameraeffect(float sw,float az,int time,int type,boolean isauto){
if(cam_time>0){//add by mxh 2013-10-8 如果当前有镜头执行 不进行重新设置
Log.e("setcameraeffect","cam_time>0");
return;
}else{
Log.e("setcameraeffect","cam_time=0");
}
cam_sw=sw;
cam_az=az;
if(time%2==0){//做取偶处理
cam_time=time;
}else{
cam_time=time-1;
}
cam_timehalf=cam_time/2;
cam_effecttype=type;
if(cam_effecttype==0){
cam_ismomentflg=true;
}
cam_zoom0=cam.zoom;
IsAuto=isauto;
}
/**
* 镜头缩放(自动) (drawLevel()下)
* add by mxh 2013-8-27
*/
public void cam_auto_amiplfy(){
if(IsAuto){
//return;
}else{
if( cam_time>0) {//
cam.zoom-=cam_sw;
// cam_sw=cam_sw+cam_az;
cam_time--;
}
// return;
}
if(cam_time>0&&IsAuto){
switch(cam_effecttype){
case 0://瞬间缩放
if(cam_ismomentflg){
cam_ismomentflg=false;
cam.zoom-=cam_sw;
}
if( cam_time == 1) {
// cam.zoom+=cam_sw;
cam.zoom=cam_zoom0;//change by mxh 2013-10-8
IsAuto=false;
}
break;
case 1://渐变缩放
if( cam_time> cam_timehalf) {//前半断时间
cam.zoom-=cam_sw;
cam_sw=cam_sw+cam_az;
}else{//
cam_sw=cam_sw-cam_az;
cam.zoom+=cam_sw;
// IsAuto=false;
}
if( cam_time == 1) {//add by mxh 2013-10-8
cam.zoom=cam_zoom0;
IsAuto=false;
}
break;
case 2://渐放瞬缩
cam.zoom-=cam_sw;
cam_sw=cam_sw+cam_az;
if( cam_time == 1) {
cam.zoom=cam_zoom0;
IsAuto=false;
}
break;
case 3://瞬放渐缩 //add by mxh 2013-10-8
if(!cam_ismomentflg){
cam_ismomentflg=true;
cam.zoom-=(cam_sw*(cam_time-1));
}else{
cam.zoom+=cam_sw;
cam_sw=cam_sw+cam_az;
}
if( cam_time == 1) {
cam.zoom=cam_zoom0;
cam_ismomentflg=false;
IsAuto=false;
}
break;
}
cam_time--;
}
//对单个对象坐标调整 (调整偏移)
if(AniCameraObject!=null){
Point position=new Point();
position=func2(AniCameraObject.m_x, AniCameraObject.m_y, (1-get_zoom()));
int xx=position.x;
int yy=position.y;
set_position(dConfig.S_WIDTH/2+xx, dConfig.S_HEIGHT/2-yy, 0); //(0.05*4*240)/2
}
}
/**
*设置绑定SpritBatch
* @param batch
*/
public void set_Batch(SpriteBatch batch){
LibCameraBatch=batch;
// batch.setProjectionMatrix(cam.combined);
}
/**
* (render 内更新)
* @param gl
* @param batch
*/
public void update(/*GL10 gl*//*,SpriteBatch batch*/){
GL10 gl = Gdx.graphics.getGL10();
gl.glViewport((int) glViewport.x, (int) glViewport.y,
(int) glViewport.width, (int) glViewport.height);
cam.update();
cam.apply(gl);
//它的作用都是通过把映射矩阵绑定给SpritBatch,告诉SpritBatch怎么去绘制图形
LibCameraBatch.setProjectionMatrix(cam.combined);
}
/**
* 数据清零
*/
public void clear(){
cam_sw=0;
cam_az=0;
cam_time=0;
cam_timehalf=0;
// cam_ismoment=false;
// cam_ismomentflg=false;
// EngineActivity.instance.myGame.cam.zoom=1.0f;
}
/**
* 将场景坐标转换为以屏幕中心为原点的屏幕坐标
* @param x1
* @param y1
* @return
*/
public Point func1(int x1,int y1){
Point new_position = new Point();
//将屏幕中心作为坐标原点
int x0=dConfig.S_WIDTH / 2;
int y0=dConfig.S_HEIGHT / 2;
//将场景坐标转换成屏幕坐标
int old_x=((x1>> dConfig.FRACTION_BITS)-Camera.cameraLeft);
int old_y=((y1>> dConfig.FRACTION_BITS)-Camera.cameraTop);
//将坐标转换成以屏幕中心作为坐标原点的新坐标
int new_x=old_x-x0;
int new_y=old_y-y0;
new_position.set(new_x, new_y);
return new_position;
}
/**
* 得到场景坐标放大后的屏幕偏移量
* @param x1
* @param y1
* @param scale
* @return
*/
public Point func2(int x1,int y1,float scale){
Point position1 = new Point();
Point position2 = new Point();
position1=func1( x1, y1);
float xx=position1.x*scale;
float yy=position1.y*scale;
position2.set((int)xx, (int)yy);
return position2;
}
}