您的位置:首页 > 其它

七,游戏世界

2015-11-04 00:14 393 查看
游戏世界GameWorld,游戏中的精灵运行在游戏世界里。这个类主要是根据每一次的迭代更新计算出精灵的碰撞效果,移动距离等。

package com.hj.lee.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Disposable;
import com.hj.lee.game.assets.Assets;
import com.hj.lee.game.assets.Constants;
import com.hj.lee.game.assets.GameMap;
import com.hj.lee.game.bodyobjects.Bullet;
import com.hj.lee.game.bodyobjects.DynamicPlatform;
import com.hj.lee.game.bodyobjects.Enemy;
import com.hj.lee.game.bodyobjects.MapBodyBuilder;
import com.hj.lee.game.bodyobjects.Mario;
import com.hj.lee.game.bodyobjects.PolygonRegionBuilder;
import com.hj.lee.game.bodyobjects.RayCast;
import com.hj.lee.game.bodyobjects.WallBodies;
import com.hj.lee.game.bodyobjects.WorldObstacle;
import com.hj.lee.game.utils.AudioManager;
import com.hj.lee.game.utils.RunTimeData;

/**
* @author lee
*
* 游戏世界的控制类,实现了box2d的碰撞接口
*/
public class WorldController implements ContactListener, Disposable {
public static String TAG = WorldController.class.getName();
// is debug enabled
boolean isDebug = true;// 和raycast一起
// member var
public World world;// box2d的世界
GameMap gameMap;// 遊戲世界的地圖
Array<WorldObstacle> worldObstacles;// 遊戲世界里的規則或者不規則的剛體
PolygonRegionBuilder polygonRegionBuilder;// 用來構造剛體
public Array<Enemy> enemies;// 敵人
public Array<Bullet> bullets;// 子彈
public Array<DynamicPlatform> dynamicPlatforms;// 可以移動的敵人站立的平台
public WallBodies wallBodies;// 遊戲世界四周的剛體
public Mario mario;// 主角
// temp
Vector2 mTmp;
// game
public DirectedGame game;
// debug
RayCast raycast;// 測試用

public WorldController(DirectedGame game) {
this.game = game;
init();

}

public void init() {
polygonRegionBuilder = new PolygonRegionBuilder();
wallBodies = new WallBodies();
world = new World(new Vector2(0, -9.8f), true);
world.setContactListener(this);
gameMap = new GameMap();
gameMap.loadTiledMap(String.valueOf(RunTimeData.choosenLevel));
worldObstacles = MapBodyBuilder.buildWorldObstacles(gameMap.getMap(), Constants.PPT, world);
enemies = MapBodyBuilder.buildEnemyObject(gameMap.getMap(), Constants.PPT, world);
mario = MapBodyBuilder.buildProtagonistLayerObject(gameMap.getMap(), Constants.PPT, world);
dynamicPlatforms = MapBodyBuilder.buildDynamicPlatform(gameMap.getMap(), Constants.PPT, world);
mario.bulluetNum = enemies.size;
MapBodyBuilder.buildFrame(gameMap.getMap(), Constants.PPT, world, wallBodies);
bullets = new Array<Bullet>();
for (int i = 0; i < worldObstacles.size; i++) {
polygonRegionBuilder.buildPolygonRegion(worldObstacles.get(i).obstaclesBody.body, Assets.instance.rock.rock1);
}
if (!Constants.isGameMapRender) {
for (int i = 0; i < wallBodies.frames.size; i++) {
polygonRegionBuilder.buildPolygonRegion(wallBodies.frames.get(i).body.body, Assets.instance.rock.rock1);
}
}

if (isDebug)
raycast = new RayCast(world);

// System.out.println("world body count :" + world.getBodyCount() + " " + TAG);
}

public void update(float delta, Vector2 touchPoint, boolean isTouched) {
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
updateBodyGameObjects(delta);
mario.update(delta, touchPoint, isTouched);
}

public void updateBodyGameObjects(float delta) {
for (Bullet b : bullets) {
b.setBodyVelocity();
if (b.isDead) {
world.destroyBody(b.bulletBody.body);
bullets.removeValue(b, true);
// System.out.println("bullets length:"+bullets.size);
}
}
for (Enemy enemy : enemies) {
enemy.update(delta);
if (enemy.isKilled() && enemy.stateTime > Enemy.ENEMY_DEAD_TIME) {
world.destroyBody(enemy.enemyBody.body);
enemies.removeValue(enemy, true);
}

}

for (DynamicPlatform dp : dynamicPlatforms) {
dp.update(delta);
}
}

// 使用玩家触摸的位置构造子弹
public void createBullet(Vector2 touchPoint) {
// Gdx.app.log("tag", "X:" + touchPoint.x + "y:" + touchPoint.y + " " + TAG);
if (!isDebug && mario.bulluetNum > 0) {
Bullet bullet = new Bullet(world, new Vector2(touchPoint.x, touchPoint.y),
new Vector2(mario.spineData.skeleton.findBone("gunTip").getWorldX() + mario.spineData.skeleton.getX(),
mario.spineData.skeleton.findBone("gunTip").getWorldY() + mario.spineData.skeleton.getY()));
bullets.add(bullet);
mario.bulluetNum--;
} else if (isDebug) {
// cast single line
// raycast.startRaycast(mario.position, touchPoint);
// cast customized lines
raycast.startRayCast(mario.position);// 发射射线调试敌人是否可以被射击到

}
}

@Override
public void beginContact(Contact contact) {
// TODO Auto-generated method stub
beginBodyContact(contact);

}

@Override
public void endContact(Contact contact) {
// TODO Auto-generated method stub
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Body bodyA = fixtureA.getBody();
Body bodyB = fixtureB.getBody();
if (fixtureA.isSensor()) {
if (fixtureA.getUserData().toString().equalsIgnoreCase(Enemy.RIGHT_BOTTOM_SENSOR) && bodyA.getLinearVelocity().x > 0) {
Enemy e = (Enemy) bodyA.getUserData();
e.hitWall();
} else if (fixtureA.getUserData().toString().equalsIgnoreCase(Enemy.LEFT_BOTTOM_SENSOR) && bodyA.getLinearVelocity().x < 0) {
Enemy e = (Enemy) bodyA.getUserData();
e.hitWall();
}
} else if (fixtureB.isSensor()) {
if (fixtureB.getUserData().toString().equalsIgnoreCase(Enemy.RIGHT_BOTTOM_SENSOR) && bodyB.getLinearVelocity().x > 0) {
Enemy e = (Enemy) bodyB.getUserData();
e.hitWall();
} else if (fixtureB.getUserData().toString().equalsIgnoreCase(Enemy.LEFT_BOTTOM_SENSOR) && bodyB.getLinearVelocity().x < 0) {
Enemy e = (Enemy) bodyB.getUserData();
e.hitWall();
}
}
// System.out.println("end contact A:" + fixtureA.getUserData() +
// " end contact B:" + fixtureB.getUserData());
}

@Override
public void postSolve(Contact contact, ContactImpulse arg1) {
// TODO Auto-generated method stub

}

@Override
public void preSolve(Contact contact, Manifold arg1) {
// TODO Auto-generated method stub
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Body bodyA = fixtureA.getBody();
Body bodyB = fixtureB.getBody();
if (bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Bullet.TAG)
&& bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG)) {
contact.setEnabled(false);
Enemy enemy = (Enemy) bodyB.getUserData();
enemy.setKilled(true);

} else if (bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Bullet.TAG)
&& bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG)) {
contact.setEnabled(false);
Enemy enemy = (Enemy) bodyA.getUserData();
enemy.setKilled(true);
}

}

private void beginBodyContact(Contact contact) {
Fixture fixtureA = contact.getFixtureA();
Fixture fixtureB = contact.getFixtureB();
Body bodyA = fixtureA.getBody();
Body bodyB = fixtureB.getBody();
// check the collisions of bullet and the wall
if (bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Bullet.TAG)
&& !bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG)) {
Bullet bullet = (Bullet) bodyA.getUserData();
AudioManager.instance.play(Assets.instance.sound.blltHitW
4000
all);
bullet.bounceTimes++;
if (bullet.bounceTimes > Bullet.BOUNCE_TIMES || bullet.bounceTimes == Bullet.BOUNCE_TIMES) {
bullet.isDead = true;
}
} else if (bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Bullet.TAG)
&& !bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG)) {
Bullet bullet = (Bullet) bodyB.getUserData();
AudioManager.instance.play(Assets.instance.sound.blltHitWall);
bullet.bounceTimes++;
if (bullet.bounceTimes > Bullet.BOUNCE_TIMES || bullet.bounceTimes == Bullet.BOUNCE_TIMES) {
bullet.isDead = true;
}
// System.out.println("bullet bounce times:" + bullet.bounceTimes);
// check the collisions of dynamic zombies
} else if (bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG) && !fixtureA.isSensor()
&& !bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Bullet.TAG)) {
Enemy e = (Enemy) bodyA.getUserData();
e.hitWall();
} else if (bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG) && !fixtureB.isSensor()
&& !bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Bullet.TAG)) {
Enemy e = (Enemy) bodyB.getUserData();
e.hitWall();
// check the collisions of the lzombies and dynamic platforms
} else if (bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(DynamicPlatform.TAG)
&& bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG)) {
Enemy e = (Enemy) bodyB.getUserData();
e.hitDynamicPlatform((DynamicPlatform) bodyA.getUserData());

} else if (bodyB.getUserData().getClass().getName().toString().equalsIgnoreCase(DynamicPlatform.TAG)
&& bodyA.getUserData().getClass().getName().toString().equalsIgnoreCase(Enemy.TAG)) {
Enemy e = (Enemy) bodyA.getUserData();
e.hitDynamicPlatform((DynamicPlatform) bodyB.getUserData());
}

}

@Override
public void dispose() {
// TODO Auto-generated method stub
world.dispose();
gameMap.dispose();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息