2db89c345912f1bca64c97a241b4b4798f9e6de6
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
.Assets
;
7 import ru
.deadsoftware
.cavecraft
.CaveGame
;
8 import ru
.deadsoftware
.cavecraft
.Items
;
9 import ru
.deadsoftware
.cavecraft
.Renderer
;
10 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
11 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
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){
33 } else if (gameProc
.world
.getBackMap(x
,y
)>0) {
35 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
);
44 private void drawWorldForeground(){
45 int minX
= (int) (camera
.position
.x
/16)-1;
46 int minY
= (int) (camera
.position
.y
/16)-1;
47 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
48 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
50 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
51 for (int y
=minY
; y
<maxY
; y
++) {
52 for (int x
=minX
; x
<maxX
; x
++) {
53 if (gameProc
.world
.getForeMap(x
,y
)>0) {
55 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
56 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
62 private void drawMob(Mob mob
) {
64 mob
.position
.x
-camera
.position
.x
-gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
66 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
68 mob
.position
.x
-camera
.position
.x
+gameProc
.world
.getWidth()*16, mob
.position
.y
-camera
.position
.y
);
71 private void drawPlayer(Player pl
) {
72 if (!pl
.moveX
.equals(Vector2
.Zero
) || Assets
.playerSprite
[0][2].getRotation()!=0) {
73 Assets
.playerSprite
[0][2].rotate(Player
.ANIM_SPEED
);
74 Assets
.playerSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
75 Assets
.playerSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
76 Assets
.playerSprite
[1][3].rotate(Player
.ANIM_SPEED
);
78 Assets
.playerSprite
[0][2].setRotation(0);
79 Assets
.playerSprite
[1][2].setRotation(0);
80 Assets
.playerSprite
[0][3].setRotation(0);
81 Assets
.playerSprite
[1][3].setRotation(0);
83 if (Assets
.playerSprite
[0][2].getRotation()>=60 || Assets
.playerSprite
[0][2].getRotation()<=-60)
84 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
87 Assets
.playerSprite
[1][2].setPosition(
88 pl
.position
.x
- camera
.position
.x
- 6,
89 pl
.position
.y
- camera
.position
.y
);
90 Assets
.playerSprite
[1][2].draw(spriteBatch
);
92 Assets
.playerSprite
[1][3].setPosition(
93 pl
.position
.x
- camera
.position
.x
- 6,
94 pl
.position
.y
- camera
.position
.y
+ 10);
95 Assets
.playerSprite
[1][3].draw(spriteBatch
);
97 Assets
.playerSprite
[0][3].setPosition(
98 pl
.position
.x
- camera
.position
.x
- 6,
99 pl
.position
.y
- camera
.position
.y
+ 10);
100 Assets
.playerSprite
[0][3].draw(spriteBatch
);
102 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][0],
103 pl
.position
.x
- camera
.position
.x
- 2,
104 pl
.position
.y
- camera
.position
.y
- 2);
106 spriteBatch
.draw(Assets
.playerSprite
[pl
.dir
][1],
107 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
109 Assets
.playerSprite
[0][2].setPosition(
110 pl
.position
.x
- camera
.position
.x
- 6,
111 pl
.position
.y
- camera
.position
.y
);
112 Assets
.playerSprite
[0][2].draw(spriteBatch
);
115 private void drawCreative() {
116 float x
= camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2;
117 float y
= camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2;
118 spriteBatch
.draw(Assets
.creativeInv
, x
, y
);
119 spriteBatch
.draw(Assets
.creativeScroll
, x
+156, y
+18);
120 for (int i
=1; i
<Items
.BLOCKS
.size
; i
++) {
121 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(i
).getTexture(),x
+8+(i
%8)*18,
124 for (int i
=0; i
<9; i
++) {
125 if (gameProc
.player
.inventory
[i
]>0)
126 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
127 x
+8+i
*18, y
+Assets
.creativeInv
.getRegionHeight()-24);
131 private void drawGUI() {
132 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
133 gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
)>0 ||
134 gameProc
.ctrlMode
==1)
135 spriteBatch
.draw(Assets
.guiCur
,
136 gameProc
.cursorX
*16-camera
.position
.x
,
137 gameProc
.cursorY
*16-camera
.position
.y
);
138 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
139 for (int i
=0; i
<9; i
++) {
140 if (gameProc
.player
.inventory
[i
]>0) {
141 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
142 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
146 spriteBatch
.draw(Assets
.invBarCur
,
147 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
151 private void drawTouchGui() {
152 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
153 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
154 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
155 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
156 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
157 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
158 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
161 private void drawGamePlay() {
162 drawWorldBackground();
164 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
165 drawPlayer(gameProc
.player
);
166 drawWorldForeground();
171 public void render() {
172 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
175 switch (CaveGame
.STATE
) {
179 case GAME_CREATIVE_INV
:
185 if (CaveGame
.TOUCH
) drawTouchGui();