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
.Vector3
;
10 import ru
.deadsoftware
.cavecraft
.Assets
;
11 import ru
.deadsoftware
.cavecraft
.Items
;
12 import ru
.deadsoftware
.cavecraft
.GameScreen
;
13 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
15 public class GameRenderer
{
17 private GameProc gameProc
;
19 public Vector3 camTargetPos
;
20 public OrthographicCamera camera
;
21 ShapeRenderer shapeRenderer
;
22 SpriteBatch spriteBatch
;
24 public GameRenderer(GameProc gameProc
) {
25 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
26 this.gameProc
= gameProc
;
27 camera
= new OrthographicCamera();
28 camera
.setToOrtho(true, 360,
29 360*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
32 camTargetPos
= camera
.position
.cpy();
33 shapeRenderer
= new ShapeRenderer();
34 shapeRenderer
.setProjectionMatrix(camera
.combined
);
35 shapeRenderer
.setAutoShapeType(true);
36 spriteBatch
= new SpriteBatch();
37 spriteBatch
.setProjectionMatrix(camera
.combined
);
40 private void setColor(int r
, int g
, int b
) {
41 shapeRenderer
.setColor(r
/255f, g
/255f, b
/255f, 1f);
44 private void fillRect(float x
, float y
, float w
, float h
) {
45 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Filled
);
46 shapeRenderer
.rect(x
,y
,w
,h
);
49 private void drawRect(float x
, float y
, float w
, float h
) {
50 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Line
);
51 shapeRenderer
.rect(x
,y
,w
,h
);
54 private void drawWorld() {
55 int minX
= (int) (camera
.position
.x
/16);
56 int minY
= (int) (camera
.position
.y
/16);
57 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
58 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
61 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
62 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
63 for (int y
=minY
; y
<maxY
; y
++) {
64 for (int x
=minX
; x
<maxX
; x
++) {
65 if (gameProc
.world
.getForeMap(x
,y
)>0) {
67 Items
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
68 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
69 } else if (gameProc
.world
.getBackMap(x
,y
)>0) {
71 Items
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture(),
72 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
73 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
74 Assets
.shade
.draw(spriteBatch
);
80 private void drawPlayer(Player pl
) {
81 Assets
.playerSprite
[pl
.dir
].setPosition(pl
.position
.x
- camera
.position
.x
,
82 pl
.position
.y
- camera
.position
.y
);
83 Assets
.playerSprite
[pl
.dir
].draw(spriteBatch
);
86 private void drawGUI() {
87 spriteBatch
.draw(Assets
.invBar
, camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2,
88 camera
.viewportHeight
- Assets
.invBar
.getRegionHeight());
89 for (int i
=0; i
<8; i
++) {
90 if (gameProc
.player
.inventory
[i
]>0) {
91 spriteBatch
.draw(Items
.BLOCKS
.getValueAt(gameProc
.player
.inventory
[i
]).getTexture(),
92 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2+3+i
*20,
93 camera
.viewportHeight
-19);
96 spriteBatch
.draw(Assets
.invCur
,
97 camera
.viewportWidth
/2 - Assets
.invBar
.getRegionWidth()/2 - 1 + 20*gameProc
.invSlot
,
98 camera
.viewportHeight
- Assets
.invBar
.getRegionHeight() - 2);
101 public void render() {
102 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
105 drawPlayer(gameProc
.player
);
109 shapeRenderer
.begin(ShapeRenderer
.ShapeType
.Line
);
110 shapeRenderer
.setColor(Color
.ORANGE
);
111 drawRect(gameProc
.cursorX
*16-camera
.position
.x
,
112 gameProc
.cursorY
*16-camera
.position
.y
,16,16);