refactor!: Created BaseScreen and BaseGame

the game runs now, still kinda broken
Entity was redundant of Actor
This commit is contained in:
Laureηt 2021-04-09 11:57:52 +02:00
parent 38e5ed9bc7
commit 9bc8b487ae
10 changed files with 142 additions and 299 deletions

View file

@ -1,86 +1,18 @@
package sagittarius;
import java.util.ArrayList;
import java.util.Iterator;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import sagittarius.view.*;
import sagittarius.model.*;
public class SagittariusGame extends Game {
public class SagittariusGame extends BaseGame {
// ---------- ATTRIBUTEs ----------
// ---------- CONSTANTs ----------
// Constants
static final int G = 100;
// Vectors
static Vector2 screenCursor;
static Vector2 worldCursor;
// Entities
static ArrayList<Planet> planetList;
static ArrayList<Moon> moonList;
static ArrayList<Player> playerList;
static ArrayList<Arrow> arrowList;
// TODO : fix this shit, too many for loops
public static final float G = 100;
// ---------- METHODs ----------
@Override
public void create() {
setScreen(new StartScreen(this));
planetList = new ArrayList<Planet>();
planetList.add( new Planet(new Vector2(400, 400), 1000, 50) );
planetList.add( new Planet(new Vector2(1400, 700), 1000, 100, Color.CYAN) );
moonList = new ArrayList<Moon>();
moonList.add( new Moon(planetList.get(1), 100, 20, 300) );
playerList = new ArrayList<Player>();
playerList.add( new Player(planetList.get(0)) );
// playerList.add( new Player(planetList.get(1)) );
arrowList = new ArrayList<Arrow>();
}
/**
* Main update loop.
*
* @param deltaTime time elapsed between 2 frames.
*/
static void update(float deltaTime) {
// 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);
// players
for (Player player : playerList) {
player.update(deltaTime);
}
// moons
for (Moon moon : moonList) {
moon.update(deltaTime);
}
// arrows
for (Iterator<Arrow> it = arrowList.iterator(); it.hasNext(); ) {
Arrow arrow = it.next();
arrow.update(deltaTime);
if (arrow.TTL <= 0) {
it.remove();
}
}
this.setScreen(new StartScreen());
}
}

View file

@ -6,8 +6,11 @@ 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;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class Arrow extends Entity {
import sagittarius.Sagittarius;
public class Arrow extends Actor {
// ---------- ATTRIBUTEs ----------
@ -32,7 +35,8 @@ public class Arrow extends Entity {
* @param shooter Bow's shooter.
*/
Arrow(float angle, float power, Player shooter) {
super(shooter.position, 1);
super();
this.setPosition(shooter.getX(), shooter.getY());
this.velocity = new Vector2(power, 0).setAngleDeg(angle);
this.acceleration = new Vector2();
this.force = computeForce();
@ -47,14 +51,14 @@ public class Arrow extends Entity {
*/
private Vector2 computeForce() {
Vector2 force = new Vector2();
for (Planet attractor : SagittariusGame.planetList) {
for (Planet attractor : Sagittarius.planetList) {
Vector2 diff = attractor.position.cpy().sub(this.position);
Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() );
Vector2 attraction = diff.scl( Sagittarius.G * attractor.mass / diff.len2() );
force.add(attraction);
}
for (Moon attractor : SagittariusGame.moonList) {
for (Moon attractor : Sagittarius.moonList) {
Vector2 diff = attractor.position.cpy().sub(this.position);
Vector2 attraction = diff.scl( SagittariusGame.G * attractor.mass / diff.len2() );
Vector2 attraction = diff.scl( Sagittarius.G * attractor.mass / diff.len2() );
force.add(attraction);
}
return force;
@ -64,14 +68,14 @@ public class Arrow extends Entity {
* Verify whether or not the Arrow contacts an Entity.
*/
private void verifyActivity() {
for (Planet planet : SagittariusGame.planetList) {
for (Planet planet : Sagittarius.planetList) {
if (this.distanceTo(planet) < planet.getRadius()) {
this.active = false;
this.crash = planet;
this.offset = this.position.cpy().sub(planet.position);
}
}
for (Moon moon : SagittariusGame.moonList) {
for (Moon moon : Sagittarius.moonList) {
if (this.distanceTo(moon) < moon.getRadius()) {
this.active = false;
this.crash = moon;

View file

@ -5,10 +5,11 @@ import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.SagittariusGame;
import sagittarius.Sagittarius;
class Bow {
class Bow extends Actor {
// ---------- ATTRIBUTEs ----------
@ -25,27 +26,23 @@ class Bow {
// ---------- CONSTRUCTORs ----------
Bow(Player shooter, boolean aimAssist) {
super();
this.shooter = shooter;
this.aimAssist = aimAssist;
}
// ---------- METHODs ----------
/**
* Updates the physical attributes of the Bow,
* wich are later used to generate an Arrow.
*
* @param deltaTime time elapsed between 2 frames.
*/
void update(double deltaTime) {
@Override
public void act(float deltaTime) {
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = SagittariusGame.worldCursor.cpy();
this.anchor = Sagittarius.worldCursor.cpy();
pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow();
} else if (pressed) {
SagittariusGame.arrowList.add(getArrow());
Sagittarius.arrowList.add(getArrow());
pressed = false;
}
@ -65,14 +62,14 @@ class Bow {
* to the Arrow's constructor.
*/
private void computeArrow() {
aim = this.anchor.cpy().sub(SagittariusGame.worldCursor);
aim = this.anchor.cpy().sub(Sagittarius.worldCursor);
angle = aim.angleDeg();
power = MathUtils.clamp(aim.len(), 0, 1000);
}
void render(ShapeRenderer shapeRenderer) {
if (pressed) {
shapeRenderer.line(this.anchor, SagittariusGame.worldCursor);
shapeRenderer.line(this.anchor, Sagittarius.worldCursor);
if (aimAssist) {
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
if (traj.length > 2) {

View file

@ -1,37 +0,0 @@
package sagittarius.model;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
abstract class Entity {
// ---------- ATTRIBUTEs ----------
protected Vector2 position;
protected float mass = 1.0f;
protected float angle;
protected Color color = Color.WHITE;
// ---------- CONSTRUCTORs ----------
// TODO : reorganize all constructors
protected Entity(Vector2 position, float mass) {
this.position = position.cpy();
this.mass = mass;
}
// ---------- METHODs ----------
/**
* Returns the euclidian distance to another entity.
*
* @param entity the other Entity whose distance we wish to evaluate.
* @return distance to Entity.
*/
float distanceTo(Entity entity) {
return this.position.cpy().sub(entity.position).len();
}
}

View file

@ -11,7 +11,7 @@ public class Planet extends Actor {
// ---------- ATTRIBUTEs ----------
private BitmapFont fontDebug;
private BitmapFont fontDebug = new BitmapFont();
private float radius;
private float mass;
@ -48,6 +48,4 @@ public class Planet extends Actor {
return this.radius;
}
}

View file

@ -1,13 +1,10 @@
package sagittarius.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
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.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class Player extends Actor {
@ -16,7 +13,7 @@ public class Player extends Actor {
private Planet home;
private Bow bow;
private BitmapFont fontDebug;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
@ -27,7 +24,7 @@ public class Player extends Actor {
this.setColor(color);
this.setOrigin(25, 50);
this.home = home;
this.bow = new Bow(this, true);
//this.bow = new Bow(this, true);
}
// ---------- METHODs ----------
@ -50,7 +47,7 @@ public class Player extends Actor {
this.setX(home.getX() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
this.setY(home.getY() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
this.bow.act(deltaTime);
//this.bow.act(deltaTime);
}
/**

View file

@ -0,0 +1,16 @@
package sagittarius.view;
import com.badlogic.gdx.Game;
public abstract class BaseGame extends Game {
private static BaseGame game;
public BaseGame() {
game = this;
}
public static void setActiveScreen(BaseScreen s) {
game.setScreen(s);
}
}

View file

@ -0,0 +1,52 @@
package sagittarius.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.Screen;
public abstract class BaseScreen implements Screen {
protected Stage mainStage;
protected Stage uiStage;
public BaseScreen() {
mainStage = new Stage();
uiStage = new Stage();
initialize();
}
public abstract void initialize();
public abstract void update(float dt);
@Override
public void render(float dt) {
// update actors
uiStage.act(dt);
mainStage.act(dt);
// update
update(dt);
// clear screen
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// draw actors on screnn
mainStage.draw();
uiStage.draw();
}
@Override public void dispose() {
uiStage.dispose();
mainStage.dispose();
}
// methods required by Screen interface
@Override public void resize(int width, int height) {}
@Override public void pause() {}
@Override public void resume(){}
@Override public void show() {}
@Override public void hide() {}
}

View file

@ -1,146 +1,38 @@
package sagittarius.view;
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;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
class GameScreen extends ScreenAdapter {
import sagittarius.model.*;
// ---------- ATTRIBUTES ----------
public class GameScreen extends BaseScreen {
// screen size
static final int WIDTH = 1920;
static final int HEIGHT = 1080;
// ---------- ATTRIBUTEs ----------
// drawing stuff
private SpriteBatch batch;
private ShapeRenderer shapeRenderer;
private BitmapFont font;
// Constants
public static final int G = 100;
// camera stuff
static Viewport viewport; // TODO : useless ?
protected static Camera camera;
// ---------- METHODs ----------
// TODO: categorize better ?
private HUD hud = new HUD();
@Override
public void initialize() {
// ---------- METHODS ----------
mainStage = new Stage();
Planet planet1 = new Planet(new Vector2(400, 400), 1000, 200, Color.WHITE);
planet1.setDebug(true);
mainStage.addActor(planet1);
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 deltaTime) {
SagittariusGame.update(deltaTime);
hud.update();
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
Player player1 = new Player(planet1, Color.WHITE);
player1.setDebug(true);
mainStage.addActor(player1);
}
@Override
public void render(float deltaTime) {
update(deltaTime);
clearScreen();
// ---------- batch ----------
batch.setProjectionMatrix(camera.projection);
batch.setTransformMatrix(camera.view);
batch.begin();
// planets
for (Planet planet : SagittariusGame.planetList) {
planet.renderDebug(batch, font);
}
// moons
for (Moon moon : SagittariusGame.moonList) {
moon.renderDebug(batch, font);
}
// players
for (Player player : SagittariusGame.playerList) {
player.renderDebug(batch, font);
}
// arrows
for (Arrow arrow : SagittariusGame.arrowList) {
arrow.renderDebug(batch, font);
}
batch.end();
// ---------- shapeRenderer ----------
shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
// planets
for (Planet planet : SagittariusGame.planetList) {
planet.render(shapeRenderer);
}
// players
for (Player player : SagittariusGame.playerList) {
player.render(shapeRenderer);
}
// arrows
for (Arrow arrow : SagittariusGame.arrowList) {
arrow.render(shapeRenderer);
}
// moons
for (Moon moon : SagittariusGame.moonList) {
moon.render(shapeRenderer);
}
// float[] vertices = {100, 100, 200, 200, 1000, 500};
// shapeRenderer.polyline(vertices);
shapeRenderer.end();
// HUD
hud.render();
}
@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);
hud.resize();
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
shapeRenderer.dispose();
public void update(float dt) {
// todo ?
}
}

View file

@ -1,46 +1,38 @@
package sagittarius.view;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ActorGestureListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.widget.VisTable;
import com.kotcrab.vis.ui.widget.VisTextButton;
public class StartScreen extends ScreenAdapter {
import sagittarius.SagittariusGame;
private static final float WORLD_WIDTH = 1600;
private static final float WORLD_HEIGHT = 900;
public class StartScreen extends BaseScreen {
private final Game game;
private Stage stage;
@Override
public void initialize() {
public StartScreen(Game game) {
this.game = game;
}
public void show() {
stage = new Stage(new FitViewport(WORLD_WIDTH, WORLD_HEIGHT));
Gdx.input.setInputProcessor(stage);
Gdx.input.setInputProcessor(mainStage);
// Table creation
VisUI.load();
VisTable root = new VisTable(true);
root.setFillParent(true);
stage.addActor(root);
VisTable table = new VisTable(true);
table.setFillParent(true);
mainStage.addActor(table);
// Start Button
VisTextButton startButton = new VisTextButton("start");
startButton.addListener(new ActorGestureListener() {
@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
super.tap(event, x, y, count, button);
game.setScreen(new GameScreen());
SagittariusGame.setActiveScreen( new GameScreen() );
}
});
// Quit button
VisTextButton quitButton = new VisTextButton("quit");
quitButton.addListener(new ActorGestureListener() {
@Override
@ -50,25 +42,25 @@ public class StartScreen extends ScreenAdapter {
}
});
root.add(startButton);
root.row();
root.add(quitButton);
root.row();
// Table structure
table.add(startButton);
table.row();
table.add(quitButton);
table.row();
}
@Override
public void update(float deltaTime) {
//
}
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
public void render(float deltaTime) {
stage.act(deltaTime);
stage.draw();
mainStage.getViewport().update(width, height, true);
}
@Override
public void dispose() {
VisUI.dispose();
stage.dispose();
super.dispose();
}