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
.glutils
.ShapeRenderer
;
7 import com
.badlogic
.gdx
.graphics
.Color
;
8 import com
.badlogic
.gdx
.math
.Rectangle
;
9 import com
.badlogic
.gdx
.math
.Vector3
;
10 import ru
.deadsoftware
.cavecraft
.GameScreen
;
11 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
13 public class GameRenderer
{
15 private GameProc gameProc
;
17 public Vector3 camTargetPos
;
18 public OrthographicCamera camera
;
19 ShapeRenderer shapeRenderer
;
21 public GameRenderer(GameProc gameProc
) {
22 Gdx
.gl
.glClearColor(0f,.6f,.6f,1f);
23 this.gameProc
= gameProc
;
24 camera
= new OrthographicCamera();
25 camera
.setToOrtho(true, 320,
26 320*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
29 camTargetPos
= camera
.position
.cpy();
30 shapeRenderer
= new ShapeRenderer();
31 shapeRenderer
.setProjectionMatrix(camera
.combined
);
32 shapeRenderer
.setAutoShapeType(true);
35 private void setColor(int r
, int g
, int b
) {
36 shapeRenderer
.setColor(r
/255f, g
/255f, b
/255f, 1f);
39 private void fillRect(float x
, float y
, float w
, float h
) {
40 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Filled
);
41 shapeRenderer
.rect(x
,y
,w
,h
);
44 private void drawRect(float x
, float y
, float w
, float h
) {
45 shapeRenderer
.set(ShapeRenderer
.ShapeType
.Line
);
46 shapeRenderer
.rect(x
,y
,w
,h
);
49 private void drawWorld() {
50 int minX
= (int) (camera
.position
.x
/16);
51 int minY
= (int) (camera
.position
.y
/16);
52 int maxX
= (int) ((camera
.position
.x
+camera
.viewportWidth
)/16)+1;
53 int maxY
= (int) ((camera
.position
.y
+camera
.viewportHeight
)/16)+1;
56 if (maxX
>=gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth()-1;
57 if (maxY
>=gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight()-1;
58 for (int y
=minY
; y
<maxY
; y
++) {
59 for (int x
=minX
; x
<maxX
; x
++) {
60 if (gameProc
.world
.getForeMap(x
,y
)>0) {
61 setColor(128,128,128);
62 fillRect(x
*16-camera
.position
.x
,
63 y
*16-camera
.position
.y
,16,16);
65 drawRect(x
*16-camera
.position
.x
,
66 y
*16-camera
.position
.y
,16,16);
68 if (gameProc
.world
.getBackMap(x
,y
)>0) {
70 fillRect(x
*16-camera
.position
.x
,
71 y
*16-camera
.position
.y
,16,16);
73 drawRect(x
*16-camera
.position
.x
,
74 y
*16-camera
.position
.y
,16,16);
80 shapeRenderer
.setColor(Color
.ORANGE
);
81 drawRect(gameProc
.cursorX
*16-camera
.position
.x
,
82 gameProc
.cursorY
*16-camera
.position
.y
,16,16);
85 private void drawPlayer(Player pl
) {
87 fillRect(pl
.position
.x
- camera
.position
.x
,
88 pl
.position
.y
- camera
.position
.y
, pl
.width
, pl
.height
);
91 public void render() {
92 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
93 shapeRenderer
.begin();
95 drawPlayer(gameProc
.player
);