Skip to content

Commit

Permalink
Lines are at proper angles
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Bell committed Feb 19, 2015
1 parent 758e143 commit 9de5dad
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 122 deletions.
42 changes: 20 additions & 22 deletions core/src/ca/michaelbell/gameobjects/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -82,15 +80,15 @@ 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);
Gdx.app.log("Player", "Down");
}
}

public void Stop(){
public void Stop() {
velocity.x = 0;
velocity.y = 0;
setDirection(0);
Expand Down
115 changes: 45 additions & 70 deletions core/src/ca/michaelbell/gameobjects/Trail.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector2 []> 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<Vector2[]> 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<Vector2[]>();
public Trail(Player player) {
trailWidth = 5; // 5 px for now
this.player = player;
TrailList = new ArrayList<Vector2[]>(); // 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<Rectangle>();
Iterator<Rectangle> 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<Vector2[]>(); // 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<Rectangle> 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<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
Iterator<String> itr = list.iterator();
while(itr.hasNext()) {
String str = itr.next();
System.out.println(str + " is " + str.length() + " chars long.");
}*/
}
28 changes: 6 additions & 22 deletions core/src/ca/michaelbell/gameworld/GameRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();


}
}
10 changes: 3 additions & 7 deletions core/src/ca/michaelbell/gameworld/GameWorld.java
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -19,7 +15,7 @@ public void update(float delta) {
player.getTrail().update();
}

public Player getPlayer(){
public Player getPlayer() {
return player;
}
}
2 changes: 2 additions & 0 deletions core/src/ca/michaelbell/helpers/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/ca/michaelbell/screens/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9de5dad

Please sign in to comment.