DEADSOFTWARE

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