movement and collisions started - MERGE

master
GammelJAN 5 years ago
parent 77103858cd
commit 9c4e4cdd3c

@ -11,6 +11,7 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
/** /**
@ -50,19 +51,41 @@ public class Player extends Actor{
movementX = speed; movementX = speed;
} }
if(Gdx.input.isKeyPressed(Input.Keys.A)){ if(Gdx.input.isKeyPressed(Input.Keys.A)){
setX(getX()-5); movementX = -speed;
} }
if(Gdx.input.isKeyPressed(Input.Keys.W)){ if(Gdx.input.isKeyPressed(Input.Keys.W)){
setY(getY()+5); movementY = speed;
} }
if(Gdx.input.isKeyPressed(Input.Keys.S)){ if(Gdx.input.isKeyPressed(Input.Keys.S)){
setY(getY()-5); movementY = -speed;
} }
if(Gdx.input.isKeyJustPressed(Input.Keys.ENTER)){
playerSprite.setRow(playerSprite.getRow()+1); boolean canMoveRight = true;
playerSprite.setFrame(0); boolean canMoveLeft = true;
boolean canMoveBoth = true;
for(Actor a : getStage().getActors()){
if(a.getName().equals("mapobject")){
Rectangle p = new Rectangle(getX()+movementX, getY(), getWidth(), getHeight());
Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight());
if(Intersector.overlaps(p, o)){
canMoveRight = false;
break;
}
}
}
for(Actor a : getStage().getActors()){
if(a.getName().equals("mapobject")){
Rectangle p = new Rectangle(getX(), getY()+movementY, getWidth(), getHeight());
Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight());
if(Intersector.overlaps(p, o)){
canMoveRight = false;
break;
}
}
} }
playerSprite.updateAnimation(delta); playerSprite.updateAnimation(delta);
super.act(delta); //To change body of generated methods, choose Tools | Templates. super.act(delta); //To change body of generated methods, choose Tools | Templates.
} }

Loading…
Cancel
Save