diff --git a/core/src/com/golddaniel/entities/Boid.java b/core/src/com/golddaniel/entities/Boid.java index b3d0048..cca3a30 100644 --- a/core/src/com/golddaniel/entities/Boid.java +++ b/core/src/com/golddaniel/entities/Boid.java @@ -35,7 +35,7 @@ */ public class Boid extends Entity { - static float SPEED_MAX = 300f; + static float SPEED_MAX = 250f; static ShapeRenderer debug = new ShapeRenderer(); @@ -61,7 +61,7 @@ public Boid(Vector2 position) float angle = MathUtils.random(MathUtils.PI*2); acceleration = new Vector2(); - velocity = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)); + velocity = new Vector2(MathUtils.cos(angle), MathUtils.sin(angle)).scl(SPEED_MAX); activeTimer = new Timer(); @@ -77,18 +77,26 @@ public Boid(Vector2 position) boids.add(this); } + /** + * Returns direction vector based on position of all boids in range + * + * @return + */ private Vector2 cohesion() { Vector2 result = new Vector2(); int count = 0; - float range =72; + float range = 64; Vector2 sum = new Vector2(); for(Boid b : boids) { float dist = position.dst(b.position); - if(dist > 0 && dist < range) + /** + * commented out as we currently want average of all boids + * + *///if(dist > 0 && dist < range) { sum.add(b.position); count++; @@ -103,6 +111,11 @@ private Vector2 cohesion() return result; } + /** + * Returns direction vector based on velocity of all boids in range + * + * @return + */ private Vector2 allignment() { Vector2 result = new Vector2(); @@ -132,6 +145,14 @@ private Vector2 allignment() return result; } + /** + * Returns direction vector based on separation of all boids in range. + * + * if a boid is in range, add a vector to the sum and give the opposite direction + * + * + * @return + */ private Vector2 separation() { Vector2 result = new Vector2(); @@ -205,6 +226,11 @@ public void run() if(active) { + float[] hsv = new float[3]; + color.toHsv(hsv); + hsv[0] += 60f*delta; + color.fromHsv(hsv); + borderCheck(model); Vector2 separation = separation(); @@ -214,7 +240,7 @@ public void run() Vector2 seek = new Vector2(); float dist = Float.MAX_VALUE; - float range = 512; + float range = 256; if(model.getEntityType(Player.class).size > 0) { Vector2 target = model.getEntityType(Player.class).first().position; @@ -232,23 +258,16 @@ public void run() acceleration.add(boundary); acceleration.add(seek); - //turn faster toward the target when near - if(dist < range) - { - acceleration.limit(SPEED_MAX/4f*delta); - } - else - { - acceleration.limit(SPEED_MAX/16f*delta); - } + + acceleration.limit(SPEED_MAX/64f*delta); + + velocity.add(acceleration); velocity.limit(SPEED_MAX*delta); position.add(velocity); acceleration.set(0, 0); - - model.applyRadialForce(position, 800f, 128); } @@ -306,7 +325,7 @@ public void kill(WorldModel model) //immune until active if(active) { - int particles = 32; + int particles = 64; for (int i = 0; i < particles; i++) { float angle = (float)i/(float)particles*360f; @@ -315,26 +334,44 @@ public void kill(WorldModel model) if(i % 2 == 0) { + + float[] hsv = new float[3]; + + Color.MAGENTA.toHsv(hsv); + + hsv[1] /= 4; + + Color c = new Color().fromHsv(hsv); + model.createParticle( new Vector2( position.x + width/2, position.y + height/2), angle, - MathUtils.random(0.5f, 0.9f), - -Globals.WIDTH/2f, - Color.CYAN.cpy(), - Color.MAGENTA.cpy(), + MathUtils.random(0.5f, 0.7f), + -Globals.WIDTH/4, + Color.MAGENTA, + Color.WHITE, Particle.TYPE.SPIN); + + model.createParticle( + new Vector2( + position.x + width/2, + position.y + height/2), + angle, + MathUtils.random(0.7f, 1.2f), + -Globals.WIDTH/2f, + Color.WHITE, + Color.FIREBRICK, + Particle.TYPE.NORMAL); } } - for(int i = 0; i < 5; i++) - { - model.applyRadialForce( - getMid(), - 2000, - 256); - } + model.applyRadialForce( + getMid(), + 10000, + 256); + Messenger.notify(Messenger.EVENT.BOUNCER_DEAD); isAlive = false; } diff --git a/core/src/com/golddaniel/entities/Bullet.java b/core/src/com/golddaniel/entities/Bullet.java index 2b67a57..d5a8f93 100644 --- a/core/src/com/golddaniel/entities/Bullet.java +++ b/core/src/com/golddaniel/entities/Bullet.java @@ -32,8 +32,6 @@ */ public class Bullet extends Entity { - - public static enum TYPE { diff --git a/core/src/com/golddaniel/entities/Particle.java b/core/src/com/golddaniel/entities/Particle.java index 1a11f8b..0635f50 100644 --- a/core/src/com/golddaniel/entities/Particle.java +++ b/core/src/com/golddaniel/entities/Particle.java @@ -125,7 +125,7 @@ public void update(WorldModel world, float delta) if(type == TYPE.SPIN) { - angle += MathUtils.random(90f, 360f) * delta * lifespan/START_LIFESPAN; + angle += MathUtils.random(180f, 360f) * delta * lifespan/START_LIFESPAN; } lerpColor(); diff --git a/core/src/com/golddaniel/entities/Player.java b/core/src/com/golddaniel/entities/Player.java index c5a9ae0..d227d86 100644 --- a/core/src/com/golddaniel/entities/Player.java +++ b/core/src/com/golddaniel/entities/Player.java @@ -130,6 +130,8 @@ public void update(WorldModel model, float delta) rightStick.y = -ControllerManager.controller. getAxis(XboxMapping.R_STICK_VERTICAL_AXIS); + + if(SharedLibraryLoader.isWindows) { rightStick.y = -rightStick.y; @@ -146,12 +148,6 @@ public void update(WorldModel model, float delta) } //----------------------------------------------------------- - - if(ControllerManager.controller.getButton(XboxMapping.A)) - { - - } - } else { @@ -253,7 +249,7 @@ private void fireBullets(WorldModel model, Vector2 rightStick, float radius) Vector2 bulletPos = new Vector2(); bulletPos.x = position.x + width/2f; - bulletPos.y = position.y + height/2f; + bulletPos.y = position. y+ height/2f; @@ -262,23 +258,22 @@ private void fireBullets(WorldModel model, Vector2 rightStick, float radius) Messenger.notify(Messenger.EVENT.PLAYER_FIRE); - - Vector3 pos = new Vector3(position, 1); - pos.x += width/2; - pos.y += height/2; - - float dif = 3f; - + + float dif = 2f; model.createBullet(bulletPos, rightStick.angle(), Bullet.TYPE.LASER_1); - model.createBullet(bulletPos, - rightStick.angle() + dif, + int extraBullets = 2; + for (int i = 0; i < extraBullets; i++) + { + model.createBullet(bulletPos, + rightStick.angle() + dif*(i+1), Bullet.TYPE.LASER_1); - model.createBullet(bulletPos, - rightStick.angle() - dif, + model.createBullet(bulletPos, + rightStick.angle() - dif*(i+1), Bullet.TYPE.LASER_1); - + } + cooldown = COOLDOWN_MAX; } @@ -326,7 +321,7 @@ public void kill(WorldModel model) position.x + width/2, position.y + height/2), pAngle, - MathUtils.random(0.5f, 1f)*3f, + MathUtils.random(0.5f, 1.5f), MathUtils.random(0.6f, 0.8f) * Globals.WIDTH/2, new Color(MathUtils.random(), MathUtils.random(), @@ -354,7 +349,7 @@ public void kill(WorldModel model) model.createParticle( pos, pAngle, - MathUtils.random(0.6f, 1.5f)*3f, + MathUtils.random(0.6f, 2f), MathUtils.random(0.6f, 0.8f) * Globals.WIDTH, new Color(MathUtils.random(), MathUtils.random(), @@ -368,6 +363,6 @@ public void kill(WorldModel model) } } - model.applyRadialForce(getMid(), -64000, model.WORLD_WIDTH); + model.applyRadialForce(getMid(), 64000, model.WORLD_WIDTH); } } diff --git a/core/src/com/golddaniel/main/AudioSystem.java b/core/src/com/golddaniel/main/AudioSystem.java index 9614bf7..dc9ff59 100644 --- a/core/src/com/golddaniel/main/AudioSystem.java +++ b/core/src/com/golddaniel/main/AudioSystem.java @@ -19,7 +19,7 @@ import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.utils.ArrayMap; -import gold.daniel.level.Level; +import gold.daniel.level.LevelModel; /** * @@ -31,7 +31,7 @@ public class AudioSystem implements MessageListener private static float MUSIC_VOLUME = 0.3f; private static float SFX_VOLUME = 1f; - public void update(Level level) + public void update(LevelModel level) { } diff --git a/core/src/com/golddaniel/main/CollisionSystem.java b/core/src/com/golddaniel/main/CollisionSystem.java index c86c3db..85dca87 100644 --- a/core/src/com/golddaniel/main/CollisionSystem.java +++ b/core/src/com/golddaniel/main/CollisionSystem.java @@ -21,7 +21,6 @@ import com.golddaniel.entities.Bullet; import com.golddaniel.entities.Player; import com.golddaniel.entities.Boid; -import gold.daniel.level.Level; /** * @@ -29,10 +28,9 @@ */ public class CollisionSystem { - public void update(Level level) + public void update(WorldModel model) { - WorldModel model = level.getModel(); - + if(model == null) return; Array bullets = model.getEntityType(Bullet.class); Array bouncers = model.getEntityType(Bouncer.class); Array boids = model.getEntityType(Boid.class); diff --git a/core/src/com/golddaniel/main/ControllerManager.java b/core/src/com/golddaniel/main/ControllerManager.java index 7c606fc..b7633d1 100644 --- a/core/src/com/golddaniel/main/ControllerManager.java +++ b/core/src/com/golddaniel/main/ControllerManager.java @@ -65,7 +65,7 @@ public void disconnected(Controller cntrlr) @Override public boolean buttonDown(Controller cntrlr, int i) { - + System.out.println(i); return true; } diff --git a/core/src/com/golddaniel/main/PhysicsGrid.java b/core/src/com/golddaniel/main/PhysicsGrid.java index 85c5382..48882bb 100644 --- a/core/src/com/golddaniel/main/PhysicsGrid.java +++ b/core/src/com/golddaniel/main/PhysicsGrid.java @@ -32,8 +32,7 @@ */ public class PhysicsGrid extends Entity { - - private final float STIFFNESS = 12.5f; + private final float STIFFNESS = 16.5f; private final float DAMPING = 10f; private final float INVERSE_MASS = 1f/0.2f; @@ -63,7 +62,7 @@ public void update(float delta) if(len > TARGET_LENGTH) { //normalize - displacement.nor().scl(len - TARGET_LENGTH); + displacement.setLength(len - TARGET_LENGTH); Vector2 dv = end2.velocity.cpy().sub(end1.velocity); @@ -84,7 +83,7 @@ private class Point Vector2 acceleration; float inverseMass; - + public Point(Vector2 position, float inverseMass) { @@ -97,12 +96,19 @@ public Point(Vector2 position, float inverseMass) public void update(float delta) { + //there is an invisible spring that connects the inital + //position to where the point currently is, these are the + //calculations for said spring. Otherwise the grid effect + //doesnt come back fast enough, also maintains the general shape + //of the grid + float stiffnessScale = 1f/16f; + // FORCE CALCULATIONS - float springForceX = -STIFFNESS/2f*(position.x - desiredPosition.x); + float springForceX = -STIFFNESS*stiffnessScale*(position.x - desiredPosition.x); float dampingForceX = DAMPING * velocity.x; float forceX = springForceX - dampingForceX; - float springForceY = -STIFFNESS/2f*(position.y - desiredPosition.y); + float springForceY = -STIFFNESS*stiffnessScale*(position.y - desiredPosition.y); float dampingForceY = DAMPING * velocity.y; float forceY = springForceY - dampingForceY; @@ -123,37 +129,35 @@ public void update(float delta) } } - public void applyForce(Vector2 force) { acceleration.x += force.x*inverseMass; acceleration.y += force.y*inverseMass; } } - + Color color; float borderHue; ShapeRenderer sh; - Vector2 worldSize; + Vector2 gridDimensions; + Array springs; Point[][] points; int rows; int cols; - final float lerpValue = 0.035f; - public boolean enableInterpolatedLines = true; - public PhysicsGrid(Vector2 worldSize, int rows, int cols) + public PhysicsGrid(Vector2 gridDimensions, int rows, int cols) { this.rows = rows; this.cols = cols; - this.worldSize = worldSize; + this.gridDimensions = gridDimensions; color = Color.MAGENTA.cpy(); color.a = 1f; @@ -166,14 +170,14 @@ public PhysicsGrid(Vector2 worldSize, int rows, int cols) { for (int j = 0; j < points[i].length; j++) { - float mass = INVERSE_MASS; + float invMass = INVERSE_MASS; if(i == 0 || i == points.length - 1 || j == 0 || j == points[i].length - 1) { - mass = 0; + invMass = 0; } points[i][j] = new Point( - new Vector2(i*worldSize.x/rows, j*worldSize.y/cols), mass); + new Vector2(i*gridDimensions.x/rows, j*gridDimensions.y/cols), invMass); } } @@ -227,8 +231,7 @@ public void update(float delta) @Override public void update(WorldModel model, float delta) { - if(delta < 0.025f) - update(delta); + update(delta); } public void applyRadialForce(Vector2 pos, float force, float radius) @@ -253,15 +256,16 @@ public void applyRadialForce(Vector2 pos, float force, float radius) public void applyDirectionalForce(Vector2 pos, Vector2 force, float radius) { - for (int i = 0; i < points.length; i++) + for (Point[] pointArr : points) { - for (int j = 0; j < points[i].length; j++) + for (Point point : pointArr) { - float dist = Vector2.dst(pos.x, pos.y, - points[i][j].position.x, points[i][j].position.y); - if(dist < radius) + float dist = Vector2.dst( + pos.x, pos.y, + point.position.x, point.position.y); + if (dist < radius) { - points[i][j].applyForce(force.cpy().scl(1f- dist/radius)); + point.applyForce(force.cpy().scl(1f- dist/radius)); } } } @@ -279,7 +283,8 @@ public void draw(SpriteBatch s) sh.setProjectionMatrix(s.getProjectionMatrix()); - color = Color.MAGENTA.cpy(); + color = Color.TEAL.cpy(); + sh.begin(ShapeRenderer.ShapeType.Line); @@ -299,7 +304,7 @@ public void draw(SpriteBatch s) { color.a = 0.3f; Vector2 mid1 = new Vector2(); - mid1.x = points[i][j].position.x + points[i+1][j].position.x; + mid1.x = points[i+1][j].position.x + points[i][j].position.x; mid1.x /= 2; mid1.y = points[i][j].position.y; @@ -346,18 +351,16 @@ public void draw(SpriteBatch s) sh.setColor(color.fromHsv(borderHue, 1f, 1f)); //BORDER - Gdx.gl20.glLineWidth(8f); - sh.line(0, 0, 0, worldSize.y); - sh.line(0, 0, worldSize.x, 0); - sh.line(0, worldSize.y, worldSize.x, worldSize.y); - sh.line(worldSize.x, worldSize.y, worldSize.x, 0); + Gdx.gl20.glLineWidth(32f); + sh.line(0, 0, 0, gridDimensions.y); + sh.line(0, 0, gridDimensions.x, 0); + sh.line(0, gridDimensions.y, gridDimensions.x, gridDimensions.y); + sh.line(gridDimensions.x, gridDimensions.y, gridDimensions.x, 0); Gdx.gl20.glLineWidth(1f); sh.end(); - Gdx.gl.glDisable(GL20.GL_BLEND); - s.begin(); } @@ -378,6 +381,5 @@ public void kill(WorldModel moidel) {} public void setColor(Color color) { this.color = color; - } - + } } diff --git a/core/src/com/golddaniel/main/WorldModel.java b/core/src/com/golddaniel/main/WorldModel.java index b144dd0..1715cfa 100644 --- a/core/src/com/golddaniel/main/WorldModel.java +++ b/core/src/com/golddaniel/main/WorldModel.java @@ -19,12 +19,10 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Pool; -import com.golddaniel.entities.Bouncer; import com.golddaniel.entities.Bullet; import com.golddaniel.entities.Entity; import com.golddaniel.entities.Particle; import com.golddaniel.entities.Player; -import com.golddaniel.entities.Boid; /** * @@ -63,7 +61,7 @@ public WorldModel(float width, float height) particles = new Array(); - particlePool = new Pool(2048) { + particlePool = new Pool(4096) { @Override protected Particle newObject() { @@ -165,8 +163,7 @@ public void killAllEntities() { for(Entity e : entities) { - if(e instanceof Boid || - e instanceof Bouncer) e.kill(); + if(!(e instanceof PhysicsGrid)) e.kill(); } } @@ -202,4 +199,9 @@ public void dispose() e.dispose(); } } + + public Player getPlayer() + { + return player; + } } diff --git a/core/src/com/golddaniel/main/WorldRenderer.java b/core/src/com/golddaniel/main/WorldRenderer.java index 527f177..0d7ac6d 100644 --- a/core/src/com/golddaniel/main/WorldRenderer.java +++ b/core/src/com/golddaniel/main/WorldRenderer.java @@ -63,7 +63,7 @@ public WorldRenderer() s = new SpriteBatch(); bloom = new Bloom(viewport, 1f); - bloom.setBloomIntesity(6f); + bloom.setBloomIntesity(2f); bloom.setTreshold(0.4f); } @@ -146,13 +146,13 @@ public void draw(WorldModel model) { g.draw(s); } - for(Entity e : model.getAllEntities()) { if(!(e instanceof Player || e instanceof PhysicsGrid)) e.draw(s); } + //draw player on top if(model.player != null) { diff --git a/core/src/com/golddaniel/main/XboxMapping.java b/core/src/com/golddaniel/main/XboxMapping.java index 635a65e..3d550ad 100644 --- a/core/src/com/golddaniel/main/XboxMapping.java +++ b/core/src/com/golddaniel/main/XboxMapping.java @@ -16,6 +16,7 @@ * limitations under the License. ******************************************************************************/ +import com.badlogic.gdx.Gdx; import com.badlogic.gdx.controllers.Controller; import com.badlogic.gdx.utils.SharedLibraryLoader; @@ -93,13 +94,13 @@ else if (SharedLibraryLoader.isLinux) { A = 0; B = 1; - X = 2; - Y = 3; + X = 3; + Y = 4; GUIDE = 8; - L_BUMPER = 4; - R_BUMPER = 5; + L_BUMPER = 6; + R_BUMPER = 7; BACK = 6; - START = 7; + START = 11; DPAD_UP = 7; DPAD_DOWN = 7; DPAD_LEFT = 6; @@ -108,10 +109,10 @@ else if (SharedLibraryLoader.isLinux) R_TRIGGER = 5; L_STICK_VERTICAL_AXIS = 1; L_STICK_HORIZONTAL_AXIS = 0; - R_STICK_VERTICAL_AXIS = 4; - R_STICK_HORIZONTAL_AXIS = 3; - L3 = 9; - R3 = 10; + R_STICK_VERTICAL_AXIS = 3; + R_STICK_HORIZONTAL_AXIS = 2; + L3 = 13; + R3 = 14; } else if (SharedLibraryLoader.isMac) { diff --git a/core/src/com/golddaniel/screens/LevelSelectScreen.java b/core/src/com/golddaniel/screens/LevelSelectScreen.java index 26e6cc2..369b831 100644 --- a/core/src/com/golddaniel/screens/LevelSelectScreen.java +++ b/core/src/com/golddaniel/screens/LevelSelectScreen.java @@ -66,7 +66,7 @@ public LevelSelectScreen(ScreenManager sm) FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/Square.ttf")); FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = 256; - font = generator.generateFont(parameter); // font size 12 pixels + font = generator.generateFont(parameter); generator.dispose(); panelTexture = new Texture("ui/glassPanel_cornerTL.png"); @@ -77,7 +77,8 @@ public LevelSelectScreen(ScreenManager sm) @Override public void render(float delta) - { + { + sm.setScreen(ScreenManager.STATE.PLAY); Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); diff --git a/core/src/com/golddaniel/screens/MainMenuScreen.java b/core/src/com/golddaniel/screens/MainMenuScreen.java index be367f5..d3edff5 100644 --- a/core/src/com/golddaniel/screens/MainMenuScreen.java +++ b/core/src/com/golddaniel/screens/MainMenuScreen.java @@ -17,6 +17,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.viewport.FitViewport; import com.golddaniel.main.ControllerManager; @@ -68,7 +69,7 @@ public MainMenuScreen(ScreenManager sm) titleColor = new Color(1f, 1f, 1f, 1f); - int spacing = 16; + int spacing = 32; g = new PhysicsGrid(new Vector2(Globals.WIDTH*2, Globals.HEIGHT*2), Globals.WIDTH*2/spacing, Globals.HEIGHT*2/spacing); @@ -79,6 +80,11 @@ public MainMenuScreen(ScreenManager sm) bloom = new Bloom(viewport, 1f); } + private float abs(float a) + { + return a > 0 ? a : -a; + } + @Override public void render(float delta) { @@ -98,22 +104,24 @@ public void render(float delta) ( (ControllerManager.controller != null && ControllerManager.controller.getButton(XboxMapping.START)) )) { - sm.setScreen(ScreenManager.STATE.PLAY); + sm.setScreen(ScreenManager.STATE.LEVEL_SELECT); } if(ControllerManager.controller != null) { + float x = ControllerManager.controller.getAxis(XboxMapping.R_STICK_HORIZONTAL_AXIS); float y = -ControllerManager.controller.getAxis(XboxMapping.R_STICK_VERTICAL_AXIS); + if(abs(x) < 0.15f) x = 0; + if(abs(y) < 0.15f) y = 0; - Vector2 dir = new Vector2(x, y); + Vector2 dir = new Vector2(x, y).nor(); point.add(dir.scl(600f*delta)); g.applyRadialForce(point, 1000f, 256); } - g.update(delta); camera.position.x = Globals.WIDTH/2f; diff --git a/core/src/com/golddaniel/screens/PlayScreen.java b/core/src/com/golddaniel/screens/PlayScreen.java index 6918739..7a21cb9 100644 --- a/core/src/com/golddaniel/screens/PlayScreen.java +++ b/core/src/com/golddaniel/screens/PlayScreen.java @@ -14,16 +14,16 @@ import com.golddaniel.main.MessageListener; import com.golddaniel.main.Messenger; import com.golddaniel.main.WorldRenderer; -import gold.daniel.level.Level; +import com.golddaniel.main.XboxMapping; +import gold.daniel.level.LevelModel; /** - * * @author wrksttn */ public class PlayScreen extends VScreen implements MessageListener { - Level level; + LevelModel level; WorldRenderer renderer; CollisionSystem cSystem; @@ -40,7 +40,7 @@ public PlayScreen(ScreenManager sm) super(sm); - level = new Level(); + level = new LevelModel(Gdx.files.internal("levels/testLevel.lvl")); renderer = new WorldRenderer(); cSystem = new CollisionSystem(); aSystem = new AudioSystem(); @@ -57,8 +57,8 @@ public PlayScreen(ScreenManager sm) uiCam = new OrthographicCamera(); uiViewport = new FitViewport(Globals.WIDTH, Globals.HEIGHT, uiCam); - uiCam.position.x = Globals.WIDTH /2; - uiCam.position.y = Globals.HEIGHT /2; + uiCam.position.x = Globals.WIDTH / 2; + uiCam.position.y = Globals.HEIGHT / 2; uiViewport.apply(); Messenger.startNotifying(); @@ -69,26 +69,18 @@ public PlayScreen(ScreenManager sm) public void render(float delta) { level.update(delta); - cSystem.update(level); - renderer.draw(level.getModel()); + cSystem.update(level.getWorldModel()); + renderer.draw(level.getWorldModel()); s.setProjectionMatrix(uiViewport.getCamera().combined); + //draw UI s.begin(); - font.draw(s, "TIME: " + level.getRemainingTime(), - 0, - Globals.HEIGHT - 64); - font.draw(s, "SCORE: " + level.getScore(), - 0, - Globals.HEIGHT - 192); - if(level.getRemainingTime() == 0) - { - font.draw(s, "LEVEL END", - Globals.WIDTH/2, - Globals.HEIGHT/2); - level.killAll(); - } + font.draw(s, + "REMAINGING TIME: " + level.getRemainingTime(), + 0, Globals.HEIGHT); + s.end(); } diff --git a/core/src/gold/daniel/level/LevelModel.java b/core/src/gold/daniel/level/LevelModel.java new file mode 100644 index 0000000..763a6b4 --- /dev/null +++ b/core/src/gold/daniel/level/LevelModel.java @@ -0,0 +1,264 @@ +/* + * Copyright 2018 . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package gold.daniel.level; + +import com.badlogic.gdx.files.FileHandle; +import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.ArrayMap; +import com.badlogic.gdx.utils.Timer; +import com.badlogic.gdx.utils.XmlReader; +import com.badlogic.gdx.utils.XmlReader.Element; +import com.golddaniel.entities.Boid; +import com.golddaniel.entities.Bouncer; +import com.golddaniel.entities.Entity; +import com.golddaniel.entities.Player; +import com.golddaniel.main.Globals; +import com.golddaniel.main.MessageListener; +import com.golddaniel.main.Messenger; +import com.golddaniel.main.PhysicsGrid; +import com.golddaniel.main.WorldModel; + +/** + * Helps manage worldModel + * @author wrksttn + */ +public class LevelModel implements MessageListener +{ + + public static enum LEVEL_TYPE + { + PASSIVE, + TIME_ATTACK, + ENDLESS, + } + + private LEVEL_TYPE levelType; + + + float remaingingTime; + float stateTime; + + + ArrayMap> toSpawn; + WorldModel worldModel; + + Timer boidSpawnTimer = new Timer(); + + public LevelModel(FileHandle levelFile) + { + Messenger.addListener(Messenger.EVENT.BOUNCER_DEAD, (MessageListener)this); + Messenger.addListener(Messenger.EVENT.TRACKER_DEAD, (MessageListener)this); + + toSpawn = new ArrayMap>(); + + buildLevel(levelFile); + } + + private void buildLevel(FileHandle levelFile) + { + XmlReader reader = new XmlReader(); + Element root = reader.parse(levelFile); + + final String TIME_KEY = "TIME"; + final String ENDLESS_KEY = "ENDLESS"; + final String PASSIVE_KEY = "PASSIVE"; + + + //LEVEL TYPE===================================== + String typeLevel = root.getChildByName("level").get("type"); + if(typeLevel.equals(TIME_KEY)) + { + this.levelType = LEVEL_TYPE.TIME_ATTACK; + } + else if(typeLevel.equals(ENDLESS_KEY)) + { + this.levelType = LEVEL_TYPE.ENDLESS; + } + else if(typeLevel.equals(PASSIVE_KEY)) + { + this.levelType = LEVEL_TYPE.PASSIVE; + } + //================================================ + + + //set the time for time attack mode + if(this.levelType == LEVEL_TYPE.TIME_ATTACK) + { + float startTime = root.getChildByName("level").getFloat("time"); + remaingingTime = startTime; + } + + //for the physics grid + float widthScale = root.getChildByName("level").getFloat("widthScale"); + float heightScale = root.getChildByName("level").getFloat("heightScale"); + + worldModel = + new WorldModel(Globals.WIDTH*widthScale, + Globals.HEIGHT*heightScale); + + int gridSpacing = root.getChildByName("level").getInt("gridSpacing"); + + PhysicsGrid g = new PhysicsGrid( + new Vector2(worldModel.WORLD_WIDTH, worldModel.WORLD_HEIGHT), + (int)worldModel.WORLD_WIDTH / gridSpacing, + (int)worldModel.WORLD_HEIGHT/ gridSpacing); + + worldModel.addEntity(g); + worldModel.addEntity(new Player(worldModel)); + + Element spawnElement = root.getChildByName("spawn"); + Array enemyElements = spawnElement.getChildrenByName("enemy"); + + for(Element el : enemyElements) + { + int spawnTime = el.getInt("time"); + + final String BOUNCER_KEY = "BOUNCER"; + final String BOID_KEY = "BOID"; + + //PARSE SPAWN POSITION================================ + Vector2 pos = new Vector2(); + String posString = el.get("position"); + String[] stringCoords = posString.split(","); + + pos.x = Float.parseFloat(stringCoords[0]); + pos.y = Float.parseFloat(stringCoords[1]); + //==================================================== + + if(!toSpawn.containsKey(spawnTime)) + { + toSpawn.put(spawnTime, new Array()); + } + + String enemyType = el.get("type"); + if(enemyType.equals(BOUNCER_KEY)) + { + String dirString = el.get("direction"); + String[] dirVec = dirString.split(","); + + Vector2 dir = new Vector2(); + dir.x = Float.parseFloat(dirVec[0]); + dir.y = Float.parseFloat(dirVec[1]); + + + Bouncer b = new Bouncer(pos, dir); + + toSpawn.get(spawnTime).add(b); + } + else if(enemyType.equals(BOID_KEY)) + { + Boid b = new Boid(pos); + + toSpawn.get(spawnTime).add(b); + } + } + } + + public void update(float delta) + { + stateTime += delta; + remaingingTime -= delta; + + + + if(levelType == LEVEL_TYPE.TIME_ATTACK) + { + if(toSpawn.containsKey((int)stateTime)) + { + Array toAdd = toSpawn.get((int)stateTime); + for(Entity e : toAdd) + { + worldModel.addEntity(e); + } + toSpawn.removeKey((int)stateTime); + } + } + //extremely temporary + // + //do work for endless levels in here. Should probably change + //how the timer is used + else if(levelType == LEVEL_TYPE.ENDLESS && boidSpawnTimer.isEmpty()) + { + + boidSpawnTimer.scheduleTask(new Timer.Task() + { + @Override + public void run() + { + for (int i = 0; i < 15; i++) + { + Boid b = new Boid( + new Vector2( + MathUtils.random(worldModel.WORLD_WIDTH), + MathUtils.random(worldModel.WORLD_HEIGHT))); + + worldModel.addEntity(b); + + if(worldModel.getPlayer() == null) + { + worldModel.addEntity(new Player(worldModel)); + } + } + } + }, 2f); + } + + worldModel.update(delta); + } + + public WorldModel getWorldModel() + { + return worldModel; + } + + + public void killAll() + { + for(Entity e : worldModel.getAllEntities()) + { + if(!(e instanceof Player)) + { + e.kill(worldModel); + } + } + } + + public int getRemainingTime() + { + int result = 0; + if(remaingingTime > 0) + { + result = MathUtils.round(remaingingTime); + } + + return result; + } + + @Override + public void onNotify(Messenger.EVENT event) + { + if(event == Messenger.EVENT.BOUNCER_DEAD) + { + + } + else if(event == Messenger.EVENT.TRACKER_DEAD) + { + + } + } +}