DEADSOFTWARE

Ciclic worlds
[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, mob.position.y-camera.position.y);
97 }
99 private void drawPlayer(Player pl) {
100 if (!pl.moveX.equals(Vector2.Zero) || Assets.playerSprite[0][2].getRotation()!=0) {
101 Assets.playerSprite[0][2].rotate(Player.ANIM_SPEED);
102 Assets.playerSprite[1][2].rotate(-Player.ANIM_SPEED);
103 Assets.playerSprite[0][3].rotate(-Player.ANIM_SPEED);
104 Assets.playerSprite[1][3].rotate(Player.ANIM_SPEED);
105 } else {
106 Assets.playerSprite[0][2].setRotation(0);
107 Assets.playerSprite[1][2].setRotation(0);
108 Assets.playerSprite[0][3].setRotation(0);
109 Assets.playerSprite[1][3].setRotation(0);
111 if (Assets.playerSprite[0][2].getRotation()>=60 || Assets.playerSprite[0][2].getRotation()<=-60)
112 Player.ANIM_SPEED = -Player.ANIM_SPEED;
114 //back hand
115 Assets.playerSprite[1][2].setPosition(
116 pl.position.x - camera.position.x - 6,
117 pl.position.y - camera.position.y);
118 Assets.playerSprite[1][2].draw(spriteBatch);
119 //back leg
120 Assets.playerSprite[1][3].setPosition(
121 pl.position.x - camera.position.x - 6,
122 pl.position.y - camera.position.y + 10);
123 Assets.playerSprite[1][3].draw(spriteBatch);
124 //front leg
125 Assets.playerSprite[0][3].setPosition(
126 pl.position.x - camera.position.x - 6,
127 pl.position.y - camera.position.y + 10);
128 Assets.playerSprite[0][3].draw(spriteBatch);
129 //head
130 spriteBatch.draw(Assets.playerSprite[pl.dir][0],
131 pl.position.x - camera.position.x - 2,
132 pl.position.y - camera.position.y - 2);
133 //body
134 spriteBatch.draw(Assets.playerSprite[pl.dir][1],
135 pl.position.x - camera.position.x - 2, pl.position.y - camera.position.y + 8);
136 //front hand
137 Assets.playerSprite[0][2].setPosition(
138 pl.position.x - camera.position.x - 6,
139 pl.position.y - camera.position.y);
140 Assets.playerSprite[0][2].draw(spriteBatch);
143 private void drawCreative() {
144 float x = camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2;
145 float y = camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2;
146 spriteBatch.draw(Assets.creativeInv, x, y);
147 spriteBatch.draw(Assets.creativeScroll, x+156, y+18);
148 for (int i=1; i<Items.BLOCKS.size; i++) {
149 spriteBatch.draw(Items.BLOCKS.getValueAt(i).getTexture(),x+8+(i%8)*18,
150 y+18+(i/8)*18);
152 for (int i=0; i<9; i++) {
153 if (gameProc.player.inventory[i]>0)
154 spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
155 x+8+i*18, y+Assets.creativeInv.getRegionHeight()-24);
159 private void drawGUI() {
160 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY)>0 ||
161 gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY)>0 ||
162 gameProc.ctrlMode==1)
163 spriteBatch.draw(Assets.guiCur,
164 gameProc.cursorX*16-camera.position.x,
165 gameProc.cursorY*16-camera.position.y);
166 spriteBatch.draw(Assets.invBar, camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2, 0);
167 for (int i=0; i<9; i++) {
168 if (gameProc.player.inventory[i]>0) {
169 spriteBatch.draw(Items.BLOCKS.getValueAt(gameProc.player.inventory[i]).getTexture(),
170 camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2+3+i*20,
171 3);
174 spriteBatch.draw(Assets.invBarCur,
175 camera.viewportWidth/2 - Assets.invBar.getRegionWidth()/2 - 1 + 20*gameProc.invSlot,
176 -1);
179 private void drawTouchGui() {
180 spriteBatch.draw(Assets.touchArrows[0],26,camera.viewportHeight-52);
181 spriteBatch.draw(Assets.touchArrows[1],0,camera.viewportHeight-26);
182 spriteBatch.draw(Assets.touchArrows[2],26,camera.viewportHeight-26);
183 spriteBatch.draw(Assets.touchArrows[3],52,camera.viewportHeight-26);
184 spriteBatch.draw(Assets.touchLMB, camera.viewportWidth-52, camera.viewportHeight-26);
185 spriteBatch.draw(Assets.touchRMB, camera.viewportWidth-26, camera.viewportHeight-26);
186 spriteBatch.draw(Assets.touchToggleMode, 78, camera.viewportHeight-26);
189 private void drawGamePlay() {
190 drawWorldBackground();
191 Mob.animateMobs();
192 for (Mob mob : gameProc.mobs) drawMob(mob);
193 drawPlayer(gameProc.player);
194 drawWorldForeground();
195 drawGUI();
198 public void render() {
199 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
201 spriteBatch.begin();
202 switch (CaveGame.STATE) {
203 case GAME_PLAY:
204 drawGamePlay();
205 break;
206 case GAME_CREATIVE_INV:
207 drawGamePlay();
208 drawCreative();
209 break;
211 spriteBatch.end();
213 if (CaveGame.TOUCH) {
214 spriteBatch.begin();
215 drawTouchGui();
216 spriteBatch.end();
219 fontBatch.begin();
220 setFontColor(255,255,255);
221 drawString("CaveCraft "+CaveGame.VERSION, 0, 0);
222 drawString("FPS: "+GameScreen.FPS, 0, 20);
223 drawString("X: "+(int)(gameProc.player.position.x/16), 0, 40);
224 drawString("Y: "+(gameProc.world.getHeight()-(int)(gameProc.player.position.y/16)), 0, 60);
225 drawString("Seed: "+WorldGen.getSeed(), 0, 80);
226 drawString("Mobs: "+gameProc.mobs.size, 0, 100);
227 fontBatch.end();