diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
index c6dd02f7053c11493d05474c99245aea3ab96e00..b93de89b8d756f9e42470882954f262fa0a03e2d 100644 (file)
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
+import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.Color;
+import com.badlogic.gdx.math.Rectangle;
+import com.badlogic.gdx.math.Vector3;
+import ru.deadsoftware.cavecraft.Assets;
import ru.deadsoftware.cavecraft.GameScreen;
+import ru.deadsoftware.cavecraft.game.objects.Player;
public class GameRenderer {
private GameProc gameProc;
- OrthographicCamera camera;
+ public Vector3 camTargetPos;
+ public OrthographicCamera camera;
ShapeRenderer shapeRenderer;
+ SpriteBatch spriteBatch;
public GameRenderer(GameProc gameProc) {
- Gdx.gl.glClearColor(0f,.8f,.8f,1f);
+ Gdx.gl.glClearColor(0f,.6f,.6f,1f);
this.gameProc = gameProc;
camera = new OrthographicCamera();
- camera.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight());
+ camera.setToOrtho(true, 360,
+ 360*((float)GameScreen.getHeight()/GameScreen.getWidth()));
camera.position.x=0;
camera.position.y=0;
+ camTargetPos = camera.position.cpy();
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(camera.combined);
+ shapeRenderer.setAutoShapeType(true);
+ spriteBatch = new SpriteBatch();
+ spriteBatch.setProjectionMatrix(camera.combined);
}
- public void drawWorld() {
- shapeRenderer.setAutoShapeType(true);
- shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
- for (int y=0; y<gameProc.world.getHeight(); y++) {
- for (int x=0; x<gameProc.world.getWidth(); x++) {
- if (gameProc.world.getBackMap(x,y)>0) {
- shapeRenderer.setColor(Color.DARK_GRAY);
- shapeRenderer.rect(x*32-camera.position.x,
- y*32-camera.position.y,32,32);
- }
+ private void setColor(int r, int g, int b) {
+ shapeRenderer.setColor(r/255f, g/255f, b/255f, 1f);
+ }
+
+ private void fillRect(float x, float y, float w, float h) {
+ shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
+ shapeRenderer.rect(x,y,w,h);
+ }
+
+ private void drawRect(float x, float y, float w, float h) {
+ shapeRenderer.set(ShapeRenderer.ShapeType.Line);
+ shapeRenderer.rect(x,y,w,h);
+ }
+
+ private void drawWorld() {
+ int minX = (int) (camera.position.x/16);
+ int minY = (int) (camera.position.y/16);
+ int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1;
+ int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1;
+ if (minX<0) minX=0;
+ if (minY<0) minY=0;
+ if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
+ if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
+ for (int y=minY; y<maxY; y++) {
+ for (int x=minX; x<maxX; x++) {
if (gameProc.world.getForeMap(x,y)>0) {
- shapeRenderer.setColor(Color.GRAY);
- shapeRenderer.rect(x*32-camera.position.x,
- y*32-camera.position.y,32,32);
+ setColor(128,128,128);
+ fillRect(x*16-camera.position.x,
+ y*16-camera.position.y,16,16);
+ setColor(0,0,0);
+ drawRect(x*16-camera.position.x,
+ y*16-camera.position.y,16,16);
+ } else {
+ if (gameProc.world.getBackMap(x,y)>0) {
+ setColor(64,64,64);
+ fillRect(x*16-camera.position.x,
+ y*16-camera.position.y,16,16);
+ setColor(0,0,0);
+ drawRect(x*16-camera.position.x,
+ y*16-camera.position.y,16,16);
+ }
}
+
}
}
shapeRenderer.setColor(Color.ORANGE);
- shapeRenderer.set(ShapeRenderer.ShapeType.Line);
- shapeRenderer.rect(gameProc.cursorX*32-camera.position.x,
- gameProc.cursorY*32-camera.position.y,32,32);
- shapeRenderer.end();
+ drawRect(gameProc.cursorX*16-camera.position.x,
+ gameProc.cursorY*16-camera.position.y,16,16);
+ }
+
+ private void drawPlayer(Player pl) {
+ spriteBatch.begin();
+ Assets.playerSprite[pl.dir].setPosition(pl.position.x - camera.position.x,
+ pl.position.y - camera.position.y);
+ Assets.playerSprite[pl.dir].draw(spriteBatch);
+ spriteBatch.end();
}
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+ shapeRenderer.begin();
drawWorld();
+ shapeRenderer.end();
+ drawPlayer(gameProc.player);
}
}