Add pacman controls.
Changed coordinates such that the center of a tile is now tile_coordinate + 0.5 rather than tile_coordinate, which makes math make more sense.
This commit is contained in:
parent
e30f7af064
commit
961b4469c5
@ -19,7 +19,7 @@ public abstract class Entity {
|
||||
}
|
||||
|
||||
public void render(SpriteBatch batch, int offsetX, int offsetY) {
|
||||
batch.draw(getSprite(), (int) (x * 8) + (offsetX - 4), (y * 8) + (offsetY - 4));
|
||||
batch.draw(getSprite(), (int) (x * 8) + (offsetX - 8), (y * 8) + (offsetY - 8));
|
||||
}
|
||||
|
||||
public abstract TextureRegion getSprite();
|
||||
|
@ -1,58 +1,100 @@
|
||||
package com.me.pacman.entity;
|
||||
|
||||
import com.me.pacman.level.LevelComponent;
|
||||
import com.me.pacman.state.LevelState;
|
||||
|
||||
public abstract class MovableEntity extends Entity {
|
||||
|
||||
// speed in tiles / second.
|
||||
public float speed;
|
||||
public Direction direction;
|
||||
public boolean moving;
|
||||
public Direction currDirection;
|
||||
private float tolerance;
|
||||
|
||||
public Direction nextDirection = null;
|
||||
public boolean canMove = true;
|
||||
|
||||
public MovableEntity(LevelState state, float x, float y, Direction direction, float speed, boolean moving) {
|
||||
public MovableEntity(LevelState state, float x, float y, float speed, boolean moving, Direction currDirection, float turnTolerance) {
|
||||
super(state, x, y);
|
||||
this.speed = speed;
|
||||
this.direction = direction;
|
||||
this.moving = moving;
|
||||
this.currDirection = currDirection;
|
||||
this.tolerance = turnTolerance;
|
||||
}
|
||||
|
||||
public void update(float dt) {
|
||||
super.update(dt);
|
||||
|
||||
if (!moving) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (nextDirection != null) {
|
||||
LevelComponent nextComponent;
|
||||
boolean turned = false;
|
||||
switch (nextDirection) {
|
||||
case NORTH:
|
||||
nextComponent = state.level.getComponent(x, y + 1f);
|
||||
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
|
||||
x = ((int) x) + 0.5f;
|
||||
turned = true;
|
||||
}
|
||||
break;
|
||||
case EAST:
|
||||
nextComponent = state.level.getComponent(x + 1f, y);
|
||||
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
|
||||
y = ((int) y) + 0.5f;
|
||||
turned = true;
|
||||
}
|
||||
break;
|
||||
case SOUTH:
|
||||
nextComponent = state.level.getComponent(x, y - 1f);
|
||||
if (nextComponent.isPassable() && Math.abs(x - ((int) x + 0.5f)) <= tolerance) {
|
||||
x = ((int) x) + 0.5f;
|
||||
turned = true;
|
||||
}
|
||||
break;
|
||||
case WEST:
|
||||
nextComponent = state.level.getComponent(x - 1f, y);
|
||||
if (nextComponent.isPassable() && Math.abs(y - ((int) y + 0.5f)) <= tolerance) {
|
||||
y = ((int) y) + 0.5f;
|
||||
turned = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (turned) {
|
||||
currDirection = nextDirection;
|
||||
nextDirection = null;
|
||||
}
|
||||
}
|
||||
|
||||
float dist = speed * dt;
|
||||
float new_y = y;
|
||||
float new_x = x;
|
||||
|
||||
switch (direction) {
|
||||
switch (currDirection) {
|
||||
case NORTH:
|
||||
new_y += speed * dt;
|
||||
canMove = state.level.getComponent(new_x, new_y + 1f).isPassable();
|
||||
new_y += dist;
|
||||
canMove = state.level.getComponent(new_x, new_y + 0.5f).isPassable();
|
||||
break;
|
||||
case EAST:
|
||||
new_x += speed * dt;
|
||||
canMove = state.level.getComponent(new_x + 1f,new_y).isPassable();
|
||||
new_x += dist;
|
||||
canMove = state.level.getComponent(new_x + 0.5f, new_y).isPassable();
|
||||
break;
|
||||
case SOUTH:
|
||||
new_y -= speed * dt;
|
||||
canMove = state.level.getComponent(new_x, new_y - 1f).isPassable();
|
||||
new_y -= dist;
|
||||
canMove = state.level.getComponent(new_x, new_y - 0.5f).isPassable();
|
||||
break;
|
||||
case WEST:
|
||||
new_x -= speed * dt;
|
||||
canMove = state.level.getComponent(new_x - 1f,new_y).isPassable();
|
||||
new_x -= dist;
|
||||
canMove = state.level.getComponent(new_x - 0.5f, new_y).isPassable();
|
||||
break;
|
||||
}
|
||||
|
||||
if (canMove) {
|
||||
x = new_x;
|
||||
y = new_y;
|
||||
} else {
|
||||
x = (int) new_x;
|
||||
y = (int) new_y;
|
||||
}
|
||||
|
||||
super.update(dt);
|
||||
// if move wasn't going to collide with wall, move normally.
|
||||
// otherwise, trim would-be decimal and center entity on tile
|
||||
x = canMove? new_x : ((int) new_x) + 0.5f;
|
||||
y = canMove? new_y : ((int) new_y) + 0.5f;
|
||||
}
|
||||
|
||||
public enum Direction {
|
||||
|
@ -11,7 +11,7 @@ public class Pacman extends MovableEntity {
|
||||
private int counter = 1;
|
||||
|
||||
public Pacman(PlayState state) {
|
||||
super(state,13.5f, 7, Direction.EAST, 11f, true);
|
||||
super(state, 14, 7.5f, 7.5f, true, Direction.EAST, 0.3f);
|
||||
this.state = state;
|
||||
sprite = state.getGame().assets.pacman;
|
||||
}
|
||||
@ -19,7 +19,7 @@ public class Pacman extends MovableEntity {
|
||||
@Override
|
||||
public TextureRegion getSprite() {
|
||||
int spriteDir;
|
||||
switch(direction) {
|
||||
switch(currDirection) {
|
||||
case NORTH:
|
||||
spriteDir = 0;
|
||||
break;
|
||||
|
@ -1,8 +1,12 @@
|
||||
package com.me.pacman.state;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.InputAdapter;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.me.pacman.PacDude;
|
||||
import com.me.pacman.entity.MovableEntity;
|
||||
import com.me.pacman.entity.Pacman;
|
||||
import com.me.pacman.level.Level;
|
||||
|
||||
@ -27,6 +31,8 @@ public class PlayState extends LevelState {
|
||||
level = new Level(game,"level");
|
||||
|
||||
pacman = new Pacman(this);
|
||||
|
||||
Gdx.input.setInputProcessor(new Controller());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -41,4 +47,33 @@ public class PlayState extends LevelState {
|
||||
public void update(float dt) {
|
||||
pacman.update(dt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
Gdx.input.setInputProcessor(null);
|
||||
}
|
||||
|
||||
private final class Controller extends InputAdapter {
|
||||
|
||||
@Override
|
||||
public boolean keyDown(int keycode) {
|
||||
switch (keycode) {
|
||||
case Input.Keys.UP:
|
||||
pacman.nextDirection = MovableEntity.Direction.NORTH;
|
||||
break;
|
||||
case Input.Keys.DOWN:
|
||||
pacman.nextDirection = MovableEntity.Direction.SOUTH;
|
||||
break;
|
||||
case Input.Keys.LEFT:
|
||||
pacman.nextDirection = MovableEntity.Direction.WEST;
|
||||
break;
|
||||
case Input.Keys.RIGHT:
|
||||
pacman.nextDirection = MovableEntity.Direction.EAST;
|
||||
break;
|
||||
}
|
||||
return super.keyDown(keycode);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user