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
.game
.mobs
.Mob
;
8 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
9 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
10 import ru
.deadsoftware
.cavecraft
.misc
.Renderer
;
12 public class GameRenderer
extends Renderer
{
14 private GameProc gameProc
;
16 public GameRenderer(GameProc gameProc
,float width
, float heigth
) {
18 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
19 this.gameProc
= gameProc
;
22 private void drawWorldBackground() {
23 int minX
= (int) (camera
.position
.x
/16)-1;
24 int minY
= (int) (camera
.position
.y
/16)-1;
25 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
26 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
28 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
29 for (int y
=minY
; y
<maxY
; y
++) {
30 for (int x
=minX
; x
<maxX
; x
++) {
31 if ((gameProc
.world
.getForeMap(x
,y
)==0 || Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).transparent
)
32 && gameProc
.world
.getBackMap(x
,y
)>0) {
34 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture()],
35 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
36 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
37 Assets
.shade
.draw(spriteBatch
);
39 if (gameProc
.world
.getForeMap(x
,y
)>0 && Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
41 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
42 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
48 private void drawWorldForeground(){
49 int minX
= (int) (camera
.position
.x
/16)-1;
50 int minY
= (int) (camera
.position
.y
/16)-1;
51 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
52 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
54 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
55 for (int y
=minY
; y
<maxY
; y
++) {
56 for (int x
=minX
; x
<maxX
; x
++) {
57 if (gameProc
.world
.getForeMap(x
,y
)>0 && !Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
59 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
60 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
66 private void drawMob(Mob mob
) {
68 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
70 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
72 mob
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
75 private void drawPlayer(Player pl
) {
76 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSprite
[0][2].getRotation()!=0) {
77 Assets
.playerSprite
[0][2].rotate(Player
.ANIM_SPEED
);
78 Assets
.playerSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
79 Assets
.playerSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
80 Assets
.playerSprite
[1][3].rotate(Player
.ANIM_SPEED
);
82 Assets
.playerSprite
[0][2].setRotation(0);
83 Assets
.playerSprite
[1][2].setRotation(0);
84 Assets
.playerSprite
[0][3].setRotation(0);
85 Assets
.playerSprite
[1][3].setRotation(0);
87 if (Assets
.playerSprite
[0][2].getRotation()>=60 || Assets
.playerSprite
[0][2].getRotation()<=-60)
88 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
91 Assets
.playerSprite
[1][2].setPosition(
92 pl
.position
.x
- camera
.position
.x
- 6,
93 pl
.position
.y
- camera
.position
.y
);
94 Assets
.playerSprite
[1][2].draw(spriteBatch
);
96 Assets
.playerSprite
[1][3].setPosition(
97 pl
.position
.x
- camera
.position
.x
- 6,
98 pl
.position
.y
- camera
.position
.y
+ 10);
99 Assets
.playerSprite
[1][3].draw(spriteBatch
);
101 Assets
.playerSprite
[0][3].setPosition(
102 pl
.position
.x
- camera
.position
.x
- 6,
103 pl
.position
.y
- camera
.position
.y
+ 10);
104 Assets
.playerSprite
[0][3].draw(spriteBatch
);
106 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
107 pl
.position
.x
- camera
.position
.x
- 2,
108 pl
.position
.y
- camera
.position
.y
- 2);
110 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
111 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
113 Assets
.playerSprite
[0][2].setPosition(
114 pl
.position
.x
- camera
.position
.x
- 6,
115 pl
.position
.y
- camera
.position
.y
);
116 Assets
.playerSprite
[0][2].draw(spriteBatch
);
119 private void drawCreative() {
120 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
121 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
122 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
123 spriteBatch
.draw(Assets
.creativeScroll
, x
+156,
124 y
+18+(gameProc
.creativeScroll
*(72/gameProc
.maxCreativeScroll
)));
125 for (int i
=gameProc
.creativeScroll
*40; i
<(gameProc
.creativeScroll
+1)*40; i
++) {
126 if (i
>0 && i
<Items
.BLOCKS
.size
)
127 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(i
).getTexture()],
128 x
+8+((i
-gameProc
.creativeScroll
*40)%8)*18,
129 y
+18+((i
-gameProc
.creativeScroll
*40)/8)*18);
131 for (int i
=0; i
<9; i
++) {
132 if (gameProc
.player
.inventory
[i
]>0)
133 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture()],
134 x
+8+i
*18, y
+Assets
.creativeInv
.getRegionHeight()-24);
138 private void drawGUI() {
139 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
140 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
141 gameProc
.ctrlMode
==1)
142 spriteBatch
.draw(Assets
.guiCur
,
143 gameProc
.cursorX
*16-camera
.position
.x
,
144 gameProc
.cursorY
*16-camera
.position
.y
);
145 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
146 for (int i
=0; i
<9; i
++) {
147 if (gameProc
.player
.inventory
[i
]>0) {
148 spriteBatch
.draw(Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture()],
149 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
153 spriteBatch
.draw(Assets
.invBarCur
,
154 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
158 private void drawTouchGui() {
159 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
160 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
161 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
162 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
163 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
164 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
165 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
168 private void drawGamePlay() {
169 drawWorldBackground();
171 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
172 drawPlayer(gameProc
.player
);
173 drawWorldForeground();
178 public void render() {
179 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
182 switch (CaveGame
.STATE
) {
186 case GAME_CREATIVE_INV
:
192 if (CaveGame
.TOUCH
) drawTouchGui();