Skip to content

Commit

Permalink
added new vector class to handle coordinates easier
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLattice committed Oct 12, 2019
1 parent 91de5dc commit a6effb3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 40 deletions.
60 changes: 28 additions & 32 deletions src/launcher/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
public class Entity
{
protected Animation curAnim;
protected int xPos, yPos;
protected int HitBoxUpDownX;
protected int HitBoxUpDownY;
protected int HitBoxLeftX;
protected int HitBoxLeftY;
protected int HitBoxRightX;
protected int HitBoxRightY;
protected Vector pos;
protected Vector HitBoxUpDown;
protected Vector HitBoxLeft;
protected Vector HitBoxRight;

protected Animation wUp, wDown, wLeft, wRight;
protected Animation lUp, lDown, lLeft, lRight;
Expand All @@ -34,50 +31,49 @@ public class Entity
lUp.setLooping(false);
lLeft = setAnimation("res/sprite/" + entity +" Llook.png", 16, 24, 10000, false);
lLeft.setLooping(false);

pos = new Vector(0);
HitBoxUpDown = new Vector(0);
HitBoxRight = new Vector(0);
HitBoxLeft = new Vector(0);
}

public void render()
{
curAnim.draw(xPos, yPos);
curAnim.draw(pos.x, pos.y);
}

public void update(int delta, float mapOffsetX, float mapOffsetY)
{
curAnim.update(delta);

HitBoxUpDownX= (int) ((xPos+3 - mapOffsetX)/8);
HitBoxUpDownY = (int) ((yPos+25 - mapOffsetY)/8);
HitBoxRightX = (int) ((xPos+1 - mapOffsetX)/8);
HitBoxRightY = (int) ((yPos+20 - mapOffsetY)/8);
HitBoxLeftX = (int) ((xPos-1 - mapOffsetX)/8);
HitBoxLeftY = (int) ((yPos+20 - mapOffsetY)/8);
HitBoxUpDown.set(
(int) ((pos.x + 3 - mapOffsetX)/8), //x
(int) ((pos.y + 25 - mapOffsetY)/8)); //y
HitBoxRight.set(
(int) ((pos.x + 1 - mapOffsetX)/8), //x
(int) ((pos.y + 20 - mapOffsetY)/8)); //y
HitBoxLeft.set(
(int) ((pos.x - 1 - mapOffsetX)/8), //x
(int) ((pos.y + 20 - mapOffsetY)/8)); //y
}

public int getXHitbox()
{
return HitBoxUpDownX;
}
public int getYHitbox()
{
return HitBoxUpDownY;
}
public int getLXHitbox()
public Vector getUpDownHitbox()
{
return HitBoxLeftX;
return HitBoxUpDown;
}
public int getLYHitbox()
{
return HitBoxLeftY;
}
public int getRXHitbox()

public Vector getLeftHitbox()
{
return HitBoxRightX;
return HitBoxLeft;
}
public int getRYHitbox()

public Vector getRightHitbox()
{
return HitBoxRightY;
return HitBoxRight;
}


private Animation setAnimation (String location, int width, int height, int duration, boolean pingPong) throws SlickException
{
Animation anim;
Expand Down
12 changes: 6 additions & 6 deletions src/launcher/Play.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public void update(GameContainer gc, StateBasedGame stb, int delta) throws Slick
player.moveUp();
mapOffsetY += delta * .1f; //increase the Y coordinates of bucky (move him up)

if(map.getTileId(player.getXHitbox(), player.getYHitbox()-2, cLayer) != 0 ||
map.getTileId(player.getXHitbox()+1, player.getYHitbox()-2, cLayer) != 0)
if(map.getTileId(player.getUpDownHitbox().x, player.getUpDownHitbox().y-2, cLayer) != 0 ||
map.getTileId(player.getUpDownHitbox().x+1, player.getUpDownHitbox().y-2, cLayer) != 0)
{
mapOffsetY -= delta * .1f; //collition detection
}
Expand All @@ -101,8 +101,8 @@ public void update(GameContainer gc, StateBasedGame stb, int delta) throws Slick
player.moveDown();
mapOffsetY -= delta * .1f;

if(map.getTileId(player.getXHitbox(), player.getYHitbox(), cLayer) != 0 ||
map.getTileId(player.getXHitbox()+1, player.getYHitbox(), cLayer) != 0)
if(map.getTileId(player.getUpDownHitbox().x, player.getUpDownHitbox().y, cLayer) != 0 ||
map.getTileId(player.getUpDownHitbox().x+1, player.getUpDownHitbox().y, cLayer) != 0)
{
mapOffsetY += delta * .1f; //collition detection
}
Expand All @@ -113,7 +113,7 @@ public void update(GameContainer gc, StateBasedGame stb, int delta) throws Slick
player.moveLeft();
mapOffsetX += delta * .1f;

if(map.getTileId(player.getLXHitbox(), player.getLYHitbox(), cLayer) != 0)
if(map.getTileId(player.getLeftHitbox().x, player.getLeftHitbox().y, cLayer) != 0)
{
mapOffsetX -= delta * .1f; //collition detection
}
Expand All @@ -124,7 +124,7 @@ public void update(GameContainer gc, StateBasedGame stb, int delta) throws Slick
player.moveRight();
mapOffsetX -= delta * .1f;

if(map.getTileId(player.getRXHitbox()+2, player.getRYHitbox(), cLayer) != 0)
if(map.getTileId(player.getRightHitbox().x+2, player.getRightHitbox().y, cLayer) != 0)
{
mapOffsetX += delta * .1f; //collition detection
}
Expand Down
4 changes: 2 additions & 2 deletions src/launcher/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class Player extends Entity

canMove = true;
curAnim = lDown;
xPos = 200;
yPos = 150;
pos.x = 200;
pos.y = 150;
}


Expand Down
29 changes: 29 additions & 0 deletions src/launcher/Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package launcher;

public class Vector
{
public int x, y;
Vector()
{
x = 0;
y = 0;
}

Vector(int x2, int y2)
{
x = x2;
y = y2;
}

Vector(int v)
{
x = v;
y = v;
}

public void set(int x2, int y2)
{
x = x2;
y = y2;
}
}

0 comments on commit a6effb3

Please sign in to comment.