Merge branch 'master' of git.inpt.fr:tobgang/sagittarius

This commit is contained in:
Laureηt 2021-04-13 20:50:32 +02:00
commit 1cd41cc5e3
5 changed files with 214 additions and 63 deletions

View file

@ -3,13 +3,15 @@ package sagittarius;
import com.badlogic.gdx.Game; import com.badlogic.gdx.Game;
import sagittarius.view.BaseScreen; import sagittarius.view.BaseScreen;
import sagittarius.view.StartScreen; import sagittarius.view.GameScreen;
public class SagittariusGame extends Game { public class SagittariusGame extends Game {
// Constants // Constants
public static final int G = 100; public static final int G = 100;
// Game
public static GameScreen gameScreen;
private static SagittariusGame game; private static SagittariusGame game;
public SagittariusGame() { public SagittariusGame() {
@ -26,6 +28,7 @@ public class SagittariusGame extends Game {
@Override @Override
public void create() { public void create() {
this.setScreen(new StartScreen()); gameScreen = new GameScreen();
this.setScreen(gameScreen);
} }
} }

View file

@ -2,6 +2,8 @@ package sagittarius.model;
import java.util.ArrayList; 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;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
@ -11,7 +13,9 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.SagittariusGame; import sagittarius.SagittariusGame;
import sagittarius.view.GameScreen; import sagittarius.view.GameScreen;
public class Arrow extends BaseActor { public class Arrow extends Actor {
private static final float initTTL = 20;
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
@ -19,7 +23,10 @@ public class Arrow extends BaseActor {
private Vector2 acceleration; private Vector2 acceleration;
private Vector2 force; private Vector2 force;
private float TTL = 20; private float TTL = initTTL;
private boolean landed;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
@ -38,6 +45,7 @@ public class Arrow extends BaseActor {
this.setOrigin(80, 2); this.setOrigin(80, 2);
this.setSize(100, 4); this.setSize(100, 4);
this.force = computeForce(); this.force = computeForce();
this.landed = false;
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -45,6 +53,7 @@ public class Arrow extends BaseActor {
@Override @Override
public void act(float dt) { public void act(float dt) {
if (!landed) {
integrationVerlet(dt); integrationVerlet(dt);
this.TTL -= dt; this.TTL -= dt;
this.setRotation(this.velocity.angleDeg()); this.setRotation(this.velocity.angleDeg());
@ -52,9 +61,21 @@ public class Arrow extends BaseActor {
if (TTL <= 0) { if (TTL <= 0) {
GameScreen.arrows.removeActor(this); GameScreen.arrows.removeActor(this);
} }
}
} }
@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 @Override
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
@ -115,6 +136,33 @@ public class Arrow extends BaseActor {
this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ), this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ),
dt * ( this.velocity.y + dt * this.acceleration.y / 2 )); dt * ( this.velocity.y + dt * this.acceleration.y / 2 ));
for (Actor actor : GameScreen.attractors.getChildren()) {
float dx = actor.getX() - this.getX();
float dy = actor.getY() - this.getY();
float len2 = dx*dx + dy*dy;
if (len2 < ((Planet) actor).getRadius()*((Planet) actor).getRadius()) {
landed = true;
break;
}
}
if (TTL < initTTL - 0.5) {
for (Actor actor : GameScreen.players.getChildren()) {
float dx = actor.getX() - this.getX();
float dy = actor.getY() - this.getY();
float len2 = dx*dx + dy*dy;
if (len2 < 200) {
SagittariusGame.gameScreen.removePlayer((Player) actor);
}
}
}
this.force = computeForce(); this.force = computeForce();
this.velocity.x += dt * ( this.acceleration.x + this.force.x ) / 2; this.velocity.x += dt * ( this.acceleration.x + this.force.x ) / 2;
@ -154,16 +202,4 @@ public class Arrow extends BaseActor {
return new Vector2( getX(), getY() ); 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

@ -9,7 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.view.GameScreen; import sagittarius.view.GameScreen;
class Bow extends Actor { public class Bow extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
@ -23,10 +23,13 @@ class Bow extends Actor {
private float power; private float power;
private boolean playing;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
Bow(Player shooter, boolean aimAssist) { Bow(Player shooter, boolean aimAssist) {
super(); super();
this.playing = false;
this.shooter = shooter; this.shooter = shooter;
this.aimAssist = aimAssist; this.aimAssist = aimAssist;
} }
@ -35,7 +38,7 @@ class Bow extends Actor {
@Override @Override
public void act(float dt) { public void act(float dt) {
if (playing) {
// TODO: probably can do better with an eventListener // TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) { if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = GameScreen.worldCursor.cpy(); this.anchor = GameScreen.worldCursor.cpy();
@ -43,8 +46,11 @@ class Bow extends Actor {
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) { } else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow(); computeArrow();
} else if (pressed) { } else if (pressed) {
// Sagittarius.arrowList.add(getArrow());
GameScreen.arrows.addActor(getArrow()); GameScreen.arrows.addActor(getArrow());
pressed = false; pressed = false;
playing = false;
}
} }
} }
@ -52,7 +58,7 @@ class Bow extends Actor {
@Override @Override
public void drawDebug(ShapeRenderer shapes) { public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes); super.drawDebug(shapes);
if (pressed) { if (pressed && playing) {
shapes.line(this.anchor, GameScreen.worldCursor); shapes.line(this.anchor, GameScreen.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);
@ -82,4 +88,28 @@ class Bow extends Actor {
return new Arrow(angle, power, shooter); return new Arrow(angle, power, shooter);
} }
/**
* Indiquer que c'est a son tour de jouer.
*/
public void startPlaying() {
playing = true;
}
/**
* Indique que ce n'est plus son tour de jouer.
*/
public void stopPlaying() {
playing = false;
}
/**
* Savoir si il est entrain de jouer.
*/
public boolean isPlaying() {
return playing;
}
} }

View file

@ -3,72 +3,120 @@ package sagittarius.model;
import com.badlogic.gdx.Gdx; 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.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.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class Player extends BaseActor { public class Player extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
private Planet home; private Planet home;
private Bow bow; private Bow bow;
private boolean playing;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
public Player(Planet home, Color color) { public Player(Planet home, Color color) {
super(); super();
this.setSize(100, 50); // TODO: fix this ? (width & height inverted)
this.setSize(50, 100); this.setRotation(MathUtils.random(360));
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);
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, "pos=" + (int)getX() + "," + (int)getY() , getX(), getY());
}
@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 (playing) {
if (Gdx.input.isKeyPressed(Keys.LEFT)) { if (Gdx.input.isKeyPressed(Keys.LEFT)) {
this.angle += 100.0f / home.getRadius(); rotateBy( 100.0f / home.getRadius());
} }
if (Gdx.input.isKeyPressed(Keys.RIGHT)) { if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
this.angle -= 100.0f / home.getRadius(); rotateBy(- 100.0f / home.getRadius());
}
} }
setX(home.getX() + (home.getRadius() + getHeight()/2)*MathUtils.cosDeg(angle)); setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation()));
setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle)); setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation()));
this.setRotation(angle-90);
bow.act(dt); bow.act(dt);
} }
@Override
protected String getInfo() {
return
"pos=" + (int)getX() + "," + (int)getY()
;
}
/** /**
* @return the Player's home Planet. * @return the Player's home Planet.
*/ */
Planet getHome() { public Planet getHome() {
return this.home; return this.home;
} }
/**
* @return the player's bow.
*/
public Bow getBow() {
return bow;
}
/**
* Indiquer que c'est son tour de jouer.
*/
public void startPlaying() {
bow.startPlaying();
playing = true;
}
/**
* Indique la fin de son tour de jeu.
*/
public void stopPlaying() {
bow.stopPlaying();
playing = false;
}
/**
* Savoir si il est entrain de jouer.
*/
public boolean isPlaying() {
return playing;
}
} }

View file

@ -4,6 +4,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
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.Actor;
import com.badlogic.gdx.scenes.scene2d.Group; import com.badlogic.gdx.scenes.scene2d.Group;
import sagittarius.model.*; import sagittarius.model.*;
@ -20,6 +21,9 @@ public class GameScreen extends BaseScreen {
// Groups // TODO: move this in SagittariusGame ? // Groups // TODO: move this in SagittariusGame ?
public static Group attractors; public static Group attractors;
public static Group arrows; public static Group arrows;
public static Group players;
private int playerTurn;
// ---------- METHODs ---------- // ---------- METHODs ----------
@ -41,11 +45,17 @@ public class GameScreen extends BaseScreen {
mainStage.addActor(attractors); mainStage.addActor(attractors);
// players // players
players = new Group();
Player player1 = new Player(planet1, Color.WHITE); Player player1 = new Player(planet1, Color.WHITE);
mainStage.addActor(player1); players.addActor(player1);
Player player2 = new Player(planet2, Color.WHITE); Player player2 = new Player(planet2, Color.WHITE);
mainStage.addActor(player2); players.addActor(player2);
mainStage.addActor(players);
playerTurn = 0;
player1.startPlaying();
// arrows // arrows
arrows = new Group(); arrows = new Group();
@ -59,7 +69,6 @@ public class GameScreen extends BaseScreen {
uiStage.addActor(mouseInfo); uiStage.addActor(mouseInfo);
mainStage.setDebugAll(true); // TODO: disable later mainStage.setDebugAll(true); // TODO: disable later
uiStage.setDebugAll(true); // TODO: disable later
} }
@ -70,6 +79,31 @@ public class GameScreen extends BaseScreen {
unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0)); unprojectedCursor = mainStage.getCamera().unproject(new Vector3(screenCursor, 0));
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y); worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
Player actualPlayer = (Player) players.getChild(playerTurn);
if (actualPlayer.isPlaying() && !actualPlayer.getBow().isPlaying()) {
actualPlayer.stopPlaying();
playerTurn++;
playerTurn %= players.getChildren().size;
actualPlayer = (Player) players.getChild(playerTurn);
actualPlayer.startPlaying();
} }
} }
public void removePlayer(Player player) {
int index = players.getChildren().indexOf(player, true);
if (index < playerTurn) {
players.removeActor(player);
playerTurn++;
playerTurn %= players.getChildren().size;
} else if (index == playerTurn) {
players.removeActor(player);
playerTurn %= players.getChildren().size;
Player actualPlayer = (Player) players.getChild(playerTurn);
actualPlayer.startPlaying();
} else {
players.removeActor(player);
}
}
}