feat: basic drawing
This commit is contained in:
parent
65c63a0327
commit
4b36703287
58
core/src/bzh/fainsin/sagittarius/Entity.java
Normal file
58
core/src/bzh/fainsin/sagittarius/Entity.java
Normal file
|
@ -0,0 +1,58 @@
|
|||
package bzh.fainsin.sagittarius;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
abstract class Entity {
|
||||
|
||||
// ---------- ATTRIBUTs ----------
|
||||
|
||||
protected Vector2 position;
|
||||
|
||||
protected float mass = 1.0f;
|
||||
protected float angle;
|
||||
protected Color color = Color.WHITE;
|
||||
|
||||
// ---------- CONSTRUCTORs ----------
|
||||
|
||||
protected Entity(Vector2 position, float mass) {
|
||||
this.position = position;
|
||||
this.mass = mass;
|
||||
}
|
||||
|
||||
protected Entity(Vector2 position, float mass, Color color) {
|
||||
this(position, mass);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
// ---------- GETs ----------
|
||||
|
||||
|
||||
Vector2 getPosition() {
|
||||
return this.position;
|
||||
}
|
||||
|
||||
float getMass() {
|
||||
return this.mass;
|
||||
}
|
||||
|
||||
float getAngle() {
|
||||
return this.angle;
|
||||
}
|
||||
|
||||
Color getColor() {
|
||||
return this.color;
|
||||
}
|
||||
|
||||
// ---------- METHODs ----------
|
||||
|
||||
/**
|
||||
* Return the euclidian distance to another entity
|
||||
* @param entity
|
||||
* @return distance to entity
|
||||
*/
|
||||
float distanceTo(Entity entity) {
|
||||
return this.position.sub(entity.getPosition()).len();
|
||||
}
|
||||
|
||||
}
|
105
core/src/bzh/fainsin/sagittarius/GameScreen.java
Normal file
105
core/src/bzh/fainsin/sagittarius/GameScreen.java
Normal file
|
@ -0,0 +1,105 @@
|
|||
package bzh.fainsin.sagittarius;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.ScreenAdapter;
|
||||
import com.badlogic.gdx.graphics.Camera;
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.utils.viewport.FitViewport;
|
||||
import com.badlogic.gdx.utils.viewport.Viewport;
|
||||
|
||||
class GameScreen extends ScreenAdapter {
|
||||
|
||||
// ---------- ATTRIBUTES ----------
|
||||
|
||||
// screen size
|
||||
static final int WIDTH = 1920;
|
||||
static final int HEIGHT = 1080;
|
||||
|
||||
// drawing stuff
|
||||
private SpriteBatch batch;
|
||||
private ShapeRenderer shapeRenderer;
|
||||
private BitmapFont font;
|
||||
|
||||
// camera stuff
|
||||
private Viewport viewport;
|
||||
public static Camera camera;
|
||||
|
||||
// TODO: categorize better ?
|
||||
private HUD hud = new HUD();
|
||||
|
||||
// ---------- METHODS ----------
|
||||
|
||||
private void clearScreen() {
|
||||
Gdx.gl.glClearColor(Color.BLACK.r, Color.BLACK.g, Color.BLACK.b, Color.BLACK.a);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
}
|
||||
|
||||
private void update(float delta) {
|
||||
SagittariusGame.update(delta);
|
||||
hud.update();
|
||||
camera.update();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
|
||||
update(delta);
|
||||
|
||||
clearScreen();
|
||||
|
||||
// ---------- batch draw ----------
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
batch.begin();
|
||||
|
||||
// ---------- HUD ----------
|
||||
hud.render(batch, font);
|
||||
|
||||
batch.end();
|
||||
|
||||
// ---------- shapeRenderer draw ----------
|
||||
shapeRenderer.setProjectionMatrix(camera.combined);
|
||||
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
|
||||
|
||||
// ---------- planets ----------
|
||||
for (Planet planet : SagittariusGame.planetList) {
|
||||
planet.render(shapeRenderer);
|
||||
}
|
||||
|
||||
shapeRenderer.end();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
|
||||
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
|
||||
camera.update();
|
||||
|
||||
viewport = new FitViewport(WIDTH, HEIGHT, camera);
|
||||
|
||||
shapeRenderer = new ShapeRenderer();
|
||||
batch = new SpriteBatch();
|
||||
font = new BitmapFont();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
viewport.update(width, height);
|
||||
camera.update();
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
shapeRenderer.setProjectionMatrix(camera.combined);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
batch.dispose();
|
||||
font.dispose();
|
||||
shapeRenderer.dispose();
|
||||
}
|
||||
|
||||
}
|
23
core/src/bzh/fainsin/sagittarius/HUD.java
Normal file
23
core/src/bzh/fainsin/sagittarius/HUD.java
Normal file
|
@ -0,0 +1,23 @@
|
|||
package bzh.fainsin.sagittarius;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
|
||||
class HUD {
|
||||
|
||||
// ATTRIBUTS
|
||||
|
||||
private int frameRate;
|
||||
|
||||
// MÉTHODES
|
||||
|
||||
public void update() {
|
||||
frameRate = Gdx.graphics.getFramesPerSecond();
|
||||
}
|
||||
|
||||
public void render(Batch batch, BitmapFont font) {
|
||||
font.draw(batch, frameRate + " fps", 3, GameScreen.HEIGHT - 3);
|
||||
}
|
||||
|
||||
}
|
42
core/src/bzh/fainsin/sagittarius/Planet.java
Normal file
42
core/src/bzh/fainsin/sagittarius/Planet.java
Normal file
|
@ -0,0 +1,42 @@
|
|||
package bzh.fainsin.sagittarius;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import com.badlogic.gdx.graphics.g2d.Batch;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
|
||||
class Planet extends Entity {
|
||||
|
||||
// ---------- ATTRIBUTEs ----------
|
||||
|
||||
private float radius = 10;
|
||||
|
||||
// ---------- CONSTRUCTORs ----------
|
||||
|
||||
Planet(Vector2 position, float mass, float radius) {
|
||||
super(position, mass);
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
Planet(Vector2 position, float mass, float radius, Color color) {
|
||||
super(position, mass, color);
|
||||
this.radius = radius;
|
||||
}
|
||||
|
||||
// ---------- METHODS ----------
|
||||
|
||||
void render(ShapeRenderer shapeRenderer) {
|
||||
shapeRenderer.setColor(color);
|
||||
shapeRenderer.circle(this.position.x, this.position.y, radius);
|
||||
}
|
||||
|
||||
void renderDebug(Batch batch, BitmapFont font) {
|
||||
font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y);
|
||||
}
|
||||
|
||||
float getRadius() {
|
||||
return this.radius;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,33 +1,40 @@
|
|||
package bzh.fainsin.sagittarius;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class SagittariusGame extends ApplicationAdapter {
|
||||
SpriteBatch batch;
|
||||
Texture img;
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
|
||||
public class SagittariusGame extends Game {
|
||||
|
||||
// ---------- ATTRIBUTEs ----------
|
||||
|
||||
// Vectors
|
||||
static Vector2 screenCursor;
|
||||
static Vector2 worldCursor;
|
||||
|
||||
// Planets
|
||||
static ArrayList<Planet> planetList;
|
||||
|
||||
// ---------- METHODs ----------
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
batch = new SpriteBatch();
|
||||
img = new Texture("badlogic.jpg");
|
||||
setScreen(new GameScreen());
|
||||
|
||||
planetList = new ArrayList<Planet>();
|
||||
planetList.add( new Planet(new Vector2(100, 100), 1, 50) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render () {
|
||||
Gdx.gl.glClearColor(1, 0, 0, 1);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
batch.begin();
|
||||
batch.draw(img, 0, 0);
|
||||
batch.end();
|
||||
static void update(float delta) {
|
||||
|
||||
// ---------- cursors ----------
|
||||
screenCursor = new Vector2(Gdx.input.getX(), Gdx.input.getY());
|
||||
Vector3 unprojectedCursor = GameScreen.camera.unproject(new Vector3(screenCursor, 0));
|
||||
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose () {
|
||||
batch.dispose();
|
||||
img.dispose();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue