From 9de5dad139d34fe90fc49cc4bdb69baff59443d2 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 18 Feb 2015 21:41:04 -0800 Subject: [PATCH] Lines are at proper angles --- .../ca/michaelbell/gameobjects/Player.java | 42 +++---- .../src/ca/michaelbell/gameobjects/Trail.java | 115 +++++++----------- .../michaelbell/gameworld/GameRenderer.java | 28 +---- .../ca/michaelbell/gameworld/GameWorld.java | 10 +- .../ca/michaelbell/helpers/InputHandler.java | 2 + .../ca/michaelbell/screens/GameScreen.java | 2 +- 6 files changed, 77 insertions(+), 122 deletions(-) diff --git a/core/src/ca/michaelbell/gameobjects/Player.java b/core/src/ca/michaelbell/gameobjects/Player.java index 9b0912c..b66c3df 100644 --- a/core/src/ca/michaelbell/gameobjects/Player.java +++ b/core/src/ca/michaelbell/gameobjects/Player.java @@ -5,31 +5,15 @@ public class Player { - public Vector2 getPosition() { - return position; - } - private Vector2 position; private Vector2 velocity; private Vector2 acceleration; - private float rotation; private int width; private int height; private int speed; - - public Trail getTrail() { - return trail; - } - private Trail trail; - private int direction; - // 1 == North - // 2 == East - // 3 == South - // 4 == West - // 0 == Stopped public Player(float x, float y, int width, int height) { direction = 1; @@ -42,23 +26,37 @@ public Player(float x, float y, int width, int height) { trail = new Trail(this); } + public Vector2 getPosition() { + return position; + } + // 1 == North + // 2 == East + // 3 == South + // 4 == West + // 0 == Stopped + + public Trail getTrail() { + return trail; + } + public void update(float delta) { //velocity.add(acceleration.cpy().scl(delta)); position.add(velocity.cpy().scl(delta)); } +// TODO: Find Better Logic System public void Left() { - if(this.direction == 1 || this.direction == 3 || this.direction == 0) { + if (this.direction == 1 || this.direction == 3 || this.direction == 0) { velocity.x = -speed; velocity.y = 0; - setDirection(2); + setDirection(2); Gdx.app.log("Player", "Left"); } } public void Right() { - if(this.direction == 1 || this.direction == 3 || this.direction == 0) { + if (this.direction == 1 || this.direction == 3 || this.direction == 0) { velocity.x = speed; velocity.y = 0; setDirection(4); @@ -73,7 +71,7 @@ public void setDirection(int direction) { } public void Up() { - if(this.direction == 2 || this.direction == 4 || this.direction == 0) { + if (this.direction == 2 || this.direction == 4 || this.direction == 0) { velocity.y = -speed; velocity.x = 0; setDirection(1); @@ -82,7 +80,7 @@ public void Up() { } public void Down() { - if(this.direction == 2 || this.direction == 4 || this.direction == 0) { + if (this.direction == 2 || this.direction == 4 || this.direction == 0) { velocity.y = speed; velocity.x = 0; setDirection(3); @@ -90,7 +88,7 @@ public void Down() { } } - public void Stop(){ + public void Stop() { velocity.x = 0; velocity.y = 0; setDirection(0); diff --git a/core/src/ca/michaelbell/gameobjects/Trail.java b/core/src/ca/michaelbell/gameobjects/Trail.java index fd3009c..9e85a50 100644 --- a/core/src/ca/michaelbell/gameobjects/Trail.java +++ b/core/src/ca/michaelbell/gameobjects/Trail.java @@ -2,98 +2,73 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; -import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import java.util.ArrayList; import java.util.Iterator; -/** - * Created by Michael on 2015-02-13. - */ public class Trail { - private Rectangle currentRectangle; - Vector2 currentLine[]; - ArrayList TrailList; - private Iterator itr; - Player player; - private float trailWidth; + Vector2 currentLine[]; // Array to hold the vector of the current path [0] is starting point, [1] is current point(updates) + ArrayList TrailList; // list of line segments + Player player; // local reference to the player + private Iterator itr; // iterator to go through line segments when rendering + private float trailWidth; // how wide the trail is so I can change it without breaking code + private Boolean push; // switches, pushes every other line x origin left + // TODO Find better var name + // rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees) - public Trail(Player player){ - trailWidth = 5; - this.player =player; - TrailList = new ArrayList(); + public Trail(Player player) { + trailWidth = 5; // 5 px for now + this.player = player; + TrailList = new ArrayList(); // ArrayList of Vector2 arrays currentLine = new Vector2[2]; - currentLine[0] = player.getPosition().cpy().add(player.getWidth()/2, player.getHeight()/2); - currentLine[1] = player.getPosition().add(player.getWidth() / 2, player.getHeight() / 2); - itr = TrailList.iterator(); -/* TrailList = new ArrayList(); - Iterator itr = TrailList.iterator(); - currentRectangle = new Rectangle(0,0,0,0)*/; - //current rectangle between old turn and new position - // center and stretch - //on turn: add rect to list of old rect, make new rect at new turn to new position... - // use iterator in game renderer for batch rendering... - // How to detect collisions?????? + currentLine[0] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); // .cpy to make it static, adds half the player size to try centering... Also needs to have half the line removed? + currentLine[1] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); + itr = TrailList.iterator(); // init the iterator + push = true; } - public void update(){ - currentLine[1] = player.getPosition().cpy(); + + public void update() { + currentLine[1] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); } - public Vector2[] newTrail(){ - Vector2 temp[] = new Vector2[2]; + public void Reset(){ + trailWidth = 5; // 5 px for now + TrailList = new ArrayList(); // ArrayList of Vector2 arrays + currentLine = new Vector2[2]; + currentLine[0] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); // .cpy to make it static, adds half the player size to try centering... Also needs to have half the line removed? + currentLine[1] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); + itr = TrailList.iterator(); // init the iterator + push = true; + } + + public Vector2[] newTrail() { + Vector2 temp[] = new Vector2[2]; // temp vector array to add currentLine to TrailList. Probably not needed? temp[0] = currentLine[0].cpy(); temp[1] = currentLine[1].cpy(); TrailList.add(temp); currentLine[0] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); - currentLine[1] = player.getPosition().cpy().add(player.getWidth()/2, player.getHeight()/2); - itr = TrailList.iterator(); -/* TrailList.add(currentRectangle); - currentRectangle = new Rectangle(0,0,0,0); - Iterator itr = TrailList.iterator();*/ - - // return new current rectangle - return currentLine; + currentLine[1] = player.getPosition().cpy().add(player.getWidth() / 2, player.getHeight() / 2); + if(push) + //currentLine[0].add(trailWidth/2,0); + push = !push; + //itr = TrailList.iterator(); + return currentLine; // if anyone wants it } - public void render(ShapeRenderer shapeRenderer){ - // Gdx.app.log("Trail", "Render"); - itr = TrailList.iterator(); - // Begin ShapeRenderer + public void render(ShapeRenderer shapeRenderer) { + itr = TrailList.iterator(); // reset on each render... Probably taxing, but I don't want to forget a render + shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); + shapeRenderer.setColor(255 / 255f, 0 / 255f, 9 / 255f, 1); // red + shapeRenderer.rectLine(currentLine[0].cpy(), currentLine[1].cpy(), 2.5f); // curent line - // Draw Background color - shapeRenderer.setColor(255/255f, 0/255f, 9/255f,1); - //shapeRenderer.rect(25f,5f, 50f,50f); - shapeRenderer.rectLine(currentLine[0].cpy(), currentLine[1].cpy(), 2.5f); - /* Gdx.app.log("CurrentLine[0]", currentLine[0].toString()); - Gdx.app.log("currentline[1]", currentLine[1].toString()); -*/ - while(itr.hasNext()) { - shapeRenderer.setColor(0/255f, 255/255f, 9/255f ,1); + while (itr.hasNext()) { // iterator + shapeRenderer.setColor(0 / 255f, 255 / 255f, 9 / 255f, 1); // green Vector2[] vect = itr.next(); Gdx.app.log("Trail", vect[1].toString()); shapeRenderer.rectLine(vect[0], vect[1], 2.5f); - } - - // End ShapeRenderer shapeRenderer.end(); } -} - -/* - -ArrayList list = new ArrayList(); -list.add("one"); - list.add("two"); - list.add("three"); - list.add("four"); - - Iterator itr = list.iterator(); - - while(itr.hasNext()) { - String str = itr.next(); - - System.out.println(str + " is " + str.length() + " chars long."); - }*/ +} \ No newline at end of file diff --git a/core/src/ca/michaelbell/gameworld/GameRenderer.java b/core/src/ca/michaelbell/gameworld/GameRenderer.java index e1d17fe..46a2a34 100644 --- a/core/src/ca/michaelbell/gameworld/GameRenderer.java +++ b/core/src/ca/michaelbell/gameworld/GameRenderer.java @@ -16,16 +16,14 @@ public class GameRenderer { private OrthographicCamera cam; private ShapeRenderer shapeRenderer; private SpriteBatch batcher; + private Player player; private int midPointY; private int gameHeight; - public GameRenderer(GameWorld world, int gameHeight, int midPointY){ + public GameRenderer(GameWorld world, int gameHeight, int midPointY) { myWorld = world; - // The word "this" refers to this instance. - // We are setting the instance variables' values to be that of the - // parameters passed in from GameScreen. this.gameHeight = gameHeight; this.midPointY = midPointY; @@ -36,35 +34,21 @@ public GameRenderer(GameWorld world, int gameHeight, int midPointY){ batcher.setProjectionMatrix(cam.combined); shapeRenderer = new ShapeRenderer(); shapeRenderer.setProjectionMatrix(cam.combined); + player = myWorld.getPlayer(); // who is that again? + } public void render(float runTime) { // Fill the entire screen with black, to prevent potential flickering. Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); - // We will move these outside of the loop for performance later. - Player player = myWorld.getPlayer(); - - player.getTrail().render(shapeRenderer); - + player.getTrail().render(shapeRenderer); // render the trail from the trail method // Begin SpriteBatch batcher.begin(); - // Disable transparency - // This is good for performance when drawing images that do not require - // transparency. - batcher.disableBlending(); - batcher.draw(AssetLoader.playerTexture, player.getX(), player.getY(), player.getWidth(), player.getHeight() ); - - // The bird needs transparency, so we enable that again. - batcher.enableBlending(); - - - // End SpriteBatch + batcher.draw(AssetLoader.playerTexture, player.getX(), player.getY(), player.getWidth(), player.getHeight()); // Draw the player... He probably could be a rectangle... Oh well batcher.end(); - - } } diff --git a/core/src/ca/michaelbell/gameworld/GameWorld.java b/core/src/ca/michaelbell/gameworld/GameWorld.java index f2511d3..61d5095 100644 --- a/core/src/ca/michaelbell/gameworld/GameWorld.java +++ b/core/src/ca/michaelbell/gameworld/GameWorld.java @@ -1,16 +1,12 @@ package ca.michaelbell.gameworld; import ca.michaelbell.gameobjects.Player; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.math.Rectangle; -/** - * Created by Michael on 2015-01-31. - */ + public class GameWorld { private Player player; - public GameWorld(int midpointX, int midpointY){ + public GameWorld(int midpointX, int midpointY) { player = new Player(midpointX, midpointY, 16, 16); } @@ -19,7 +15,7 @@ public void update(float delta) { player.getTrail().update(); } - public Player getPlayer(){ + public Player getPlayer() { return player; } } diff --git a/core/src/ca/michaelbell/helpers/InputHandler.java b/core/src/ca/michaelbell/helpers/InputHandler.java index ec49401..d5a6eca 100644 --- a/core/src/ca/michaelbell/helpers/InputHandler.java +++ b/core/src/ca/michaelbell/helpers/InputHandler.java @@ -24,6 +24,8 @@ public boolean keyDown(int keycode) { player.Down(); if(keycode == Input.Keys.SPACE) player.Stop(); + if(keycode == Input.Keys.R) + player.getTrail().Reset(); return true; } diff --git a/core/src/ca/michaelbell/screens/GameScreen.java b/core/src/ca/michaelbell/screens/GameScreen.java index 8038ab6..1111fbd 100644 --- a/core/src/ca/michaelbell/screens/GameScreen.java +++ b/core/src/ca/michaelbell/screens/GameScreen.java @@ -19,7 +19,7 @@ public GameScreen() { float screenWidth = Gdx.graphics.getWidth(); float screenHeight = Gdx.graphics.getHeight(); - float gameWidth = 136; + float gameWidth = 1920; float gameHeight = screenHeight / (screenWidth / gameWidth); int midPointY = (int) (gameHeight / 2);