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
.Rectangle
;
10 import com
.badlogic
.gdx
.math
.Vector3
;
11 import ru
.deadsoftware
.cavecraft
.Assets
;
12 import ru
.deadsoftware
.cavecraft
.BlocksLoader
;
13 import ru
.deadsoftware
.cavecraft
.GameScreen
;
14 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
16 public class GameRenderer
{
18 private GameProc gameProc
;
20 public Vector3 camTargetPos
;
21 public OrthographicCamera camera
;
22 ShapeRenderer shapeRenderer
;
23 SpriteBatch spriteBatch
;
25 public GameRenderer(GameProc gameProc
) {
26 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
27 this.gameProc
= gameProc
;
28 camera
= new OrthographicCamera();
29 camera
.setToOrtho(true, 360,
30 360*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
33 camTargetPos
= camera
.position
.cpy();
34 shapeRenderer
= new ShapeRenderer();
35 shapeRenderer
.setProjectionMatrix(camera
.combined
);
36 shapeRenderer
.setAutoShapeType(true);
37 spriteBatch
= new SpriteBatch();
38 spriteBatch
.setProjectionMatrix(camera
.combined
);
41 private void setColor(int r
, int g
, int b
) {
42 shapeRenderer
.setColor(r
/255f, g
/255f, b
/255f, 1f);
45 private void fillRect(float x
, float y
, float w
, float h
) {
46 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Filled
);
47 shapeRenderer
.rect(x
,y
,w
,h
);
50 private void drawRect(float x
, float y
, float w
, float h
) {
51 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Line
);
52 shapeRenderer
.rect(x
,y
,w
,h
);
55 private void drawWorld() {
56 int minX
= (int) (camera
.position
.x
/16);
57 int minY
= (int) (camera
.position
.y
/16);
58 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
59 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
62 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
63 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
64 for (int y
=minY
; y
<maxY
; y
++) {
65 for (int x
=minX
; x
<maxX
; x
++) {
66 if (gameProc
.world
.getForeMap(x
,y
)>0) {
68 BlocksLoader
.BLOCKS
.getValueAt(gameProc
.world
.getForeMap(x
,y
)).getTexture(),
69 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
70 } else if (gameProc
.world
.getBackMap(x
,y
)>0) {
72 BlocksLoader
.BLOCKS
.getValueAt(gameProc
.world
.getBackMap(x
,y
)).getTexture(),
73 x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
74 Assets
.shade
.setPosition(x
* 16 - camera
.position
.x
,y
* 16 - camera
.position
.y
);
75 Assets
.shade
.draw(spriteBatch
);
81 private void drawPlayer(Player pl
) {
82 Assets
.playerSprite
[pl
.dir
].setPosition(pl
.position
.x
- camera
.position
.x
,
83 pl
.position
.y
- camera
.position
.y
);
84 Assets
.playerSprite
[pl
.dir
].draw(spriteBatch
);
87 public void render() {
88 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
91 drawPlayer(gameProc
.player
);
93 shapeRenderer
.begin(ShapeRenderer
.ShapeType
.Line
);
94 shapeRenderer
.setColor(Color
.ORANGE
);
95 drawRect(gameProc
.cursorX
*16-camera
.position
.x
,
96 gameProc
.cursorY
*16-camera
.position
.y
,16,16);