feat: remove BaseActor, some cleaning necessary

This commit is contained in:
Laureηt 2021-04-22 17:53:00 +02:00
parent eb0a6a6253
commit ea826d2ca7
7 changed files with 28 additions and 102 deletions

View file

@ -67,6 +67,7 @@ public class Arrow extends EntityQuad {
@Override @Override
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
if (getStage() != null) shapes.setColor(getStage().getDebugColor());
for (Actor actor : GameScreen.attractors.getChildren()) { for (Actor actor : GameScreen.attractors.getChildren()) {
shapes.line(getX(), getY(), actor.getX(), actor.getY()); shapes.line(getX(), getY(), actor.getX(), actor.getY());
} }

View file

@ -1,95 +0,0 @@
package sagittarius.model;
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.Intersector;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
public abstract class BaseActor extends Actor {
// ---------- ATTRIBUTEs ----------
static private BitmapFont fontDebug = new BitmapFont();
protected Polygon hitbox = new Polygon(test());
protected float angle; // encapsulate ?
// ---------- METHODs ----------
@Override
public void act(float dt) {
super.act(dt);
hitbox.setVertices(test());
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
if (getDebug()) fontDebug.draw(batch, getInfo(), getX(), getY());
}
@Override
protected void drawDebugBounds(ShapeRenderer shapes) {
if (!getDebug()) return;
shapes.set(ShapeType.Line);
if (getStage() != null) shapes.setColor(getColor());
shapes.polygon(test());
}
/**
* {@link ShapeRenderer#rect}
* @return vertices of the hitbox polygon
*/
protected float[] test() { // convert position + rotation and other shit to polygon
float cos = MathUtils.cosDeg(getRotation());
float sin = MathUtils.sinDeg(getRotation());
float fx = -getOriginX();
float fy = -getOriginY();
float fx2 = getWidth() - getOriginX();
float fy2 = getHeight() - getOriginY();
if (getScaleX() != 1 || getScaleX() != 1) {
fx *= getScaleX();
fy *= getScaleY();
fx2 *= getScaleX();
fy2 *= getScaleY();
}
float worldOriginX = getX();
float worldOriginY = getY();
float x1 = cos * fx - sin * fy + worldOriginX;
float y1 = sin * fx + cos * fy + worldOriginY;
float x2 = cos * fx2 - sin * fy + worldOriginX;
float y2 = sin * fx2 + cos * fy + worldOriginY;
float x3 = cos * fx2 - sin * fy2 + worldOriginX;
float y3 = sin * fx2 + cos * fy2 + worldOriginY;
float x4 = x1 + (x3 - x2);
float y4 = y3 - (y2 - y1);
return new float[] { x1, y1, x2, y2, x3, y3, x4, y4 };
}
/**
* @return debug String displayed in {@link #draw()}
*/
protected abstract String getInfo();
protected boolean overlaps(BaseActor other) {
return Intersector.overlapConvexPolygons(hitbox, other.hitbox);
}
public Vector2 getPosition() {
return new Vector2( getX(), getY() );
}
}

View file

@ -2,19 +2,18 @@ package sagittarius.model;
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.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Polygon; import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.view.GameScreen;
public abstract class Entity extends Actor { public abstract class Entity extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
protected static BitmapFont fontDebug = new BitmapFont();
// private @Null String name; // private @Null String name;
// private Touchable touchable = Touchable.enabled; // private Touchable touchable = Touchable.enabled;
// private boolean visible = true, debug; // private boolean visible = true, debug;
@ -120,14 +119,14 @@ public abstract class Entity extends Actor {
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
if (getDebug()) fontDebug.draw(batch, getInfo(), getX(), getY()); if (getDebug()) GameScreen.fontDebug.draw(batch, getInfo(), getX(), getY());
} }
@Override @Override
protected void drawDebugBounds(ShapeRenderer shapes) { protected void drawDebugBounds(ShapeRenderer shapes) {
if (!getDebug()) return; if (!getDebug()) return;
shapes.set(ShapeType.Line); shapes.set(ShapeType.Line);
if (getStage() != null) shapes.setColor(this.getColor()); shapes.setColor(this.getColor());
shapes.polygon(hitbox.getVertices()); shapes.polygon(hitbox.getVertices());
} }

View file

@ -1,9 +1,13 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.Stage;
public class FPS extends BaseActor { import sagittarius.view.GameScreen;
public class FPS extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
@ -23,6 +27,11 @@ public class FPS extends BaseActor {
} }
@Override @Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
if (getDebug()) GameScreen.fontDebug.draw(batch, getInfo(), getX(), getY());
}
protected String getInfo() { protected String getInfo() {
return frameRate + " fps"; return frameRate + " fps";
} }

View file

@ -1,8 +1,11 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.view.GameScreen; import sagittarius.view.GameScreen;
public class MouseInfo extends BaseActor { public class MouseInfo extends Actor {
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -14,6 +17,11 @@ public class MouseInfo extends BaseActor {
} }
@Override @Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
if (getDebug()) GameScreen.fontDebug.draw(batch, getInfo(), getX(), getY());
}
protected String getInfo() { protected String getInfo() {
return return
"screen=" + (int)GameScreen.screenCursor.x + "," + (int)GameScreen.screenCursor.y + "\n" "screen=" + (int)GameScreen.screenCursor.x + "," + (int)GameScreen.screenCursor.y + "\n"

View file

@ -30,6 +30,7 @@ public class Player extends EntityQuad {
@Override @Override
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
if (getStage() != null) shapes.setColor(getStage().getDebugColor());
shapes.line(home.getX(), home.getY(), getX(), getY()); shapes.line(home.getX(), home.getY(), getX(), getY());
} }

View file

@ -2,6 +2,7 @@ package sagittarius.view;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Group; import com.badlogic.gdx.scenes.scene2d.Group;
@ -13,6 +14,8 @@ public class GameScreen extends BaseScreen {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
public static BitmapFont fontDebug = new BitmapFont();
// Cursors // Cursors
public static Vector2 screenCursor; public static Vector2 screenCursor;
private static Vector3 unprojectedCursor; private static Vector3 unprojectedCursor;