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
, touchCam
;
22 ShapeRenderer shapeRenderer
;
23 SpriteBatch spriteBatch
, fontBatch
, touchBatch
;
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, 240,
34 240 * ((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
);
46 touchCam
= new OrthographicCamera();
47 touchCam
.setToOrtho(true, 240,
48 240*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
49 touchBatch
= new SpriteBatch();
50 touchBatch
.setProjectionMatrix(touchCam
.combined
);
53 private void setFontColor(int r
, int g
, int b
) {
54 Assets
.minecraftFont
.setColor(r
/255f, g
/255f, b
/255f, 1f);
57 private void drawString(String str
, float x
, float y
) {
58 Assets
.minecraftFont
.draw(fontBatch
, str
, x
, y
);
61 private void drawWorld() {
62 int minX
= (int) (camera
.position
.x
/16);
63 int minY
= (int) (camera
.position
.y
/16);
64 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
65 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
68 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
69 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
70 for (int y
=minY
; y
<maxY
; y
++) {
71 for (int x
=minX
; x
<maxX
; x
++) {
72 if (gameProc
.world
.getForeMap(x
,y
)>0){/* &&
73 !Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {
75 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).getTexture(),
76 x * 16 - camera.position.x,y * 16 - camera.position.y);*/
77 } else if (gameProc
.world
.getBackMap(x
,y
)>0) {
79 Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture(),
80 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
81 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
82 Assets
.shade
.draw(spriteBatch
);
88 private void drawWorldForeground(){
89 int minX
= (int) (camera
.position
.x
/16);
90 int minY
= (int) (camera
.position
.y
/16);
91 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
92 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
95 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
96 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
97 for (int y
=minY
; y
<maxY
; y
++) {
98 for (int x
=minX
; x
<maxX
; x
++) {
99 if (gameProc
.world
.getForeMap(x
,y
)>0) { /*&&
100 Items.BLOCKS.getValueAt(gameProc.world.getForeMap(x,y)).foreground) {*/
102 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
103 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
109 private void drawMob(Mob mob
) {
110 mob
.draw(spriteBatch
,
111 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
114 private void drawPlayer(Player pl
) {
115 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSkin
[0][2].getRotation()!=0) {
116 Assets
.playerSkin
[0][2].rotate(Mob
.ANIM_SPEED
);
117 Assets
.playerSkin
[1][2].rotate(-Mob
.ANIM_SPEED
);
118 Assets
.playerSkin
[0][3].rotate(-Mob
.ANIM_SPEED
);
119 Assets
.playerSkin
[1][3].rotate(Mob
.ANIM_SPEED
);
121 Assets
.playerSkin
[0][2].setRotation(0);
122 Assets
.playerSkin
[1][2].setRotation(0);
123 Assets
.playerSkin
[0][3].setRotation(0);
124 Assets
.playerSkin
[1][3].setRotation(0);
126 if (Assets
.playerSkin
[0][2].getRotation()>=60 || Assets
.playerSkin
[0][2].getRotation()<=-60)
127 Mob
.ANIM_SPEED
= -Mob
.ANIM_SPEED
;
130 Assets
.playerSkin
[1][2].setPosition(
131 pl
.position
.x
- camera
.position
.x
- 6,
132 pl
.position
.y
- camera
.position
.y
);
133 Assets
.playerSkin
[1][2].draw(spriteBatch
);
135 Assets
.playerSkin
[1][3].setPosition(
136 pl
.position
.x
- camera
.position
.x
- 6,
137 pl
.position
.y
- camera
.position
.y
+ 10);
138 Assets
.playerSkin
[1][3].draw(spriteBatch
);
140 Assets
.playerSkin
[0][3].setPosition(
141 pl
.position
.x
- camera
.position
.x
- 6,
142 pl
.position
.y
- camera
.position
.y
+ 10);
143 Assets
.playerSkin
[0][3].draw(spriteBatch
);
145 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][0],
146 pl
.position
.x
- camera
.position
.x
- 2,
147 pl
.position
.y
- camera
.position
.y
- 2);
149 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][1],
150 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
152 Assets
.playerSkin
[0][2].setPosition(
153 pl
.position
.x
- camera
.position
.x
- 6,
154 pl
.position
.y
- camera
.position
.y
);
155 Assets
.playerSkin
[0][2].draw(spriteBatch
);
158 private void drawCreative() {
159 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
160 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
161 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
162 spriteBatch
.draw(Assets
.creativeScroll
, x
+156, y
+18);
163 for (int i
=1; i
<Items
.BLOCKS
.size
; i
++) {
164 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(i
).getTexture(),x
+8+(i
%8)*18,
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(),
174 private void drawGUI() {
175 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
176 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
177 gameProc
.ctrlMode
==1)
178 spriteBatch
.draw(Assets
.guiCur
,
179 gameProc
.cursorX
*16-camera
.position
.x
,
180 gameProc
.cursorY
*16-camera
.position
.y
);
181 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
182 for (int i
=0; i
<9; i
++) {
183 if (gameProc
.player
.inventory
[i
]>0) {
184 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
185 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
189 spriteBatch
.draw(Assets
.invBarCur
,
190 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
194 private void drawTouchGui() {
195 touchBatch
.draw(Assets
.touchArrows
[0],26,touchCam
.viewportHeight
-52);
196 touchBatch
.draw(Assets
.touchArrows
[1],0,touchCam
.viewportHeight
-26);
197 touchBatch
.draw(Assets
.touchArrows
[2],26,touchCam
.viewportHeight
-26);
198 touchBatch
.draw(Assets
.touchArrows
[3],52,touchCam
.viewportHeight
-26);
199 touchBatch
.draw(Assets
.touchLMB
, touchCam
.viewportWidth
-52, touchCam
.viewportHeight
-26);
200 touchBatch
.draw(Assets
.touchRMB
, touchCam
.viewportWidth
-26, touchCam
.viewportHeight
-26);
201 touchBatch
.draw(Assets
.touchToggleMode
, 78, touchCam
.viewportHeight
-26);
204 private void drawGamePlay() {
206 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
207 drawPlayer(gameProc
.player
);
208 drawWorldForeground();
212 public void render() {
213 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
216 switch (CaveGame
.STATE
) {
220 case GAME_CREATIVE_INV
:
227 if (CaveGame
.TOUCH
) {
234 setFontColor(255,255,255);
235 drawString("CaveCraft "+CaveGame
.VERSION
, 0, 0);
236 drawString("FPS: "+GameScreen
.FPS
, 0, 20);
237 drawString("X: "+(int)(gameProc
.player
.position
.x
/16), 0, 40);
238 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16), 0, 60);