Player Collision Rectangle added - Textbox positioning added

master
GammelJAN 5 years ago
parent 17e1ba53c5
commit 5b220f4bd4

@ -13,10 +13,12 @@ public class Main extends Game{
// 0: normal game world, 1: dialogue, 2: fight
public static int gamestate = 0;
public static float CAMERA_WIDTH = 400f;
public static float CAMERA_HEIGHT = 400f;
@Override
public void create () {
screen = new GameScreen(this, 400f, 400f);
screen = new GameScreen(this, CAMERA_WIDTH, CAMERA_HEIGHT);
}
@Override

@ -33,18 +33,22 @@ public class Player extends Actor{
// 0: up, 1: left, 2: down, 3: right
int facing = 0;
Rectangle collisionRect;
public Player(int xPos, int yPos){
setName("player");
t = new Texture(Gdx.files.internal("player.png"));
playerSprite = new AnimatedSprite(t, 64, 64);
playerSprite.setRow(0);
collisionRect = new Rectangle(xPos + 16, yPos, 32, 48);
setBounds(xPos, yPos, playerSprite.getSprite().getWidth(), playerSprite.getSprite().getHeight());
}
@Override
protected void positionChanged() {
playerSprite.setSpritePosition((int)getX(), (int)getY());
collisionRect = new Rectangle(getX() + 16, getY(), 32, 48);
super.positionChanged(); //To change body of generated methods, choose Tools | Templates.
}
@ -71,8 +75,10 @@ public class Player extends Actor{
}
if(Gdx.input.isKeyJustPressed(Input.Keys.E)) {
Main.gamestate = 1;
String[] ans = {"Feier ich", "Ehre", "Hallo"};
getStage().addActor(new Textbox("Dies ist eine coole Test Textbox", ans));
String[] ans = {"Antwort1", "Antwort2"};
getStage().addActor(new Textbox("Frage", ans, getX()+getWidth()/2, getY()+getHeight()/2, Main.CAMERA_WIDTH, Main.CAMERA_HEIGHT));
movementX = 0;
movementY = 0;
}
}
else if(Main.gamestate == 1) {
@ -138,9 +144,9 @@ public class Player extends Actor{
boolean value = false;
for(Actor a : getStage().getActors()){
if(a.getName().equals("mapobject")){
Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight());
//Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight());
Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight());
if(Intersector.overlaps(p, o)){
if(Intersector.overlaps(collisionRect, o)){
value = true;
break;
}

@ -12,6 +12,8 @@ import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import java.util.ArrayList;
@ -28,19 +30,22 @@ public class Textbox extends Actor{
int printChar;
ArrayList<String> splitted;
ShapeRenderer renderer;
int state; // 0: drawing 1: waiting for input 2: finished
int selectedAsw = 0;
String[] ans;
public Textbox(String toPrint, String[] ans) {
public Textbox(String toPrint, String[] ans, float xPos, float yPos, float cameraWidth, float cameraHeight) {
renderer = new ShapeRenderer();
printLine = 0;
printChar = 0;
this.ans = ans;
setName("textbox");
font = new BitmapFont();
r = new Rectangle(50, 50, Gdx.graphics.getWidth() - 100, Gdx.graphics.getHeight()/5);
r = new Rectangle(xPos - cameraWidth/2 + 20, yPos - cameraHeight/2 + 20, cameraWidth - 40, cameraHeight/5);
setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight());
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
@ -105,20 +110,30 @@ public class Textbox extends Actor{
@Override
public void draw(Batch batch, float parentAlpha) {
font.setColor(Color.BLACK);
batch.end();
renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(Color.BLUE);
renderer.rect(getX(), getY(), getWidth(), getHeight());
renderer.end();
renderer.begin(ShapeRenderer.ShapeType.Line);
renderer.setColor(Color.RED);
renderer.rect(getX(), getY(), getWidth(), getHeight());
renderer.end();
batch.begin();
if(state == 0){
for(int i = 0; i < splitted.size(); i++){
if(i == printLine){
font.draw(batch, splitted.get(i).substring(0, printChar), 0, getX() + getHeight()-i*1.2f*getTextHeight("A"));
font.draw(batch, splitted.get(i).substring(0, printChar), getX(), getY() + getHeight()-i*1.2f*getTextHeight("A"));
}
else if(i < printLine){
font.draw(batch, splitted.get(i), 0, getX() + getHeight()-i*1.2f*getTextHeight("A"));
font.draw(batch, splitted.get(i), getX(), getY() + getHeight()-i*1.2f*getTextHeight("A"));
}
}
}
else{
for(int i = 0; i <= printLine; i++){
font.draw(batch, splitted.get(i), 0, getX() + getHeight()-i*1.2f*getTextHeight("A"));
font.draw(batch, splitted.get(i), getX(), getY() + getHeight()-i*1.2f*getTextHeight("A"));
}
for(int i = 0; i < ans.length; i++) {

Loading…
Cancel
Save