GammelJan 6 years ago
parent 79b7e6a4d7
commit 52af84e652

@ -9,9 +9,11 @@ import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import model.Goal;
import model.Level;
@ -24,6 +26,7 @@ import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Timer;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import view.Leveleditor;
import view.Titlescreen;
import view.Winscreen;
@ -41,6 +44,7 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
Levelscreen ls;
Gamescreen gs;
Winscreen ws;
Leveleditor le;
int levelAmount;
SpriteBatch batch;
Timer stepTimer;
@ -63,6 +67,7 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
ls = null;
gs = null;
ws = null;
le = null;
levelAmount = 9;
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
@ -181,6 +186,9 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
}
}
else if(ws != null) ws.render(batch);
else if(le != null){
le.render(batch);
}
batch.end();
}
@ -208,9 +216,16 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
@Override
public boolean touchDown(int x, int y, int i2, int i3) {
if(ts != null){
ts.dispose();
ts = null;
ls = new Levelscreen(beatenLevel, GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
if(x > 0.05 * Gdx.graphics.getWidth()){
ts.dispose();
ts = null;
ls = new Levelscreen(beatenLevel, GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
else{
ts.dispose();
ts = null;
le = new Leveleditor(GAME_WORLD_WIDTH, GAME_WORLD_HEIGHT, camera.combined);
}
}
else if(ls != null){
if(x < Gdx.graphics.getWidth() * 0.15){
@ -250,13 +265,14 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
}
}
else if(le != null){
le.touchDown(x,y);
}
return true;
}
@Override
public boolean touchUp(int i, int i1, int i2, int i3) {
if(gs != null){
}
return true;
}
@ -267,6 +283,15 @@ public class Controller extends ApplicationAdapter implements InputProcessor{
@Override
public boolean mouseMoved(int i, int i1) {
float x = ((float)i / (float)Gdx.graphics.getWidth()) *(float) GAME_WORLD_WIDTH;
float y = GAME_WORLD_HEIGHT - ((float)i1 / (float)Gdx.graphics.getHeight()) * (float)GAME_WORLD_HEIGHT;
//System.out.println("x:" + x + " y:" + y);
if(gs != null){
gs.setMousePos(x, y);
}
if(le != null){
le.setMousePos(x, y);
}
return false;
}

@ -0,0 +1,124 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import com.badlogic.gdx.math.Rectangle;
/**
*
* @author Jan
*/
public class Button {
private String text;
private int xPos;
private int yPos;
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){
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;
rect = new Rectangle(xPos, yPos, width, height);
}
/**
* @return the text
*/
public String getText() {
return text;
}
/**
* @param text the text to set
*/
public void setText(String text) {
this.text = text;
}
/**
* @return the xPos
*/
public int getxPos() {
return xPos;
}
/**
* @param xPos the xPos to set
*/
public void setxPos(int xPos) {
this.xPos = xPos;
}
/**
* @return the yPos
*/
public int getyPos() {
return yPos;
}
/**
* @param yPos the yPos to set
*/
public void setyPos(int yPos) {
this.yPos = yPos;
}
/**
* @return the width
*/
public int getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(int width) {
this.width = width;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(int height) {
this.height = height;
}
public void setRectangle(Rectangle rect){
this.rect = rect;
}
public Rectangle getRectangle(){
return rect;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public int getListId(){
return listId;
}
}

@ -90,7 +90,7 @@ public class Level {
angleSpeed += 0.0001;
angle -= angleSpeed;
System.out.println(Math.toDegrees(angle));
//System.out.println(Math.toDegrees(angle));
Vector2 newPosVector = math.pivotGetNewPos(this.angle, this.xPosPivot, this.yPosPivot, RADIUS);
this.projectile.setxPos(xPosPivot + (int) newPosVector.x);
@ -155,4 +155,16 @@ public class Level {
public void addRectangle(int x, int y, int width, int height){
objects.add(new Rectangle(x,y,width,height));
}
public void setPivot(int x, int y){
xPosPivot = x;
yPosPivot = y;
}
public void setGoal(int x, int y){
goal.setxPos(x);
goal.setyPos(y);
goal.setSizeX(300);
goal.setSizeY(200);
}
}

@ -6,6 +6,7 @@
package view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
@ -56,6 +57,9 @@ public class Gamescreen{
boolean win;
float mouseX;
float mouseY;
public Gamescreen(Level level, float width, float height, Matrix4 matrix){
GAME_WORLD_WIDTH = width;
GAME_WORLD_HEIGHT = height;
@ -115,6 +119,8 @@ public class Gamescreen{
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.circle(mouseX, mouseY, 5);
if(level.released()) {
for (int i = 0; i < level.traces.length; i++) {
if(level.isTraceInitialised[i]){
@ -127,8 +133,6 @@ public class Gamescreen{
shapeRenderer.rectLine((float) level.getPivotX(), (float) level.getPivotY(), (float) level.getProjectile().getxPos(), (float) level.getProjectile().getyPos(), 3);
}
shapeRenderer.setColor(Color.BLACK);
shapeRenderer.rect(x, y,th * w, h);
shapeRenderer.rect(x + th * w,y, th*3 * w,th * h);
@ -201,6 +205,11 @@ public class Gamescreen{
public ArrayList<Rectangle> getObjectRects(){
return objectRects;
}
public void setMousePos(float x, float y){
mouseX = x;
mouseY = y;
}
}

@ -0,0 +1,195 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
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.Circle;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Polygon;
import com.badlogic.gdx.math.Vector2;
import java.awt.Point;
import java.util.ArrayList;
import model.Button;
import model.Goal;
import model.Level;
import model.Projectile;
/**
*
* @author Jan
*/
public class Leveleditor{
ShapeRenderer shapeRenderer;
float GAME_WORLD_WIDTH;
float GAME_WORLD_HEIGHT;
ArrayList<Button> buttons;
float mouseX;
float mouseY;
int listNumber;
int selectedRect;
int state; //-1: nothig selected, 0: place pivot, 2: pivot direction, 3: goal, 4: obstacles
BitmapFont font;
Level level;
public Leveleditor(float width, float height, Matrix4 matrix){
GAME_WORLD_WIDTH = width;
GAME_WORLD_HEIGHT = height;
level = new Level(new Goal(0, 0, 200, 150, 0.2f), new Projectile(0, 0, 0), 0, 0);
state = -1;
shapeRenderer = new ShapeRenderer();
shapeRenderer.setProjectionMatrix(matrix);
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++;
font = new BitmapFont();
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);
}
public void render(SpriteBatch batch) {
batch.end();
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
shapeRenderer.setColor(Color.BLACK);
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());
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);
}
}
for(Rectangle rect : level.getObjects()){
shapeRenderer.rect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
}
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);
shapeRenderer.end();
batch.begin();
}
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:
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();
}
break;
}
}
break;
case 0:
level.setPivot(x, (int)GAME_WORLD_HEIGHT - y);
state = -1;
break;
case 1:
level.setGoal(x, (int)GAME_WORLD_HEIGHT-y);
state = -1;
break;
case 2:
buttons.remove(buttons.size()-1);
buttons.add(new Button("Rectangle", (int)(GAME_WORLD_WIDTH * 0.02), (int)(GAME_WORLD_HEIGHT - (buttons.size()+1) * 0.07 * GAME_WORLD_HEIGHT), 200, 50, 3, 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++;
state = -1;
break;
case 3:
if(buttons.get(selectedRect - 1) != null){
level.addRectangle(x,(int)GAME_WORLD_HEIGHT - y, 100, 100);
}
state = -1;
break;
case 4:
break;
}
}
public float getTextWidth(String text){
GlyphLayout glyphLayout = new GlyphLayout();
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