fix: separated angle and rotation in Player

This commit is contained in:
Laureηt 2021-04-13 19:12:18 +02:00
parent b279519b55
commit ec764c56fb
2 changed files with 29 additions and 23 deletions

View file

@ -2,6 +2,8 @@ package sagittarius.model;
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.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
public abstract class BaseActor extends Actor { public abstract class BaseActor extends Actor {
@ -10,7 +12,7 @@ public abstract class BaseActor extends Actor {
protected abstract String getInfo(); protected abstract String getInfo();
protected float angle; protected float angle; // encapsulate ?
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
@ -18,4 +20,16 @@ public abstract class BaseActor extends Actor {
fontDebug.draw(batch, getInfo(), getX(), getY()); fontDebug.draw(batch, getInfo(), getX(), getY());
} }
@Override
protected void drawDebugBounds(ShapeRenderer shapes) {
if (!getDebug()) return;
shapes.set(ShapeType.Line);
if (getStage() != null) shapes.setColor(getStage().getDebugColor());
shapes.rect(getX() - getOriginX(), getY() - getOriginY(),
getOriginX(), getOriginY(),
getWidth(), getHeight(),
getScaleX(), getScaleY(),
getRotation());
}
} }

View file

@ -4,7 +4,6 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
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.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
public class Player extends BaseActor { public class Player extends BaseActor {
@ -18,10 +17,14 @@ public class Player extends BaseActor {
public Player(Planet home, Color color) { public Player(Planet home, Color color) {
super(); super();
this.setSize(100, 50); // TODO: fix this ? (width & height inverted) => use BaseActor.angle
this.setRotation(MathUtils.random(360)); this.setSize(50, 100);
this.setOrigin(getWidth()/2, getHeight()/2);
this.setColor(color); this.setColor(color);
this.setOrigin(0, getHeight()/2);
this.angle = MathUtils.random(360);
this.setRotation(angle-90);
this.home = home; this.home = home;
this.bow = new Bow(this, true); this.bow = new Bow(this, true);
} }
@ -31,36 +34,25 @@ public class Player extends BaseActor {
@Override @Override
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
shapes.line(home.getX(), home.getY(), getX(), getY());
bow.drawDebug(shapes); bow.drawDebug(shapes);
} }
@Override
protected void drawDebugBounds(ShapeRenderer shapes) {
if (!getDebug()) return;
shapes.set(ShapeType.Line);
if (getStage() != null) shapes.setColor(getStage().getDebugColor());
shapes.rect(getX(), getY() - getOriginY(),
getOriginX(), getOriginY(),
getWidth(), getHeight(),
getScaleX(), getScaleY(),
getRotation());
shapes.line(home.getX(), home.getY(), getX(), getY());
}
@Override @Override
public void act(float dt) { public void act(float dt) {
super.act(dt); super.act(dt);
if (Gdx.input.isKeyPressed(Keys.LEFT)) { if (Gdx.input.isKeyPressed(Keys.LEFT)) {
rotateBy( 100.0f / home.getRadius()); this.angle += 100.0f / home.getRadius();
} }
if (Gdx.input.isKeyPressed(Keys.RIGHT)) { if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
rotateBy(- 100.0f / home.getRadius()); this.angle -= 100.0f / home.getRadius();
} }
setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation())); setX(home.getX() + (home.getRadius() + getHeight()/2)*MathUtils.cosDeg(angle));
setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation())); setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle));
this.setRotation(angle-90);
bow.act(dt); bow.act(dt);
} }