DEADSOFTWARE

Update input handling (pretty messy)
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameRenderer.java
index cc3bcdee8c9f849e89f1d8184bb68b397c4660e8..682984a5611adc334464374b8aad5fa46223746f 100644 (file)
@@ -19,16 +19,18 @@ public class GameRenderer {
 
     private GameProc gameProc;
 
-    public OrthographicCamera camera, fontCam;
+    public boolean showCreative = false;
+
+    public OrthographicCamera camera, fontCam, touchCam;
     ShapeRenderer shapeRenderer;
-    SpriteBatch spriteBatch, fontBatch;
+    SpriteBatch spriteBatch, fontBatch, touchBatch;
 
     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.setToOrtho(true, 480,
+                480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
 
         shapeRenderer = new ShapeRenderer();
         shapeRenderer.setProjectionMatrix(camera.combined);
@@ -40,6 +42,11 @@ public class GameRenderer {
         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 setFontColor(int r, int g, int b) {
@@ -61,7 +68,8 @@ public class GameRenderer {
         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(),
                             x * 16 - camera.position.x,y * 16 - camera.position.y);
@@ -76,6 +84,27 @@ public class GameRenderer {
         }
     }
 
+    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);
@@ -119,9 +148,25 @@ public class GameRenderer {
         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() {
         spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, 0);
-        for (int i=0; i<8; i++) {
+        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,
@@ -132,20 +177,18 @@ public class GameRenderer {
                 camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot,
                 -1);
 
-        if (CaveGame.TOUCH) {
-            spriteBatch.draw(Assets.touchArrows[0],26,camera.viewportHeight-52);
-            spriteBatch.draw(Assets.touchArrows[1],0,camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchArrows[2],26,camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchArrows[3],52,camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchSpace, camera.viewportWidth/2-52, camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchLMB, camera.viewportWidth-52, camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchRMB, camera.viewportWidth-26, camera.viewportHeight-26);
-            spriteBatch.draw(Assets.touchToggleMode, 78, camera.viewportHeight-26);
-            if (gameProc.ctrlMode==1) {
-                Assets.shade.setPosition(83, camera.viewportHeight-21);
-                Assets.shade.draw(spriteBatch);
-            }
-        }
+        if (showCreative) drawCreative();
+    }
+
+    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.touchSpace, touchCam.viewportWidth/2-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);
     }
 
     public void render() {
@@ -155,9 +198,16 @@ public class GameRenderer {
         drawWorld();
         for (Mob mob : gameProc.mobs) drawMob(mob);
         drawPlayer(gameProc.player);
+        drawWorldForeground();
         drawGUI();
         spriteBatch.end();
 
+        if (CaveGame.TOUCH) {
+            touchBatch.begin();
+            drawTouchGui();
+            touchBatch.end();
+        }
+
         if (gameProc.ctrlMode==1) {
             shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
             shapeRenderer.setColor(Color.ORANGE);
@@ -175,7 +225,7 @@ public class GameRenderer {
         drawString("Y: "+(int)(gameProc.player.position.y/16), 0, 60);
         drawString("Block: "+
                 Items.BLOCKS.keys().toArray().get(gameProc.world.getForeMap(
-                        (int)(gameProc.player.position.x/16),
+                        (int)((gameProc.player.position.x+gameProc.player.texWidth/2)/16),
                         (int)(gameProc.player.position.y/16+2))),
                 0, 80);
         fontBatch.end();