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
.Drop
;
13 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
14 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
15 import ru
.deadsoftware
.cavecraft
.misc
.Renderer
;
17 public class GameRenderer
extends Renderer
{
19 private GameProc gameProc
;
21 public GameRenderer(GameProc gameProc
,float width
, float heigth
) {
23 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
24 this.gameProc
= gameProc
;
27 private void drawWorldBackground() {
28 int minX
= (int) (camera
.position
.x
/16)-1;
29 int minY
= (int) (camera
.position
.y
/16)-1;
30 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
31 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
33 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
34 for (int y
=minY
; y
<maxY
; y
++) {
35 for (int x
=minX
; x
<maxX
; x
++) {
36 if ((gameProc
.world
.getForeMap(x
,y
)==0 || Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).transparent
)
37 && gameProc
.world
.getBackMap(x
,y
)>0) {
39 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture()],
40 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
41 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
42 Assets
.shade
.draw(spriteBatch
);
44 if (gameProc
.world
.getForeMap(x
,y
)>0 && Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
46 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
47 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
53 private void drawWorldForeground(){
54 int minX
= (int) (camera
.position
.x
/16)-1;
55 int minY
= (int) (camera
.position
.y
/16)-1;
56 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
57 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
59 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
60 for (int y
=minY
; y
<maxY
; y
++) {
61 for (int x
=minX
; x
<maxX
; x
++) {
62 if (gameProc
.world
.getForeMap(x
,y
)>0 && !Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).background
) {
64 Assets
.blockTextures
[Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture()],
65 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
71 private void drawMob(Mob mob
) {
73 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
75 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
77 mob
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
80 private void drawDrop(Drop drop
) {
81 switch (Items
.ITEMS
.get(drop
.getId()).getType()) {
83 Assets
.blockTextures
[Items
.ITEMS
.get(drop
.getId()).getTexture()].setPosition(drop
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, drop
.position
.y
-camera
.position
.y
);
84 Assets
.blockTextures
[Items
.ITEMS
.get(drop
.getId()).getTexture()].draw(spriteBatch
);
85 Assets
.blockTextures
[Items
.ITEMS
.get(drop
.getId()).getTexture()].setPosition(drop
.position
.x
-camera
.position
.x
, drop
.position
.y
-camera
.position
.y
);
86 Assets
.blockTextures
[Items
.ITEMS
.get(drop
.getId()).getTexture()].draw(spriteBatch
);
87 Assets
.blockTextures
[Items
.ITEMS
.get(drop
.getId()).getTexture()].setPosition(drop
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, drop
.position
.y
-camera
.position
.y
);
88 Assets
.blockTextures
[Items
.ITEMS
.get(drop
.getId()).getTexture()].draw(spriteBatch
);
92 private void drawPlayer(Player pl
) {
93 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSprite
[0][2].getRotation()!=0) {
94 Assets
.playerSprite
[0][2].rotate(Player
.ANIM_SPEED
);
95 Assets
.playerSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
96 Assets
.playerSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
97 Assets
.playerSprite
[1][3].rotate(Player
.ANIM_SPEED
);
99 Assets
.playerSprite
[0][2].setRotation(0);
100 Assets
.playerSprite
[1][2].setRotation(0);
101 Assets
.playerSprite
[0][3].setRotation(0);
102 Assets
.playerSprite
[1][3].setRotation(0);
104 if (Assets
.playerSprite
[0][2].getRotation()>=60 || Assets
.playerSprite
[0][2].getRotation()<=-60)
105 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
108 Assets
.playerSprite
[1][2].setPosition(
109 pl
.position
.x
- camera
.position
.x
- 6,
110 pl
.position
.y
- camera
.position
.y
);
111 Assets
.playerSprite
[1][2].draw(spriteBatch
);
113 Assets
.playerSprite
[1][3].setPosition(
114 pl
.position
.x
- camera
.position
.x
- 6,
115 pl
.position
.y
- camera
.position
.y
+ 10);
116 Assets
.playerSprite
[1][3].draw(spriteBatch
);
118 Assets
.playerSprite
[0][3].setPosition(
119 pl
.position
.x
- camera
.position
.x
- 6,
120 pl
.position
.y
- camera
.position
.y
+ 10);
121 Assets
.playerSprite
[0][3].draw(spriteBatch
);
123 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
124 pl
.position
.x
- camera
.position
.x
- 2,
125 pl
.position
.y
- camera
.position
.y
- 2);
127 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
128 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
130 if (pl
.inventory
[gameProc
.invSlot
]>0)
131 switch (Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getType()) {
133 Assets
.blockTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].setPosition(
134 pl
.position
.x
- camera
.position
.x
- 8*MathUtils
.sin(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()),
135 pl
.position
.y
- camera
.position
.y
+ 6 + 8*MathUtils
.cos(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()));
136 Assets
.blockTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].draw(spriteBatch
);
139 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].flip((pl
.dir
==0), false);
140 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].setRotation(
141 -45+pl
.dir
*90+Assets
.playerSprite
[0][2].getRotation());
142 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].setPosition(
143 pl
.position
.x
- camera
.position
.x
-10+(12*pl
.dir
) - 8*MathUtils
.sin(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()),
144 pl
.position
.y
- camera
.position
.y
+ 2 + 8*MathUtils
.cos(MathUtils
.degRad
*Assets
.playerSprite
[0][2].getRotation()));
145 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].draw(spriteBatch
);
146 Assets
.itemTextures
[Items
.ITEMS
.get(pl
.inventory
[gameProc
.invSlot
]).getTexture()].flip((pl
.dir
==0), false);
150 Assets
.playerSprite
[0][2].setPosition(
151 pl
.position
.x
- camera
.position
.x
- 6,
152 pl
.position
.y
- camera
.position
.y
);
153 Assets
.playerSprite
[0][2].draw(spriteBatch
);
156 private void drawCreative() {
157 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
158 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
159 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
160 spriteBatch
.draw(Assets
.creativeScroll
, x
+156,
161 y
+18+(gameProc
.creativeScroll
*(72/gameProc
.maxCreativeScroll
)));
162 for (int i
=gameProc
.creativeScroll
*8; i
<gameProc
.creativeScroll
*8+40; i
++) {
163 if (i
>0 && i
<Items
.ITEMS
.size())
164 switch (Items
.ITEMS
.get(i
).getType()) {
166 spriteBatch
.draw(Assets
.blockTextures
[Items
.ITEMS
.get(i
).getTexture()],
167 x
+ 8 + ((i
- gameProc
.creativeScroll
* 8) % 8) * 18,
168 y
+ 18 + ((i
- gameProc
.creativeScroll
* 8) / 8) * 18);
171 spriteBatch
.draw(Assets
.itemTextures
[Items
.ITEMS
.get(i
).getTexture()],
172 x
+ 8 + ((i
- gameProc
.creativeScroll
* 8) % 8) * 18,
173 y
+ 18 + ((i
- gameProc
.creativeScroll
* 8) / 8) * 18);
177 for (int i
=0; i
<9; i
++) {
178 if (gameProc
.player
.inventory
[i
]>0)
179 switch (Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getType()) {
181 spriteBatch
.draw(Assets
.blockTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
182 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
185 spriteBatch
.draw(Assets
.itemTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
186 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
192 private void drawGUI() {
193 if (gameProc
.blockDmg
> 0) {
194 spriteBatch
.draw(Assets
.wreck
[
195 10*gameProc
.blockDmg
/
196 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)).getHp()],
197 gameProc
.cursorX
*16-camera
.position
.x
,
198 gameProc
.cursorY
*16-camera
.position
.y
);
200 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
201 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
202 gameProc
.ctrlMode
==1 ||
204 spriteBatch
.draw(Assets
.guiCur
,
205 gameProc
.cursorX
*16-camera
.position
.x
,
206 gameProc
.cursorY
*16-camera
.position
.y
);
207 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
208 for (int i
=0; i
<9; i
++) {
209 if (gameProc
.player
.inventory
[i
]>0) {
210 switch (Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getType()) {
212 spriteBatch
.draw(Assets
.blockTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
213 camera
.viewportWidth
/ 2 - Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
217 spriteBatch
.draw(Assets
.itemTextures
[Items
.ITEMS
.get(gameProc
.player
.inventory
[i
]).getTexture()],
218 camera
.viewportWidth
/ 2 - Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
224 spriteBatch
.draw(Assets
.invBarCur
,
225 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
229 private void drawTouchGui() {
230 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
231 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
232 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
233 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
234 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
235 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
236 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
237 if (gameProc
.ctrlMode
==1) {
238 Assets
.shade
.setPosition(83, camera
.viewportHeight
-21);
239 Assets
.shade
.draw(spriteBatch
);
243 private void drawGamePlay() {
244 drawWorldBackground();
245 drawPlayer(gameProc
.player
);
246 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
247 for (Drop drop
: gameProc
.drops
) drawDrop(drop
);
248 drawWorldForeground();
253 public void render() {
254 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
257 switch (CaveGame
.STATE
) {
261 case GAME_CREATIVE_INV
:
267 if (CaveGame
.TOUCH
) drawTouchGui();
269 if (GameScreen
.SHOW_DEBUG
) {
270 drawString("FPS: "+GameScreen
.FPS
,0, 0);
271 drawString("X: "+(int)(gameProc
.player
.position
.x
/16),0, 10);
272 drawString("Y: "+(int)(gameProc
.player
.position
.y
/16),0, 20);
273 drawString("Mobs: "+gameProc
.mobs
.size(), 0, 30);
274 drawString("Drops: "+gameProc
.drops
.size(), 0, 40);
275 drawString("Block: "+Items
.BLOCKS
.getKeyAt(gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)), 0, 50);