libgdx Tiled 实例分析(下)

来源:互联网 发布:php数组到字符串转换 编辑:程序博客网 时间:2024/06/06 00:45
我们来看下最后一个接口:
这个函数用来完成玛丽的移动。


里面使用
isTouched(0.75f, 1)
来完成判断是否触摸屏幕此区域,如果是,则koala.velocity.y += Koala.JUMP_VELOCITY; 这个koala.velocity是需要在玛丽身上添加的一个增量。
// apply gravity if we are falling
koala.velocity.add(0, GRAVITY);
增加一个重力加速度。
然后是一堆计算位置,方向计算。
然后使用getTiles(startX, startY, endX, endY, tiles); 获取到对应位置的地图,
判断是否碰撞上墙,如果是
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get(1);
layer.setCell((int)tile.x, (int)tile.y, null);
将对应的墙移除掉,不渲染了。
算出增量,移动玛丽:

koala.position.add(koala.velocity);
还原回去增量值
koala.velocity.scl(1 / deltaTime);



总结:
简单来说,地图文件只是保存了一些数据,代码将它加载起来,然后我们使用TiledMap提供的接口操作,获取地图文件数据,根据人物运动进行检测,移动人物和地图即可。
地图文件被加载,我们使用接口获取一部分地图数据,进行渲染和人物碰撞检测即可,这就是地图的作用。(将地图数据脱离代码)。
如果我们不使用地图,我们也是自己构造结构描述地图状态,里面的元素,以及位置关系,和人物的交互,进行创建怪物或者云朵,而地图TiledMap便是帮你维护这个数据的。
这就是地图TiledMap的作用:帮我们维护一张地图,我们拿出一部分进行描画,判断等等即可。
源码为:






private void updateKoala (float deltaTime) {
if (deltaTime == 0) return;
koala.stateTime += deltaTime;


// check input and apply to velocity & state
if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.75f, 1)) && koala.grounded) {
koala.velocity.y += Koala.JUMP_VELOCITY;
koala.state = Koala.State.Jumping;
koala.grounded = false;
}


if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
koala.velocity.x = -Koala.MAX_VELOCITY;
if (koala.grounded) koala.state = Koala.State.Walking;
koala.facesRight = false;
}


if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
koala.velocity.x = Koala.MAX_VELOCITY;
if (koala.grounded) koala.state = Koala.State.Walking;
koala.facesRight = true;
}


// apply gravity if we are falling
koala.velocity.add(0, GRAVITY);


// clamp the velocity to the maximum, x-axis only
if (Math.abs(koala.velocity.x) > Koala.MAX_VELOCITY) {
koala.velocity.x = Math.signum(koala.velocity.x) * Koala.MAX_VELOCITY;
}


// clamp the velocity to 0 if it's < 1, and set the state to standign
if (Math.abs(koala.velocity.x) < 1) {
koala.velocity.x = 0;
if (koala.grounded) koala.state = Koala.State.Standing;
}


// multiply by delta time so we know how far we go
// in this frame
koala.velocity.scl(deltaTime);


// perform collision detection & response, on each axis, separately
// if the koala is moving right, check the tiles to the right of it's
// right bounding box edge, otherwise check the ones to the left
Rectangle koalaRect = rectPool.obtain();
koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
int startX, startY, endX, endY;
if (koala.velocity.x > 0) {
startX = endX = (int)(koala.position.x + Koala.WIDTH + koala.velocity.x);
} else {
startX = endX = (int)(koala.position.x + koala.velocity.x);
}
startY = (int)(koala.position.y);
endY = (int)(koala.position.y + Koala.HEIGHT);
getTiles(startX, startY, endX, endY, tiles);
koalaRect.x += koala.velocity.x;
for (Rectangle tile : tiles) {
if (koalaRect.overlaps(tile)) {
koala.velocity.x = 0;
break;
}
}
koalaRect.x = koala.position.x;


// if the koala is moving upwards, check the tiles to the top of it's
// top bounding box edge, otherwise check the ones to the bottom
if (koala.velocity.y > 0) {
startY = endY = (int)(koala.position.y + Koala.HEIGHT + koala.velocity.y);
} else {
startY = endY = (int)(koala.position.y + koala.velocity.y);
}
startX = (int)(koala.position.x);
endX = (int)(koala.position.x + Koala.WIDTH);
getTiles(startX, startY, endX, endY, tiles);
koalaRect.y += koala.velocity.y;
for (Rectangle tile : tiles) {
if (koalaRect.overlaps(tile)) {
// we actually reset the koala y-position here
// so it is just below/above the tile we collided with
// this removes bouncing :)
if (koala.velocity.y > 0) {
koala.position.y = tile.y - Koala.HEIGHT;
// we hit a block jumping upwards, let's destroy it!
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get(1);
layer.setCell((int)tile.x, (int)tile.y, null);
} else {
koala.position.y = tile.y + tile.height;
// if we hit the ground, mark us as grounded so we can jump
koala.grounded = true;
}
koala.velocity.y = 0;
break;
}
}
rectPool.free(koalaRect);


// unscale the velocity by the inverse delta time and set
// the latest position
koala.position.add(koala.velocity);
koala.velocity.scl(1 / deltaTime);


// Apply damping to the velocity on the x-axis so we don't
// walk infinitely once a key was pressed
koala.velocity.x *= Koala.DAMPING;


}
0 0