Jan Ehehalt 6 years ago
parent 6e12c88978
commit b1a8ee5522

@ -17,6 +17,8 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import java.io.File;
@ -80,7 +82,7 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
float aspectRatio = (float)Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
camera = new OrthographicCamera();
viewport = new ExtendViewport(GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT * aspectRatio, camera);
viewport = new StretchViewport(GAME_WORLD_WIDTH/* *aspectRatio*/, GAME_WORLD_HEIGHT, camera);
viewport.apply();
camera.position.set(GAME_WORLD_WIDTH/2, GAME_WORLD_HEIGHT/2, 0);
@ -267,8 +269,10 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
@Override
public boolean touchDown(int x, int y, int i2, int i3) {
x = (int)((float)x / (float)Gdx.graphics.getWidth() * (float)GAME_WORLD_WIDTH);
y = (int)GAME_WORLD_HEIGHT-(int)((float)y / Gdx.graphics.getHeight() * GAME_WORLD_HEIGHT);
if(ts != null){
if(x > 0.05 * Gdx.graphics.getWidth()){
if(x > 0.05 * GAME_WORLD_WIDTH){
ts.dispose();
ts = null;
//ls = new Levelscreen(beatenLevel, GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
@ -281,18 +285,26 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
}
}
else if(ls != null){
if(x < Gdx.graphics.getWidth() * 0.15){
if(ls.getSelectedLevel() > 0)ls.setSelectedLevel(ls.getSelectedLevel()-1);
}
else if(x > Gdx.graphics.getWidth() * 0.85){
if(ls.getSelectedLevel() < beatenLevel && beatenLevel <= levelAmount)
ls.setSelectedLevel(ls.getSelectedLevel()+1);
}
else{
ls.dispose();
ls = null;
gs = new Gamescreen(level.get(currentChapter).get(currentLevel), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
stepTimer.start();
int n = ls.touchDown(x,y);
switch(n){
case 0:
ls.dispose();
ls = null;
cs = new Chapterscreen(5,GAME_WORLD_WIDTH,GAME_WORLD_HEIGHT,camera.combined);
break;
case 1:
if(ls.getSelectedLevel() > 0)ls.setSelectedLevel(ls.getSelectedLevel()-1);
break;
case 2:
if(ls.getSelectedLevel() < beatenLevel && beatenLevel <= levelAmount)
ls.setSelectedLevel(ls.getSelectedLevel()+1);
break;
case 3:
ls.dispose();
ls = null;
gs = new Gamescreen(level.get(currentChapter).get(currentLevel), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
stepTimer.start();
break;
}
}
else if(gs != null){
@ -301,11 +313,11 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
}
}
else if(ws != null){
if(x < Gdx.graphics.getWidth() * 0.33){
if(x < GAME_WORLD_WIDTH * 0.33){
ls = new Levelscreen(beatenLevel, GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
ws = null;
}
else if(x < Gdx.graphics.getWidth() * 0.66){
else if(x < GAME_WORLD_WIDTH * 0.66){
gs = new Gamescreen(level.get(currentChapter).get(currentLevel), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
stepTimer.start();
ws = null;
@ -325,35 +337,10 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
if(cs.touchDown(x,y) == -1){
}
else if (cs.touchDown(x,y) == 0){
currentChapter = 0;
cs = null;
ls = new Levelscreen(level.get(currentChapter).size(), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else if (cs.touchDown(x,y) == 1){
currentChapter = 1;
cs = null;
ls = new Levelscreen(level.get(currentChapter).size(), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else if (cs.touchDown(x,y) == 2){
currentChapter = 2;
cs = null;
ls = new Levelscreen(level.get(currentChapter).size(), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else if (cs.touchDown(x,y) == 3){
currentChapter = 3;
cs = null;
ls = new Levelscreen(level.get(currentChapter).size(), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else if (cs.touchDown(x,y) == 4){
currentChapter = 4;
cs = null;
ls = new Levelscreen(level.get(currentChapter).size(), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else if (cs.touchDown(x,y) == 5){
currentChapter = 5;
else if (cs.touchDown(x,y) >= 0 && cs.touchDown(x,y) <= 5){
currentChapter = cs.touchDown(x,y);
cs = null;
ls = new Levelscreen(level.get(currentChapter).size(), GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
ls = new Levelscreen(level.get(currentChapter).size()-1, GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else if(cs.touchDown(x,y) == 6){
cs.dispose();

@ -58,7 +58,7 @@ public class Chapterscreen{
buttons.add(new Button("Chapter 4", (int)(0.05 * GAME_WORLD_WIDTH - 0.5 * 3/80 * GAME_WORLD_WIDTH), (int)(0.3 * GAME_WORLD_HEIGHT), (int)((1f/3f) * 0.8 *GAME_WORLD_WIDTH), (int)(0.2 * GAME_WORLD_HEIGHT), 4));
buttons.add(new Button("Chapter 5", (int)(0.1 * GAME_WORLD_WIDTH + buttons.get(0).getWidth() - 0.5 * 3/80 * GAME_WORLD_WIDTH), (int)(0.3 * GAME_WORLD_HEIGHT), (int)((1f/3f) * 0.8 *GAME_WORLD_WIDTH), (int)(0.2 * GAME_WORLD_HEIGHT), 5));
buttons.add(new Button("User Level",(int)(0.15 * GAME_WORLD_WIDTH + buttons.get(0).getWidth()*2 - 0.5 * 3/80 * GAME_WORLD_WIDTH), (int)(0.3 * GAME_WORLD_HEIGHT), (int)((1f/3f) * 0.8 *GAME_WORLD_WIDTH), (int)(0.2 * GAME_WORLD_HEIGHT), 0));
buttons.add(new Button("back", (int)(0.05 * GAME_WORLD_WIDTH), (int)(0.02 * GAME_WORLD_HEIGHT), (int)(0.2 * GAME_WORLD_WIDTH), (int)(0.1 * GAME_WORLD_HEIGHT), 6));
buttons.add(new Button("< back", 20,10,200,80,6));
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
@ -100,7 +100,7 @@ public class Chapterscreen{
public int touchDown(int x, int y){
Rectangle mouse = new Rectangle(x, GAME_WORLD_HEIGHT - y, 1, 1);
Rectangle mouse = new Rectangle(x, y, 1, 1);
for(Button button : buttons){
if(Intersector.overlaps(mouse, button.getRectangle())){
return button.getId();

@ -75,10 +75,10 @@ public class Leveleditor{
// 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));
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));
buttons.add(new Button("Pivot", (int)(GAME_WORLD_WIDTH * 0.02),(int) (GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.1 * GAME_WORLD_HEIGHT), 250, 80, 0));
buttons.add(new Button("Goal", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.1 * GAME_WORLD_HEIGHT), 250, 80, 1));
buttons.add(new Button("new Obstacle", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.1 * GAME_WORLD_HEIGHT), 250, 80, 2));
buttons.add(new Button("SAVE", (int)(GAME_WORLD_WIDTH * 0.8), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.1 * GAME_WORLD_HEIGHT), 250, 80, 4));
// CREATE BITMAP FONT
font = new BitmapFont();
@ -100,6 +100,9 @@ public class Leveleditor{
shapeRenderer.setColor(Color.BLACK);
// DRAW PIVOT
if(pivotSet) {
shapeRenderer.setColor(Color.LIGHT_GRAY);
shapeRenderer.circle(level.getPivotX(), level.getPivotY(), 150);
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);
@ -120,8 +123,9 @@ public class Leveleditor{
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.getData().setScale(1.6f);
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);
}
@ -158,7 +162,7 @@ public class Leveleditor{
public void touchDown(int x, int y){
switch(state){
case -1: // NO BUTTON SELECTED
Rectangle mouse = new Rectangle(x, (int)GAME_WORLD_HEIGHT -y, 1, 1);
Rectangle mouse = new Rectangle(x, y, 1, 1);
for(Button button : buttons){
if(Intersector.overlaps(mouse, button.getRectangle())){
if(button.getId() == 4 && (!goalSet || !pivotSet)){ // DONT SAVE WHEN GOAL OR PIVOT NOT SET
@ -180,19 +184,19 @@ public class Leveleditor{
}
break;
case 0: // SET PIVOT
level.setPivot(x, (int)GAME_WORLD_HEIGHT - y);
level.setPivot(x, y);
level.getProjectile().setxPos(x);
level.getProjectile().setyPos((int) GAME_WORLD_HEIGHT - y + 150);
level.getProjectile().setyPos(y + 150);
pivotSet = true;
state = -1;
break;
case 1: // SET GOAL
level.setGoal(x, (int)GAME_WORLD_HEIGHT-y);
level.setGoal(x, y);
state = -1;
goalSet = true;
break;
case 2: // NEW OBSTACLE
level.addRectangle(x,(int)GAME_WORLD_HEIGHT-y,200,100);
level.addRectangle(x - 100,y - 50,200,100);
state = -1;
break;
default:

@ -18,9 +18,14 @@ import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Timer;
import java.util.ArrayList;
import model.Button;
import model.Goal;
import model.Level;
import model.Projectile;
@ -31,44 +36,65 @@ import model.Projectile;
*/
public class Levelscreen{
float GAME_WORLD_WIDTH;
float GAME_WORLD_HEIGHT;
// SPRITES LEFT-RIGHT
Sprite buttonRight;
Sprite buttonLeft;
int selectedLevel;
BitmapFont font;
Timer t;
boolean movement;
// VARIABLES
int selectedLevel;
int levelAmount;
float GAME_WORLD_WIDTH;
float GAME_WORLD_HEIGHT;
// SHAPE RENDERER
ShapeRenderer shapeRenderer;
// BUTTONS
ArrayList<Button> buttons;
// BITMAP FONT
BitmapFont font;
// TIMER FOR SPRITE ANIMATION
Timer t;
public Levelscreen(int levelAmount, float width, float height, Matrix4 matrix){
GAME_WORLD_WIDTH = width;
GAME_WORLD_HEIGHT = height;
// SPRITES LEFT-RIGHT
buttonRight = new Sprite(new Texture("buttonRight.png"));
buttonRight.setY(GAME_WORLD_HEIGHT/ 2 - buttonRight.getHeight() / 2);
buttonRight.setX(GAME_WORLD_WIDTH - 10 - buttonRight.getWidth());
buttonLeft = new Sprite(new Texture("buttonLeft.png"));
buttonLeft.setY(GAME_WORLD_HEIGHT / 2 - buttonLeft.getHeight() / 2);
buttonLeft.setX(10);
// VARIABLES
selectedLevel = 0;
this.levelAmount = levelAmount;
// SHAPE RENDERER
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(matrix);
// BUTTONS
buttons = new ArrayList<>();
buttons.add(new Button("< back", 20,10,200,80,0));
// BITMAP FONT
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 21;
font = generator.generateFont(parameter);
generator.dispose();
font.setColor(Color.BLACK);
// TIMER FOR SPRITES ANIMATION
t = new Timer();
t.scheduleTask(new Timer.Task() {
@Override
public void run() {
@ -91,13 +117,16 @@ public class Levelscreen{
}
public void render(SpriteBatch batch, Level level) {
// LEFT - RIGHT BUTTON
if(selectedLevel > 0){
buttonLeft.draw(batch);
}
if(selectedLevel < levelAmount){
buttonRight.draw(batch);
}
// LEVEL NAME
font.getData().setScale(6);
font.draw(batch,"LEVEL: "+ (selectedLevel + 1), GAME_WORLD_WIDTH / 2 - getTextWidth("LEVEL: "+ selectedLevel) / 2, GAME_WORLD_HEIGHT * 0.95f);
@ -105,8 +134,11 @@ public class Levelscreen{
font.draw(batch, "click to start ...", GAME_WORLD_WIDTH / 2 - getTextWidth("click to start ...") / 2, GAME_WORLD_HEIGHT * 0.1f);
batch.end();
// PREVIEW
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.BLACK);
// BORDER
shapeRenderer.rectLine(0.15f * GAME_WORLD_WIDTH, 0.15f * GAME_WORLD_HEIGHT, 0.85f * GAME_WORLD_WIDTH, 0.15f * GAME_WORLD_HEIGHT, 3);
shapeRenderer.rectLine(0.15f * GAME_WORLD_WIDTH, 0.15f * GAME_WORLD_HEIGHT, 0.15f * GAME_WORLD_WIDTH, 0.85f * GAME_WORLD_HEIGHT, 3);
shapeRenderer.rectLine(0.85f * GAME_WORLD_WIDTH, 0.15f * GAME_WORLD_HEIGHT, 0.85f * GAME_WORLD_WIDTH, 0.85f * GAME_WORLD_HEIGHT, 3);
@ -116,14 +148,14 @@ public class Levelscreen{
float previewY = 0.15f * GAME_WORLD_HEIGHT;
float previewWidth = 0.7f * GAME_WORLD_WIDTH;
float previewHeight = 0.7f * GAME_WORLD_HEIGHT;
// PIVOT
shapeRenderer.rectLine(previewX + level.getPivotX() * 0.7f, previewY + level.getPivotY() * 0.7f, previewX + (float)level.getProjectile().getxPos() * 0.7f, previewY + (float)level.getProjectile().getyPos()* 0.7f, 3);
shapeRenderer.setColor(Color.GRAY);
shapeRenderer.circle(previewX + level.getPivotX() * 0.7f, previewY + level.getPivotY() * 0.7f, 5);
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.circle(previewX + (float)level.getProjectile().getxPos() * 0.7f, previewY + (float)level.getProjectile().getyPos()* 0.7f, 8);
// GOAL
float x = level.getGoal().getxPos();
float y = level.getGoal().getyPos();
float th = level.getGoal().getThickness();
@ -134,20 +166,50 @@ public class Levelscreen{
shapeRenderer.rect(previewX + (x + th * w) * 0.7f, previewY + y * 0.7f, (th*3 * w) * 0.7f,(th * h)*0.7f);
shapeRenderer.rect(previewX + (x + th *w + th*3 * w) * 0.7f, previewY + y * 0.7f, (th * w) * 0.7f ,h * 0.7f);
// OBSTACLES
for(Rectangle rect : level.getObjects()){
shapeRenderer.rect(previewX + rect.getX() * 0.7f, previewY + rect.getY() * 0.7f, rect.getWidth() * 0.7f, rect.getHeight() * 0.7f);
}
//
// BUTTONS
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);
}
shapeRenderer.end();
batch.begin();
// END
}
public void dispose(){
t.clear();
}
public int touchDown(int x, int y){
if(Intersector.overlaps(buttons.get(0).getRectangle(), new Rectangle(x,y,1,1))){
return 0; // BACK
}
else if(x < GAME_WORLD_WIDTH * 0.15){
return 1; // LEFT
}
else if(x > GAME_WORLD_WIDTH * 0.85){
return 2; // RIGHT
}
else{
return 3; // START LEVEL
}
}
public void setSelectedLevel(int i){
selectedLevel = i;
@ -160,6 +222,11 @@ public class Levelscreen{
glyphLayout.setText(font,text);
return glyphLayout.width;
}
public float getTextHeight(String text){
GlyphLayout glyphLayout = new GlyphLayout();
glyphLayout.setText(font,text);
return glyphLayout.height;
}
}

Loading…
Cancel
Save