feat: created BaseActor + factorized some code

This commit is contained in:
Laureηt 2021-04-13 18:43:41 +02:00
parent 613dc0a757
commit b279519b55
7 changed files with 58 additions and 45 deletions

View file

@ -3,7 +3,7 @@ package sagittarius;
import com.badlogic.gdx.Game;
import sagittarius.view.BaseScreen;
import sagittarius.view.GameScreen;
import sagittarius.view.StartScreen;
public class SagittariusGame extends Game {
@ -26,6 +26,6 @@ public class SagittariusGame extends Game {
@Override
public void create() {
this.setScreen(new GameScreen());
this.setScreen(new StartScreen());
}
}

View file

@ -2,8 +2,6 @@ package sagittarius.model;
import java.util.ArrayList;
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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
@ -13,7 +11,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.SagittariusGame;
import sagittarius.view.GameScreen;
public class Arrow extends Actor {
public class Arrow extends BaseActor {
// ---------- ATTRIBUTEs ----------
@ -23,8 +21,6 @@ public class Arrow extends Actor {
private float TTL = 20;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
/**
@ -60,17 +56,6 @@ public class Arrow extends Actor {
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, "TTL=" + (int)TTL, getX(), getY() + fontDebug.getCapHeight()*5);
fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY(), getX(), getY() + fontDebug.getCapHeight()*4);
fontDebug.draw(batch, "speed=" + (int)velocity.x + "," + (int)velocity.y, getX(), getY() + fontDebug.getCapHeight()*3);
fontDebug.draw(batch, "accel=" + (int)acceleration.x + "," + (int)acceleration.y, getX(), getY() + fontDebug.getCapHeight()*2);
fontDebug.draw(batch, "force=" + (int)force.x + "," + (int)force.y, getX(), getY() + fontDebug.getCapHeight()*1);
fontDebug.draw(batch, "angle=" + (int)getRotation(), getX(), getY());
}
@Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
@ -170,4 +155,16 @@ public class Arrow extends Actor {
return new Vector2( getX(), getY() );
}
@Override
protected String getInfo() {
return
"TTL=" + (int)TTL + "\n"
+ "pos=" + (int)getX() + "," + (int)getY() + "\n"
+ "speed=" + (int)velocity.x + "," + (int)velocity.y + "\n"
+ "accel=" + (int)acceleration.x + "," + (int)acceleration.y +"\n"
+ "force=" + (int)force.x + "," + (int)force.y + "\n"
+ "angle=" + (int)getRotation()
;
}
}

View file

@ -0,0 +1,21 @@
package sagittarius.model;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
public abstract class BaseActor extends Actor {
static private BitmapFont fontDebug = new BitmapFont();
protected abstract String getInfo();
protected float angle;
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, getInfo(), getX(), getY());
}
}

View file

@ -43,7 +43,6 @@ class Bow extends Actor {
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow();
} else if (pressed) {
// Sagittarius.arrowList.add(getArrow());
GameScreen.arrows.addActor(getArrow());
pressed = false;
}

View file

@ -1,18 +1,13 @@
package sagittarius.model;
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;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
public class Planet extends Actor {
public class Planet extends BaseActor {
// ---------- ATTRIBUTEs ----------
private BitmapFont fontDebug = new BitmapFont();
private float radius;
private float mass;
@ -33,14 +28,6 @@ public class Planet extends Actor {
super.act(dt);
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, "radius=" + (int)radius, getX(), getY() + fontDebug.getCapHeight()*2);
fontDebug.draw(batch, "mass=" + (int)mass, getX(), getY() + fontDebug.getCapHeight());
fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY(), getX(), getY());
}
@Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
@ -63,4 +50,13 @@ public class Planet extends Actor {
return new Vector2( getX(), getY() );
}
@Override
protected String getInfo() {
return
"radius=" + (int)radius + "\n"
+ "mass=" + (int)mass + "\n"
+ "pos=" + (int)getX() + "," + (int)getY()
;
}
}

View file

@ -3,26 +3,22 @@ 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.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class Player extends Actor {
public class Player extends BaseActor {
// ---------- ATTRIBUTEs ----------
private Planet home;
private Bow bow;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
public Player(Planet home, Color color) {
super();
this.setSize(100, 50); // TODO: fix this ? (width & height inverted)
this.setSize(100, 50); // TODO: fix this ? (width & height inverted) => use BaseActor.angle
this.setRotation(MathUtils.random(360));
this.setColor(color);
this.setOrigin(0, getHeight()/2);
@ -32,12 +28,6 @@ public class Player extends Actor {
// ---------- METHODs ----------
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY() , getX(), getY());
}
@Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
@ -82,4 +72,11 @@ public class Player extends Actor {
return this.home;
}
@Override
protected String getInfo() {
return
"pos=" + (int)getX() + "," + (int)getY()
;
}
}

View file

@ -44,6 +44,9 @@ public class GameScreen extends BaseScreen {
Player player1 = new Player(planet1, Color.WHITE);
mainStage.addActor(player1);
Player player2 = new Player(planet2, Color.WHITE);
mainStage.addActor(player2);
// arrows
arrows = new Group();
mainStage.addActor(arrows);