Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/src/com/mygdx/pmd/model/components/MoveComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ public class MoveComponent extends Component {

private Vector2 fDest;
private Direction fDirection;
private int fSpeed;

public MoveComponent(Direction direction, Vector2 curPos) {
this(direction, curPos, c -> {
});
}

public MoveComponent(Direction direction, Vector2 curPos, Consumer<Component> onRemove) {
super(onRemove);
this(direction, curPos, 1, onRemove);
}

public MoveComponent(Direction direction, Vector2 curPos, int speed, Consumer<Component> onRemove) {
super(onRemove);
fDirection = direction;
fDest = new Vector2(curPos);
fSpeed = speed;

Vector2 x = new Vector2(1, 0);
Vector2 y = new Vector2(0, 1);
Expand Down Expand Up @@ -59,4 +64,8 @@ public Direction getDirection() {
public Vector2 getDest() {
return fDest;
}

public int getSpeed() {
return fSpeed;
}
}
5 changes: 3 additions & 2 deletions core/src/com/mygdx/pmd/system/MovementSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ public void update(float dt) {
for (Entity entity : fEntities) {
MoveComponent mc = Mappers.Movement.get(entity);
PositionComponent pc = Mappers.Position.get(entity);
int speed = mc.getSpeed();

Vector2 x = new Vector2(1 / PPM, 0);
Vector2 y = new Vector2(0, 1 / PPM);
Vector2 x = new Vector2(speed / PPM, 0);
Vector2 y = new Vector2(0, speed / PPM);

if (pc.getPos().x < mc.getDest().x) {
pc.getPos().add(x);
Expand Down
7 changes: 6 additions & 1 deletion core/src/com/mygdx/pmd/system/input/PokemonInputSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ private void handleMove(Entity entity) {
InputComponent ic = Mappers.Input.get(entity);
DirectionComponent dc = Mappers.Direction.get(entity);
TurnComponent tc = Mappers.Turn.get(entity);
int speed = 1;

// Don't move if pressing shift
if (ic.pressed(Input.Keys.SHIFT_LEFT)) {
return;
}

if (ic.pressed(Input.Keys.S)) {
speed = 8;
}

Vector2 pos = pc.getPos();
Tile currentTile = fFloor.getTile(pos);

Expand All @@ -94,7 +99,7 @@ private void handleMove(Entity entity) {

currentTile.removeEntity(entity);
nextTile.addEntity(entity);
entity.add(new MoveComponent(dir, pos, (c) -> {
entity.add(new MoveComponent(dir, pos, speed, (c) -> {
entity.add(new AnimationComponent(nc.getName(), dir.format("idle")));
entity.remove(InputLockComponent.class);
}));
Expand Down
23 changes: 23 additions & 0 deletions tests/src/MovementSystemTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.junit.Test;
import org.mockito.Mockito;

import java.util.Arrays;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;

Expand Down Expand Up @@ -123,4 +125,25 @@ public void updatePositionOnMove() {
Vector2 expected = beforePos.add(1, 0);
Assert.assertEquals(expected, pc.getPos());
}

@Test
public void speedChangeOnKeyPress() {
PositionComponent pc = Mappers.Position.get(fEntity);
Vector2 beforePos = new Vector2(pc.getPos());

Floor floor = new Floor();
fEngine.addSystem(new PokemonInputSystem(floor));

fEntity.add(new InputComponent(Arrays.asList(Input.Keys.S, Input.Keys.RIGHT)));
fEntity.add(new DirectionComponent());
MoveComponent mc = Mappers.Movement.get(fEntity);

for (int i = 0; i < 5; i++) {
fEngine.update(.16f);
}

Vector2 expected = beforePos.add(1,0);
Assert.assertEquals(expected, pc.getPos());
}

}