DEADSOFTWARE

Fix water in the sky
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameRenderer.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.graphics.Color;
5 import com.badlogic.gdx.graphics.GL20;
6 import com.badlogic.gdx.graphics.OrthographicCamera;
7 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
8 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
9 import com.badlogic.gdx.math.Vector2;
10 import ru.deadsoftware.cavecraft.Assets;
11 import ru.deadsoftware.cavecraft.CaveGame;
12 import ru.deadsoftware.cavecraft.Items;
13 import ru.deadsoftware.cavecraft.GameScreen;
14 import ru.deadsoftware.cavecraft.game.mobs.Mob;
15 import ru.deadsoftware.cavecraft.game.objects.Player;
17 public class GameRenderer {
19 private GameProc gameProc;
21 public OrthographicCamera camera, fontCam;
22 ShapeRenderer shapeRenderer;
23 SpriteBatch spriteBatch, fontBatch;
25 public GameRenderer(GameProc gameProc) {
26 Gdx.gl.glClearColor(0f,.6f,.6f,1f);
27 this.gameProc = gameProc;
28 camera = new OrthographicCamera();
29 if (!CaveGame.TOUCH) {
30 camera.setToOrtho(true, 480,
31 480 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
32 } else {
33 camera.setToOrtho(true, 320,
34 320 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
35 }
36 shapeRenderer = new ShapeRenderer();
37 shapeRenderer.setProjectionMatrix(camera.combined);
38 shapeRenderer.setAutoShapeType(true);
39 spriteBatch = new SpriteBatch();
40 spriteBatch.setProjectionMatrix(camera.combined);
42 fontCam = new OrthographicCamera();
43 fontCam.setToOrtho(true, GameScreen.getWidth(), GameScreen.getHeight());
44 fontBatch = new SpriteBatch();
45 fontBatch.setProjectionMatrix(fontCam.combined);
46 }
48 private void setFontColor(int r, int g, int b) {
49 Assets.minecraftFont.setColor(r/255f, g/255f, b/255f, 1f);
50 }
52 private void drawString(String str, float x, float y) {
53 Assets.minecraftFont.draw(fontBatch, str, x, y);
54 }
56 private void drawWorld() {
57 int minX = (int) (camera.position.x/16);
58 int minY = (int) (camera.position.y/16);
59 int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1;
60 int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1;
61 if (minX<0) minX=0;
62 if (minY<0) minY=0;
63 if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
64 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
65 for (int y=minY; y<maxY; y++) {
66 for (int x=minX; x<maxX; x++) {
67 if (gameProc.world.getForeMap(x,y)>0){/* &&
68 !Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {
69 spriteBatch.draw(
70 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
71 x * 16 - camera.position.x,y * 16 - camera.position.y);*/
72 } else if (gameProc.world.getBackMap(x,y)>0) {
73 spriteBatch.draw(
74 Items.BLOCKS.getValueAt(gameProc.world.getBackMap(x,y)).getTexture(),
75 x * 16 - camera.position.x,y * 16 - camera.position.y);
76 Assets.shade.setPosition(x * 16 - camera.position.x,y * 16 - camera.position.y);
77 Assets.shade.draw(spriteBatch);
78 }
79 }
80 }
81 }
83 private void drawWorldForeground(){
84 int minX = (int) (camera.position.x/16);
85 int minY = (int) (camera.position.y/16);
86 int maxX = (int) ((camera.position.x+camera.viewportWidth)/16)+1;
87 int maxY = (int) ((camera.position.y+camera.viewportHeight)/16)+1;
88 if (minX<0) minX=0;
89 if (minY<0) minY=0;
90 if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
91 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
92 for (int y=minY; y<maxY; y++) {
93 for (int x=minX; x<maxX; x++) {
94 if (gameProc.world.getForeMap(x,y)>0) { /*&&
95 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {*/
96 spriteBatch.draw(
97 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
98 x * 16 - camera.position.x,y * 16 - camera.position.y);
99 }
104 private void drawMob(Mob mob) {
105 mob.draw(spriteBatch,
106 mob.position.x-camera.position.x, mob.position.y-camera.position.y);
109 private void drawPlayer(Player pl) {
110 if (!pl.moveX.equals(Vector2.Zero) || Assets.playerSprite[0][2].getRotation()!=0) {
111 Assets.playerSprite[0][2].rotate(Player.ANIM_SPEED);
112 Assets.playerSprite[1][2].rotate(-Player.ANIM_SPEED);
113 Assets.playerSprite[0][3].rotate(-Player.ANIM_SPEED);
114 Assets.playerSprite[1][3].rotate(Player.ANIM_SPEED);
115 } else {
116 Assets.playerSprite[0][2].setRotation(0);
117 Assets.playerSprite[1][2].setRotation(0);
118 Assets.playerSprite[0][3].setRotation(0);
119 Assets.playerSprite[1][3].setRotation(0);
121 if (Assets.playerSprite[0][2].getRotation()>=60 || Assets.playerSprite[0][2].getRotation()<=-60)
122 Player.ANIM_SPEED = -Player.ANIM_SPEED;
124 //back hand
125 Assets.playerSprite[1][2].setPosition(
126 pl.position.x - camera.position.x - 6,
127 pl.position.y - camera.position.y);
128 Assets.playerSprite[1][2].draw(spriteBatch);
129 //back leg
130 Assets.playerSprite[1][3].setPosition(
131 pl.position.x - camera.position.x - 6,
132 pl.position.y - camera.position.y + 10);
133 Assets.playerSprite[1][3].draw(spriteBatch);
134 //front leg
135 Assets.playerSprite[0][3].setPosition(
136 pl.position.x - camera.position.x - 6,
137 pl.position.y - camera.position.y + 10);
138 Assets.playerSprite[0][3].draw(spriteBatch);
139 //head
140 spriteBatch.draw(Assets.playerSprite[pl.dir][0],
141 pl.position.x - camera.position.x - 2,
142 pl.position.y - camera.position.y - 2);
143 //body
144 spriteBatch.draw(Assets.playerSprite[pl.dir][1],
145 pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8);
146 //front hand
147 Assets.playerSprite[0][2].setPosition(
148 pl.position.x - camera.position.x - 6,
149 pl.position.y - camera.position.y);
150 Assets.playerSprite[0][2].draw(spriteBatch);
153 private void drawCreative() {
154 float x = camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2;
155 float y = camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2;
156 spriteBatch.draw(Assets.creativeInv, x, y);
157 spriteBatch.draw(Assets.creativeScroll, x+156, y+18);
158 for (int i=1; i<Items.BLOCKS.size; i++) {
159 spriteBatch.draw(Items.BLOCKS.getValueAt(i).getTexture(),x+8+(i%8)*18,
160 y+18+(i/8)*18);
162 for (int i=0; i<9; i++) {
163 if (gameProc.player.inventory[i]>0)
164 spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
165 x+8+i*18, y+Assets.creativeInv.getRegionHeight()-24);
169 private void drawGUI() {
170 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY)>0 ||
171 gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY)>0 ||
172 gameProc.ctrlMode==1)
173 spriteBatch.draw(Assets.guiCur,
174 gameProc.cursorX*16-camera.position.x,
175 gameProc.cursorY*16-camera.position.y);
176 spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, 0);
177 for (int i=0; i<9; i++) {
178 if (gameProc.player.inventory[i]>0) {
179 spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
180 camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20,
181 3);
184 spriteBatch.draw(Assets.invBarCur,
185 camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot,
186 -1);
189 private void drawTouchGui() {
190 spriteBatch.draw(Assets.touchArrows[0],26,camera.viewportHeight-52);
191 spriteBatch.draw(Assets.touchArrows[1],0,camera.viewportHeight-26);
192 spriteBatch.draw(Assets.touchArrows[2],26,camera.viewportHeight-26);
193 spriteBatch.draw(Assets.touchArrows[3],52,camera.viewportHeight-26);
194 spriteBatch.draw(Assets.touchLMB, camera.viewportWidth-52, camera.viewportHeight-26);
195 spriteBatch.draw(Assets.touchRMB, camera.viewportWidth-26, camera.viewportHeight-26);
196 spriteBatch.draw(Assets.touchToggleMode, 78, camera.viewportHeight-26);
199 private void drawGamePlay() {
200 drawWorld();
201 Mob.animateMobs();
202 for (Mob mob : gameProc.mobs) drawMob(mob);
203 drawPlayer(gameProc.player);
204 drawWorldForeground();
205 drawGUI();
208 public void render() {
209 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
211 spriteBatch.begin();
212 switch (CaveGame.STATE) {
213 case GAME_PLAY:
214 drawGamePlay();
215 break;
216 case GAME_CREATIVE_INV:
217 drawGamePlay();
218 drawCreative();
219 break;
221 spriteBatch.end();
223 if (CaveGame.TOUCH) {
224 spriteBatch.begin();
225 drawTouchGui();
226 spriteBatch.end();
229 fontBatch.begin();
230 setFontColor(255,255,255);
231 drawString("CaveCraft "+CaveGame.VERSION, 0, 0);
232 drawString("FPS: "+GameScreen.FPS, 0, 20);
233 drawString("X: "+(int)(gameProc.player.position.x/16), 0, 40);
234 drawString("Y: "+(int)(gameProc.player.position.y/16), 0, 60);
235 drawString("Seed: "+WorldGen.getSeed(), 0, 80);
236 drawString("Mobs: "+gameProc.mobs.size, 0, 100);
237 fontBatch.end();