I’m getting an unwanted teleporting effect and the fact that It will not walk unless i press the button again isnt anything I have wrapped my head around though.
Help would be appriciated.
I’m using Slick2d library and this entity component system http://slick.cokeandcode.com/wiki/doku.php?id=entity_tutorial
Here’s the code:
package rpggame.component.movement;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
import rpggame.component.Component;
public class TopDownMovement extends Component {
float speed;
float direction;
Vector2f targetPosition;
boolean headingUp = false;
boolean headingDown = false;
boolean headingLeft = false;
boolean headingRight = false;
public TopDownMovement(String id)
{
this.id = id;
}
public float getSpeed()
{
return speed;
}
public float getDirection()
{
return direction;
}
public boolean checkMoving()
{
if(!headingUp || !headingDown || !headingLeft || !headingRight)
{
System.out.println("Isnt moving");
return false;
}
return true;
}
public void movement(GameContainer gc, int delta)
{
Input input = gc.getInput();
Vector2f currentPosition = owner.getPosition();
if(!checkMoving())
{
if(input.isKeyPressed(Input.KEY_W))
{
headingUp = true;
targetPosition = currentPosition;
targetPosition.y -= 32;
advance(delta);
}
}
else{
advance(delta);
}
}
public void advance(int delta)
{
Vector2f currentPosition = owner.getPosition();
if(headingUp)
{
if(currentPosition != targetPosition)
{
currentPosition.y -= 1 * delta;
}
else{
headingUp = false;
}
}
}
public void update(GameContainer gc, StateBasedGame sb, int delta)
{
movement(gc, delta);
}
}