-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraComponent.java
45 lines (37 loc) · 1.11 KB
/
CameraComponent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.csse3200.game.components;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
public class CameraComponent extends Component {
private final Camera camera;
private Vector2 lastPosition;
public CameraComponent() {
this(new OrthographicCamera());
}
public CameraComponent(Camera camera) {
this.camera = camera;
lastPosition = Vector2.Zero.cpy();
}
@Override
public void update() {
Vector2 position = entity.getPosition();
if (!lastPosition.epsilonEquals(entity.getPosition())) {
camera.position.set(position.x, position.y, 0f);
lastPosition = position;
camera.update();
}
}
public Matrix4 getProjectionMatrix() {
return camera.combined;
}
public Camera getCamera() {
return camera;
}
public void resize(int screenWidth, int screenHeight, float gameWidth) {
float ratio = (float) screenHeight / screenWidth;
camera.viewportWidth = gameWidth;
camera.viewportHeight = gameWidth * ratio;
camera.update();
}
}