From df1d27d2820ad7398c5a69cb0935417c673b57b6 Mon Sep 17 00:00:00 2001 From: GammelJAN Date: Sat, 26 Sep 2020 13:49:45 +0200 Subject: [PATCH] walls disappear and despawn --- core/src/com/trs/game/Controller.java | 80 +++++++++++------------ core/src/com/trs/game/model/Model.java | 14 +++- core/src/com/trs/game/model/PermWall.java | 5 ++ core/src/com/trs/game/model/TempWall.java | 1 + core/src/com/trs/game/model/Wall.java | 1 + 5 files changed, 57 insertions(+), 44 deletions(-) diff --git a/core/src/com/trs/game/Controller.java b/core/src/com/trs/game/Controller.java index 53b6499..d546a1a 100644 --- a/core/src/com/trs/game/Controller.java +++ b/core/src/com/trs/game/Controller.java @@ -34,6 +34,8 @@ public class Controller extends ApplicationAdapter implements InputProcessor { final int GAME_WORLD_WIDTH = 1600; final int GAME_WORLD_HEIGHT = 900; + final int WALL_LIFETIME = 75; + SpriteBatch batch; ShapeRenderer renderer; PolygonSpriteBatch polygonSpriteBatch; @@ -77,10 +79,7 @@ public class Controller extends ApplicationAdapter implements InputProcessor { // //POLYGON STUFF - pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888); - pix.setColor(Color.BLACK); // DE is red, AD is green and BE is blue. - pix.fill(); - textureSolid = new Texture(pix); + screen = new MainMenuScreen(GAME_WORLD_WIDTH,GAME_WORLD_HEIGHT); @@ -90,6 +89,7 @@ public class Controller extends ApplicationAdapter implements InputProcessor { @Override public void render () { Gdx.gl.glClearColor(1, 1, 1, 1); + //Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // WORKAROUND, BECAUSE MOUSEMOVED NOT WORKING WHILE TOUCHDOWN if(screen.getId() == 1) model.adjustWall(Gdx.input.getX(), GAME_WORLD_HEIGHT - Gdx.input.getY()); @@ -100,56 +100,35 @@ public class Controller extends ApplicationAdapter implements InputProcessor { if(screen.getId() == 1){ // DRAW WALLS for(Wall wall : model.getWalls()){ - PolygonSprite poly; - PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), - wall.getPolygon().getVertices(), new short[] { - 0, 1, 2, // Two triangles using vertex indices. - 0, 2, 3 // Take care of the counter-clockwise direction. - }); - poly = new PolygonSprite(polyReg); - polygonSpriteBatch.begin(); - poly.draw(polygonSpriteBatch); - polygonSpriteBatch.end(); + if(wall.getLifetime() == -1){ + drawPolygon(wall.getPolygon(),Color.BLACK); + } + else{ + //float Value = ((float)(((float)wall.getLifetime() / (float)WALL_LIFETIME) * 255f))/255f); + float Value = 1f-(((float)wall.getLifetime()) / ((float)WALL_LIFETIME)); + System.out.println(Value); + Color color = new Color(Value,Value,Value,1); + drawPolygon(wall.getPolygon(),color); + } } - // DRAW PROJECTILES renderer.begin(ShapeRenderer.ShapeType.Filled); for(Projectile projectile : model.getProjectiles()){ - PolygonSprite poly; - PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), - projectile.getPolygon().getVertices(), new short[] { - 0, 1, 2, // Two triangles using vertex indices. - 0, 2, 3 // Take care of the counter-clockwise direction. - }); - poly = new PolygonSprite(polyReg); - polygonSpriteBatch.begin(); - poly.draw(polygonSpriteBatch); - polygonSpriteBatch.end(); + drawPolygon(projectile.getPolygon(), Color.BLACK); } // TEMP POLYGON if(model.getTempPolygon() != null){ - PolygonSprite poly2; - PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), - model.getTempPolygon().getVertices(), new short[]{ - 0, 1, 2, // Two triangles using vertex indices. - 0, 2, 3 // Take care of the counter-clockwise direction. - }); - poly2 = new PolygonSprite(polyReg); - polygonSpriteBatch.begin(); - poly2.draw(polygonSpriteBatch); - polygonSpriteBatch.end(); - + drawPolygon(model.getTempPolygon(),Color.BLACK); renderer.end(); - batch.begin(); - font.setColor(Color.GRAY); - if(model.getCurrentLength()!=0)font.draw(batch, ""+model.getCurrentLength(), Gdx.input.getX()+10, GAME_WORLD_HEIGHT - Gdx.input.getY()+10); - batch.end(); - renderer.begin(ShapeRenderer.ShapeType.Filled); + // LENGTH + batch.begin(); + font.setColor(Color.GRAY); + if(model.getCurrentLength()!=0)font.draw(batch, ""+model.getCurrentLength(), Gdx.input.getX()+10, GAME_WORLD_HEIGHT - Gdx.input.getY()+10); + batch.end(); } // renderer.end(); - // write left Length batch.begin(); font.setColor(Color.BLACK); @@ -233,4 +212,21 @@ public class Controller extends ApplicationAdapter implements InputProcessor { break; } } + + public void drawPolygon(Polygon polygon, Color color){ + pix = new Pixmap(1, 1, Pixmap.Format.RGBA8888); + pix.setColor(color); // DE is red, AD is green and BE is blue. + pix.fill(); + textureSolid = new Texture(pix); + PolygonSprite poly; + PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid), + polygon.getVertices(), new short[] { + 0, 1, 2, // Two triangles using vertex indices. + 0, 2, 3 // Take care of the counter-clockwise direction. + }); + poly = new PolygonSprite(polyReg); + polygonSpriteBatch.begin(); + poly.draw(polygonSpriteBatch); + polygonSpriteBatch.end(); + } } diff --git a/core/src/com/trs/game/model/Model.java b/core/src/com/trs/game/model/Model.java index 3aff92d..af8407a 100644 --- a/core/src/com/trs/game/model/Model.java +++ b/core/src/com/trs/game/model/Model.java @@ -8,6 +8,9 @@ import com.trs.game.StaticMath; import java.util.ArrayList; public class Model { + + final int WALL_LIFETIME = 75; + private Monster monster; private ArrayList walls; private ArrayList projectiles; @@ -16,7 +19,7 @@ public class Model { private boolean drawing = false; private int currentLength; - private int leftWallLength = 2500; + private int leftWallLength = 5000; public Model(){ monster = new Monster(250,150); @@ -33,6 +36,13 @@ public class Model { for(Projectile projectile : projectiles){ projectile.move(walls); } + for(int i = 0; i < walls.size(); i++){ + walls.get(i).timerStep(); + if(walls.get(i).getLifetime() == 0){ + walls.remove(i); + i--; + } + } } public void startWall(int x, int y){ @@ -73,7 +83,7 @@ public class Model { if(possible){ leftWallLength -= Vector2.dst(tempStart.x,tempStart.y,x,y); - walls.add(new TempWall(angle-Math.PI, tempPolygon, 500)); + walls.add(new TempWall(angle-Math.PI, tempPolygon, WALL_LIFETIME)); } } tempPolygon = null; diff --git a/core/src/com/trs/game/model/PermWall.java b/core/src/com/trs/game/model/PermWall.java index a0c7ef3..f18c3e0 100644 --- a/core/src/com/trs/game/model/PermWall.java +++ b/core/src/com/trs/game/model/PermWall.java @@ -31,6 +31,11 @@ public class PermWall implements Wall { return collisionPolygons; } + @Override + public int getLifetime() { + return -1; + } + public void setRotation(double rotation) { this.rotation = rotation; } diff --git a/core/src/com/trs/game/model/TempWall.java b/core/src/com/trs/game/model/TempWall.java index 9995650..4fb3fb6 100644 --- a/core/src/com/trs/game/model/TempWall.java +++ b/core/src/com/trs/game/model/TempWall.java @@ -20,6 +20,7 @@ public class TempWall implements Wall { @Override public Wall timerStep() { + lifetime--; return this; } diff --git a/core/src/com/trs/game/model/Wall.java b/core/src/com/trs/game/model/Wall.java index 0e3a139..900575d 100644 --- a/core/src/com/trs/game/model/Wall.java +++ b/core/src/com/trs/game/model/Wall.java @@ -7,4 +7,5 @@ public interface Wall { public Polygon getPolygon(); public double getRotation(); public Polygon[] getCollisionPolygons(); + public int getLifetime(); }