1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.graphics
.GL20
;
5 import com
.badlogic
.gdx
.graphics
.OrthographicCamera
;
6 import com
.badlogic
.gdx
.graphics
.g2d
.SpriteBatch
;
7 import com
.badlogic
.gdx
.graphics
.glutils
.ShapeRenderer
;
8 import com
.badlogic
.gdx
.graphics
.Color
;
9 import com
.badlogic
.gdx
.math
.Vector2
;
10 import com
.badlogic
.gdx
.math
.Vector3
;
11 import ru
.deadsoftware
.cavecraft
.Assets
;
12 import ru
.deadsoftware
.cavecraft
.CaveGame
;
13 import ru
.deadsoftware
.cavecraft
.Items
;
14 import ru
.deadsoftware
.cavecraft
.GameScreen
;
15 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
16 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
18 public class GameRenderer
{
20 private GameProc gameProc
;
22 public Vector3 camTargetPos
;
23 public OrthographicCamera camera
;
24 ShapeRenderer shapeRenderer
;
25 SpriteBatch spriteBatch
;
27 public GameRenderer(GameProc gameProc
) {
28 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
29 this.gameProc
= gameProc
;
30 camera
= new OrthographicCamera();
31 camera
.setToOrtho(true, 360,
32 360*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
35 camTargetPos
= camera
.position
.cpy();
36 shapeRenderer
= new ShapeRenderer();
37 shapeRenderer
.setProjectionMatrix(camera
.combined
);
38 shapeRenderer
.setAutoShapeType(true);
39 spriteBatch
= new SpriteBatch();
40 spriteBatch
.setProjectionMatrix(camera
.combined
);
43 private void drawWorld() {
44 int minX
= (int) (camera
.position
.x
/16);
45 int minY
= (int) (camera
.position
.y
/16);
46 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
47 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
50 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
51 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
52 for (int y
=minY
; y
<maxY
; y
++) {
53 for (int x
=minX
; x
<maxX
; x
++) {
54 if (gameProc
.world
.getForeMap(x
,y
)>0) {
56 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
57 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
58 } else if (gameProc
.world
.getBackMap(x
,y
)>0) {
60 Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture(),
61 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
62 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
63 Assets
.shade
.draw(spriteBatch
);
69 private void drawMob(Mob mob
) {
71 mob
.position
.x
-camera
.position
.x
, mob
.position
.y
-camera
.position
.y
);
74 private void drawPlayer(Player pl
) {
75 if (!pl
.moveX
.equals(Vector2
.Zero
)) {
76 Assets
.playerSkin
[0][2].rotate(Mob
.ANIM_SPEED
);
77 Assets
.playerSkin
[1][2].rotate(-Mob
.ANIM_SPEED
);
78 Assets
.playerSkin
[0][3].rotate(-Mob
.ANIM_SPEED
);
79 Assets
.playerSkin
[1][3].rotate(Mob
.ANIM_SPEED
);
81 Assets
.playerSkin
[0][2].setRotation(0);
82 Assets
.playerSkin
[1][2].setRotation(0);
83 Assets
.playerSkin
[0][3].setRotation(0);
84 Assets
.playerSkin
[1][3].setRotation(0);
86 if (Assets
.playerSkin
[0][2].getRotation()>=60 || Assets
.playerSkin
[0][2].getRotation()<=-60)
87 Mob
.ANIM_SPEED
= -Mob
.ANIM_SPEED
;
88 Assets
.playerSkin
[1][2].setPosition(
89 pl
.position
.x
- camera
.position
.x
- 6,
90 pl
.position
.y
- camera
.position
.y
);
91 Assets
.playerSkin
[1][2].draw(spriteBatch
);
92 Assets
.playerSkin
[1][3].setPosition(
93 pl
.position
.x
- camera
.position
.x
- 6,
94 pl
.position
.y
- camera
.position
.y
+ 10);
95 Assets
.playerSkin
[1][3].draw(spriteBatch
);
96 Assets
.playerSkin
[0][3].setPosition(
97 pl
.position
.x
- camera
.position
.x
- 6,
98 pl
.position
.y
- camera
.position
.y
+ 10);
99 Assets
.playerSkin
[0][3].draw(spriteBatch
);
101 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][0],
102 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
- 2);
103 spriteBatch
.draw(Assets
.playerSkin
[pl
.dir
][1],
104 pl
.position
.x
- camera
.position
.x
- 2, pl
.position
.y
- camera
.position
.y
+ 8);
106 Assets
.playerSkin
[0][2].setPosition(
107 pl
.position
.x
- camera
.position
.x
- 6,
108 pl
.position
.y
- camera
.position
.y
);
109 Assets
.playerSkin
[0][2].draw(spriteBatch
);
112 private void drawGUI() {
113 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2, 0);
114 for (int i
=0; i
<8; i
++) {
115 if (gameProc
.player
.inventory
[i
]>0) {
116 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
117 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
121 spriteBatch
.draw(Assets
.invCur
,
122 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
125 if (CaveGame
.TOUCH
) {
126 spriteBatch
.draw(Assets
.touchArrows
[0],26,camera
.viewportHeight
-52);
127 spriteBatch
.draw(Assets
.touchArrows
[1],0,camera
.viewportHeight
-26);
128 spriteBatch
.draw(Assets
.touchArrows
[2],26,camera
.viewportHeight
-26);
129 spriteBatch
.draw(Assets
.touchArrows
[3],52,camera
.viewportHeight
-26);
130 spriteBatch
.draw(Assets
.touchSpace
, camera
.viewportWidth
/2-52, camera
.viewportHeight
-26);
131 spriteBatch
.draw(Assets
.touchLMB
, camera
.viewportWidth
-52, camera
.viewportHeight
-26);
132 spriteBatch
.draw(Assets
.touchRMB
, camera
.viewportWidth
-26, camera
.viewportHeight
-26);
133 spriteBatch
.draw(Assets
.touchToggleMode
, 78, camera
.viewportHeight
-26);
134 if (gameProc
.ctrlMode
==1) {
135 Assets
.shade
.setPosition(83, camera
.viewportHeight
-21);
136 Assets
.shade
.draw(spriteBatch
);
141 public void render() {
142 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
146 for (Mob mob
: gameProc
.mobs
) drawMob(mob
);
147 drawPlayer(gameProc
.player
);
151 if (gameProc
.ctrlMode
==1) {
152 shapeRenderer
.begin(ShapeRenderer
.ShapeType
.Line
);
153 shapeRenderer
.setColor(Color
.ORANGE
);
154 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Line
);
155 shapeRenderer
.rect(gameProc
.cursorX
* 16 - camera
.position
.x
,
156 gameProc
.cursorY
* 16 - camera
.position
.y
, 16, 16);