Compare commits
3 Commits
451ca13566
...
a9bca04f1c
Author | SHA1 | Date | |
---|---|---|---|
a9bca04f1c | |||
84cdddc4b9 | |||
4d27a2671a |
@ -42,9 +42,7 @@ public class Brick extends Entity implements PhysicsBody, CollisionListener {
|
||||
@Override
|
||||
public void render(SpriteBatch sb, ShapeRenderer sr) {
|
||||
sb.setColor(type.getColor());
|
||||
float x = pos.x - BRICK_WIDTH/2;
|
||||
float y = pos.y - BRICK_HEIGHT/2;
|
||||
sb.draw(region, x, y,
|
||||
sb.draw(region, pos.x - BRICK_WIDTH/2, pos.y - BRICK_HEIGHT/2,
|
||||
BRICK_WIDTH/2, BRICK_HEIGHT/2,
|
||||
BRICK_WIDTH, BRICK_HEIGHT,
|
||||
1f, 1f,
|
||||
|
@ -9,7 +9,6 @@ import com.badlogic.gdx.physics.box2d.Body;
|
||||
import com.badlogic.gdx.physics.box2d.BodyDef;
|
||||
import com.badlogic.gdx.physics.box2d.CircleShape;
|
||||
import com.badlogic.gdx.physics.box2d.FixtureDef;
|
||||
import com.me.brickbuster.Utils;
|
||||
import com.me.brickbuster.entity.Ball;
|
||||
import com.me.brickbuster.entity.Entity;
|
||||
import com.me.brickbuster.entity.Paddle;
|
||||
@ -17,7 +16,6 @@ import com.me.brickbuster.physics.CollisionListener;
|
||||
import com.me.brickbuster.physics.EntityType;
|
||||
import com.me.brickbuster.physics.PhysicsBody;
|
||||
import com.me.brickbuster.state.PlayState;
|
||||
import net.dermetfan.utils.Pair;
|
||||
|
||||
public abstract class PowerUp extends Entity implements PhysicsBody, CollisionListener {
|
||||
|
||||
|
@ -2,17 +2,20 @@ package com.me.brickbuster.state;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.me.brickbuster.BrickBuster;
|
||||
import com.me.brickbuster.utils.CompositeIterator;
|
||||
import com.me.brickbuster.entity.*;
|
||||
import com.me.brickbuster.entity.powerup.PowerUp;
|
||||
import com.me.brickbuster.layout.*;
|
||||
import com.me.brickbuster.layout.BrickLayout;
|
||||
import com.me.brickbuster.layout.FileLevelLoader;
|
||||
import com.me.brickbuster.layout.Level;
|
||||
import com.me.brickbuster.layout.LevelLoader;
|
||||
import com.me.brickbuster.physics.Box2dContactListener;
|
||||
import com.me.brickbuster.physics.EntityType;
|
||||
import com.me.brickbuster.physics.PhysicsBody;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
@ -81,25 +84,20 @@ public class PlayState extends FieldState {
|
||||
long start = System.nanoTime();
|
||||
|
||||
game.sb.begin();
|
||||
for (Brick block : bricks) {
|
||||
block.render(game.sb, game.sr);
|
||||
}
|
||||
for (Ball ball : balls) {
|
||||
ball.render(game.sb, game.sr);
|
||||
Iterator<? extends Entity> it = CompositeIterator.concat(balls, bricks);
|
||||
while (it.hasNext()) {
|
||||
it.next().render(game.sb, game.sr);
|
||||
}
|
||||
game.sb.end();
|
||||
|
||||
for (PowerUp powerUp : powerUps) {
|
||||
powerUp.render(game.sb, game.sr);
|
||||
}
|
||||
for (Shield shield : shields) {
|
||||
shield.render(game.sb, game.sr);
|
||||
it = CompositeIterator.concat(powerUps, shields);
|
||||
while (it.hasNext()) {
|
||||
it.next().render(game.sb, game.sr);
|
||||
}
|
||||
paddle.render(game.sb, game.sr);
|
||||
|
||||
//debugRenderer.render(world, game.cam.combined.cpy().scl(PIXEL_PER_METER));
|
||||
long renderTime = System.nanoTime() - start;
|
||||
|
||||
game.fb.begin();
|
||||
game.font.setColor(Color.GRAY);
|
||||
game.font.draw(game.fb, String.format("FPS: %d Update: %.2f ms Render: %.2f ms",
|
||||
@ -110,47 +108,26 @@ public class PlayState extends FieldState {
|
||||
@Override
|
||||
public void update(float dt) {
|
||||
long start = System.nanoTime();
|
||||
|
||||
paddle.update(dt);
|
||||
|
||||
for (Iterator<Ball> it = balls.iterator(); it.hasNext();) {
|
||||
Ball ball = it.next();
|
||||
ball.update(dt);
|
||||
if (ball.isDeleted()) {
|
||||
Iterator<? extends Entity> it = CompositeIterator.concat(balls, powerUps, shields, bricks);
|
||||
while (it.hasNext()) {
|
||||
Entity ent = it.next();
|
||||
ent.update(dt);
|
||||
if (ent.isDeleted()) {
|
||||
it.remove();
|
||||
world.destroyBody(ball.getBody());
|
||||
if (ent instanceof PhysicsBody) {
|
||||
world.destroyBody(((PhysicsBody) ent).getBody());
|
||||
}
|
||||
ent.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
if (balls.size == 0) {
|
||||
ballReset();
|
||||
}
|
||||
|
||||
for (Iterator<PowerUp> it = powerUps.iterator(); it.hasNext();) {
|
||||
PowerUp powerUp = it.next();
|
||||
powerUp.update(dt);
|
||||
if(powerUp.isDeleted()) {
|
||||
it.remove();
|
||||
world.destroyBody(powerUp.getBody());
|
||||
}
|
||||
}
|
||||
|
||||
for (Iterator<Shield> it = shields.iterator(); it.hasNext();) {
|
||||
Shield shield = it.next();
|
||||
shield.update(dt);
|
||||
if(shield.isDeleted()) {
|
||||
it.remove();
|
||||
world.destroyBody(shield.getBody());
|
||||
}
|
||||
}
|
||||
|
||||
for (Iterator<Brick> it = bricks.iterator(); it.hasNext();) {
|
||||
Brick brick = it.next();
|
||||
brick.update(dt);
|
||||
if (brick.isDeleted()) {
|
||||
it.remove();
|
||||
brick.dispose();
|
||||
world.destroyBody(brick.getBody());
|
||||
}
|
||||
}
|
||||
if (bricks.size == 0) {
|
||||
currentLevel = levelLoader.getNextLevel();
|
||||
if (currentLevel == null) {
|
||||
@ -161,7 +138,6 @@ public class PlayState extends FieldState {
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
world.step(dt, 6, 2);
|
||||
updateTime = System.nanoTime() - start;
|
||||
}
|
||||
|
43
core/src/com/me/brickbuster/utils/CompositeIterator.java
Normal file
43
core/src/com/me/brickbuster/utils/CompositeIterator.java
Normal file
@ -0,0 +1,43 @@
|
||||
package com.me.brickbuster.utils;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class CompositeIterator<T> implements Iterator<T> {
|
||||
|
||||
private short index = 0;
|
||||
private Iterator<? extends T> iterator;
|
||||
private Iterable<? extends T>[] iterables;
|
||||
|
||||
private CompositeIterator(Iterable<? extends T>... iterables) {
|
||||
this.iterables = iterables;
|
||||
this.iterator = iterables[index].iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (iterator.hasNext()) {
|
||||
return true;
|
||||
} else {
|
||||
if (++index < iterables.length) {
|
||||
iterator = iterables[index].iterator();
|
||||
return hasNext();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
return iterator.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
iterator.remove();
|
||||
}
|
||||
|
||||
public static <E> CompositeIterator<E> concat(Iterable<? extends E>... iterables) {
|
||||
return new CompositeIterator<E>(iterables);
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.me.brickbuster;
|
||||
package com.me.brickbuster.utils;
|
||||
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
Loading…
Reference in New Issue
Block a user