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
.MathUtils
;
10 import com
.badlogic
.gdx
.math
.Vector2
;
11 import ru
.deadsoftware
.cavecraft
.Assets
;
12 import ru
.deadsoftware
.cavecraft
.CaveGame
;
13 import ru
.deadsoftware
.cavecraft
.Items
;
14 import ru
.deadsoftware
.cavecraft
.GameScreen
;
15 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
16 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
18 public class GameRenderer
{
20 private GameProc gameProc
;
22 public OrthographicCamera camera
, fontCam
;
23 ShapeRenderer shapeRenderer
;
24 SpriteBatch spriteBatch
, fontBatch
;
26 public GameRenderer(GameProc gameProc
) {
27 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
28 this.gameProc
= gameProc
;
29 camera
= new OrthographicCamera();
30 if (!CaveGame
.TOUCH
) {
31 camera
.setToOrtho(true, 480,
32 480 * ((float) GameScreen
.getHeight() / GameScreen
.getWidth()));
34 camera
.setToOrtho(true, 320,
35 320 * ((float) GameScreen
.getHeight() / GameScreen
.getWidth()));
37 shapeRenderer
= new ShapeRenderer();
38 shapeRenderer
.setProjectionMatrix(camera
.combined
);
39 shapeRenderer
.setAutoShapeType(true);
40 spriteBatch
= new SpriteBatch();
41 spriteBatch
.setProjectionMatrix(camera
.combined
);
43 fontCam
= new OrthographicCamera();
44 fontCam
.setToOrtho(true, GameScreen
.getWidth(), GameScreen
.getHeight());
45 fontBatch
= new SpriteBatch();
46 fontBatch
.setProjectionMatrix(fontCam
.combined
);
49 private void setFontColor(int r
, int g
, int b
) {
50 Assets
.minecraftFont
.setColor(r
/255f, g
/255f, b
/255f, 1f);
53 private void drawString(String str
, float x
, float y
) {
54 Assets
.minecraftFont
.draw(fontBatch
, str
, x
, y
);
57 private void drawWorld() {
58 int minX
= (int) (camera
.position
.x
/16);
59 int minY
= (int) (camera
.position
.y
/16);
60 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
61 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
64 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
65 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
66 for (int y
=minY
; y
<maxY
; y
++) {
67 for (int x
=minX
; x
<maxX
; x
++) {
68 if (gameProc
.world
.getForeMap(x
,y
)>0){/* &&
69 !Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {
71 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
72 x * 16 - camera.position.x,y * 16 - camera.position.y);*/
73 } else if (gameProc
.world
.getBackMap(x
,y
)>0) {
75 Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture(),
76 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
77 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
78 Assets
.shade
.draw(spriteBatch
);
84 private void drawWorldForeground(){
85 int minX
= (int) (camera
.position
.x
/16);
86 int minY
= (int) (camera
.position
.y
/16);
87 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
88 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
91 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
92 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
93 for (int y
=minY
; y
<maxY
; y
++) {
94 for (int x
=minX
; x
<maxX
; x
++) {
95 if (gameProc
.world
.getForeMap(x
,y
)>0) { /*&&
96 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {*/
98 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
99 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
105 private void drawMob(Mob mob
) {
106 mob
.draw(spriteBatch
,
107 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
110 private void drawPlayer(Player pl
) {
111 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSkin
[0][2].getRotation()!=0) {
112 Assets
.playerSkin
[0][2].rotate(Mob
.ANIM_SPEED
);
113 Assets
.playerSkin
[1][2].rotate(-Mob
.ANIM_SPEED
);
114 Assets
.playerSkin
[0][3].rotate(-Mob
.ANIM_SPEED
);
115 Assets
.playerSkin
[1][3].rotate(Mob
.ANIM_SPEED
);
117 Assets
.playerSkin
[0][2].setRotation(0);
118 Assets
.playerSkin
[1][2].setRotation(0);
119 Assets
.playerSkin
[0][3].setRotation(0);
120 Assets
.playerSkin
[1][3].setRotation(0);
122 if (Assets
.playerSkin
[0][2].getRotation()>=60 || Assets
.playerSkin
[0][2].getRotation()<=-60)
123 Mob
.ANIM_SPEED
= -Mob
.ANIM_SPEED
;
126 Assets
.playerSkin
[1][2].setPosition(
127 pl
.position
.x
- camera
.position
.x
- 6,
128 pl
.position
.y
- camera
.position
.y
);
129 Assets
.playerSkin
[1][2].draw(spriteBatch
);
131 Assets
.playerSkin
[1][3].setPosition(
132 pl
.position
.x
- camera
.position
.x
- 6,
133 pl
.position
.y
- camera
.position
.y
+ 10);
134 Assets
.playerSkin
[1][3].draw(spriteBatch
);
136 Assets
.playerSkin
[0][3].setPosition(
137 pl
.position
.x
- camera
.position
.x
- 6,
138 pl
.position
.y
- camera
.position
.y
+ 10);
139 Assets
.playerSkin
[0][3].draw(spriteBatch
);
141 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][0],
142 pl
.position
.x
- camera
.position
.x
- 2,
143 pl
.position
.y
- camera
.position
.y
- 2);
145 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][1],
146 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
148 Assets
.playerSkin
[0][2].setPosition(
149 pl
.position
.x
- camera
.position
.x
- 6,
150 pl
.position
.y
- camera
.position
.y
);
151 Assets
.playerSkin
[0][2].draw(spriteBatch
);
154 private void drawCreative() {
155 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
156 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
157 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
158 spriteBatch
.draw(Assets
.creativeScroll
, x
+156, y
+18);
159 for (int i
=1; i
<Items
.BLOCKS
.size
; i
++) {
160 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(i
).getTexture(),x
+8+(i
%8)*18,
163 for (int i
=0; i
<9; i
++) {
164 if (gameProc
.player
.inventory
[i
]>0)
165 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
166 x
+8+i
*18, y
+Assets
.creativeInv
.getRegionHeight()-24);
170 private void drawGUI() {
171 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
172 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
173 gameProc
.ctrlMode
==1)
174 spriteBatch
.draw(Assets
.guiCur
,
175 gameProc
.cursorX
*16-camera
.position
.x
,
176 gameProc
.cursorY
*16-camera
.position
.y
);
177 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
178 for (int i
=0; i
<9; i
++) {
179 if (gameProc
.player
.inventory
[i
]>0) {
180 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
181 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
185 spriteBatch
.draw(Assets
.invBarCur
,
186 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
190 private void drawTouchGui() {
191 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
192 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
193 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
194 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
195 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
196 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
197 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
200 private void drawGamePlay() {
202 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
203 drawPlayer(gameProc
.player
);
204 drawWorldForeground();
208 public void render() {
209 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
212 switch (CaveGame
.STATE
) {
216 case GAME_CREATIVE_INV
:
223 shapeRenderer
.begin(ShapeRenderer
.ShapeType
.Line
);
224 shapeRenderer
.setColor(Color
.ORANGE
);
225 shapeRenderer
.line(0-camera
.position
.x
, 128*16-camera
.position
.y
,
226 gameProc
.world
.getWidth()*16-camera
.position
.x
, 128*16-camera
.position
.y
);
229 if (CaveGame
.TOUCH
) {
236 setFontColor(255,255,255);
237 drawString("CaveCraft "+CaveGame
.VERSION
, 0, 0);
238 drawString("FPS: "+GameScreen
.FPS
, 0, 20);
239 drawString("X: "+(int)(gameProc
.player
.position
.x
/16), 0, 40);
240 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16), 0, 60);
241 drawString("Seed: "+WorldGen
.getSeed(), 0, 80);