ee9d6d4d9d68c83542c76877831bfc851259e784
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
.glutils
.ShapeRenderer
;
7 import com
.badlogic
.gdx
.math
.MathUtils
;
8 import com
.badlogic
.gdx
.math
.Vector2
;
9 import ru
.deadsoftware
.cavecraft
.CaveGame
;
10 import ru
.deadsoftware
.cavecraft
.GameScreen
;
11 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
12 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
13 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
14 import ru
.deadsoftware
.cavecraft
.misc
.Renderer
;
16 public class GameRenderer
extends Renderer
{
18 private GameProc gameProc
;
20 public GameRenderer(GameProc gameProc
,float width
, float heigth
) {
22 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
23 this.gameProc
= gameProc
;
26 private void drawWorldBackground() {
27 int minX
= (int) (camera
.position
.x
/16)-1;
28 int minY
= (int) (camera
.position
.y
/16)-1;
29 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
30 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
32 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
33 for (int y
=minY
; y
<maxY
; y
++) {
34 for (int x
=minX
; x
<maxX
; x
++) {
35 if ((gameProc
.world
.getForeMap(x
,y
)==0 || Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).transparent
)
36 && gameProc
.world
.getBackMap(x
,y
)>0) {
38 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture()],
39 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
40 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
41 Assets
.shade
.draw(spriteBatch
);
43 if (gameProc
.world
.getForeMap(x
,y
)>0 && Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
45 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
46 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
52 private void drawWorldForeground(){
53 int minX
= (int) (camera
.position
.x
/16)-1;
54 int minY
= (int) (camera
.position
.y
/16)-1;
55 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
56 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
58 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
59 for (int y
=minY
; y
<maxY
; y
++) {
60 for (int x
=minX
; x
<maxX
; x
++) {
61 if (gameProc
.world
.getForeMap(x
,y
)>0 && !Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
63 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
64 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
70 private void drawMob(Mob mob
) {
72 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
74 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
76 mob
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
79 private void drawPlayer(Player pl
) {
80 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSprite
[0][2].getRotation()!=0) {
81 Assets
.playerSprite
[0][2].rotate(Player
.ANIM_SPEED
);
82 Assets
.playerSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
83 Assets
.playerSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
84 Assets
.playerSprite
[1][3].rotate(Player
.ANIM_SPEED
);
86 Assets
.playerSprite
[0][2].setRotation(0);
87 Assets
.playerSprite
[1][2].setRotation(0);
88 Assets
.playerSprite
[0][3].setRotation(0);
89 Assets
.playerSprite
[1][3].setRotation(0);
91 if (Assets
.playerSprite
[0][2].getRotation()>=60 || Assets
.playerSprite
[0][2].getRotation()<=-60)
92 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
95 Assets
.playerSprite
[1][2].setPosition(
96 pl
.position
.x
- camera
.position
.x
- 6,
97 pl
.position
.y
- camera
.position
.y
);
98 Assets
.playerSprite
[1][2].draw(spriteBatch
);
100 Assets
.playerSprite
[1][3].setPosition(
101 pl
.position
.x
- camera
.position
.x
- 6,
102 pl
.position
.y
- camera
.position
.y
+ 10);
103 Assets
.playerSprite
[1][3].draw(spriteBatch
);
105 Assets
.playerSprite
[0][3].setPosition(
106 pl
.position
.x
- camera
.position
.x
- 6,
107 pl
.position
.y
- camera
.position
.y
+ 10);
108 Assets
.playerSprite
[0][3].draw(spriteBatch
);
110 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
111 pl
.position
.x
- camera
.position
.x
- 2,
112 pl
.position
.y
- camera
.position
.y
- 2);
114 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
115 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
117 if (pl
.inventory
[gameProc
.invSlot
]>0)
118 switch (Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getType()) {
120 Assets
.blockTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].setPosition(
121 pl
.position
.x
- camera
.position
.x
- 8*MathUtils
.sin(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()),
122 pl
.position
.y
- camera
.position
.y
+ 6 + 8*MathUtils
.cos(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()));
123 Assets
.blockTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].draw(spriteBatch
);
126 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].flip((pl
.dir
==0), false);
127 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].setRotation(
128 -45+pl
.dir
*90+Assets
.playerSprite
[0][2].getRotation());
129 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].setPosition(
130 pl
.position
.x
- camera
.position
.x
-10+(12*pl
.dir
) - 8*MathUtils
.sin(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()),
131 pl
.position
.y
- camera
.position
.y
+ 2 + 8*MathUtils
.cos(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()));
132 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].draw(spriteBatch
);
133 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].flip((pl
.dir
==0), false);
137 Assets
.playerSprite
[0][2].setPosition(
138 pl
.position
.x
- camera
.position
.x
- 6,
139 pl
.position
.y
- camera
.position
.y
);
140 Assets
.playerSprite
[0][2].draw(spriteBatch
);
143 private void drawCreative() {
144 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
145 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
146 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
147 spriteBatch
.draw(Assets
.creativeScroll
, x
+156,
148 y
+18+(gameProc
.creativeScroll
*(72/gameProc
.maxCreativeScroll
)));
149 for (int i
=gameProc
.creativeScroll
*8; i
<gameProc
.creativeScroll
*8+40; i
++) {
150 if (i
>0 && i
<Items
.ITEMS
.size())
151 switch (Items
.ITEMS
.get(i
).getType()) {
153 spriteBatch
.draw(Assets
.blockTextures
[Items
.ITEMS
.get(i
).getTexture()],
154 x
+ 8 + ((i
- gameProc
.creativeScroll
* 8) % 8) * 18,
155 y
+ 18 + ((i
- gameProc
.creativeScroll
* 8) / 8) * 18);
158 spriteBatch
.draw(Assets
.itemTextures
[Items
.ITEMS
.get(i
).getTexture()],
159 x
+ 8 + ((i
- gameProc
.creativeScroll
* 8) % 8) * 18,
160 y
+ 18 + ((i
- gameProc
.creativeScroll
* 8) / 8) * 18);
164 for (int i
=0; i
<9; i
++) {
165 if (gameProc
.player
.inventory
[i
]>0)
166 switch (Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getType()) {
168 spriteBatch
.draw(Assets
.blockTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
169 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
172 spriteBatch
.draw(Assets
.itemTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
173 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
179 private void drawGUI() {
180 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
181 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
182 gameProc
.ctrlMode
==1 ||
184 spriteBatch
.draw(Assets
.guiCur
,
185 gameProc
.cursorX
*16-camera
.position
.x
,
186 gameProc
.cursorY
*16-camera
.position
.y
);
187 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
188 for (int i
=0; i
<9; i
++) {
189 if (gameProc
.player
.inventory
[i
]>0) {
190 switch (Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getType()) {
192 spriteBatch
.draw(Assets
.blockTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
193 camera
.viewportWidth
/ 2 - Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
197 spriteBatch
.draw(Assets
.itemTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
198 camera
.viewportWidth
/ 2 - Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
204 spriteBatch
.draw(Assets
.invBarCur
,
205 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
209 private void drawTouchGui() {
210 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
211 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
212 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
213 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
214 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
215 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
216 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
217 if (gameProc
.ctrlMode
==1) {
218 Assets
.shade
.setPosition(83, camera
.viewportHeight
-21);
219 Assets
.shade
.draw(spriteBatch
);
223 private void drawGamePlay() {
224 drawWorldBackground();
225 drawPlayer(gameProc
.player
);
226 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
227 drawWorldForeground();
232 public void render() {
233 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
236 switch (CaveGame
.STATE
) {
240 case GAME_CREATIVE_INV
:
246 if (CaveGame
.TOUCH
) drawTouchGui();
248 if (GameScreen
.SHOW_DEBUG
) {
249 drawString("FPS: "+GameScreen
.FPS
,0, 0);
250 drawString("X: "+(int)(gameProc
.player
.position
.x
/16),0, 10);
251 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16),0, 20);
252 drawString("Mobs: "+gameProc
.mobs
.size(), 0, 30);
253 drawString("Block: "+Items
.BLOCKS
.getKeyAt(gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)), 0, 40);