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; 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.view.*;
import sagittarius.model.*;
public class SagittariusGame extends Game { public class SagittariusGame extends BaseGame {
// ---------- ATTRIBUTEs ---------- // ---------- CONSTANTs ----------
// Constants public static final float G = 100;
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
// ---------- METHODs ---------- // ---------- METHODs ----------
@Override @Override
public void create() { public void create() {
setScreen(new StartScreen(this)); this.setScreen(new StartScreen());
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();
}
}
} }
} }

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.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2; 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 ---------- // ---------- ATTRIBUTEs ----------
@ -32,7 +35,8 @@ public class Arrow extends Entity {
* @param shooter Bow's shooter. * @param shooter Bow's shooter.
*/ */
Arrow(float angle, float power, Player 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.velocity = new Vector2(power, 0).setAngleDeg(angle);
this.acceleration = new Vector2(); this.acceleration = new Vector2();
this.force = computeForce(); this.force = computeForce();
@ -47,14 +51,14 @@ public class Arrow extends Entity {
*/ */
private Vector2 computeForce() { private Vector2 computeForce() {
Vector2 force = new Vector2(); Vector2 force = new Vector2();
for (Planet attractor : SagittariusGame.planetList) { for (Planet attractor : Sagittarius.planetList) {
Vector2 diff = attractor.position.cpy().sub(this.position); 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); force.add(attraction);
} }
for (Moon attractor : SagittariusGame.moonList) { for (Moon attractor : Sagittarius.moonList) {
Vector2 diff = attractor.position.cpy().sub(this.position); 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); force.add(attraction);
} }
return force; return force;
@ -64,14 +68,14 @@ public class Arrow extends Entity {
* Verify whether or not the Arrow contacts an Entity. * Verify whether or not the Arrow contacts an Entity.
*/ */
private void verifyActivity() { private void verifyActivity() {
for (Planet planet : SagittariusGame.planetList) { for (Planet planet : Sagittarius.planetList) {
if (this.distanceTo(planet) < planet.getRadius()) { if (this.distanceTo(planet) < planet.getRadius()) {
this.active = false; this.active = false;
this.crash = planet; this.crash = planet;
this.offset = this.position.cpy().sub(planet.position); this.offset = this.position.cpy().sub(planet.position);
} }
} }
for (Moon moon : SagittariusGame.moonList) { for (Moon moon : Sagittarius.moonList) {
if (this.distanceTo(moon) < moon.getRadius()) { if (this.distanceTo(moon) < moon.getRadius()) {
this.active = false; this.active = false;
this.crash = moon; 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.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; 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 ---------- // ---------- ATTRIBUTEs ----------
@ -25,27 +26,23 @@ class Bow {
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
Bow(Player shooter, boolean aimAssist) { Bow(Player shooter, boolean aimAssist) {
super();
this.shooter = shooter; this.shooter = shooter;
this.aimAssist = aimAssist; this.aimAssist = aimAssist;
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
/** @Override
* Updates the physical attributes of the Bow, public void act(float deltaTime) {
* wich are later used to generate an Arrow.
*
* @param deltaTime time elapsed between 2 frames.
*/
void update(double deltaTime) {
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = SagittariusGame.worldCursor.cpy(); this.anchor = Sagittarius.worldCursor.cpy();
pressed = true; pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow(); computeArrow();
} else if (pressed) { } else if (pressed) {
SagittariusGame.arrowList.add(getArrow()); Sagittarius.arrowList.add(getArrow());
pressed = false; pressed = false;
} }
@ -65,14 +62,14 @@ class Bow {
* to the Arrow's constructor. * to the Arrow's constructor.
*/ */
private void computeArrow() { private void computeArrow() {
aim = this.anchor.cpy().sub(SagittariusGame.worldCursor); aim = this.anchor.cpy().sub(Sagittarius.worldCursor);
angle = aim.angleDeg(); angle = aim.angleDeg();
power = MathUtils.clamp(aim.len(), 0, 1000); power = MathUtils.clamp(aim.len(), 0, 1000);
} }
void render(ShapeRenderer shapeRenderer) { void render(ShapeRenderer shapeRenderer) {
if (pressed) { if (pressed) {
shapeRenderer.line(this.anchor, SagittariusGame.worldCursor); shapeRenderer.line(this.anchor, Sagittarius.worldCursor);
if (aimAssist) { if (aimAssist) {
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f); float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
if (traj.length > 2) { 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 ---------- // ---------- ATTRIBUTEs ----------
private BitmapFont fontDebug; private BitmapFont fontDebug = new BitmapFont();
private float radius; private float radius;
private float mass; private float mass;
@ -48,6 +48,4 @@ public class Planet extends Actor {
return this.radius; return this.radius;
} }
} }

View file

@ -1,13 +1,10 @@
package sagittarius.model; 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.Color;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
public class Player extends Actor { public class Player extends Actor {
@ -16,7 +13,7 @@ public class Player extends Actor {
private Planet home; private Planet home;
private Bow bow; private Bow bow;
private BitmapFont fontDebug; private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -27,7 +24,7 @@ public class Player extends Actor {
this.setColor(color); this.setColor(color);
this.setOrigin(25, 50); this.setOrigin(25, 50);
this.home = home; this.home = home;
this.bow = new Bow(this, true); //this.bow = new Bow(this, true);
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -50,7 +47,7 @@ public class Player extends Actor {
this.setX(home.getX() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation())); this.setX(home.getX() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
this.setY(home.getY() + 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; 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.Color;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.scenes.scene2d.Stage;
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 { import sagittarius.model.*;
// ---------- ATTRIBUTES ---------- public class GameScreen extends BaseScreen {
// screen size // ---------- ATTRIBUTEs ----------
static final int WIDTH = 1920;
static final int HEIGHT = 1080;
// drawing stuff // Constants
private SpriteBatch batch; public static final int G = 100;
private ShapeRenderer shapeRenderer;
private BitmapFont font;
// camera stuff // ---------- METHODs ----------
static Viewport viewport; // TODO : useless ?
protected 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 deltaTime) {
SagittariusGame.update(deltaTime);
hud.update();
camera.position.set(WIDTH / 2, HEIGHT / 2, 0);
camera.update();
}
@Override @Override
public void render(float deltaTime) { public void initialize() {
update(deltaTime); mainStage = new Stage();
clearScreen(); Planet planet1 = new Planet(new Vector2(400, 400), 1000, 200, Color.WHITE);
planet1.setDebug(true);
mainStage.addActor(planet1);
// ---------- batch ---------- Player player1 = new Player(planet1, Color.WHITE);
batch.setProjectionMatrix(camera.projection); player1.setDebug(true);
batch.setTransformMatrix(camera.view); mainStage.addActor(player1);
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 @Override
public void show() { public void update(float dt) {
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // todo ?
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();
} }
} }

View file

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