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()));
32 camera
.setToOrtho(true, 320,
33 320 * ((float) GameScreen
.getHeight() / GameScreen
.getWidth()));
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
);
47 private void setFontColor(int r
, int g
, int b
) {
48 Assets
.minecraftFont
.setColor(r
/255f, g
/255f, b
/255f, 1f);
51 private void drawString(String str
, float x
, float y
) {
52 Assets
.minecraftFont
.draw(fontBatch
, str
, x
, y
);
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;
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) {
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
);
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;
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) {
87 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
88 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
94 private void drawMob(Mob mob
) {
96 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
98 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
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
);
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
;
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
);
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
);
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
);
134 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
135 pl
.position
.x
- camera
.position
.x
- 2,
136 pl
.position
.y
- camera
.position
.y
- 2);
138 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
139 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
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,
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,
178 spriteBatch
.draw(Assets
.invBarCur
,
179 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
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();
196 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
197 drawPlayer(gameProc
.player
);
198 drawWorldForeground();
202 public void render() {
203 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
206 switch (CaveGame
.STATE
) {
210 case GAME_CREATIVE_INV
:
217 if (CaveGame
.TOUCH
) {
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);