diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java b/core/src/ru/deadsoftware/cavecraft/game/GameRenderer.java
index 3c6d7b38b7aa26b3ee4ec750220c0c1862867b9c..f1e830d61586e77267c6ca88b8494f8e84aeff6d 100644 (file)
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.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.Vector3;
+import com.badlogic.gdx.math.MathUtils;
+import com.badlogic.gdx.math.Vector2;
import ru.deadsoftware.cavecraft.Assets;
import ru.deadsoftware.cavecraft.Assets;
+import ru.deadsoftware.cavecraft.CaveGame;
import ru.deadsoftware.cavecraft.Items;
import ru.deadsoftware.cavecraft.GameScreen;
import ru.deadsoftware.cavecraft.Items;
import ru.deadsoftware.cavecraft.GameScreen;
+import ru.deadsoftware.cavecraft.game.mobs.Mob;
import ru.deadsoftware.cavecraft.game.objects.Player;
public class GameRenderer {
private GameProc gameProc;
import ru.deadsoftware.cavecraft.game.objects.Player;
public class GameRenderer {
private GameProc gameProc;
- public Vector3 camTargetPos;
- public OrthographicCamera camera;
+ public OrthographicCamera camera, fontCam, touchCam;
ShapeRenderer shapeRenderer;
ShapeRenderer shapeRenderer;
- SpriteBatch spriteBatch;
+ SpriteBatch spriteBatch, fontBatch, touchBatch;
public GameRenderer(GameProc gameProc) {
Gdx.gl.glClearColor(0f,.6f,.6f,1f);
this.gameProc = gameProc;
camera = new OrthographicCamera();
public GameRenderer(GameProc gameProc) {
Gdx.gl.glClearColor(0f,.6f,.6f,1f);
this.gameProc = gameProc;
camera = new OrthographicCamera();
- camera.setToOrtho(true, 360,
- 360*((float)GameScreen.getHeight()/GameScreen.getWidth()));
- camera.position.x=0;
- camera.position.y=0;
- camTargetPos = camera.position.cpy();
+ if (!CaveGame.TOUCH) {
+ camera.setToOrtho(true, 480,
+ 480 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
+ } else {
+ camera.setToOrtho(true, 240,
+ 240 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
+ }
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.setAutoShapeType(true);
spriteBatch = new SpriteBatch();
spriteBatch.setProjectionMatrix(camera.combined);
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.setAutoShapeType(true);
spriteBatch = new SpriteBatch();
spriteBatch.setProjectionMatrix(camera.combined);
- }
- private void setColor(int r, int g, int b) {
- shapeRenderer.setColor(r/255f, g/255f, b/255f, 1f);
+ fontCam = new OrthographicCamera();
+ fontCam.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight());
+ fontBatch = new SpriteBatch();
+ fontBatch.setProjectionMatrix(fontCam.combined);
+ touchCam = new OrthographicCamera();
+ touchCam.setToOrtho(true, 240,
+ 240*((float)GameScreen.getHeight()/GameScreen.getWidth()));
+ touchBatch = new SpriteBatch();
+ touchBatch.setProjectionMatrix(touchCam.combined);
}
}
- private void fillRect(float x, float y, float w, float h) {
- shapeRenderer.set(ShapeRenderer.ShapeType.Filled);
- shapeRenderer.rect(x,y,w,h);
+ private void setFontColor(int r, int g, int b) {
+ Assets.minecraftFont.setColor(r/255f, g/255f, b/255f, 1f);
}
}
- private void drawRect(float x, float y, float w, float h) {
- shapeRenderer.set(ShapeRenderer.ShapeType.Line);
- shapeRenderer.rect(x,y,w,h);
+ private void drawString(String str, float x, float y) {
+ Assets.minecraftFont.draw(fontBatch, str, x, y);
}
private void drawWorld() {
}
private void drawWorld() {
if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
for (int y=minY; y<maxY; y++) {
for (int x=minX; x<maxX; x++) {
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) {
+ if (gameProc.world.getForeMap(x,y)>0){/* &&
+ !Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {
spriteBatch.draw(
Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
spriteBatch.draw(
Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
- x * 16 - camera.position.x,y * 16 - camera.position.y);
+ x * 16 - camera.position.x,y * 16 - camera.position.y);*/
} else if (gameProc.world.getBackMap(x,y)>0) {
spriteBatch.draw(
Items.BLOCKS.getValueAt(gameProc.world.getBackMap(x,y)).getTexture(),
} else if (gameProc.world.getBackMap(x,y)>0) {
spriteBatch.draw(
Items.BLOCKS.getValueAt(gameProc.world.getBackMap(x,y)).getTexture(),
}
}
}
}
+ private void drawWorldForeground(){
+ 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) { /*&&
+ Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {*/
+ spriteBatch.draw(
+ Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
+ x * 16 - camera.position.x,y * 16 - camera.position.y);
+ }
+ }
+ }
+ }
+
+ private void drawMob(Mob mob) {
+ mob.draw(spriteBatch,
+ mob.position.x-camera.position.x, mob.position.y-camera.position.y);
+ }
+
private void drawPlayer(Player pl) {
private void drawPlayer(Player pl) {
- Assets.playerSprite[pl.dir].setPosition(pl.position.x - camera.position.x,
+ if (!pl.moveX.equals(Vector2.Zero) || Assets.playerSkin[0][2].getRotation()!=0) {
+ Assets.playerSkin[0][2].rotate(Mob.ANIM_SPEED);
+ Assets.playerSkin[1][2].rotate(-Mob.ANIM_SPEED);
+ Assets.playerSkin[0][3].rotate(-Mob.ANIM_SPEED);
+ Assets.playerSkin[1][3].rotate(Mob.ANIM_SPEED);
+ } else {
+ Assets.playerSkin[0][2].setRotation(0);
+ Assets.playerSkin[1][2].setRotation(0);
+ Assets.playerSkin[0][3].setRotation(0);
+ Assets.playerSkin[1][3].setRotation(0);
+ }
+ if (Assets.playerSkin[0][2].getRotation()>=60 || Assets.playerSkin[0][2].getRotation()<=-60)
+ Mob.ANIM_SPEED = -Mob.ANIM_SPEED;
+
+ //back hand
+ Assets.playerSkin[1][2].setPosition(
+ pl.position.x - camera.position.x - 6,
+ pl.position.y - camera.position.y);
+ Assets.playerSkin[1][2].draw(spriteBatch);
+ //back leg
+ Assets.playerSkin[1][3].setPosition(
+ pl.position.x - camera.position.x - 6,
+ pl.position.y - camera.position.y + 10);
+ Assets.playerSkin[1][3].draw(spriteBatch);
+ //front leg
+ Assets.playerSkin[0][3].setPosition(
+ pl.position.x - camera.position.x - 6,
+ pl.position.y - camera.position.y + 10);
+ Assets.playerSkin[0][3].draw(spriteBatch);
+ //head
+ spriteBatch.draw(Assets.playerSkin[pl.dir][0],
+ pl.position.x - camera.position.x - 2,
+ pl.position.y - camera.position.y - 2);
+ //body
+ spriteBatch.draw(Assets.playerSkin[pl.dir][1],
+ pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8);
+ //front hand
+ Assets.playerSkin[0][2].setPosition(
+ pl.position.x - camera.position.x - 6,
pl.position.y - camera.position.y);
pl.position.y - camera.position.y);
- Assets.playerSprite[pl.dir].draw(spriteBatch);
+ Assets.playerSkin[0][2].draw(spriteBatch);
+ }
+
+ private void drawCreative() {
+ float x = camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2;
+ float y = camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2;
+ spriteBatch.draw(Assets.creativeInv, x, y);
+ spriteBatch.draw(Assets.creativeScroll, x+156, y+18);
+ for (int i=1; i<Items.BLOCKS.size; i++) {
+ spriteBatch.draw(Items.BLOCKS.getValueAt(i).getTexture(),x+8+(i%8)*18,
+ y+18+(i/8)*18);
+ }
+ for (int i=0; i<9; i++) {
+ if (gameProc.player.inventory[i]>0)
+ spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
+ x+8+i*18, y+184);
+ }
}
private void drawGUI() {
}
private void drawGUI() {
- spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2,
- camera.viewportHeight - Assets.invBar.getRegionHeight());
- for (int i=0; i<8; i++) {
+ if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY)>0 ||
+ gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY)>0 ||
+ gameProc.ctrlMode==1)
+ spriteBatch.draw(Assets.guiCur,
+ gameProc.cursorX*16-camera.position.x,
+ gameProc.cursorY*16-camera.position.y);
+ spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, 0);
+ for (int i=0; i<9; i++) {
if (gameProc.player.inventory[i]>0) {
spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20,
if (gameProc.player.inventory[i]>0) {
spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20,
- camera.viewportHeight-19);
+ 3);
}
}
}
}
- spriteBatch.draw(Assets.invCur,
+ spriteBatch.draw(Assets.invBarCur,
camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot,
camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot,
- camera.viewportHeight - Assets.invBar.getRegionHeight() - 2);
+ -1);
}
}
- public void render() {
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- spriteBatch.begin();
+ private void drawTouchGui() {
+ touchBatch.draw(Assets.touchArrows[0],26,touchCam.viewportHeight-52);
+ touchBatch.draw(Assets.touchArrows[1],0,touchCam.viewportHeight-26);
+ touchBatch.draw(Assets.touchArrows[2],26,touchCam.viewportHeight-26);
+ touchBatch.draw(Assets.touchArrows[3],52,touchCam.viewportHeight-26);
+ touchBatch.draw(Assets.touchLMB, touchCam.viewportWidth-52, touchCam.viewportHeight-26);
+ touchBatch.draw(Assets.touchRMB, touchCam.viewportWidth-26, touchCam.viewportHeight-26);
+ touchBatch.draw(Assets.touchToggleMode, 78, touchCam.viewportHeight-26);
+ }
+
+ private void drawGamePlay() {
drawWorld();
drawWorld();
+ for (Mob mob : gameProc.mobs) drawMob(mob);
drawPlayer(gameProc.player);
drawPlayer(gameProc.player);
+ drawWorldForeground();
drawGUI();
drawGUI();
+ }
+
+ public void render() {
+ Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
+
+ spriteBatch.begin();
+ switch (CaveGame.STATE) {
+ case GAME_PLAY:
+ drawGamePlay();
+ break;
+ case GAME_CREATIVE_INV:
+ drawGamePlay();
+ drawCreative();
+ break;
+ }
spriteBatch.end();
spriteBatch.end();
- shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
- shapeRenderer.setColor(Color.ORANGE);
- drawRect(gameProc.cursorX*16-camera.position.x,
- gameProc.cursorY*16-camera.position.y,16,16);
- shapeRenderer.end();
+ if (CaveGame.TOUCH) {
+ touchBatch.begin();
+ drawTouchGui();
+ touchBatch.end();
+ }
+
+ fontBatch.begin();
+ setFontColor(255,255,255);
+ drawString("CaveCraft "+CaveGame.VERSION, 0, 0);
+ drawString("FPS: "+GameScreen.FPS, 0, 20);
+ drawString("X: "+(int)(gameProc.player.position.x/16), 0, 40);
+ drawString("Y: "+(int)(gameProc.player.position.y/16), 0, 60);
+ fontBatch.end();
}
}
}
}