Ca marche ?

This commit is contained in:
Damien 2021-04-13 19:45:49 +02:00
parent 41a997b1d0
commit 2c5b66eb65
5 changed files with 214 additions and 63 deletions

View file

@ -3,13 +3,15 @@ package sagittarius;
import com.badlogic.gdx.Game;
import sagittarius.view.BaseScreen;
import sagittarius.view.StartScreen;
import sagittarius.view.GameScreen;
public class SagittariusGame extends Game {
// Constants
public static final int G = 100;
// Game
public static GameScreen gameScreen;
private static SagittariusGame game;
public SagittariusGame() {
@ -26,6 +28,7 @@ public class SagittariusGame extends Game {
@Override
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 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;
@ -11,7 +13,9 @@ import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.SagittariusGame;
import sagittarius.view.GameScreen;
public class Arrow extends BaseActor {
public class Arrow extends Actor {
private static final float initTTL = 20;
// ---------- ATTRIBUTEs ----------
@ -19,7 +23,10 @@ public class Arrow extends BaseActor {
private Vector2 acceleration;
private Vector2 force;
private float TTL = 20;
private float TTL = initTTL;
private boolean landed;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
@ -39,6 +46,7 @@ public class Arrow extends BaseActor {
this.setOrigin(80, 2);
this.setSize(100, 4);
this.force = computeForce();
this.landed = false;
}
// ---------- METHODs ----------
@ -46,16 +54,29 @@ public class Arrow extends BaseActor {
@Override
public void act(float dt) {
integrationVerlet(dt);
this.TTL -= dt;
this.setRotation(this.velocity.angleDeg());
if (!landed) {
integrationVerlet(dt);
this.TTL -= dt;
this.setRotation(this.velocity.angleDeg());
if (TTL <= 0) {
GameScreen.arrows.removeActor(this);
if (TTL <= 0) {
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
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
@ -116,6 +137,33 @@ public class Arrow extends BaseActor {
this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 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.velocity.x += dt * ( this.acceleration.x + this.force.x ) / 2;
@ -155,16 +203,4 @@ public class Arrow extends BaseActor {
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;
class Bow extends Actor {
public class Bow extends Actor {
// ---------- ATTRIBUTEs ----------
@ -23,10 +23,13 @@ class Bow extends Actor {
private float power;
private boolean playing;
// ---------- CONSTRUCTORs ----------
Bow(Player shooter, boolean aimAssist) {
super();
this.playing = false;
this.shooter = shooter;
this.aimAssist = aimAssist;
}
@ -35,16 +38,19 @@ class Bow extends Actor {
@Override
public void act(float dt) {
// TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = GameScreen.worldCursor.cpy();
pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow();
} else if (pressed) {
GameScreen.arrows.addActor(getArrow());
pressed = false;
if (playing) {
// TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = GameScreen.worldCursor.cpy();
pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow();
} else if (pressed) {
// Sagittarius.arrowList.add(getArrow());
GameScreen.arrows.addActor(getArrow());
pressed = false;
playing = false;
}
}
}
@ -52,7 +58,7 @@ class Bow extends Actor {
@Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
if (pressed) {
if (pressed && playing) {
shapes.line(this.anchor, GameScreen.worldCursor);
if (aimAssist) {
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
@ -82,4 +88,28 @@ class Bow extends Actor {
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.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 BaseActor {
public class Player extends Actor {
// ---------- ATTRIBUTEs ----------
private Planet home;
private Bow bow;
private boolean playing;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
public Player(Planet home, Color color) {
super();
this.setSize(50, 100);
this.setOrigin(getWidth()/2, getHeight()/2);
this.setSize(100, 50); // TODO: fix this ? (width & height inverted)
this.setRotation(MathUtils.random(360));
this.setColor(color);
this.angle = MathUtils.random(360);
this.setRotation(angle-90);
this.setOrigin(0, getHeight()/2);
this.home = home;
this.bow = new Bow(this, true);
}
// ---------- 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);
shapes.line(home.getX(), home.getY(), getX(), getY());
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
public void act(float dt) {
super.act(dt);
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
this.angle += 100.0f / home.getRadius();
}
if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
this.angle -= 100.0f / home.getRadius();
if (playing) {
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
rotateBy( 100.0f / home.getRadius());
}
if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
rotateBy(- 100.0f / home.getRadius());
}
}
setX(home.getX() + (home.getRadius() + getHeight()/2)*MathUtils.cosDeg(angle));
setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle));
this.setRotation(angle-90);
setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation()));
setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation()));
bow.act(dt);
}
@Override
protected String getInfo() {
return
"pos=" + (int)getX() + "," + (int)getY()
;
}
/**
* @return the Player's home Planet.
*/
Planet getHome() {
public Planet getHome() {
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.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import sagittarius.model.*;
@ -20,6 +21,9 @@ public class GameScreen extends BaseScreen {
// Groups // TODO: move this in SagittariusGame ?
public static Group attractors;
public static Group arrows;
public static Group players;
private int playerTurn;
// ---------- METHODs ----------
@ -41,11 +45,17 @@ public class GameScreen extends BaseScreen {
mainStage.addActor(attractors);
// players
players = new Group();
Player player1 = new Player(planet1, Color.WHITE);
mainStage.addActor(player1);
players.addActor(player1);
Player player2 = new Player(planet2, Color.WHITE);
mainStage.addActor(player2);
players.addActor(player2);
mainStage.addActor(players);
playerTurn = 0;
player1.startPlaying();
// arrows
arrows = new Group();
@ -59,7 +69,6 @@ public class GameScreen extends BaseScreen {
uiStage.addActor(mouseInfo);
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));
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);
}
}
}