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
.Vector2
;
8 import ru
.deadsoftware
.cavecraft
.CaveGame
;
9 import ru
.deadsoftware
.cavecraft
.GameScreen
;
10 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
11 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
12 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
13 import ru
.deadsoftware
.cavecraft
.misc
.Renderer
;
15 public class GameRenderer
extends Renderer
{
17 private GameProc gameProc
;
19 public GameRenderer(GameProc gameProc
,float width
, float heigth
) {
21 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
22 this.gameProc
= gameProc
;
25 private void drawWorldBackground() {
26 int minX
= (int) (camera
.position
.x
/16)-1;
27 int minY
= (int) (camera
.position
.y
/16)-1;
28 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
29 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
31 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
32 for (int y
=minY
; y
<maxY
; y
++) {
33 for (int x
=minX
; x
<maxX
; x
++) {
34 if ((gameProc
.world
.getForeMap(x
,y
)==0 || Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).transparent
)
35 && gameProc
.world
.getBackMap(x
,y
)>0) {
37 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture()],
38 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
39 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
40 Assets
.shade
.draw(spriteBatch
);
42 if (gameProc
.world
.getForeMap(x
,y
)>0 && Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
44 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
45 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
51 private void drawWorldForeground(){
52 int minX
= (int) (camera
.position
.x
/16)-1;
53 int minY
= (int) (camera
.position
.y
/16)-1;
54 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
55 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
57 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
58 for (int y
=minY
; y
<maxY
; y
++) {
59 for (int x
=minX
; x
<maxX
; x
++) {
60 if (gameProc
.world
.getForeMap(x
,y
)>0 && !Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
62 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
63 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
69 private void drawMob(Mob mob
) {
71 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
73 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
75 mob
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
78 private void drawPlayer(Player pl
) {
79 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSprite
[0][2].getRotation()!=0) {
80 Assets
.playerSprite
[0][2].rotate(Player
.ANIM_SPEED
);
81 Assets
.playerSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
82 Assets
.playerSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
83 Assets
.playerSprite
[1][3].rotate(Player
.ANIM_SPEED
);
85 Assets
.playerSprite
[0][2].setRotation(0);
86 Assets
.playerSprite
[1][2].setRotation(0);
87 Assets
.playerSprite
[0][3].setRotation(0);
88 Assets
.playerSprite
[1][3].setRotation(0);
90 if (Assets
.playerSprite
[0][2].getRotation()>=60 || Assets
.playerSprite
[0][2].getRotation()<=-60)
91 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
94 Assets
.playerSprite
[1][2].setPosition(
95 pl
.position
.x
- camera
.position
.x
- 6,
96 pl
.position
.y
- camera
.position
.y
);
97 Assets
.playerSprite
[1][2].draw(spriteBatch
);
99 Assets
.playerSprite
[1][3].setPosition(
100 pl
.position
.x
- camera
.position
.x
- 6,
101 pl
.position
.y
- camera
.position
.y
+ 10);
102 Assets
.playerSprite
[1][3].draw(spriteBatch
);
104 Assets
.playerSprite
[0][3].setPosition(
105 pl
.position
.x
- camera
.position
.x
- 6,
106 pl
.position
.y
- camera
.position
.y
+ 10);
107 Assets
.playerSprite
[0][3].draw(spriteBatch
);
109 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
110 pl
.position
.x
- camera
.position
.x
- 2,
111 pl
.position
.y
- camera
.position
.y
- 2);
113 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
114 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
116 Assets
.playerSprite
[0][2].setPosition(
117 pl
.position
.x
- camera
.position
.x
- 6,
118 pl
.position
.y
- camera
.position
.y
);
119 Assets
.playerSprite
[0][2].draw(spriteBatch
);
122 private void drawCreative() {
123 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
124 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
125 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
126 spriteBatch
.draw(Assets
.creativeScroll
, x
+156,
127 y
+18+(gameProc
.creativeScroll
*(72/gameProc
.maxCreativeScroll
)));
128 for (int i
=gameProc
.creativeScroll
*8; i
<gameProc
.creativeScroll
*8+40; i
++) {
129 if (i
>0 && i
<Items
.BLOCKS
.size
)
130 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(i
).getTexture()],
131 x
+8+((i
-gameProc
.creativeScroll
*8)%8)*18,
132 y
+18+((i
-gameProc
.creativeScroll
*8)/8)*18);
134 for (int i
=0; i
<9; i
++) {
135 if (gameProc
.player
.inventory
[i
]>0)
136 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture()],
137 x
+8+i
*18, y
+Assets
.creativeInv
.getRegionHeight()-24);
141 private void drawGUI() {
142 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
143 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
144 gameProc
.ctrlMode
==1)
145 spriteBatch
.draw(Assets
.guiCur
,
146 gameProc
.cursorX
*16-camera
.position
.x
,
147 gameProc
.cursorY
*16-camera
.position
.y
);
148 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
149 for (int i
=0; i
<9; i
++) {
150 if (gameProc
.player
.inventory
[i
]>0) {
151 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture()],
152 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
156 spriteBatch
.draw(Assets
.invBarCur
,
157 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
161 private void drawTouchGui() {
162 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
163 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
164 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
165 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
166 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
167 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
168 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
169 if (gameProc
.ctrlMode
==1) {
170 Assets
.shade
.setPosition(83, camera
.viewportHeight
-21);
171 Assets
.shade
.draw(spriteBatch
);
175 private void drawGamePlay() {
176 drawWorldBackground();
177 drawPlayer(gameProc
.player
);
178 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
179 drawWorldForeground();
184 public void render() {
185 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
188 switch (CaveGame
.STATE
) {
192 case GAME_CREATIVE_INV
:
198 if (CaveGame
.TOUCH
) drawTouchGui();
200 if (GameScreen
.SHOW_DEBUG
) {
201 drawString("FPS: "+GameScreen
.FPS
,0, 0);
202 drawString("X: "+(int)(gameProc
.player
.position
.x
/16),0, 10);
203 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16),0, 20);
204 drawString("Mobs: "+gameProc
.mobs
.size(), 0, 30);