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
.MathUtils
;
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()));
33 camera
.setToOrtho(true, 320,
34 320 * ((float) GameScreen
.getHeight() / GameScreen
.getWidth()));
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
);
48 private void setFontColor(int r
, int g
, int b
) {
49 Assets
.minecraftFont
.setColor(r
/255f, g
/255f, b
/255f, 1f);
52 private void drawString(String str
, float x
, float y
) {
53 Assets
.minecraftFont
.draw(fontBatch
, str
, x
, y
);
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;
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) {
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) {
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
);
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;
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) {*/
97 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
98 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
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
.playerSkin
[0][2].getRotation()!=0) {
111 Assets
.playerSkin
[0][2].rotate(Mob
.ANIM_SPEED
);
112 Assets
.playerSkin
[1][2].rotate(-Mob
.ANIM_SPEED
);
113 Assets
.playerSkin
[0][3].rotate(-Mob
.ANIM_SPEED
);
114 Assets
.playerSkin
[1][3].rotate(Mob
.ANIM_SPEED
);
116 Assets
.playerSkin
[0][2].setRotation(0);
117 Assets
.playerSkin
[1][2].setRotation(0);
118 Assets
.playerSkin
[0][3].setRotation(0);
119 Assets
.playerSkin
[1][3].setRotation(0);
121 if (Assets
.playerSkin
[0][2].getRotation()>=60 || Assets
.playerSkin
[0][2].getRotation()<=-60)
122 Mob
.ANIM_SPEED
= -Mob
.ANIM_SPEED
;
125 Assets
.playerSkin
[1][2].setPosition(
126 pl
.position
.x
- camera
.position
.x
- 6,
127 pl
.position
.y
- camera
.position
.y
);
128 Assets
.playerSkin
[1][2].draw(spriteBatch
);
130 Assets
.playerSkin
[1][3].setPosition(
131 pl
.position
.x
- camera
.position
.x
- 6,
132 pl
.position
.y
- camera
.position
.y
+ 10);
133 Assets
.playerSkin
[1][3].draw(spriteBatch
);
135 Assets
.playerSkin
[0][3].setPosition(
136 pl
.position
.x
- camera
.position
.x
- 6,
137 pl
.position
.y
- camera
.position
.y
+ 10);
138 Assets
.playerSkin
[0][3].draw(spriteBatch
);
140 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][0],
141 pl
.position
.x
- camera
.position
.x
- 2,
142 pl
.position
.y
- camera
.position
.y
- 2);
144 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][1],
145 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
147 Assets
.playerSkin
[0][2].setPosition(
148 pl
.position
.x
- camera
.position
.x
- 6,
149 pl
.position
.y
- camera
.position
.y
);
150 Assets
.playerSkin
[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,
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,
184 spriteBatch
.draw(Assets
.invBarCur
,
185 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
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() {
201 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
202 drawPlayer(gameProc
.player
);
203 drawWorldForeground();
207 public void render() {
208 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
211 switch (CaveGame
.STATE
) {
215 case GAME_CREATIVE_INV
:
222 if (CaveGame
.TOUCH
) {
229 setFontColor(255,255,255);
230 drawString("CaveCraft "+CaveGame
.VERSION
, 0, 0);
231 drawString("FPS: "+GameScreen
.FPS
, 0, 20);
232 drawString("X: "+(int)(gameProc
.player
.position
.x
/16), 0, 40);
233 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16), 0, 60);