1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.graphics
.GL20
;
5 import com
.badlogic
.gdx
.math
.Vector2
;
6 import ru
.deadsoftware
.cavecraft
.CaveGame
;
7 import ru
.deadsoftware
.cavecraft
.GameScreen
;
8 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
9 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
10 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
11 import ru
.deadsoftware
.cavecraft
.misc
.Renderer
;
13 public class GameRenderer
extends Renderer
{
15 private GameProc gameProc
;
17 public GameRenderer(GameProc gameProc
,float width
, float heigth
) {
19 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
20 this.gameProc
= gameProc
;
23 private void drawWorldBackground() {
24 int minX
= (int) (camera
.position
.x
/16)-1;
25 int minY
= (int) (camera
.position
.y
/16)-1;
26 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
27 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
29 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
30 for (int y
=minY
; y
<maxY
; y
++) {
31 for (int x
=minX
; x
<maxX
; x
++) {
32 if ((gameProc
.world
.getForeMap(x
,y
)==0 || Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).transparent
)
33 && gameProc
.world
.getBackMap(x
,y
)>0) {
35 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture()],
36 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
37 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
38 Assets
.shade
.draw(spriteBatch
);
40 if (gameProc
.world
.getForeMap(x
,y
)>0 && Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
42 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
43 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
49 private void drawWorldForeground(){
50 int minX
= (int) (camera
.position
.x
/16)-1;
51 int minY
= (int) (camera
.position
.y
/16)-1;
52 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
53 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
55 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
56 for (int y
=minY
; y
<maxY
; y
++) {
57 for (int x
=minX
; x
<maxX
; x
++) {
58 if (gameProc
.world
.getForeMap(x
,y
)>0 && !Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
60 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
61 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
67 private void drawMob(Mob mob
) {
69 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
71 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
73 mob
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
76 private void drawPlayer(Player pl
) {
77 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSprite
[0][2].getRotation()!=0) {
78 Assets
.playerSprite
[0][2].rotate(Player
.ANIM_SPEED
);
79 Assets
.playerSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
80 Assets
.playerSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
81 Assets
.playerSprite
[1][3].rotate(Player
.ANIM_SPEED
);
83 Assets
.playerSprite
[0][2].setRotation(0);
84 Assets
.playerSprite
[1][2].setRotation(0);
85 Assets
.playerSprite
[0][3].setRotation(0);
86 Assets
.playerSprite
[1][3].setRotation(0);
88 if (Assets
.playerSprite
[0][2].getRotation()>=60 || Assets
.playerSprite
[0][2].getRotation()<=-60)
89 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
92 Assets
.playerSprite
[1][2].setPosition(
93 pl
.position
.x
- camera
.position
.x
- 6,
94 pl
.position
.y
- camera
.position
.y
);
95 Assets
.playerSprite
[1][2].draw(spriteBatch
);
97 Assets
.playerSprite
[1][3].setPosition(
98 pl
.position
.x
- camera
.position
.x
- 6,
99 pl
.position
.y
- camera
.position
.y
+ 10);
100 Assets
.playerSprite
[1][3].draw(spriteBatch
);
102 Assets
.playerSprite
[0][3].setPosition(
103 pl
.position
.x
- camera
.position
.x
- 6,
104 pl
.position
.y
- camera
.position
.y
+ 10);
105 Assets
.playerSprite
[0][3].draw(spriteBatch
);
107 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
108 pl
.position
.x
- camera
.position
.x
- 2,
109 pl
.position
.y
- camera
.position
.y
- 2);
111 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
112 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
114 Assets
.playerSprite
[0][2].setPosition(
115 pl
.position
.x
- camera
.position
.x
- 6,
116 pl
.position
.y
- camera
.position
.y
);
117 Assets
.playerSprite
[0][2].draw(spriteBatch
);
120 private void drawCreative() {
121 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
122 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
123 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
124 spriteBatch
.draw(Assets
.creativeScroll
, x
+156,
125 y
+18+(gameProc
.creativeScroll
*(72/gameProc
.maxCreativeScroll
)));
126 for (int i
=gameProc
.creativeScroll
*8; i
<(gameProc
.creativeScroll
+1)*40; i
++) {
127 if (i
>0 && i
<Items
.BLOCKS
.size
)
128 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(i
).getTexture()],
129 x
+8+((i
-gameProc
.creativeScroll
*8)%8)*18,
130 y
+18+((i
-gameProc
.creativeScroll
*8)/8)*18);
132 for (int i
=0; i
<9; i
++) {
133 if (gameProc
.player
.inventory
[i
]>0)
134 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture()],
135 x
+8+i
*18, y
+Assets
.creativeInv
.getRegionHeight()-24);
139 private void drawGUI() {
140 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
141 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
142 gameProc
.ctrlMode
==1)
143 spriteBatch
.draw(Assets
.guiCur
,
144 gameProc
.cursorX
*16-camera
.position
.x
,
145 gameProc
.cursorY
*16-camera
.position
.y
);
146 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
147 for (int i
=0; i
<9; i
++) {
148 if (gameProc
.player
.inventory
[i
]>0) {
149 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture()],
150 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
154 spriteBatch
.draw(Assets
.invBarCur
,
155 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
159 private void drawTouchGui() {
160 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
161 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
162 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
163 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
164 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
165 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
166 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
167 if (gameProc
.ctrlMode
==1) {
168 Assets
.shade
.setPosition(83, camera
.viewportHeight
-21);
169 Assets
.shade
.draw(spriteBatch
);
173 private void drawGamePlay() {
174 drawWorldBackground();
176 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
177 drawPlayer(gameProc
.player
);
178 drawWorldForeground();
183 public void render() {
184 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
187 switch (CaveGame
.STATE
) {
191 case GAME_CREATIVE_INV
:
197 if (CaveGame
.TOUCH
) drawTouchGui();
199 if (GameScreen
.SHOW_DEBUG
) {
200 drawString("FPS: "+GameScreen
.FPS
,0, 0);
201 drawString("X: "+(int)(gameProc
.player
.position
.x
/16),0, 10);
202 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16),0, 20);
203 drawString("Mobs: "+gameProc
.mobs
.size(), 0, 30);