Jan Ehehalt 6 years ago
parent 02b858201f
commit 3ada61b494

@ -210,6 +210,11 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
else if(ws != null) ws.render(batch);
else if(le != null){
le.render(batch);
if(le.getToSave()){
le.dispose();
le = null;
ts = new Titlescreen(GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT);
}
}
batch.end();
}

@ -19,17 +19,15 @@ public class Button {
private int width;
private int height;
private int id;
private int listId;
private Rectangle rect;
public Button(String text, int xPos, int yPos, int width, int height, int id, int listId){
public Button(String text, int xPos, int yPos, int width, int height, int id){
this.text = text;
this.xPos = xPos;
this.yPos = yPos;
this.width = width;
this.height = height;
this.id = id; //0: pivot, 1: goal, 2: rect, 3: addRect
this.listId = listId;
this.id = id; //0: pivot, 1: goal, 2: rect, 3: addRect, 4: save
rect = new Rectangle(xPos, yPos, width, height);
}
@ -117,8 +115,5 @@ public class Button {
public int getId(){
return id;
}
public int getListId(){
return listId;
}
}

@ -36,44 +36,51 @@ import model.Projectile;
* @author Jan
*/
public class Leveleditor{
ShapeRenderer shapeRenderer;
float GAME_WORLD_WIDTH;
float GAME_WORLD_HEIGHT;
// SHAPE RENDERER
ShapeRenderer shapeRenderer;
// BUTTONS
ArrayList<Button> buttons;
float mouseX;
float mouseY;
int listNumber;
int selectedRect;
// VARIABLES
boolean goalSet;
boolean pivotSet;
boolean toSave;
int state; //-1: nothig selected, 0: place pivot, 2: pivot direction, 3: goal, 4: obstacles
// BITMAP FONT
BitmapFont font;
// LEVEL TO BE EDITED AND SAVED
Level level;
public Leveleditor(float width, float height, Matrix4 matrix){
GAME_WORLD_WIDTH = width;
GAME_WORLD_HEIGHT = height;
// CREATING LEVEL, TO BE EDITED AND SAVED
level = new Level(new Goal(0, 0, 200, 150, 0.2f), new Projectile(0, 0, 0), 0, 0);
// VARIABLES
state = -1;
toSave = false;
// SHAPE RENDERER
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(matrix);
// CREATE DEFAULT BUTTONS
buttons = new ArrayList();
buttons.add(new Button("Pivot", (int)(GAME_WORLD_WIDTH * 0.02),(int) (GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 0, listNumber));
listNumber++;
buttons.add(new Button("Goal", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 1, listNumber));
listNumber++;
buttons.add(new Button("add Rect", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 2, listNumber));
listNumber++;
buttons.add(new Button("SAVE", (int)(GAME_WORLD_WIDTH * 0.8), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 4, listNumber));
listNumber++;
buttons.add(new Button("Pivot", (int)(GAME_WORLD_WIDTH * 0.02),(int) (GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 0));
buttons.add(new Button("Goal", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 1));
buttons.add(new Button("new Obstacle", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 2));
buttons.add(new Button("SAVE", (int)(GAME_WORLD_WIDTH * 0.8), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 4));
// CREATE BITMAP FONT
font = new BitmapFont();
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
@ -91,41 +98,53 @@ public class Leveleditor{
batch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.rectLine((float) level.getPivotX(), (float) level.getPivotY(), (float) level.getProjectile().getxPos(), (float) level.getProjectile().getyPos(), 3);
shapeRenderer.setColor(Color.GRAY);
shapeRenderer.circle(level.getPivotX(), level.getPivotY(), 5);
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.circle(level.getPivotX(), level.getPivotY() + 150, level.getProjectile().getRadius());
// DRAW PIVOT
if(pivotSet) {
shapeRenderer.rectLine((float) level.getPivotX(), (float) level.getPivotY(), (float) level.getProjectile().getxPos(), (float) level.getProjectile().getyPos(), 3);
shapeRenderer.setColor(Color.GRAY);
shapeRenderer.circle(level.getPivotX(), level.getPivotY(), 5);
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.circle(level.getPivotX(), level.getPivotY() + 150, level.getProjectile().getRadius());
}
// DRAW BUTTONS
if(state == -1){
for(Button button : buttons){
shapeRenderer.rectLine(button.getxPos(), button.getyPos(), button.getxPos() + button.getWidth(), button.getyPos(), 4);
shapeRenderer.rectLine(button.getxPos(), button.getyPos(), button.getxPos(), button.getyPos() + button.getHeight(), 4);
shapeRenderer.rectLine(button.getxPos(), button.getyPos()+button.getHeight(), button.getxPos()+button.getWidth(), button.getyPos() + button.getHeight(), 4);
shapeRenderer.rectLine(button.getxPos() + button.getWidth(), button.getyPos(),button.getxPos() + button.getWidth(), button.getyPos() + button.getHeight(), 4);
shapeRenderer.end();
batch.begin();
font.getData().setScale(1.5f);
font.draw(batch, button.getText(),button.getxPos() + button.getWidth()/2 - getTextWidth(button.getText())/2, button.getyPos() + button.getHeight()/2 + getTextHeight(button.getText())/2);
batch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
if(button.getId() == 4 && (!goalSet || !pivotSet)){
}
else{
shapeRenderer.rectLine(button.getxPos(), button.getyPos(), button.getxPos() + button.getWidth(), button.getyPos(), 4);
shapeRenderer.rectLine(button.getxPos(), button.getyPos(), button.getxPos(), button.getyPos() + button.getHeight(), 4);
shapeRenderer.rectLine(button.getxPos(), button.getyPos()+button.getHeight(), button.getxPos()+button.getWidth(), button.getyPos() + button.getHeight(), 4);
shapeRenderer.rectLine(button.getxPos() + button.getWidth(), button.getyPos(),button.getxPos() + button.getWidth(), button.getyPos() + button.getHeight(), 4);
shapeRenderer.end();
batch.begin();
font.getData().setScale(1.5f);
font.draw(batch, button.getText(),button.getxPos() + button.getWidth()/2 - getTextWidth(button.getText())/2, button.getyPos() + button.getHeight()/2 + getTextHeight(button.getText())/2);
batch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
}
}
}
// DRAW OBSTACLES
for(Rectangle rect : level.getObjects()){
shapeRenderer.rect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
// DRAW GOAL
shapeRenderer.setColor(Color.BLACK);
float x = level.getGoal().getxPos();
float y = level.getGoal().getyPos();
float w = level.getGoal().getSizeX();
float h = level.getGoal().getSizeY();
float th = level.getGoal().getThickness();
shapeRenderer.rect(x, y,th * w, h);
shapeRenderer.rect(x + th * w,y, th*3 * w,th * h);
shapeRenderer.rect(x + th *w + th*3 * w, y, th * w,h);
if(goalSet) {
float x = level.getGoal().getxPos();
float y = level.getGoal().getyPos();
float w = level.getGoal().getSizeX();
float h = level.getGoal().getSizeY();
float th = level.getGoal().getThickness();
shapeRenderer.rect(x, y, th * w, h);
shapeRenderer.rect(x + th * w, y, th * 3 * w, th * h);
shapeRenderer.rect(x + th * w + th * 3 * w, y, th * w, h);
}
shapeRenderer.end();
batch.begin();
@ -133,61 +152,54 @@ public class Leveleditor{
}
public void dispose() {
}
public void setMousePos(float x, float y){
mouseX = x;
mouseY = y;
}
public void setGoal(){}
public void touchDown(int x, int y){
switch(state){
case -1:
case -1: // NO BUTTON SELECTED
Rectangle mouse = new Rectangle(x, (int)GAME_WORLD_HEIGHT -y, 1, 1);
for(Button button : buttons){
if(Intersector.overlaps(mouse, button.getRectangle())){
state = button.getId();
if(button.getId() == 3){
selectedRect = button.getListId();
if(button.getId() == 4 && (!goalSet || !pivotSet)){ // DONT SAVE WHEN GOAL OR PIVOT NOT SET
}
else if(button.getId() == 4){ // SAVE IF GOAL AND PIVOT SET
save();
}
else { // NOT SAVE -> STATE = BUTTONS ID
state = button.getId();
}
break;
}
}
for(Rectangle rect : level.getObjects()){
for(Rectangle rect : level.getObjects()){ // DELETE RECTANGLE IF PRESSED
if(Intersector.overlaps(mouse, rect)){
level.removeObstacle(rect);
break;
}
}
break;
case 0:
case 0: // SET PIVOT
level.setPivot(x, (int)GAME_WORLD_HEIGHT - y);
level.getProjectile().setxPos(x);
level.getProjectile().setyPos((int) GAME_WORLD_HEIGHT - y + 150);
pivotSet = true;
state = -1;
break;
case 1:
case 1: // SET GOAL
level.setGoal(x, (int)GAME_WORLD_HEIGHT-y);
state = -1;
goalSet = true;
break;
case 2:
case 2: // NEW OBSTACLE
level.addRectangle(x,(int)GAME_WORLD_HEIGHT-y,200,100);
state = -1;
break;
case 3:
state = -1;
break;
case 4:
Json json = new Json();
FileHandle file = Gdx.files.local("levels/level2.json");
file.writeString(json.toJson(level), false);
default:
break;
}
}
public float getTextWidth(String text){
GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font,text);
@ -198,5 +210,16 @@ public class Leveleditor{
glyphLayout.setText(font,text);
return glyphLayout.height;
}
public boolean getToSave(){
return toSave;
}
public void save(){
Json json = new Json();
FileHandle file = Gdx.files.local("levels/level2.json");
file.writeString(json.toJson(level), false);
toSave = true;
}
}

@ -1 +1 @@
{goal:{xPos:560,yPos:355,sizeX:300,sizeY:200,thickness:0.2},projectile:{xPos:306,yPos:707,mass:5,radius:10},math:{},xPosPivot:306,yPosPivot:557,isTraceInitialised:[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],traces:[{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10}],objects:[]}
{goal:{xPos:764,yPos:492,sizeX:300,sizeY:200,thickness:0.2},projectile:{xPos:401,yPos:501,mass:5,radius:10},math:{},xPosPivot:401,yPosPivot:351,isTraceInitialised:[false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false],traces:[{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10},{x:-10,y:-10}],objects:[{x:356,y:468,width:200,height:100}]}
Loading…
Cancel
Save